Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 def2f970 2021-09-28 thomas #endif
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
44 9f7d7167 2018-04-29 stsp
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 dd038bc6 2021-09-21 thomas.ad
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 9f7d7167 2018-04-29 stsp
62 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
112 fc2737d5 2022-09-15 thomas };
113 fc2737d5 2022-09-15 thomas
114 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
116 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
124 d6b05b5b 2018-08-04 stsp };
125 c3e9aa98 2019-05-13 jcs
126 a5d43cac 2022-07-01 thomas enum tog_view_mode {
127 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
130 a5d43cac 2022-07-01 thomas };
131 a5d43cac 2022-07-01 thomas
132 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
133 a5d43cac 2022-07-01 thomas
134 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
135 d6b05b5b 2018-08-04 stsp
136 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
137 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
138 ba4f502b 2018-08-04 stsp struct got_object_id *id;
139 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
140 1a76625f 2018-10-22 stsp int idx;
141 ba4f502b 2018-08-04 stsp };
142 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 ba4f502b 2018-08-04 stsp struct commit_queue {
144 ba4f502b 2018-08-04 stsp int ncommits;
145 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
146 6d17833f 2019-11-08 stsp };
147 6d17833f 2019-11-08 stsp
148 f26dddb7 2019-11-08 stsp struct tog_color {
149 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
150 6d17833f 2019-11-08 stsp regex_t regex;
151 6d17833f 2019-11-08 stsp short colorpair;
152 15a087fe 2019-02-21 stsp };
153 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
154 11b20872 2019-11-08 stsp
155 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
157 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
158 2183bbf6 2022-01-23 thomas
159 2183bbf6 2022-01-23 thomas static const struct got_error *
160 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 2183bbf6 2022-01-23 thomas struct got_reference* re2)
162 2183bbf6 2022-01-23 thomas {
163 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
164 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
165 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
166 2183bbf6 2022-01-23 thomas
167 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
168 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
171 2183bbf6 2022-01-23 thomas *cmp = -1;
172 2183bbf6 2022-01-23 thomas return NULL;
173 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
174 2183bbf6 2022-01-23 thomas *cmp = 1;
175 2183bbf6 2022-01-23 thomas return NULL;
176 2183bbf6 2022-01-23 thomas }
177 2183bbf6 2022-01-23 thomas
178 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 2183bbf6 2022-01-23 thomas return NULL;
180 2183bbf6 2022-01-23 thomas }
181 51a10b52 2020-12-26 stsp
182 11b20872 2019-11-08 stsp static const struct got_error *
183 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
184 51a10b52 2020-12-26 stsp {
185 51a10b52 2020-12-26 stsp const struct got_error *err;
186 51a10b52 2020-12-26 stsp
187 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 3bfadbd4 2021-11-20 thomas repo);
190 51a10b52 2020-12-26 stsp if (err)
191 51a10b52 2020-12-26 stsp return err;
192 51a10b52 2020-12-26 stsp
193 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
194 51a10b52 2020-12-26 stsp repo);
195 51a10b52 2020-12-26 stsp }
196 51a10b52 2020-12-26 stsp
197 51a10b52 2020-12-26 stsp static void
198 51a10b52 2020-12-26 stsp tog_free_refs(void)
199 51a10b52 2020-12-26 stsp {
200 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
201 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
202 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
203 51a10b52 2020-12-26 stsp }
204 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
205 51a10b52 2020-12-26 stsp }
206 51a10b52 2020-12-26 stsp
207 51a10b52 2020-12-26 stsp static const struct got_error *
208 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
209 11b20872 2019-11-08 stsp int idx, short color)
210 11b20872 2019-11-08 stsp {
211 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
212 11b20872 2019-11-08 stsp struct tog_color *tc;
213 11b20872 2019-11-08 stsp int regerr = 0;
214 11b20872 2019-11-08 stsp
215 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
216 11b20872 2019-11-08 stsp return NULL;
217 11b20872 2019-11-08 stsp
218 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
219 11b20872 2019-11-08 stsp
220 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
221 11b20872 2019-11-08 stsp if (tc == NULL)
222 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
223 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
224 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
225 11b20872 2019-11-08 stsp if (regerr) {
226 11b20872 2019-11-08 stsp static char regerr_msg[512];
227 11b20872 2019-11-08 stsp static char err_msg[512];
228 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
229 11b20872 2019-11-08 stsp sizeof(regerr_msg));
230 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
231 11b20872 2019-11-08 stsp regerr_msg);
232 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 11b20872 2019-11-08 stsp free(tc);
234 11b20872 2019-11-08 stsp return err;
235 11b20872 2019-11-08 stsp }
236 11b20872 2019-11-08 stsp tc->colorpair = idx;
237 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
238 11b20872 2019-11-08 stsp return NULL;
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 11b20872 2019-11-08 stsp static void
242 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
247 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
248 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
249 11b20872 2019-11-08 stsp regfree(&tc->regex);
250 11b20872 2019-11-08 stsp free(tc);
251 11b20872 2019-11-08 stsp }
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 ef20f542 2022-06-26 thomas static struct tog_color *
255 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
258 11b20872 2019-11-08 stsp
259 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
260 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
261 11b20872 2019-11-08 stsp return tc;
262 11b20872 2019-11-08 stsp }
263 11b20872 2019-11-08 stsp
264 11b20872 2019-11-08 stsp return NULL;
265 11b20872 2019-11-08 stsp }
266 11b20872 2019-11-08 stsp
267 11b20872 2019-11-08 stsp static int
268 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
269 11b20872 2019-11-08 stsp {
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
273 11b20872 2019-11-08 stsp return COLOR_CYAN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
275 11b20872 2019-11-08 stsp return COLOR_YELLOW;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
277 11b20872 2019-11-08 stsp return COLOR_GREEN;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
283 91b8c405 2020-01-25 stsp return COLOR_CYAN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_GREEN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
287 11b20872 2019-11-08 stsp return COLOR_GREEN;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
289 11b20872 2019-11-08 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_YELLOW;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_GREEN;
294 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
296 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
297 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
298 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
299 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
300 11b20872 2019-11-08 stsp
301 11b20872 2019-11-08 stsp return -1;
302 11b20872 2019-11-08 stsp }
303 11b20872 2019-11-08 stsp
304 11b20872 2019-11-08 stsp static int
305 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
306 11b20872 2019-11-08 stsp {
307 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
308 11b20872 2019-11-08 stsp
309 11b20872 2019-11-08 stsp if (val == NULL)
310 11b20872 2019-11-08 stsp return default_color_value(envvar);
311 15a087fe 2019-02-21 stsp
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
313 11b20872 2019-11-08 stsp return COLOR_BLACK;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
315 11b20872 2019-11-08 stsp return COLOR_RED;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
317 11b20872 2019-11-08 stsp return COLOR_GREEN;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
319 11b20872 2019-11-08 stsp return COLOR_YELLOW;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
321 11b20872 2019-11-08 stsp return COLOR_BLUE;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
323 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
325 11b20872 2019-11-08 stsp return COLOR_CYAN;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
327 11b20872 2019-11-08 stsp return COLOR_WHITE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
329 11b20872 2019-11-08 stsp return -1;
330 11b20872 2019-11-08 stsp
331 11b20872 2019-11-08 stsp return default_color_value(envvar);
332 11b20872 2019-11-08 stsp }
333 11b20872 2019-11-08 stsp
334 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
335 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
336 3dbaef42 2020-11-24 stsp const char *label1, *label2;
337 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
338 19a6a6b5 2022-07-01 thomas int fd1, fd2;
339 82c78e96 2022-08-06 thomas int lineno;
340 15a087fe 2019-02-21 stsp int first_displayed_line;
341 15a087fe 2019-02-21 stsp int last_displayed_line;
342 15a087fe 2019-02-21 stsp int eof;
343 15a087fe 2019-02-21 stsp int diff_context;
344 3dbaef42 2020-11-24 stsp int ignore_whitespace;
345 64453f7e 2020-11-21 stsp int force_text_diff;
346 15a087fe 2019-02-21 stsp struct got_repository *repo;
347 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
348 fe621944 2020-11-10 stsp size_t nlines;
349 f44b1f58 2020-02-02 tracey int matched_line;
350 f44b1f58 2020-02-02 tracey int selected_line;
351 15a087fe 2019-02-21 stsp
352 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
353 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
354 b01e7d3b 2018-08-04 stsp };
355 b01e7d3b 2018-08-04 stsp
356 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
358 1a76625f 2018-10-22 stsp
359 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
360 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
361 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
362 1a76625f 2018-10-22 stsp int commits_needed;
363 fb280deb 2021-08-30 stsp int load_all;
364 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
365 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
366 1a76625f 2018-10-22 stsp const char *in_repo_path;
367 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp struct got_repository *repo;
369 1af5eddf 2022-06-23 thomas int *pack_fds;
370 1a76625f 2018-10-22 stsp int log_complete;
371 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
372 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
373 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
374 13add988 2019-10-15 stsp int *searching;
375 13add988 2019-10-15 stsp int *search_next_done;
376 13add988 2019-10-15 stsp regex_t *regex;
377 7e8004ba 2022-09-11 thomas int *limiting;
378 7e8004ba 2022-09-11 thomas int limit_match;
379 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
380 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
381 1a76625f 2018-10-22 stsp };
382 1a76625f 2018-10-22 stsp
383 1a76625f 2018-10-22 stsp struct tog_log_view_state {
384 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
385 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
386 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
387 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
388 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
389 b01e7d3b 2018-08-04 stsp int selected;
390 b01e7d3b 2018-08-04 stsp char *in_repo_path;
391 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
392 b672a97a 2020-01-27 stsp int log_branches;
393 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
394 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
395 1a76625f 2018-10-22 stsp sig_atomic_t quit;
396 1a76625f 2018-10-22 stsp pthread_t thread;
397 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
398 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
399 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
400 11b20872 2019-11-08 stsp struct tog_colors colors;
401 f69c5a46 2022-07-19 thomas int use_committer;
402 7e8004ba 2022-09-11 thomas int limit_view;
403 7e8004ba 2022-09-11 thomas regex_t limit_regex;
404 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
405 ba4f502b 2018-08-04 stsp };
406 11b20872 2019-11-08 stsp
407 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
408 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
409 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
411 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
412 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
413 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
414 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
415 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
416 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
417 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
418 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
419 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
420 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
421 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
422 ba4f502b 2018-08-04 stsp
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
425 e9424729 2018-08-04 stsp int nlines;
426 e9424729 2018-08-04 stsp
427 e9424729 2018-08-04 stsp struct tog_view *view;
428 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
429 e9424729 2018-08-04 stsp int *quit;
430 e9424729 2018-08-04 stsp };
431 e9424729 2018-08-04 stsp
432 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
433 e9424729 2018-08-04 stsp const char *path;
434 e9424729 2018-08-04 stsp struct got_repository *repo;
435 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
436 e9424729 2018-08-04 stsp int *complete;
437 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
438 fc06ba56 2019-08-22 stsp void *cancel_arg;
439 e9424729 2018-08-04 stsp };
440 e9424729 2018-08-04 stsp
441 e9424729 2018-08-04 stsp struct tog_blame {
442 e9424729 2018-08-04 stsp FILE *f;
443 be659d10 2020-11-18 stsp off_t filesize;
444 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
445 6fcac457 2018-11-19 stsp int nlines;
446 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
447 e9424729 2018-08-04 stsp pthread_t thread;
448 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
449 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
450 e9424729 2018-08-04 stsp const char *path;
451 7cd52833 2022-06-23 thomas int *pack_fds;
452 e9424729 2018-08-04 stsp };
453 e9424729 2018-08-04 stsp
454 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
455 7cbe629d 2018-08-04 stsp int first_displayed_line;
456 7cbe629d 2018-08-04 stsp int last_displayed_line;
457 7cbe629d 2018-08-04 stsp int selected_line;
458 4fc71f3b 2022-07-12 thomas int last_diffed_line;
459 7cbe629d 2018-08-04 stsp int blame_complete;
460 e5a0f69f 2018-08-18 stsp int eof;
461 e5a0f69f 2018-08-18 stsp int done;
462 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
463 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
464 e5a0f69f 2018-08-18 stsp char *path;
465 7cbe629d 2018-08-04 stsp struct got_repository *repo;
466 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
467 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
468 e9424729 2018-08-04 stsp struct tog_blame blame;
469 6c4c42e0 2019-06-24 stsp int matched_line;
470 11b20872 2019-11-08 stsp struct tog_colors colors;
471 ad80ab7b 2018-08-04 stsp };
472 ad80ab7b 2018-08-04 stsp
473 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
474 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
475 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
476 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
478 ad80ab7b 2018-08-04 stsp int selected;
479 ad80ab7b 2018-08-04 stsp };
480 ad80ab7b 2018-08-04 stsp
481 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
482 ad80ab7b 2018-08-04 stsp
483 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
484 ad80ab7b 2018-08-04 stsp char *tree_label;
485 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
486 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
487 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
489 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
490 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
491 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
492 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
493 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
494 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
495 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
496 6458efa5 2020-11-24 stsp struct tog_colors colors;
497 6458efa5 2020-11-24 stsp };
498 6458efa5 2020-11-24 stsp
499 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
500 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
501 6458efa5 2020-11-24 stsp struct got_reference *ref;
502 6458efa5 2020-11-24 stsp int idx;
503 6458efa5 2020-11-24 stsp };
504 6458efa5 2020-11-24 stsp
505 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
506 6458efa5 2020-11-24 stsp
507 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
508 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
512 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 6458efa5 2020-11-24 stsp struct got_repository *repo;
514 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
515 bddb1296 2019-11-08 stsp struct tog_colors colors;
516 fc2737d5 2022-09-15 thomas };
517 fc2737d5 2022-09-15 thomas
518 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
519 fc2737d5 2022-09-15 thomas FILE *f;
520 fc2737d5 2022-09-15 thomas off_t *line_offsets;
521 fc2737d5 2022-09-15 thomas size_t nlines;
522 fc2737d5 2022-09-15 thomas int lineno;
523 fc2737d5 2022-09-15 thomas int first_displayed_line;
524 fc2737d5 2022-09-15 thomas int last_displayed_line;
525 fc2737d5 2022-09-15 thomas int eof;
526 fc2737d5 2022-09-15 thomas int matched_line;
527 fc2737d5 2022-09-15 thomas int selected_line;
528 fc2737d5 2022-09-15 thomas int all;
529 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
530 fc2737d5 2022-09-15 thomas };
531 fc2737d5 2022-09-15 thomas
532 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
533 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
536 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
537 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
540 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
541 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
542 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
543 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
544 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
545 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
546 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
547 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
548 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
549 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
550 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
551 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
552 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
553 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
554 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
555 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
556 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
557 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
558 fc2737d5 2022-09-15 thomas \
559 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
561 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
562 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
563 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
565 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
566 fc2737d5 2022-09-15 thomas " commit"), \
567 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
568 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
571 fc2737d5 2022-09-15 thomas \
572 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
577 fc2737d5 2022-09-15 thomas " data"), \
578 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
579 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
580 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
581 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
582 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
583 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
584 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
585 fc2737d5 2022-09-15 thomas \
586 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
588 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
590 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
591 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
592 fc2737d5 2022-09-15 thomas " selected line's commit"), \
593 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
594 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
595 fc2737d5 2022-09-15 thomas \
596 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
598 fc2737d5 2022-09-15 thomas " selected file"), \
599 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
600 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
601 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
602 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
603 fc2737d5 2022-09-15 thomas \
604 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
606 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
607 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
609 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
611 fc2737d5 2022-09-15 thomas
612 fc2737d5 2022-09-15 thomas struct tog_key_map {
613 fc2737d5 2022-09-15 thomas const char *keys;
614 fc2737d5 2022-09-15 thomas const char *info;
615 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
616 7cbe629d 2018-08-04 stsp };
617 7cbe629d 2018-08-04 stsp
618 669b5ffa 2018-10-07 stsp /*
619 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
620 669b5ffa 2018-10-07 stsp *
621 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
622 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
623 669b5ffa 2018-10-07 stsp * there is enough screen estate.
624 669b5ffa 2018-10-07 stsp *
625 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
626 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
627 669b5ffa 2018-10-07 stsp *
628 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
629 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
630 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
631 669b5ffa 2018-10-07 stsp *
632 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
633 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
634 669b5ffa 2018-10-07 stsp */
635 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
636 669b5ffa 2018-10-07 stsp
637 cc3c9aac 2018-08-01 stsp struct tog_view {
638 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
639 26ed57b2 2018-05-19 stsp WINDOW *window;
640 26ed57b2 2018-05-19 stsp PANEL *panel;
641 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
642 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
643 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
644 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
645 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
646 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
647 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
648 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
649 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
650 9970f7fc 2020-12-03 stsp int dying;
651 669b5ffa 2018-10-07 stsp struct tog_view *parent;
652 669b5ffa 2018-10-07 stsp struct tog_view *child;
653 5dc9f4bc 2018-08-04 stsp
654 e78dc838 2020-12-04 stsp /*
655 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
656 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
657 e78dc838 2020-12-04 stsp * between parent and child.
658 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
659 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
660 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
661 e78dc838 2020-12-04 stsp * situations.
662 e78dc838 2020-12-04 stsp */
663 e78dc838 2020-12-04 stsp int focus_child;
664 e78dc838 2020-12-04 stsp
665 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
666 5dc9f4bc 2018-08-04 stsp /* type-specific state */
667 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
668 5dc9f4bc 2018-08-04 stsp union {
669 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
670 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
671 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
672 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
673 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
674 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
675 5dc9f4bc 2018-08-04 stsp } state;
676 e5a0f69f 2018-08-18 stsp
677 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
678 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
679 e78dc838 2020-12-04 stsp struct tog_view *, int);
680 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
681 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
682 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
683 60493ae3 2019-06-20 stsp
684 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
685 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
686 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
687 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
688 c0c4acc8 2021-01-24 stsp int search_started;
689 60493ae3 2019-06-20 stsp int searching;
690 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
691 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
692 60493ae3 2019-06-20 stsp int search_next_done;
693 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
694 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
695 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
696 1803e47f 2019-06-22 stsp regex_t regex;
697 41605754 2020-11-12 stsp regmatch_t regmatch;
698 cc3c9aac 2018-08-01 stsp };
699 cd0acaa7 2018-05-20 stsp
700 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
701 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
702 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
703 78756c87 2020-11-24 stsp struct got_repository *);
704 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
705 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
706 e78dc838 2020-12-04 stsp struct tog_view *, int);
707 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
708 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
709 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
710 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
711 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
712 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
713 e5a0f69f 2018-08-18 stsp
714 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
715 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
716 78756c87 2020-11-24 stsp const char *, const char *, int);
717 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
718 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
719 e78dc838 2020-12-04 stsp struct tog_view *, int);
720 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
721 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
722 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
723 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
724 e5a0f69f 2018-08-18 stsp
725 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
726 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
727 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
728 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
729 e78dc838 2020-12-04 stsp struct tog_view *, int);
730 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
731 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
732 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
733 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
734 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
735 e5a0f69f 2018-08-18 stsp
736 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
737 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
738 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
739 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
740 e78dc838 2020-12-04 stsp struct tog_view *, int);
741 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
742 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
743 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
744 6458efa5 2020-11-24 stsp
745 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
746 6458efa5 2020-11-24 stsp struct got_repository *);
747 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
748 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
749 e78dc838 2020-12-04 stsp struct tog_view *, int);
750 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
751 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
752 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
753 2a7d3cd7 2022-09-17 thomas
754 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
755 2a7d3cd7 2022-09-17 thomas struct tog_view *);
756 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
757 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
758 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
759 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
760 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
761 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
762 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
763 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
764 25791caa 2018-10-24 stsp
765 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
766 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
767 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
768 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
769 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
770 25791caa 2018-10-24 stsp
771 25791caa 2018-10-24 stsp static void
772 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
773 25791caa 2018-10-24 stsp {
774 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
775 83baff54 2019-08-12 stsp }
776 83baff54 2019-08-12 stsp
777 83baff54 2019-08-12 stsp static void
778 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
779 83baff54 2019-08-12 stsp {
780 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
781 25791caa 2018-10-24 stsp }
782 26ed57b2 2018-05-19 stsp
783 61266923 2020-01-14 stsp static void
784 61266923 2020-01-14 stsp tog_sigcont(int signo)
785 61266923 2020-01-14 stsp {
786 61266923 2020-01-14 stsp tog_sigcont_received = 1;
787 61266923 2020-01-14 stsp }
788 61266923 2020-01-14 stsp
789 296152d1 2022-05-31 thomas static void
790 296152d1 2022-05-31 thomas tog_sigint(int signo)
791 296152d1 2022-05-31 thomas {
792 296152d1 2022-05-31 thomas tog_sigint_received = 1;
793 296152d1 2022-05-31 thomas }
794 296152d1 2022-05-31 thomas
795 296152d1 2022-05-31 thomas static void
796 296152d1 2022-05-31 thomas tog_sigterm(int signo)
797 296152d1 2022-05-31 thomas {
798 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
799 296152d1 2022-05-31 thomas }
800 296152d1 2022-05-31 thomas
801 296152d1 2022-05-31 thomas static int
802 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
803 296152d1 2022-05-31 thomas {
804 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
805 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
806 296152d1 2022-05-31 thomas }
807 296152d1 2022-05-31 thomas
808 e5a0f69f 2018-08-18 stsp static const struct got_error *
809 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
810 ea5e7bb5 2018-08-01 stsp {
811 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
812 e5a0f69f 2018-08-18 stsp
813 669b5ffa 2018-10-07 stsp if (view->child) {
814 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
815 669b5ffa 2018-10-07 stsp view->child = NULL;
816 669b5ffa 2018-10-07 stsp }
817 e5a0f69f 2018-08-18 stsp if (view->close)
818 e5a0f69f 2018-08-18 stsp err = view->close(view);
819 ea5e7bb5 2018-08-01 stsp if (view->panel)
820 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
821 ea5e7bb5 2018-08-01 stsp if (view->window)
822 ea5e7bb5 2018-08-01 stsp delwin(view->window);
823 ea5e7bb5 2018-08-01 stsp free(view);
824 f2d749db 2022-07-12 thomas return err ? err : child_err;
825 ea5e7bb5 2018-08-01 stsp }
826 ea5e7bb5 2018-08-01 stsp
827 ea5e7bb5 2018-08-01 stsp static struct tog_view *
828 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
829 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
830 ea5e7bb5 2018-08-01 stsp {
831 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
832 ea5e7bb5 2018-08-01 stsp
833 ea5e7bb5 2018-08-01 stsp if (view == NULL)
834 ea5e7bb5 2018-08-01 stsp return NULL;
835 ea5e7bb5 2018-08-01 stsp
836 d6b05b5b 2018-08-04 stsp view->type = type;
837 f7d12f7e 2018-08-01 stsp view->lines = LINES;
838 f7d12f7e 2018-08-01 stsp view->cols = COLS;
839 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
840 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
841 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
842 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
843 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
844 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
845 96a765a8 2018-08-04 stsp view_close(view);
846 ea5e7bb5 2018-08-01 stsp return NULL;
847 ea5e7bb5 2018-08-01 stsp }
848 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
849 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
850 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
851 96a765a8 2018-08-04 stsp view_close(view);
852 ea5e7bb5 2018-08-01 stsp return NULL;
853 ea5e7bb5 2018-08-01 stsp }
854 ea5e7bb5 2018-08-01 stsp
855 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
856 ea5e7bb5 2018-08-01 stsp return view;
857 cdf1ee82 2018-08-01 stsp }
858 cdf1ee82 2018-08-01 stsp
859 0cf4efb1 2018-09-29 stsp static int
860 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
861 0cf4efb1 2018-09-29 stsp {
862 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
863 0cf4efb1 2018-09-29 stsp return 0;
864 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
865 5c60c32a 2018-10-18 stsp }
866 5c60c32a 2018-10-18 stsp
867 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
868 a5d43cac 2022-07-01 thomas static int
869 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
870 a5d43cac 2022-07-01 thomas {
871 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
872 a5d43cac 2022-07-01 thomas }
873 a5d43cac 2022-07-01 thomas
874 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
875 5c60c32a 2018-10-18 stsp
876 5c60c32a 2018-10-18 stsp static const struct got_error *
877 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
878 5c60c32a 2018-10-18 stsp {
879 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
880 5c60c32a 2018-10-18 stsp
881 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
882 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
883 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
884 53d2bdd3 2022-07-10 thomas else
885 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
886 a5d43cac 2022-07-01 thomas view->begin_x = 0;
887 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
888 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
889 53d2bdd3 2022-07-10 thomas view->cols > 119)
890 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
891 53d2bdd3 2022-07-10 thomas else
892 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
893 a5d43cac 2022-07-01 thomas view->begin_y = 0;
894 a5d43cac 2022-07-01 thomas }
895 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
896 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
897 5c60c32a 2018-10-18 stsp view->lines = LINES;
898 5c60c32a 2018-10-18 stsp view->cols = COLS;
899 5c60c32a 2018-10-18 stsp err = view_resize(view);
900 5c60c32a 2018-10-18 stsp if (err)
901 5c60c32a 2018-10-18 stsp return err;
902 5c60c32a 2018-10-18 stsp
903 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
904 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
905 a5d43cac 2022-07-01 thomas
906 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
907 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
908 5c60c32a 2018-10-18 stsp
909 5c60c32a 2018-10-18 stsp return NULL;
910 5c60c32a 2018-10-18 stsp }
911 5c60c32a 2018-10-18 stsp
912 5c60c32a 2018-10-18 stsp static const struct got_error *
913 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
914 5c60c32a 2018-10-18 stsp {
915 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
916 5c60c32a 2018-10-18 stsp
917 5c60c32a 2018-10-18 stsp view->begin_x = 0;
918 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
919 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
920 5c60c32a 2018-10-18 stsp view->ncols = COLS;
921 5c60c32a 2018-10-18 stsp view->lines = LINES;
922 5c60c32a 2018-10-18 stsp view->cols = COLS;
923 5c60c32a 2018-10-18 stsp err = view_resize(view);
924 5c60c32a 2018-10-18 stsp if (err)
925 5c60c32a 2018-10-18 stsp return err;
926 5c60c32a 2018-10-18 stsp
927 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
928 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
929 5c60c32a 2018-10-18 stsp
930 5c60c32a 2018-10-18 stsp return NULL;
931 0cf4efb1 2018-09-29 stsp }
932 0cf4efb1 2018-09-29 stsp
933 5c60c32a 2018-10-18 stsp static int
934 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
935 5c60c32a 2018-10-18 stsp {
936 5c60c32a 2018-10-18 stsp return view->parent == NULL;
937 5c60c32a 2018-10-18 stsp }
938 5c60c32a 2018-10-18 stsp
939 b65b3ea0 2022-06-23 thomas static int
940 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
941 b65b3ea0 2022-06-23 thomas {
942 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
943 e44940c3 2022-07-01 thomas }
944 e44940c3 2022-07-01 thomas
945 e44940c3 2022-07-01 thomas static int
946 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
947 e44940c3 2022-07-01 thomas {
948 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
949 b65b3ea0 2022-06-23 thomas }
950 b65b3ea0 2022-06-23 thomas
951 444d5325 2022-07-03 thomas static int
952 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
953 444d5325 2022-07-03 thomas {
954 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
955 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
956 444d5325 2022-07-03 thomas }
957 444d5325 2022-07-03 thomas
958 a5d43cac 2022-07-01 thomas static void
959 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
960 a5d43cac 2022-07-01 thomas {
961 a5d43cac 2022-07-01 thomas PANEL *panel;
962 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
963 b65b3ea0 2022-06-23 thomas
964 a5d43cac 2022-07-01 thomas if (view->parent)
965 a5d43cac 2022-07-01 thomas return view_border(view->parent);
966 a5d43cac 2022-07-01 thomas
967 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
968 a5d43cac 2022-07-01 thomas if (panel == NULL)
969 a5d43cac 2022-07-01 thomas return;
970 a5d43cac 2022-07-01 thomas
971 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
972 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
973 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
974 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
975 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
976 a5d43cac 2022-07-01 thomas else
977 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
978 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
979 a5d43cac 2022-07-01 thomas }
980 a5d43cac 2022-07-01 thomas
981 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
982 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
983 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
984 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
985 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
986 a5d43cac 2022-07-01 thomas
987 4d8c2215 2018-08-19 stsp static const struct got_error *
988 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
989 f7d12f7e 2018-08-01 stsp {
990 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
991 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
992 f7d12f7e 2018-08-01 stsp
993 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
994 a5d43cac 2022-07-01 thomas
995 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
996 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
997 0cf4efb1 2018-09-29 stsp else
998 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
999 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1000 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1001 0cf4efb1 2018-09-29 stsp else
1002 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1003 6d0fee91 2018-08-01 stsp
1004 4918811f 2022-07-01 thomas if (view->child) {
1005 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1006 a5d43cac 2022-07-01 thomas
1007 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1008 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1009 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1010 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1011 40236d76 2022-06-23 thomas ncols = COLS;
1012 40236d76 2022-06-23 thomas
1013 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1014 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1015 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1016 5c60c32a 2018-10-18 stsp else
1017 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1018 5c60c32a 2018-10-18 stsp } else {
1019 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1020 40236d76 2022-06-23 thomas
1021 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1022 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1023 a5d43cac 2022-07-01 thomas }
1024 a5d43cac 2022-07-01 thomas /*
1025 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1026 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1027 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1028 a5d43cac 2022-07-01 thomas */
1029 a5d43cac 2022-07-01 thomas if (hs) {
1030 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1031 a5d43cac 2022-07-01 thomas if (err)
1032 a5d43cac 2022-07-01 thomas return err;
1033 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1034 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1035 a5d43cac 2022-07-01 thomas if (err)
1036 a5d43cac 2022-07-01 thomas return err;
1037 a5d43cac 2022-07-01 thomas }
1038 a5d43cac 2022-07-01 thomas view_border(view);
1039 a5d43cac 2022-07-01 thomas update_panels();
1040 a5d43cac 2022-07-01 thomas doupdate();
1041 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1042 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1043 a5d43cac 2022-07-01 thomas }
1044 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1045 40236d76 2022-06-23 thomas ncols = COLS;
1046 669b5ffa 2018-10-07 stsp
1047 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1048 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1049 ea0bff04 2022-07-19 thomas if (err)
1050 ea0bff04 2022-07-19 thomas return err;
1051 ea0bff04 2022-07-19 thomas }
1052 ea0bff04 2022-07-19 thomas
1053 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1054 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1055 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1056 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1057 40236d76 2022-06-23 thomas wclear(view->window);
1058 40236d76 2022-06-23 thomas
1059 40236d76 2022-06-23 thomas view->nlines = nlines;
1060 40236d76 2022-06-23 thomas view->ncols = ncols;
1061 40236d76 2022-06-23 thomas view->lines = LINES;
1062 40236d76 2022-06-23 thomas view->cols = COLS;
1063 40236d76 2022-06-23 thomas
1064 5c60c32a 2018-10-18 stsp return NULL;
1065 ea0bff04 2022-07-19 thomas }
1066 ea0bff04 2022-07-19 thomas
1067 ea0bff04 2022-07-19 thomas static const struct got_error *
1068 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1069 ea0bff04 2022-07-19 thomas {
1070 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1071 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1072 3a0139e8 2022-07-22 thomas int n = 0;
1073 ea0bff04 2022-07-19 thomas
1074 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1075 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1076 3a0139e8 2022-07-22 thomas
1077 ea0bff04 2022-07-19 thomas /*
1078 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1079 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1080 ea0bff04 2022-07-19 thomas */
1081 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1082 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1083 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1084 ea0bff04 2022-07-19 thomas }
1085 ea0bff04 2022-07-19 thomas
1086 ea0bff04 2022-07-19 thomas return err;
1087 97cb21cd 2022-07-12 thomas }
1088 97cb21cd 2022-07-12 thomas
1089 97cb21cd 2022-07-12 thomas static void
1090 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1091 97cb21cd 2022-07-12 thomas {
1092 97cb21cd 2022-07-12 thomas if (n == 0)
1093 97cb21cd 2022-07-12 thomas return;
1094 97cb21cd 2022-07-12 thomas
1095 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1096 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1097 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1098 97cb21cd 2022-07-12 thomas else
1099 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1100 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1101 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1102 97cb21cd 2022-07-12 thomas view->offset -= n;
1103 97cb21cd 2022-07-12 thomas else
1104 97cb21cd 2022-07-12 thomas view->offset = 0;
1105 97cb21cd 2022-07-12 thomas }
1106 53d2bdd3 2022-07-10 thomas }
1107 53d2bdd3 2022-07-10 thomas
1108 53d2bdd3 2022-07-10 thomas static const struct got_error *
1109 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1110 53d2bdd3 2022-07-10 thomas {
1111 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1112 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1113 53d2bdd3 2022-07-10 thomas
1114 53d2bdd3 2022-07-10 thomas if (view->parent)
1115 53d2bdd3 2022-07-10 thomas v = view->parent;
1116 53d2bdd3 2022-07-10 thomas else
1117 53d2bdd3 2022-07-10 thomas v = view;
1118 53d2bdd3 2022-07-10 thomas
1119 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1120 53d2bdd3 2022-07-10 thomas return NULL;
1121 53d2bdd3 2022-07-10 thomas
1122 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1123 53d2bdd3 2022-07-10 thomas
1124 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1125 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1126 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1127 53d2bdd3 2022-07-10 thomas if (view->parent)
1128 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1129 53d2bdd3 2022-07-10 thomas else
1130 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1131 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1132 53d2bdd3 2022-07-10 thomas view->count = 0;
1133 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1134 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1135 53d2bdd3 2022-07-10 thomas view->count = 0;
1136 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1137 53d2bdd3 2022-07-10 thomas }
1138 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1139 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1140 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1141 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1142 53d2bdd3 2022-07-10 thomas if (err)
1143 53d2bdd3 2022-07-10 thomas return err;
1144 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1145 53d2bdd3 2022-07-10 thomas } else {
1146 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1147 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1148 53d2bdd3 2022-07-10 thomas if (view->parent)
1149 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1150 53d2bdd3 2022-07-10 thomas else
1151 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1152 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1153 53d2bdd3 2022-07-10 thomas view->count = 0;
1154 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1155 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1156 53d2bdd3 2022-07-10 thomas view->count = 0;
1157 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1158 53d2bdd3 2022-07-10 thomas }
1159 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1160 53d2bdd3 2022-07-10 thomas }
1161 53d2bdd3 2022-07-10 thomas
1162 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1163 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1164 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1165 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1166 53d2bdd3 2022-07-10 thomas
1167 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1168 53d2bdd3 2022-07-10 thomas if (err)
1169 53d2bdd3 2022-07-10 thomas return err;
1170 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1171 53d2bdd3 2022-07-10 thomas if (err)
1172 53d2bdd3 2022-07-10 thomas return err;
1173 53d2bdd3 2022-07-10 thomas
1174 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1175 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1176 53d2bdd3 2022-07-10 thomas if (err)
1177 53d2bdd3 2022-07-10 thomas return err;
1178 53d2bdd3 2022-07-10 thomas }
1179 53d2bdd3 2022-07-10 thomas
1180 3a0139e8 2022-07-22 thomas if (v->resize)
1181 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1182 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1183 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1184 53d2bdd3 2022-07-10 thomas
1185 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1186 53d2bdd3 2022-07-10 thomas
1187 53d2bdd3 2022-07-10 thomas return err;
1188 53d2bdd3 2022-07-10 thomas }
1189 53d2bdd3 2022-07-10 thomas
1190 53d2bdd3 2022-07-10 thomas static void
1191 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1192 53d2bdd3 2022-07-10 thomas {
1193 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1194 53d2bdd3 2022-07-10 thomas
1195 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1196 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1197 669b5ffa 2018-10-07 stsp }
1198 669b5ffa 2018-10-07 stsp
1199 669b5ffa 2018-10-07 stsp static const struct got_error *
1200 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1201 669b5ffa 2018-10-07 stsp {
1202 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1203 669b5ffa 2018-10-07 stsp
1204 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1205 669b5ffa 2018-10-07 stsp return NULL;
1206 669b5ffa 2018-10-07 stsp
1207 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1208 669b5ffa 2018-10-07 stsp view->child = NULL;
1209 669b5ffa 2018-10-07 stsp return err;
1210 669b5ffa 2018-10-07 stsp }
1211 669b5ffa 2018-10-07 stsp
1212 40236d76 2022-06-23 thomas static const struct got_error *
1213 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1214 669b5ffa 2018-10-07 stsp {
1215 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1216 53d2bdd3 2022-07-10 thomas
1217 669b5ffa 2018-10-07 stsp view->child = child;
1218 669b5ffa 2018-10-07 stsp child->parent = view;
1219 40236d76 2022-06-23 thomas
1220 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1221 53d2bdd3 2022-07-10 thomas if (err)
1222 53d2bdd3 2022-07-10 thomas return err;
1223 53d2bdd3 2022-07-10 thomas
1224 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1225 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1226 53d2bdd3 2022-07-10 thomas
1227 53d2bdd3 2022-07-10 thomas return err;
1228 bfddd0d9 2018-09-29 stsp }
1229 2a31b33b 2022-07-23 thomas
1230 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1231 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1232 2a31b33b 2022-07-23 thomas
1233 2a31b33b 2022-07-23 thomas static const struct got_error *
1234 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1235 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1236 2a31b33b 2022-07-23 thomas {
1237 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1238 2a31b33b 2022-07-23 thomas const struct got_error *err;
1239 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1240 2a31b33b 2022-07-23 thomas
1241 2a31b33b 2022-07-23 thomas *requested = NULL;
1242 2a31b33b 2022-07-23 thomas
1243 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1244 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1245 bfddd0d9 2018-09-29 stsp
1246 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1247 2a31b33b 2022-07-23 thomas if (err)
1248 2a31b33b 2022-07-23 thomas return err;
1249 2a31b33b 2022-07-23 thomas
1250 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1251 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1252 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1253 2a31b33b 2022-07-23 thomas if (err)
1254 2a31b33b 2022-07-23 thomas return err;
1255 2a31b33b 2022-07-23 thomas }
1256 2a31b33b 2022-07-23 thomas
1257 2a31b33b 2022-07-23 thomas view->focussed = 0;
1258 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1259 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1260 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1261 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1262 2a31b33b 2022-07-23 thomas
1263 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1264 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1265 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1266 2a31b33b 2022-07-23 thomas if (err)
1267 2a31b33b 2022-07-23 thomas return err;
1268 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1269 2a31b33b 2022-07-23 thomas if (err)
1270 2a31b33b 2022-07-23 thomas return err;
1271 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1272 2a31b33b 2022-07-23 thomas } else
1273 2a31b33b 2022-07-23 thomas *requested = new_view;
1274 2a31b33b 2022-07-23 thomas
1275 2a31b33b 2022-07-23 thomas return NULL;
1276 2a31b33b 2022-07-23 thomas }
1277 2a31b33b 2022-07-23 thomas
1278 34bc9ec9 2019-02-22 stsp static void
1279 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1280 25791caa 2018-10-24 stsp {
1281 25791caa 2018-10-24 stsp int cols, lines;
1282 25791caa 2018-10-24 stsp struct winsize size;
1283 25791caa 2018-10-24 stsp
1284 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1285 25791caa 2018-10-24 stsp cols = 80; /* Default */
1286 25791caa 2018-10-24 stsp lines = 24;
1287 25791caa 2018-10-24 stsp } else {
1288 25791caa 2018-10-24 stsp cols = size.ws_col;
1289 25791caa 2018-10-24 stsp lines = size.ws_row;
1290 25791caa 2018-10-24 stsp }
1291 25791caa 2018-10-24 stsp resize_term(lines, cols);
1292 2b49a8ae 2019-06-22 stsp }
1293 2b49a8ae 2019-06-22 stsp
1294 2b49a8ae 2019-06-22 stsp static const struct got_error *
1295 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1296 2b49a8ae 2019-06-22 stsp {
1297 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1298 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1299 2b49a8ae 2019-06-22 stsp char pattern[1024];
1300 2b49a8ae 2019-06-22 stsp int ret;
1301 c0c4acc8 2021-01-24 stsp
1302 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1303 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1304 c0c4acc8 2021-01-24 stsp view->searching = 0;
1305 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1306 c0c4acc8 2021-01-24 stsp }
1307 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1308 2b49a8ae 2019-06-22 stsp
1309 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1310 2b49a8ae 2019-06-22 stsp return NULL;
1311 2b49a8ae 2019-06-22 stsp
1312 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1313 a5d43cac 2022-07-01 thomas v = view->child;
1314 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1315 d07291c6 2022-12-30 thomas v = view->parent;
1316 2b49a8ae 2019-06-22 stsp
1317 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1318 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1319 a5d43cac 2022-07-01 thomas
1320 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1321 2b49a8ae 2019-06-22 stsp nocbreak();
1322 2b49a8ae 2019-06-22 stsp echo();
1323 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1324 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1325 2b49a8ae 2019-06-22 stsp cbreak();
1326 2b49a8ae 2019-06-22 stsp noecho();
1327 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1328 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1329 2b49a8ae 2019-06-22 stsp return NULL;
1330 2b49a8ae 2019-06-22 stsp
1331 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1332 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1333 7c32bd05 2019-06-22 stsp if (err) {
1334 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1335 7c32bd05 2019-06-22 stsp return err;
1336 7c32bd05 2019-06-22 stsp }
1337 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1338 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1339 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1340 2b49a8ae 2019-06-22 stsp view->search_next(view);
1341 2b49a8ae 2019-06-22 stsp }
1342 2b49a8ae 2019-06-22 stsp
1343 2b49a8ae 2019-06-22 stsp return NULL;
1344 64486692 2022-07-07 thomas }
1345 64486692 2022-07-07 thomas
1346 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1347 64486692 2022-07-07 thomas static const struct got_error *
1348 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1349 64486692 2022-07-07 thomas {
1350 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1351 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1352 64486692 2022-07-07 thomas
1353 64486692 2022-07-07 thomas if (view->parent)
1354 64486692 2022-07-07 thomas v = view->parent;
1355 64486692 2022-07-07 thomas else
1356 64486692 2022-07-07 thomas v = view;
1357 64486692 2022-07-07 thomas
1358 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1359 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1360 ddbc4d37 2022-07-12 thomas else
1361 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1362 64486692 2022-07-07 thomas
1363 ddbc4d37 2022-07-12 thomas if (!v->child)
1364 ddbc4d37 2022-07-12 thomas return NULL;
1365 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1366 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1367 ddbc4d37 2022-07-12 thomas
1368 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1369 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1370 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1371 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1372 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1373 64486692 2022-07-07 thomas
1374 ddbc4d37 2022-07-12 thomas
1375 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1376 64486692 2022-07-07 thomas v->ncols = COLS;
1377 64486692 2022-07-07 thomas v->child->ncols = COLS;
1378 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1379 64486692 2022-07-07 thomas
1380 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1381 64486692 2022-07-07 thomas if (err)
1382 64486692 2022-07-07 thomas return err;
1383 64486692 2022-07-07 thomas }
1384 64486692 2022-07-07 thomas v->child->mode = v->mode;
1385 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1386 64486692 2022-07-07 thomas v->focus_child = 1;
1387 64486692 2022-07-07 thomas
1388 64486692 2022-07-07 thomas err = view_fullscreen(v);
1389 64486692 2022-07-07 thomas if (err)
1390 64486692 2022-07-07 thomas return err;
1391 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1392 64486692 2022-07-07 thomas if (err)
1393 64486692 2022-07-07 thomas return err;
1394 64486692 2022-07-07 thomas
1395 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1396 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1397 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1398 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1399 cf1fe301 2022-07-29 thomas if (err)
1400 cf1fe301 2022-07-29 thomas return err;
1401 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1402 cf1fe301 2022-07-29 thomas if (err)
1403 cf1fe301 2022-07-29 thomas return err;
1404 ddbc4d37 2022-07-12 thomas } else {
1405 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1406 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1407 64486692 2022-07-07 thomas }
1408 f4e6231a 2022-07-22 thomas if (v->resize)
1409 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1410 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1411 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1412 64486692 2022-07-07 thomas
1413 64486692 2022-07-07 thomas return err;
1414 0cf4efb1 2018-09-29 stsp }
1415 6d0fee91 2018-08-01 stsp
1416 07b0611c 2022-06-23 thomas /*
1417 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1418 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1419 07b0611c 2022-06-23 thomas */
1420 07b0611c 2022-06-23 thomas static int
1421 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1422 07b0611c 2022-06-23 thomas {
1423 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1424 a5d43cac 2022-07-01 thomas int x, n = 0;
1425 07b0611c 2022-06-23 thomas
1426 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1427 a5d43cac 2022-07-01 thomas v = view->child;
1428 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1429 a5d43cac 2022-07-01 thomas v = view->parent;
1430 a5d43cac 2022-07-01 thomas
1431 07b0611c 2022-06-23 thomas view->count = 0;
1432 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1433 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1434 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1435 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1436 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1437 07b0611c 2022-06-23 thomas
1438 07b0611c 2022-06-23 thomas do {
1439 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1440 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1441 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1442 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1443 a5d43cac 2022-07-01 thomas }
1444 a5d43cac 2022-07-01 thomas
1445 07b0611c 2022-06-23 thomas /*
1446 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1447 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1448 07b0611c 2022-06-23 thomas */
1449 07b0611c 2022-06-23 thomas if (n >= 9999999)
1450 07b0611c 2022-06-23 thomas n = 9999999;
1451 07b0611c 2022-06-23 thomas else
1452 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1453 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1454 07b0611c 2022-06-23 thomas
1455 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1456 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1457 07dd3ed3 2022-08-06 thomas n = 0;
1458 07dd3ed3 2022-08-06 thomas c = 0;
1459 07dd3ed3 2022-08-06 thomas }
1460 07dd3ed3 2022-08-06 thomas
1461 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1462 07b0611c 2022-06-23 thomas view->count = n;
1463 07b0611c 2022-06-23 thomas
1464 07b0611c 2022-06-23 thomas return c;
1465 07b0611c 2022-06-23 thomas }
1466 07b0611c 2022-06-23 thomas
1467 0cf4efb1 2018-09-29 stsp static const struct got_error *
1468 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1469 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1470 e5a0f69f 2018-08-18 stsp {
1471 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1472 669b5ffa 2018-10-07 stsp struct tog_view *v;
1473 1a76625f 2018-10-22 stsp int ch, errcode;
1474 e5a0f69f 2018-08-18 stsp
1475 e5a0f69f 2018-08-18 stsp *new = NULL;
1476 8f4ed634 2020-03-26 stsp
1477 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1478 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1479 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1480 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1481 07b0611c 2022-06-23 thomas view->count = 0;
1482 07b0611c 2022-06-23 thomas }
1483 e5a0f69f 2018-08-18 stsp
1484 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1485 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1486 82954512 2020-02-03 stsp if (errcode)
1487 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1488 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1489 3da8ef85 2021-09-21 stsp sched_yield();
1490 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1491 82954512 2020-02-03 stsp if (errcode)
1492 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1493 82954512 2020-02-03 stsp "pthread_mutex_lock");
1494 60493ae3 2019-06-20 stsp view->search_next(view);
1495 60493ae3 2019-06-20 stsp return NULL;
1496 60493ae3 2019-06-20 stsp }
1497 60493ae3 2019-06-20 stsp
1498 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1499 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1500 1a76625f 2018-10-22 stsp if (errcode)
1501 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1502 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1503 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1504 634cb454 2022-07-03 thomas cbreak();
1505 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1506 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1507 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1508 634cb454 2022-07-03 thomas view->count = 0;
1509 634cb454 2022-07-03 thomas else
1510 634cb454 2022-07-03 thomas ch = view->ch;
1511 634cb454 2022-07-03 thomas } else {
1512 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1513 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1514 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1515 07b0611c 2022-06-23 thomas }
1516 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1517 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1518 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1519 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1520 1a76625f 2018-10-22 stsp if (errcode)
1521 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1522 25791caa 2018-10-24 stsp
1523 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1524 25791caa 2018-10-24 stsp tog_resizeterm();
1525 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1526 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1527 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1528 25791caa 2018-10-24 stsp err = view_resize(v);
1529 25791caa 2018-10-24 stsp if (err)
1530 25791caa 2018-10-24 stsp return err;
1531 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1532 25791caa 2018-10-24 stsp if (err)
1533 25791caa 2018-10-24 stsp return err;
1534 cdfcfb03 2020-12-06 stsp if (v->child) {
1535 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1536 cdfcfb03 2020-12-06 stsp if (err)
1537 cdfcfb03 2020-12-06 stsp return err;
1538 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1539 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1540 cdfcfb03 2020-12-06 stsp if (err)
1541 cdfcfb03 2020-12-06 stsp return err;
1542 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1543 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1544 53d2bdd3 2022-07-10 thomas if (err)
1545 53d2bdd3 2022-07-10 thomas return err;
1546 53d2bdd3 2022-07-10 thomas }
1547 cdfcfb03 2020-12-06 stsp }
1548 25791caa 2018-10-24 stsp }
1549 25791caa 2018-10-24 stsp }
1550 25791caa 2018-10-24 stsp
1551 e5a0f69f 2018-08-18 stsp switch (ch) {
1552 fc2737d5 2022-09-15 thomas case '?':
1553 fc2737d5 2022-09-15 thomas case 'H':
1554 fc2737d5 2022-09-15 thomas case KEY_F(1):
1555 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1556 fc2737d5 2022-09-15 thomas err = view->reset(view);
1557 fc2737d5 2022-09-15 thomas else
1558 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1559 fc2737d5 2022-09-15 thomas break;
1560 1e37a5c2 2019-05-12 jcs case '\t':
1561 07b0611c 2022-06-23 thomas view->count = 0;
1562 1e37a5c2 2019-05-12 jcs if (view->child) {
1563 e78dc838 2020-12-04 stsp view->focussed = 0;
1564 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1565 e78dc838 2020-12-04 stsp view->focus_child = 1;
1566 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1567 e78dc838 2020-12-04 stsp view->focussed = 0;
1568 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1569 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1570 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1571 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1572 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1573 3a0139e8 2022-07-22 thomas 0);
1574 a5d43cac 2022-07-01 thomas if (err)
1575 a5d43cac 2022-07-01 thomas return err;
1576 a5d43cac 2022-07-01 thomas }
1577 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1578 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1579 a5d43cac 2022-07-01 thomas if (err)
1580 a5d43cac 2022-07-01 thomas return err;
1581 a5d43cac 2022-07-01 thomas }
1582 1e37a5c2 2019-05-12 jcs }
1583 1e37a5c2 2019-05-12 jcs break;
1584 1e37a5c2 2019-05-12 jcs case 'q':
1585 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1586 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1587 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1588 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1589 a5d43cac 2022-07-01 thomas if (err)
1590 a5d43cac 2022-07-01 thomas break;
1591 a5d43cac 2022-07-01 thomas }
1592 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1593 a5d43cac 2022-07-01 thomas }
1594 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1595 9970f7fc 2020-12-03 stsp view->dying = 1;
1596 1e37a5c2 2019-05-12 jcs break;
1597 1e37a5c2 2019-05-12 jcs case 'Q':
1598 1e37a5c2 2019-05-12 jcs *done = 1;
1599 1e37a5c2 2019-05-12 jcs break;
1600 1c5e5faa 2022-06-23 thomas case 'F':
1601 07b0611c 2022-06-23 thomas view->count = 0;
1602 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1603 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1604 1e37a5c2 2019-05-12 jcs break;
1605 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1606 e78dc838 2020-12-04 stsp view->focussed = 0;
1607 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1608 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1609 53d2bdd3 2022-07-10 thomas } else {
1610 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1611 53d2bdd3 2022-07-10 thomas if (!err)
1612 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1613 53d2bdd3 2022-07-10 thomas }
1614 1e37a5c2 2019-05-12 jcs if (err)
1615 1e37a5c2 2019-05-12 jcs break;
1616 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1617 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1618 1e37a5c2 2019-05-12 jcs } else {
1619 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1620 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1621 e78dc838 2020-12-04 stsp view->focussed = 1;
1622 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1623 1e37a5c2 2019-05-12 jcs } else {
1624 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1625 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1626 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1627 53d2bdd3 2022-07-10 thomas if (!err)
1628 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1629 669b5ffa 2018-10-07 stsp }
1630 1e37a5c2 2019-05-12 jcs if (err)
1631 1e37a5c2 2019-05-12 jcs break;
1632 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1633 a5d43cac 2022-07-01 thomas }
1634 a5d43cac 2022-07-01 thomas if (err)
1635 a5d43cac 2022-07-01 thomas break;
1636 3a0139e8 2022-07-22 thomas if (view->resize) {
1637 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1638 a5d43cac 2022-07-01 thomas if (err)
1639 a5d43cac 2022-07-01 thomas break;
1640 1e37a5c2 2019-05-12 jcs }
1641 a5d43cac 2022-07-01 thomas if (view->parent)
1642 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1643 a5d43cac 2022-07-01 thomas if (!err)
1644 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1645 1e37a5c2 2019-05-12 jcs break;
1646 64486692 2022-07-07 thomas case 'S':
1647 53d2bdd3 2022-07-10 thomas view->count = 0;
1648 64486692 2022-07-07 thomas err = switch_split(view);
1649 53d2bdd3 2022-07-10 thomas break;
1650 53d2bdd3 2022-07-10 thomas case '-':
1651 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1652 64486692 2022-07-07 thomas break;
1653 53d2bdd3 2022-07-10 thomas case '+':
1654 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1655 53d2bdd3 2022-07-10 thomas break;
1656 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1657 60493ae3 2019-06-20 stsp break;
1658 60493ae3 2019-06-20 stsp case '/':
1659 07b0611c 2022-06-23 thomas view->count = 0;
1660 60493ae3 2019-06-20 stsp if (view->search_start)
1661 2b49a8ae 2019-06-22 stsp view_search_start(view);
1662 60493ae3 2019-06-20 stsp else
1663 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1664 1e37a5c2 2019-05-12 jcs break;
1665 b1bf1435 2019-06-21 stsp case 'N':
1666 60493ae3 2019-06-20 stsp case 'n':
1667 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1668 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1669 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1670 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1671 60493ae3 2019-06-20 stsp view->search_next(view);
1672 60493ae3 2019-06-20 stsp } else
1673 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1674 adf4c9e0 2022-07-03 thomas break;
1675 adf4c9e0 2022-07-03 thomas case 'A':
1676 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1677 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1678 adf4c9e0 2022-07-03 thomas else
1679 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1680 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1681 adf4c9e0 2022-07-03 thomas if (v->reset) {
1682 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1683 adf4c9e0 2022-07-03 thomas if (err)
1684 adf4c9e0 2022-07-03 thomas return err;
1685 adf4c9e0 2022-07-03 thomas }
1686 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1687 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1688 adf4c9e0 2022-07-03 thomas if (err)
1689 adf4c9e0 2022-07-03 thomas return err;
1690 adf4c9e0 2022-07-03 thomas }
1691 adf4c9e0 2022-07-03 thomas }
1692 60493ae3 2019-06-20 stsp break;
1693 1e37a5c2 2019-05-12 jcs default:
1694 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1695 1e37a5c2 2019-05-12 jcs break;
1696 e5a0f69f 2018-08-18 stsp }
1697 e5a0f69f 2018-08-18 stsp
1698 e5a0f69f 2018-08-18 stsp return err;
1699 bcbd79e2 2018-08-19 stsp }
1700 bcbd79e2 2018-08-19 stsp
1701 ef20f542 2022-06-26 thomas static int
1702 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1703 a3404814 2018-09-02 stsp {
1704 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1705 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1706 669b5ffa 2018-10-07 stsp return 0;
1707 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1708 669b5ffa 2018-10-07 stsp return 0;
1709 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1710 a3404814 2018-09-02 stsp return 0;
1711 a3404814 2018-09-02 stsp
1712 669b5ffa 2018-10-07 stsp return view->focussed;
1713 a3404814 2018-09-02 stsp }
1714 a3404814 2018-09-02 stsp
1715 bcbd79e2 2018-08-19 stsp static const struct got_error *
1716 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1717 e5a0f69f 2018-08-18 stsp {
1718 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1719 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1720 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1721 64486692 2022-07-07 thomas char *mode;
1722 fd823528 2018-10-22 stsp int fast_refresh = 10;
1723 1a76625f 2018-10-22 stsp int done = 0, errcode;
1724 e5a0f69f 2018-08-18 stsp
1725 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1726 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1727 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1728 64486692 2022-07-07 thomas else
1729 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1730 64486692 2022-07-07 thomas
1731 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1732 1a76625f 2018-10-22 stsp if (errcode)
1733 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1734 1a76625f 2018-10-22 stsp
1735 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1736 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1737 e5a0f69f 2018-08-18 stsp
1738 1004088d 2018-09-29 stsp view->focussed = 1;
1739 878940b7 2018-09-29 stsp err = view->show(view);
1740 0cf4efb1 2018-09-29 stsp if (err)
1741 0cf4efb1 2018-09-29 stsp return err;
1742 0cf4efb1 2018-09-29 stsp update_panels();
1743 0cf4efb1 2018-09-29 stsp doupdate();
1744 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1745 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1746 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1747 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1748 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1749 fd823528 2018-10-22 stsp
1750 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1751 e5a0f69f 2018-08-18 stsp if (err)
1752 e5a0f69f 2018-08-18 stsp break;
1753 9970f7fc 2020-12-03 stsp if (view->dying) {
1754 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1755 669b5ffa 2018-10-07 stsp
1756 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1757 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1758 9970f7fc 2020-12-03 stsp entry);
1759 e78dc838 2020-12-04 stsp else if (view->parent)
1760 669b5ffa 2018-10-07 stsp prev = view->parent;
1761 669b5ffa 2018-10-07 stsp
1762 e78dc838 2020-12-04 stsp if (view->parent) {
1763 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1764 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1765 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1766 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1767 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1768 40236d76 2022-06-23 thomas if (err)
1769 40236d76 2022-06-23 thomas break;
1770 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1771 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1772 e78dc838 2020-12-04 stsp } else
1773 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1774 669b5ffa 2018-10-07 stsp
1775 9970f7fc 2020-12-03 stsp err = view_close(view);
1776 fb59748f 2020-12-05 stsp if (err)
1777 e5a0f69f 2018-08-18 stsp goto done;
1778 669b5ffa 2018-10-07 stsp
1779 e78dc838 2020-12-04 stsp view = NULL;
1780 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1781 e78dc838 2020-12-04 stsp if (v->focussed)
1782 e78dc838 2020-12-04 stsp break;
1783 0cf4efb1 2018-09-29 stsp }
1784 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1785 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1786 e78dc838 2020-12-04 stsp if (prev)
1787 e78dc838 2020-12-04 stsp view = prev;
1788 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1789 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1790 e78dc838 2020-12-04 stsp tog_view_list_head);
1791 e78dc838 2020-12-04 stsp }
1792 e78dc838 2020-12-04 stsp if (view) {
1793 e78dc838 2020-12-04 stsp if (view->focus_child) {
1794 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1795 e78dc838 2020-12-04 stsp view = view->child;
1796 e78dc838 2020-12-04 stsp } else
1797 e78dc838 2020-12-04 stsp view->focussed = 1;
1798 e78dc838 2020-12-04 stsp }
1799 e78dc838 2020-12-04 stsp }
1800 e5a0f69f 2018-08-18 stsp }
1801 bcbd79e2 2018-08-19 stsp if (new_view) {
1802 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1803 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1804 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1805 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1806 86c66b02 2018-10-18 stsp continue;
1807 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1808 86c66b02 2018-10-18 stsp err = view_close(v);
1809 86c66b02 2018-10-18 stsp if (err)
1810 86c66b02 2018-10-18 stsp goto done;
1811 86c66b02 2018-10-18 stsp break;
1812 86c66b02 2018-10-18 stsp }
1813 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1814 fed7eaa8 2018-10-24 stsp view = new_view;
1815 7cd52833 2022-06-23 thomas }
1816 669b5ffa 2018-10-07 stsp if (view) {
1817 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1818 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1819 e78dc838 2020-12-04 stsp view = view->child;
1820 e78dc838 2020-12-04 stsp } else {
1821 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1822 e78dc838 2020-12-04 stsp view = view->parent;
1823 1a76625f 2018-10-22 stsp }
1824 e78dc838 2020-12-04 stsp show_panel(view->panel);
1825 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1826 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1827 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1828 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1829 669b5ffa 2018-10-07 stsp if (err)
1830 1a76625f 2018-10-22 stsp goto done;
1831 669b5ffa 2018-10-07 stsp }
1832 669b5ffa 2018-10-07 stsp err = view->show(view);
1833 0cf4efb1 2018-09-29 stsp if (err)
1834 1a76625f 2018-10-22 stsp goto done;
1835 669b5ffa 2018-10-07 stsp if (view->child) {
1836 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1837 669b5ffa 2018-10-07 stsp if (err)
1838 1a76625f 2018-10-22 stsp goto done;
1839 669b5ffa 2018-10-07 stsp }
1840 1a76625f 2018-10-22 stsp update_panels();
1841 1a76625f 2018-10-22 stsp doupdate();
1842 0cf4efb1 2018-09-29 stsp }
1843 e5a0f69f 2018-08-18 stsp }
1844 e5a0f69f 2018-08-18 stsp done:
1845 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1846 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1847 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1848 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1849 f2d749db 2022-07-12 thomas close_err = view_close(view);
1850 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1851 f2d749db 2022-07-12 thomas err = close_err;
1852 e5a0f69f 2018-08-18 stsp }
1853 1a76625f 2018-10-22 stsp
1854 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1855 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1856 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1857 1a76625f 2018-10-22 stsp
1858 e5a0f69f 2018-08-18 stsp return err;
1859 ea5e7bb5 2018-08-01 stsp }
1860 ea5e7bb5 2018-08-01 stsp
1861 4ed7e80c 2018-05-20 stsp __dead static void
1862 9f7d7167 2018-04-29 stsp usage_log(void)
1863 9f7d7167 2018-04-29 stsp {
1864 80ddbec8 2018-04-29 stsp endwin();
1865 c70c5802 2018-08-01 stsp fprintf(stderr,
1866 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1867 9f7d7167 2018-04-29 stsp getprogname());
1868 9f7d7167 2018-04-29 stsp exit(1);
1869 80ddbec8 2018-04-29 stsp }
1870 80ddbec8 2018-04-29 stsp
1871 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1872 80ddbec8 2018-04-29 stsp static const struct got_error *
1873 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1874 963b370f 2018-05-20 stsp {
1875 00dfcb92 2018-06-11 stsp char *vis = NULL;
1876 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1877 963b370f 2018-05-20 stsp
1878 963b370f 2018-05-20 stsp *ws = NULL;
1879 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1880 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1881 00dfcb92 2018-06-11 stsp int vislen;
1882 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1883 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1884 00dfcb92 2018-06-11 stsp
1885 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1886 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1887 00dfcb92 2018-06-11 stsp if (err)
1888 00dfcb92 2018-06-11 stsp return err;
1889 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1890 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1891 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1892 a7f50699 2018-06-11 stsp goto done;
1893 a7f50699 2018-06-11 stsp }
1894 00dfcb92 2018-06-11 stsp }
1895 963b370f 2018-05-20 stsp
1896 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1897 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1898 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1899 a7f50699 2018-06-11 stsp goto done;
1900 a7f50699 2018-06-11 stsp }
1901 963b370f 2018-05-20 stsp
1902 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1903 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1904 a7f50699 2018-06-11 stsp done:
1905 00dfcb92 2018-06-11 stsp free(vis);
1906 963b370f 2018-05-20 stsp if (err) {
1907 963b370f 2018-05-20 stsp free(*ws);
1908 963b370f 2018-05-20 stsp *ws = NULL;
1909 963b370f 2018-05-20 stsp *wlen = 0;
1910 963b370f 2018-05-20 stsp }
1911 963b370f 2018-05-20 stsp return err;
1912 05171be4 2022-06-23 thomas }
1913 05171be4 2022-06-23 thomas
1914 05171be4 2022-06-23 thomas static const struct got_error *
1915 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1916 05171be4 2022-06-23 thomas {
1917 05171be4 2022-06-23 thomas char *dst;
1918 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1919 05171be4 2022-06-23 thomas
1920 05171be4 2022-06-23 thomas *ptr = NULL;
1921 05171be4 2022-06-23 thomas n = len = strlen(src);
1922 83316834 2022-06-23 thomas dst = malloc(n + 1);
1923 05171be4 2022-06-23 thomas if (dst == NULL)
1924 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1925 05171be4 2022-06-23 thomas
1926 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1927 05171be4 2022-06-23 thomas const char c = src[idx];
1928 05171be4 2022-06-23 thomas
1929 05171be4 2022-06-23 thomas if (c == '\t') {
1930 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1931 b235ad5d 2022-06-23 thomas char *p;
1932 b235ad5d 2022-06-23 thomas
1933 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1934 83316834 2022-06-23 thomas if (p == NULL) {
1935 83316834 2022-06-23 thomas free(dst);
1936 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1937 83316834 2022-06-23 thomas
1938 83316834 2022-06-23 thomas }
1939 83316834 2022-06-23 thomas dst = p;
1940 05171be4 2022-06-23 thomas n += nb;
1941 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1942 05171be4 2022-06-23 thomas sz += nb;
1943 05171be4 2022-06-23 thomas } else
1944 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1945 05171be4 2022-06-23 thomas ++idx;
1946 05171be4 2022-06-23 thomas }
1947 05171be4 2022-06-23 thomas
1948 05171be4 2022-06-23 thomas dst[sz] = '\0';
1949 05171be4 2022-06-23 thomas *ptr = dst;
1950 05171be4 2022-06-23 thomas return NULL;
1951 963b370f 2018-05-20 stsp }
1952 963b370f 2018-05-20 stsp
1953 8d208d34 2022-06-23 thomas /*
1954 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1955 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1956 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1957 8d208d34 2022-06-23 thomas * *rcol.
1958 f91a2b48 2022-06-23 thomas */
1959 8d208d34 2022-06-23 thomas static int
1960 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1961 8d208d34 2022-06-23 thomas {
1962 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1963 f91a2b48 2022-06-23 thomas
1964 8d208d34 2022-06-23 thomas if (n == 0) {
1965 8d208d34 2022-06-23 thomas *rcol = cols;
1966 8d208d34 2022-06-23 thomas return off;
1967 8d208d34 2022-06-23 thomas }
1968 f91a2b48 2022-06-23 thomas
1969 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1970 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1971 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1972 8d208d34 2022-06-23 thomas else
1973 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1974 f91a2b48 2022-06-23 thomas
1975 8d208d34 2022-06-23 thomas if (width == -1) {
1976 8d208d34 2022-06-23 thomas width = 1;
1977 8d208d34 2022-06-23 thomas wline[i] = L'.';
1978 f91a2b48 2022-06-23 thomas }
1979 f91a2b48 2022-06-23 thomas
1980 8d208d34 2022-06-23 thomas if (cols + width > n)
1981 8d208d34 2022-06-23 thomas break;
1982 8d208d34 2022-06-23 thomas cols += width;
1983 f91a2b48 2022-06-23 thomas }
1984 f91a2b48 2022-06-23 thomas
1985 8d208d34 2022-06-23 thomas *rcol = cols;
1986 8d208d34 2022-06-23 thomas return i;
1987 f91a2b48 2022-06-23 thomas }
1988 f91a2b48 2022-06-23 thomas
1989 f91a2b48 2022-06-23 thomas /*
1990 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1991 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1992 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1993 f91a2b48 2022-06-23 thomas */
1994 f91a2b48 2022-06-23 thomas static const struct got_error *
1995 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1996 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1997 f91a2b48 2022-06-23 thomas {
1998 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1999 8d208d34 2022-06-23 thomas int cols;
2000 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2001 05171be4 2022-06-23 thomas char *exstr = NULL;
2002 963b370f 2018-05-20 stsp size_t wlen;
2003 8d208d34 2022-06-23 thomas int i, scrollx;
2004 963b370f 2018-05-20 stsp
2005 963b370f 2018-05-20 stsp *wlinep = NULL;
2006 b700b5d6 2018-07-10 stsp *widthp = 0;
2007 963b370f 2018-05-20 stsp
2008 05171be4 2022-06-23 thomas if (expand) {
2009 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2010 05171be4 2022-06-23 thomas if (err)
2011 05171be4 2022-06-23 thomas return err;
2012 05171be4 2022-06-23 thomas }
2013 05171be4 2022-06-23 thomas
2014 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2015 05171be4 2022-06-23 thomas free(exstr);
2016 963b370f 2018-05-20 stsp if (err)
2017 963b370f 2018-05-20 stsp return err;
2018 963b370f 2018-05-20 stsp
2019 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2020 f91a2b48 2022-06-23 thomas
2021 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2022 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2023 3f670bfb 2020-12-10 stsp wlen--;
2024 3f670bfb 2020-12-10 stsp }
2025 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2026 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2027 3f670bfb 2020-12-10 stsp wlen--;
2028 3f670bfb 2020-12-10 stsp }
2029 3f670bfb 2020-12-10 stsp
2030 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2031 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2032 27a741e5 2019-09-11 stsp
2033 b700b5d6 2018-07-10 stsp if (widthp)
2034 b700b5d6 2018-07-10 stsp *widthp = cols;
2035 f91a2b48 2022-06-23 thomas if (scrollxp)
2036 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2037 963b370f 2018-05-20 stsp if (err)
2038 963b370f 2018-05-20 stsp free(wline);
2039 963b370f 2018-05-20 stsp else
2040 963b370f 2018-05-20 stsp *wlinep = wline;
2041 963b370f 2018-05-20 stsp return err;
2042 963b370f 2018-05-20 stsp }
2043 963b370f 2018-05-20 stsp
2044 8b473291 2019-02-21 stsp static const struct got_error*
2045 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2046 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2047 8b473291 2019-02-21 stsp {
2048 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2049 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2050 8b473291 2019-02-21 stsp char *s;
2051 8b473291 2019-02-21 stsp const char *name;
2052 8b473291 2019-02-21 stsp
2053 8b473291 2019-02-21 stsp *refs_str = NULL;
2054 8b473291 2019-02-21 stsp
2055 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2056 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2057 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2058 52b5abe1 2019-08-13 stsp int cmp;
2059 52b5abe1 2019-08-13 stsp
2060 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2061 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2062 8b473291 2019-02-21 stsp continue;
2063 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2064 8b473291 2019-02-21 stsp name += 5;
2065 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2066 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2067 7143d404 2019-03-12 stsp continue;
2068 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2069 8b473291 2019-02-21 stsp name += 6;
2070 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2071 8b473291 2019-02-21 stsp name += 8;
2072 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2073 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2074 79cc719f 2020-04-24 stsp continue;
2075 79cc719f 2020-04-24 stsp }
2076 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2077 48cae60d 2020-09-22 stsp if (err)
2078 48cae60d 2020-09-22 stsp break;
2079 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2080 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2081 5d844a1e 2019-08-13 stsp if (err) {
2082 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2083 48cae60d 2020-09-22 stsp free(ref_id);
2084 5d844a1e 2019-08-13 stsp break;
2085 48cae60d 2020-09-22 stsp }
2086 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2087 5d844a1e 2019-08-13 stsp err = NULL;
2088 5d844a1e 2019-08-13 stsp tag = NULL;
2089 5d844a1e 2019-08-13 stsp }
2090 52b5abe1 2019-08-13 stsp }
2091 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2092 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2093 48cae60d 2020-09-22 stsp free(ref_id);
2094 52b5abe1 2019-08-13 stsp if (tag)
2095 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2096 52b5abe1 2019-08-13 stsp if (cmp != 0)
2097 52b5abe1 2019-08-13 stsp continue;
2098 8b473291 2019-02-21 stsp s = *refs_str;
2099 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2100 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2101 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2102 8b473291 2019-02-21 stsp free(s);
2103 8b473291 2019-02-21 stsp *refs_str = NULL;
2104 8b473291 2019-02-21 stsp break;
2105 8b473291 2019-02-21 stsp }
2106 8b473291 2019-02-21 stsp free(s);
2107 8b473291 2019-02-21 stsp }
2108 8b473291 2019-02-21 stsp
2109 8b473291 2019-02-21 stsp return err;
2110 8b473291 2019-02-21 stsp }
2111 8b473291 2019-02-21 stsp
2112 963b370f 2018-05-20 stsp static const struct got_error *
2113 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2114 27a741e5 2019-09-11 stsp int col_tab_align)
2115 5813d178 2019-03-09 stsp {
2116 e6b8b890 2020-12-29 naddy char *smallerthan;
2117 5813d178 2019-03-09 stsp
2118 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2119 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2120 5813d178 2019-03-09 stsp author = smallerthan + 1;
2121 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2122 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2123 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2124 5813d178 2019-03-09 stsp }
2125 5813d178 2019-03-09 stsp
2126 5813d178 2019-03-09 stsp static const struct got_error *
2127 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2128 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2129 8fdc79fe 2020-12-01 naddy int author_display_cols)
2130 80ddbec8 2018-04-29 stsp {
2131 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2132 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2133 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2134 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2135 5813d178 2019-03-09 stsp char *author = NULL;
2136 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2137 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2138 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2139 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2140 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2141 ccb26ccd 2018-11-05 stsp struct tm tm;
2142 45d799e2 2018-12-23 stsp time_t committer_time;
2143 11b20872 2019-11-08 stsp struct tog_color *tc;
2144 80ddbec8 2018-04-29 stsp
2145 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2146 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2147 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2148 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2149 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2150 b39d25c7 2018-07-10 stsp
2151 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2152 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2153 b39d25c7 2018-07-10 stsp else
2154 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2155 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2156 11b20872 2019-11-08 stsp if (tc)
2157 11b20872 2019-11-08 stsp wattr_on(view->window,
2158 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2159 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2160 11b20872 2019-11-08 stsp if (tc)
2161 11b20872 2019-11-08 stsp wattr_off(view->window,
2162 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2163 27a741e5 2019-09-11 stsp col = limit;
2164 b39d25c7 2018-07-10 stsp if (col > avail)
2165 b39d25c7 2018-07-10 stsp goto done;
2166 6570a66d 2019-11-08 stsp
2167 6570a66d 2019-11-08 stsp if (avail >= 120) {
2168 6570a66d 2019-11-08 stsp char *id_str;
2169 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2170 6570a66d 2019-11-08 stsp if (err)
2171 6570a66d 2019-11-08 stsp goto done;
2172 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2173 11b20872 2019-11-08 stsp if (tc)
2174 11b20872 2019-11-08 stsp wattr_on(view->window,
2175 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2176 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2177 11b20872 2019-11-08 stsp if (tc)
2178 11b20872 2019-11-08 stsp wattr_off(view->window,
2179 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2180 6570a66d 2019-11-08 stsp free(id_str);
2181 6570a66d 2019-11-08 stsp col += 9;
2182 6570a66d 2019-11-08 stsp if (col > avail)
2183 6570a66d 2019-11-08 stsp goto done;
2184 6570a66d 2019-11-08 stsp }
2185 b39d25c7 2018-07-10 stsp
2186 f69c5a46 2022-07-19 thomas if (s->use_committer)
2187 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2188 f69c5a46 2022-07-19 thomas else
2189 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2190 5813d178 2019-03-09 stsp if (author == NULL) {
2191 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2192 80ddbec8 2018-04-29 stsp goto done;
2193 80ddbec8 2018-04-29 stsp }
2194 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2195 bb737323 2018-05-20 stsp if (err)
2196 bb737323 2018-05-20 stsp goto done;
2197 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2198 11b20872 2019-11-08 stsp if (tc)
2199 11b20872 2019-11-08 stsp wattr_on(view->window,
2200 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2201 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2202 bb737323 2018-05-20 stsp col += author_width;
2203 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2204 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2205 bb737323 2018-05-20 stsp col++;
2206 bb737323 2018-05-20 stsp author_width++;
2207 bb737323 2018-05-20 stsp }
2208 f31d6c3b 2022-09-09 thomas if (tc)
2209 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2210 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2211 9c2eaf34 2018-05-20 stsp if (col > avail)
2212 9c2eaf34 2018-05-20 stsp goto done;
2213 80ddbec8 2018-04-29 stsp
2214 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2215 5943eee2 2019-08-13 stsp if (err)
2216 6d9fbc00 2018-04-29 stsp goto done;
2217 bb737323 2018-05-20 stsp logmsg = logmsg0;
2218 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2219 bb737323 2018-05-20 stsp logmsg++;
2220 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2221 bb737323 2018-05-20 stsp if (newline)
2222 bb737323 2018-05-20 stsp *newline = '\0';
2223 f91a2b48 2022-06-23 thomas limit = avail - col;
2224 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2225 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2226 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2227 f91a2b48 2022-06-23 thomas limit, col, 1);
2228 331b1a16 2022-06-23 thomas if (err)
2229 331b1a16 2022-06-23 thomas goto done;
2230 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2231 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2232 27a741e5 2019-09-11 stsp while (col < avail) {
2233 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2234 bb737323 2018-05-20 stsp col++;
2235 881b2d3e 2018-04-30 stsp }
2236 80ddbec8 2018-04-29 stsp done:
2237 80ddbec8 2018-04-29 stsp free(logmsg0);
2238 bb737323 2018-05-20 stsp free(wlogmsg);
2239 5813d178 2019-03-09 stsp free(author);
2240 bb737323 2018-05-20 stsp free(wauthor);
2241 80ddbec8 2018-04-29 stsp free(line);
2242 80ddbec8 2018-04-29 stsp return err;
2243 80ddbec8 2018-04-29 stsp }
2244 26ed57b2 2018-05-19 stsp
2245 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2246 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2247 899d86c2 2018-05-10 stsp struct got_object_id *id)
2248 80ddbec8 2018-04-29 stsp {
2249 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2250 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2251 80ddbec8 2018-04-29 stsp
2252 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2253 80ddbec8 2018-04-29 stsp if (entry == NULL)
2254 899d86c2 2018-05-10 stsp return NULL;
2255 99db9666 2018-05-07 stsp
2256 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2257 d68b9737 2022-09-05 thomas if (dup == NULL) {
2258 d68b9737 2022-09-05 thomas free(entry);
2259 d68b9737 2022-09-05 thomas return NULL;
2260 d68b9737 2022-09-05 thomas }
2261 d68b9737 2022-09-05 thomas
2262 d68b9737 2022-09-05 thomas entry->id = dup;
2263 99db9666 2018-05-07 stsp entry->commit = commit;
2264 899d86c2 2018-05-10 stsp return entry;
2265 99db9666 2018-05-07 stsp }
2266 80ddbec8 2018-04-29 stsp
2267 99db9666 2018-05-07 stsp static void
2268 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2269 99db9666 2018-05-07 stsp {
2270 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2271 99db9666 2018-05-07 stsp
2272 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2273 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2274 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2275 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2276 d68b9737 2022-09-05 thomas free(entry->id);
2277 99db9666 2018-05-07 stsp free(entry);
2278 99db9666 2018-05-07 stsp }
2279 99db9666 2018-05-07 stsp
2280 99db9666 2018-05-07 stsp static void
2281 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2282 99db9666 2018-05-07 stsp {
2283 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2284 99db9666 2018-05-07 stsp pop_commit(commits);
2285 c4972b91 2018-05-07 stsp }
2286 c4972b91 2018-05-07 stsp
2287 c4972b91 2018-05-07 stsp static const struct got_error *
2288 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2289 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2290 13add988 2019-10-15 stsp {
2291 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2292 13add988 2019-10-15 stsp regmatch_t regmatch;
2293 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2294 13add988 2019-10-15 stsp
2295 13add988 2019-10-15 stsp *have_match = 0;
2296 13add988 2019-10-15 stsp
2297 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2298 13add988 2019-10-15 stsp if (err)
2299 13add988 2019-10-15 stsp return err;
2300 13add988 2019-10-15 stsp
2301 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2302 13add988 2019-10-15 stsp if (err)
2303 13add988 2019-10-15 stsp goto done;
2304 13add988 2019-10-15 stsp
2305 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2306 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2307 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2308 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2309 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2310 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2311 13add988 2019-10-15 stsp *have_match = 1;
2312 13add988 2019-10-15 stsp done:
2313 13add988 2019-10-15 stsp free(id_str);
2314 13add988 2019-10-15 stsp free(logmsg);
2315 13add988 2019-10-15 stsp return err;
2316 13add988 2019-10-15 stsp }
2317 13add988 2019-10-15 stsp
2318 13add988 2019-10-15 stsp static const struct got_error *
2319 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2320 c4972b91 2018-05-07 stsp {
2321 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2322 9ba79e04 2018-06-11 stsp
2323 1a76625f 2018-10-22 stsp /*
2324 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2325 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2326 1a76625f 2018-10-22 stsp * while updating the display.
2327 1a76625f 2018-10-22 stsp */
2328 4e0d2870 2020-12-07 naddy do {
2329 7210b715 2022-09-11 thomas struct got_object_id id;
2330 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2331 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2332 7e8004ba 2022-09-11 thomas int limit_match = 0;
2333 1a76625f 2018-10-22 stsp int errcode;
2334 899d86c2 2018-05-10 stsp
2335 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2336 4e0d2870 2020-12-07 naddy NULL, NULL);
2337 7210b715 2022-09-11 thomas if (err)
2338 ecb28ae0 2018-07-16 stsp break;
2339 899d86c2 2018-05-10 stsp
2340 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2341 9ba79e04 2018-06-11 stsp if (err)
2342 9ba79e04 2018-06-11 stsp break;
2343 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2344 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2345 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2346 9ba79e04 2018-06-11 stsp break;
2347 9ba79e04 2018-06-11 stsp }
2348 93e45b7c 2018-09-24 stsp
2349 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2350 1a76625f 2018-10-22 stsp if (errcode) {
2351 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2352 13add988 2019-10-15 stsp "pthread_mutex_lock");
2353 1a76625f 2018-10-22 stsp break;
2354 1a76625f 2018-10-22 stsp }
2355 1a76625f 2018-10-22 stsp
2356 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2357 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2358 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2359 1a76625f 2018-10-22 stsp
2360 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2361 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2362 7e8004ba 2022-09-11 thomas a->limit_regex);
2363 7e8004ba 2022-09-11 thomas if (err)
2364 7e8004ba 2022-09-11 thomas break;
2365 7e8004ba 2022-09-11 thomas
2366 7e8004ba 2022-09-11 thomas if (limit_match) {
2367 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2368 7e8004ba 2022-09-11 thomas
2369 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2370 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2371 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2372 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2373 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2374 7e8004ba 2022-09-11 thomas break;
2375 7e8004ba 2022-09-11 thomas }
2376 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2377 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2378 7e8004ba 2022-09-11 thomas
2379 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2380 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2381 7e8004ba 2022-09-11 thomas matched, entry);
2382 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2383 7e8004ba 2022-09-11 thomas }
2384 7e8004ba 2022-09-11 thomas
2385 7e8004ba 2022-09-11 thomas /*
2386 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2387 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2388 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2389 7e8004ba 2022-09-11 thomas */
2390 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2391 7e8004ba 2022-09-11 thomas }
2392 7e8004ba 2022-09-11 thomas
2393 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2394 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2395 7c1452c1 2020-03-26 stsp int have_match;
2396 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2397 7c1452c1 2020-03-26 stsp if (err)
2398 7c1452c1 2020-03-26 stsp break;
2399 7e8004ba 2022-09-11 thomas
2400 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2401 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2402 7e8004ba 2022-09-11 thomas *a->search_next_done =
2403 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2404 7e8004ba 2022-09-11 thomas } else if (have_match)
2405 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2406 13add988 2019-10-15 stsp }
2407 13add988 2019-10-15 stsp
2408 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2409 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2410 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2411 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2412 7c1452c1 2020-03-26 stsp if (err)
2413 13add988 2019-10-15 stsp break;
2414 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2415 899d86c2 2018-05-10 stsp
2416 9ba79e04 2018-06-11 stsp return err;
2417 0553a4e3 2018-04-30 stsp }
2418 0553a4e3 2018-04-30 stsp
2419 2b779855 2020-12-05 naddy static void
2420 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2421 2b779855 2020-12-05 naddy {
2422 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2423 2b779855 2020-12-05 naddy int ncommits = 0;
2424 2b779855 2020-12-05 naddy
2425 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2426 2b779855 2020-12-05 naddy while (entry) {
2427 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2428 2b779855 2020-12-05 naddy s->selected_entry = entry;
2429 2b779855 2020-12-05 naddy break;
2430 2b779855 2020-12-05 naddy }
2431 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2432 2b779855 2020-12-05 naddy ncommits++;
2433 2b779855 2020-12-05 naddy }
2434 2b779855 2020-12-05 naddy }
2435 2b779855 2020-12-05 naddy
2436 0553a4e3 2018-04-30 stsp static const struct got_error *
2437 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2438 0553a4e3 2018-04-30 stsp {
2439 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2440 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2441 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2442 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2443 60493ae3 2019-06-20 stsp int width;
2444 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2445 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2446 8b473291 2019-02-21 stsp char *refs_str = NULL;
2447 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2448 11b20872 2019-11-08 stsp struct tog_color *tc;
2449 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2450 bd3f8225 2022-08-12 thomas
2451 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2452 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2453 0553a4e3 2018-04-30 stsp
2454 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2455 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2456 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2457 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2458 1a76625f 2018-10-22 stsp if (err)
2459 ecb28ae0 2018-07-16 stsp return err;
2460 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2461 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2462 d2075bf3 2020-12-25 stsp if (refs) {
2463 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2464 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2465 d2075bf3 2020-12-25 stsp if (err)
2466 d2075bf3 2020-12-25 stsp goto done;
2467 d2075bf3 2020-12-25 stsp }
2468 867c6645 2018-07-10 stsp }
2469 359bfafd 2019-02-22 stsp
2470 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2471 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2472 1a76625f 2018-10-22 stsp
2473 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2474 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2475 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2476 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2477 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2478 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2479 8f4ed634 2020-03-26 stsp goto done;
2480 8f4ed634 2020-03-26 stsp }
2481 8f4ed634 2020-03-26 stsp } else {
2482 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2483 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2484 f9686aa5 2020-03-27 stsp
2485 f9686aa5 2020-03-27 stsp if (view->searching) {
2486 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2487 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2488 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2489 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2490 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2491 f9686aa5 2020-03-27 stsp search_str = "searching...";
2492 f9686aa5 2020-03-27 stsp }
2493 f9686aa5 2020-03-27 stsp
2494 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2495 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2496 7e8004ba 2022-09-11 thomas
2497 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2498 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2499 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2500 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2501 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2502 8f4ed634 2020-03-26 stsp goto done;
2503 8f4ed634 2020-03-26 stsp }
2504 8b473291 2019-02-21 stsp }
2505 1a76625f 2018-10-22 stsp
2506 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2507 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2508 91198554 2022-06-23 thomas "........................................",
2509 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2510 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2511 1a76625f 2018-10-22 stsp header = NULL;
2512 1a76625f 2018-10-22 stsp goto done;
2513 1a76625f 2018-10-22 stsp }
2514 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2515 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2516 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2517 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2518 1a76625f 2018-10-22 stsp header = NULL;
2519 1a76625f 2018-10-22 stsp goto done;
2520 ecb28ae0 2018-07-16 stsp }
2521 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2522 1a76625f 2018-10-22 stsp if (err)
2523 1a76625f 2018-10-22 stsp goto done;
2524 867c6645 2018-07-10 stsp
2525 2814baeb 2018-08-01 stsp werase(view->window);
2526 867c6645 2018-07-10 stsp
2527 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2528 a3404814 2018-09-02 stsp wstandout(view->window);
2529 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2530 11b20872 2019-11-08 stsp if (tc)
2531 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2532 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2533 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2534 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2535 1a76625f 2018-10-22 stsp width++;
2536 1a76625f 2018-10-22 stsp }
2537 86f4aab9 2022-09-09 thomas if (tc)
2538 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2539 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2540 a3404814 2018-09-02 stsp wstandend(view->window);
2541 ecb28ae0 2018-07-16 stsp free(wline);
2542 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2543 1a76625f 2018-10-22 stsp goto done;
2544 0553a4e3 2018-04-30 stsp
2545 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2546 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2547 5813d178 2019-03-09 stsp ncommits = 0;
2548 05171be4 2022-06-23 thomas view->maxx = 0;
2549 5813d178 2019-03-09 stsp while (entry) {
2550 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2551 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2552 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2553 5813d178 2019-03-09 stsp int width;
2554 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2555 5813d178 2019-03-09 stsp break;
2556 f69c5a46 2022-07-19 thomas if (s->use_committer)
2557 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2558 f69c5a46 2022-07-19 thomas else
2559 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2560 5813d178 2019-03-09 stsp if (author == NULL) {
2561 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2562 5813d178 2019-03-09 stsp goto done;
2563 5813d178 2019-03-09 stsp }
2564 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2565 27a741e5 2019-09-11 stsp date_display_cols);
2566 5813d178 2019-03-09 stsp if (author_cols < width)
2567 5813d178 2019-03-09 stsp author_cols = width;
2568 5813d178 2019-03-09 stsp free(wauthor);
2569 5813d178 2019-03-09 stsp free(author);
2570 ef944b8b 2022-07-21 thomas if (err)
2571 ef944b8b 2022-07-21 thomas goto done;
2572 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2573 05171be4 2022-06-23 thomas if (err)
2574 05171be4 2022-06-23 thomas goto done;
2575 05171be4 2022-06-23 thomas msg = msg0;
2576 05171be4 2022-06-23 thomas while (*msg == '\n')
2577 05171be4 2022-06-23 thomas ++msg;
2578 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2579 331b1a16 2022-06-23 thomas *eol = '\0';
2580 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2581 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2582 331b1a16 2022-06-23 thomas if (err)
2583 331b1a16 2022-06-23 thomas goto done;
2584 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2585 05171be4 2022-06-23 thomas free(msg0);
2586 331b1a16 2022-06-23 thomas free(wmsg);
2587 7ca04879 2019-10-19 stsp ncommits++;
2588 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2589 5813d178 2019-03-09 stsp }
2590 5813d178 2019-03-09 stsp
2591 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2592 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2593 867c6645 2018-07-10 stsp ncommits = 0;
2594 899d86c2 2018-05-10 stsp while (entry) {
2595 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2596 899d86c2 2018-05-10 stsp break;
2597 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2598 2814baeb 2018-08-01 stsp wstandout(view->window);
2599 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2600 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2601 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2602 2814baeb 2018-08-01 stsp wstandend(view->window);
2603 0553a4e3 2018-04-30 stsp if (err)
2604 60493ae3 2019-06-20 stsp goto done;
2605 0553a4e3 2018-04-30 stsp ncommits++;
2606 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2607 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2608 80ddbec8 2018-04-29 stsp }
2609 80ddbec8 2018-04-29 stsp
2610 a5d43cac 2022-07-01 thomas view_border(view);
2611 1a76625f 2018-10-22 stsp done:
2612 1a76625f 2018-10-22 stsp free(id_str);
2613 8b473291 2019-02-21 stsp free(refs_str);
2614 1a76625f 2018-10-22 stsp free(ncommits_str);
2615 1a76625f 2018-10-22 stsp free(header);
2616 80ddbec8 2018-04-29 stsp return err;
2617 9f7d7167 2018-04-29 stsp }
2618 07b55e75 2018-05-10 stsp
2619 07b55e75 2018-05-10 stsp static void
2620 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2621 07b55e75 2018-05-10 stsp {
2622 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2623 07b55e75 2018-05-10 stsp int nscrolled = 0;
2624 07b55e75 2018-05-10 stsp
2625 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2626 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2627 07b55e75 2018-05-10 stsp return;
2628 9f7d7167 2018-04-29 stsp
2629 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2630 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2631 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2632 07b55e75 2018-05-10 stsp if (entry) {
2633 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2634 07b55e75 2018-05-10 stsp nscrolled++;
2635 07b55e75 2018-05-10 stsp }
2636 07b55e75 2018-05-10 stsp }
2637 aa075928 2018-05-10 stsp }
2638 aa075928 2018-05-10 stsp
2639 aa075928 2018-05-10 stsp static const struct got_error *
2640 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2641 aa075928 2018-05-10 stsp {
2642 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2643 5e224a3e 2019-02-22 stsp int errcode;
2644 8a42fca8 2019-02-22 stsp
2645 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2646 aa075928 2018-05-10 stsp
2647 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2648 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2649 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2650 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2651 7aafa0d1 2019-02-22 stsp if (errcode)
2652 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2653 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2654 7c1452c1 2020-03-26 stsp
2655 7c1452c1 2020-03-26 stsp /*
2656 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2657 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2658 7c1452c1 2020-03-26 stsp */
2659 7c1452c1 2020-03-26 stsp if (!wait)
2660 7c1452c1 2020-03-26 stsp break;
2661 7c1452c1 2020-03-26 stsp
2662 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2663 ffe38506 2020-12-01 naddy show_log_view(view);
2664 7c1452c1 2020-03-26 stsp update_panels();
2665 7c1452c1 2020-03-26 stsp doupdate();
2666 7c1452c1 2020-03-26 stsp
2667 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2668 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2669 82954512 2020-02-03 stsp if (errcode)
2670 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2671 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2672 82954512 2020-02-03 stsp
2673 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2674 ffe38506 2020-12-01 naddy show_log_view(view);
2675 7c1452c1 2020-03-26 stsp update_panels();
2676 7c1452c1 2020-03-26 stsp doupdate();
2677 5e224a3e 2019-02-22 stsp }
2678 5e224a3e 2019-02-22 stsp
2679 5e224a3e 2019-02-22 stsp return NULL;
2680 a5d43cac 2022-07-01 thomas }
2681 a5d43cac 2022-07-01 thomas
2682 a5d43cac 2022-07-01 thomas static const struct got_error *
2683 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2684 a5d43cac 2022-07-01 thomas {
2685 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2686 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2687 fe731b51 2022-07-14 thomas
2688 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2689 fe731b51 2022-07-14 thomas return NULL;
2690 a5d43cac 2022-07-01 thomas
2691 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2692 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2693 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2694 a5d43cac 2022-07-01 thomas
2695 a5d43cac 2022-07-01 thomas return err;
2696 5e224a3e 2019-02-22 stsp }
2697 5e224a3e 2019-02-22 stsp
2698 5e224a3e 2019-02-22 stsp static const struct got_error *
2699 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2700 5e224a3e 2019-02-22 stsp {
2701 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2702 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2703 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2704 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2705 5e224a3e 2019-02-22 stsp
2706 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2707 5e224a3e 2019-02-22 stsp return NULL;
2708 5e224a3e 2019-02-22 stsp
2709 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2710 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2711 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2712 08ebd0a9 2019-02-22 stsp /*
2713 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2714 08ebd0a9 2019-02-22 stsp */
2715 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2716 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2717 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2718 5e224a3e 2019-02-22 stsp if (err)
2719 5e224a3e 2019-02-22 stsp return err;
2720 7aafa0d1 2019-02-22 stsp }
2721 b295e71b 2019-02-22 stsp
2722 7aafa0d1 2019-02-22 stsp do {
2723 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2724 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2725 88048b54 2019-02-21 stsp break;
2726 88048b54 2019-02-21 stsp
2727 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2728 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
2729 aa075928 2018-05-10 stsp
2730 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2731 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2732 dd0a52c1 2018-05-20 stsp break;
2733 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2734 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2735 aa075928 2018-05-10 stsp
2736 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2737 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2738 a5d43cac 2022-07-01 thomas else
2739 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2740 a5d43cac 2022-07-01 thomas
2741 dd0a52c1 2018-05-20 stsp return err;
2742 07b55e75 2018-05-10 stsp }
2743 4a7f7875 2018-05-10 stsp
2744 cd0acaa7 2018-05-20 stsp static const struct got_error *
2745 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2746 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2747 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2748 cd0acaa7 2018-05-20 stsp {
2749 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2750 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2751 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2752 cd0acaa7 2018-05-20 stsp
2753 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2754 15a94983 2018-12-23 stsp if (diff_view == NULL)
2755 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2756 ea5e7bb5 2018-08-01 stsp
2757 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2758 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2759 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2760 e5a0f69f 2018-08-18 stsp if (err == NULL)
2761 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2762 cd0acaa7 2018-05-20 stsp return err;
2763 4a7f7875 2018-05-10 stsp }
2764 4a7f7875 2018-05-10 stsp
2765 80ddbec8 2018-04-29 stsp static const struct got_error *
2766 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2767 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2768 9343a5fb 2018-06-23 stsp {
2769 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2770 941e9f74 2019-05-21 stsp
2771 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2772 941e9f74 2019-05-21 stsp if (parent == NULL)
2773 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2774 941e9f74 2019-05-21 stsp
2775 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2776 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2777 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2778 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2779 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2780 941e9f74 2019-05-21 stsp s->tree = subtree;
2781 941e9f74 2019-05-21 stsp s->selected = 0;
2782 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2783 941e9f74 2019-05-21 stsp return NULL;
2784 941e9f74 2019-05-21 stsp }
2785 941e9f74 2019-05-21 stsp
2786 941e9f74 2019-05-21 stsp static const struct got_error *
2787 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2788 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2789 941e9f74 2019-05-21 stsp {
2790 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2791 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2792 941e9f74 2019-05-21 stsp const char *p;
2793 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2794 9343a5fb 2018-06-23 stsp
2795 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2796 941e9f74 2019-05-21 stsp p = path;
2797 941e9f74 2019-05-21 stsp while (*p) {
2798 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2799 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2800 56e0773d 2019-11-28 stsp char *te_name;
2801 33cbf02b 2020-01-12 stsp
2802 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2803 33cbf02b 2020-01-12 stsp p++;
2804 941e9f74 2019-05-21 stsp
2805 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2806 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2807 941e9f74 2019-05-21 stsp if (slash == NULL)
2808 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2809 33cbf02b 2020-01-12 stsp else
2810 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2811 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2812 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2813 56e0773d 2019-11-28 stsp break;
2814 941e9f74 2019-05-21 stsp }
2815 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2816 56e0773d 2019-11-28 stsp if (te == NULL) {
2817 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2818 56e0773d 2019-11-28 stsp free(te_name);
2819 941e9f74 2019-05-21 stsp break;
2820 941e9f74 2019-05-21 stsp }
2821 56e0773d 2019-11-28 stsp free(te_name);
2822 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2823 941e9f74 2019-05-21 stsp
2824 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2825 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2826 b03c880f 2019-05-21 stsp
2827 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2828 941e9f74 2019-05-21 stsp if (slash)
2829 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2830 941e9f74 2019-05-21 stsp else
2831 941e9f74 2019-05-21 stsp subpath = strdup(path);
2832 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2833 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2834 941e9f74 2019-05-21 stsp break;
2835 941e9f74 2019-05-21 stsp }
2836 941e9f74 2019-05-21 stsp
2837 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2838 941e9f74 2019-05-21 stsp subpath);
2839 941e9f74 2019-05-21 stsp if (err)
2840 941e9f74 2019-05-21 stsp break;
2841 941e9f74 2019-05-21 stsp
2842 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2843 941e9f74 2019-05-21 stsp free(tree_id);
2844 941e9f74 2019-05-21 stsp if (err)
2845 941e9f74 2019-05-21 stsp break;
2846 941e9f74 2019-05-21 stsp
2847 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2848 941e9f74 2019-05-21 stsp if (err) {
2849 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2850 941e9f74 2019-05-21 stsp break;
2851 941e9f74 2019-05-21 stsp }
2852 941e9f74 2019-05-21 stsp if (slash == NULL)
2853 941e9f74 2019-05-21 stsp break;
2854 941e9f74 2019-05-21 stsp free(subpath);
2855 941e9f74 2019-05-21 stsp subpath = NULL;
2856 941e9f74 2019-05-21 stsp p = slash;
2857 941e9f74 2019-05-21 stsp }
2858 941e9f74 2019-05-21 stsp
2859 941e9f74 2019-05-21 stsp free(subpath);
2860 1a76625f 2018-10-22 stsp return err;
2861 61266923 2020-01-14 stsp }
2862 61266923 2020-01-14 stsp
2863 61266923 2020-01-14 stsp static const struct got_error *
2864 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2865 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2866 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2867 55cccc34 2020-02-20 stsp {
2868 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2869 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2870 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2871 55cccc34 2020-02-20 stsp
2872 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2873 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2874 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2875 55cccc34 2020-02-20 stsp
2876 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2877 bc573f3b 2021-07-10 stsp if (err)
2878 55cccc34 2020-02-20 stsp return err;
2879 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2880 55cccc34 2020-02-20 stsp
2881 55cccc34 2020-02-20 stsp *new_view = tree_view;
2882 55cccc34 2020-02-20 stsp
2883 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2884 55cccc34 2020-02-20 stsp return NULL;
2885 55cccc34 2020-02-20 stsp
2886 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2887 55cccc34 2020-02-20 stsp }
2888 55cccc34 2020-02-20 stsp
2889 55cccc34 2020-02-20 stsp static const struct got_error *
2890 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2891 61266923 2020-01-14 stsp {
2892 61266923 2020-01-14 stsp sigset_t sigset;
2893 61266923 2020-01-14 stsp int errcode;
2894 61266923 2020-01-14 stsp
2895 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2896 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2897 61266923 2020-01-14 stsp
2898 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2899 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2900 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2901 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2902 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2903 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2904 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2905 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2906 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2907 61266923 2020-01-14 stsp
2908 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2909 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2910 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2911 61266923 2020-01-14 stsp
2912 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2913 61266923 2020-01-14 stsp if (errcode)
2914 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2915 61266923 2020-01-14 stsp
2916 61266923 2020-01-14 stsp return NULL;
2917 1a76625f 2018-10-22 stsp }
2918 1a76625f 2018-10-22 stsp
2919 1a76625f 2018-10-22 stsp static void *
2920 1a76625f 2018-10-22 stsp log_thread(void *arg)
2921 1a76625f 2018-10-22 stsp {
2922 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2923 1a76625f 2018-10-22 stsp int errcode = 0;
2924 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2925 1a76625f 2018-10-22 stsp int done = 0;
2926 61266923 2020-01-14 stsp
2927 f2d749db 2022-07-12 thomas /*
2928 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2929 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2930 f2d749db 2022-07-12 thomas */
2931 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2932 f2d749db 2022-07-12 thomas if (errcode) {
2933 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2934 61266923 2020-01-14 stsp return (void *)err;
2935 f2d749db 2022-07-12 thomas }
2936 1a76625f 2018-10-22 stsp
2937 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2938 f2d749db 2022-07-12 thomas if (err) {
2939 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2940 f2d749db 2022-07-12 thomas goto done;
2941 f2d749db 2022-07-12 thomas }
2942 f2d749db 2022-07-12 thomas
2943 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2944 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2945 f2d749db 2022-07-12 thomas if (errcode) {
2946 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2947 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2948 f2d749db 2022-07-12 thomas goto done;
2949 f2d749db 2022-07-12 thomas }
2950 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2951 1a76625f 2018-10-22 stsp if (err) {
2952 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2953 f2d749db 2022-07-12 thomas goto done;
2954 1a76625f 2018-10-22 stsp err = NULL;
2955 1a76625f 2018-10-22 stsp done = 1;
2956 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
2957 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2958 7e8004ba 2022-09-11 thomas if (a->limit_match)
2959 7e8004ba 2022-09-11 thomas a->commits_needed--;
2960 7e8004ba 2022-09-11 thomas } else
2961 7e8004ba 2022-09-11 thomas a->commits_needed--;
2962 7e8004ba 2022-09-11 thomas }
2963 1a76625f 2018-10-22 stsp
2964 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2965 3abe8080 2019-04-10 stsp if (errcode) {
2966 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2967 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2968 f2d749db 2022-07-12 thomas goto done;
2969 3abe8080 2019-04-10 stsp } else if (*a->quit)
2970 1a76625f 2018-10-22 stsp done = 1;
2971 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
2972 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2973 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
2974 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2975 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
2976 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
2977 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
2978 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
2979 1a76625f 2018-10-22 stsp }
2980 1a76625f 2018-10-22 stsp
2981 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2982 7c1452c1 2020-03-26 stsp if (errcode) {
2983 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2984 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2985 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2986 f2d749db 2022-07-12 thomas goto done;
2987 7c1452c1 2020-03-26 stsp }
2988 7c1452c1 2020-03-26 stsp
2989 1a76625f 2018-10-22 stsp if (done)
2990 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2991 7c1452c1 2020-03-26 stsp else {
2992 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2993 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2994 7c1452c1 2020-03-26 stsp &tog_mutex);
2995 f2d749db 2022-07-12 thomas if (errcode) {
2996 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2997 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2998 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2999 f2d749db 2022-07-12 thomas goto done;
3000 f2d749db 2022-07-12 thomas }
3001 21355643 2020-12-06 stsp if (*a->quit)
3002 21355643 2020-12-06 stsp done = 1;
3003 7c1452c1 2020-03-26 stsp }
3004 1a76625f 2018-10-22 stsp }
3005 1a76625f 2018-10-22 stsp }
3006 3abe8080 2019-04-10 stsp a->log_complete = 1;
3007 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3008 f2d749db 2022-07-12 thomas if (errcode)
3009 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3010 f2d749db 2022-07-12 thomas done:
3011 f2d749db 2022-07-12 thomas if (err) {
3012 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3013 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3014 f2d749db 2022-07-12 thomas }
3015 1a76625f 2018-10-22 stsp return (void *)err;
3016 1a76625f 2018-10-22 stsp }
3017 1a76625f 2018-10-22 stsp
3018 1a76625f 2018-10-22 stsp static const struct got_error *
3019 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3020 1a76625f 2018-10-22 stsp {
3021 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3022 1a76625f 2018-10-22 stsp int errcode;
3023 1a76625f 2018-10-22 stsp
3024 1a76625f 2018-10-22 stsp if (s->thread) {
3025 1a76625f 2018-10-22 stsp s->quit = 1;
3026 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3027 1a76625f 2018-10-22 stsp if (errcode)
3028 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3029 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3030 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3031 1a76625f 2018-10-22 stsp if (errcode)
3032 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3033 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3034 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3035 1a76625f 2018-10-22 stsp if (errcode)
3036 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3037 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3038 1a76625f 2018-10-22 stsp if (errcode)
3039 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3040 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3041 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3042 1a76625f 2018-10-22 stsp }
3043 1a76625f 2018-10-22 stsp
3044 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3045 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3046 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3047 1af5eddf 2022-06-23 thomas }
3048 1af5eddf 2022-06-23 thomas
3049 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3050 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3051 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3052 1af5eddf 2022-06-23 thomas if (err == NULL)
3053 1af5eddf 2022-06-23 thomas err = pack_err;
3054 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3055 1a76625f 2018-10-22 stsp }
3056 1a76625f 2018-10-22 stsp
3057 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3058 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3059 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3060 1a76625f 2018-10-22 stsp }
3061 1a76625f 2018-10-22 stsp
3062 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3063 9343a5fb 2018-06-23 stsp }
3064 9343a5fb 2018-06-23 stsp
3065 9343a5fb 2018-06-23 stsp static const struct got_error *
3066 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3067 1a76625f 2018-10-22 stsp {
3068 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3069 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3070 276b94a1 2020-11-13 naddy int errcode;
3071 1a76625f 2018-10-22 stsp
3072 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3073 276b94a1 2020-11-13 naddy
3074 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3075 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3076 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3077 276b94a1 2020-11-13 naddy
3078 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3079 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3080 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3081 276b94a1 2020-11-13 naddy
3082 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3083 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3084 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3085 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3086 1a76625f 2018-10-22 stsp free(s->start_id);
3087 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3088 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3089 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3090 1a76625f 2018-10-22 stsp return err;
3091 1a76625f 2018-10-22 stsp }
3092 1a76625f 2018-10-22 stsp
3093 7e8004ba 2022-09-11 thomas /*
3094 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3095 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3096 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3097 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3098 7e8004ba 2022-09-11 thomas * works with very slight change.
3099 7e8004ba 2022-09-11 thomas */
3100 1a76625f 2018-10-22 stsp static const struct got_error *
3101 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3102 7e8004ba 2022-09-11 thomas {
3103 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3104 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3105 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3106 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3107 7e8004ba 2022-09-11 thomas char pattern[1024];
3108 7e8004ba 2022-09-11 thomas int ret;
3109 7e8004ba 2022-09-11 thomas
3110 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3111 7e8004ba 2022-09-11 thomas v = view->child;
3112 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3113 7e8004ba 2022-09-11 thomas v = view->parent;
3114 7e8004ba 2022-09-11 thomas
3115 7e8004ba 2022-09-11 thomas /* Get the pattern */
3116 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3117 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3118 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3119 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3120 7e8004ba 2022-09-11 thomas nocbreak();
3121 7e8004ba 2022-09-11 thomas echo();
3122 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3123 7e8004ba 2022-09-11 thomas cbreak();
3124 7e8004ba 2022-09-11 thomas noecho();
3125 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3126 7e8004ba 2022-09-11 thomas if (ret == ERR)
3127 7e8004ba 2022-09-11 thomas return NULL;
3128 7e8004ba 2022-09-11 thomas
3129 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3130 7e8004ba 2022-09-11 thomas /*
3131 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3132 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3133 7e8004ba 2022-09-11 thomas */
3134 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3135 7e8004ba 2022-09-11 thomas return NULL;
3136 7e8004ba 2022-09-11 thomas
3137 7e8004ba 2022-09-11 thomas /*
3138 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3139 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3140 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3141 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3142 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3143 7e8004ba 2022-09-11 thomas * the queue.
3144 7e8004ba 2022-09-11 thomas */
3145 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3146 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3147 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3148 7e8004ba 2022-09-11 thomas s->selected = 0;
3149 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3150 7e8004ba 2022-09-11 thomas
3151 7e8004ba 2022-09-11 thomas return NULL;
3152 7e8004ba 2022-09-11 thomas }
3153 7e8004ba 2022-09-11 thomas
3154 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3155 7e8004ba 2022-09-11 thomas return NULL;
3156 7e8004ba 2022-09-11 thomas
3157 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3158 7e8004ba 2022-09-11 thomas
3159 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3160 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3161 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3162 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3163 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3164 7e8004ba 2022-09-11 thomas
3165 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3166 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3167 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3168 7e8004ba 2022-09-11 thomas
3169 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3170 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3171 7e8004ba 2022-09-11 thomas int have_match = 0;
3172 7e8004ba 2022-09-11 thomas
3173 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3174 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3175 7e8004ba 2022-09-11 thomas if (err)
3176 7e8004ba 2022-09-11 thomas return err;
3177 7e8004ba 2022-09-11 thomas
3178 7e8004ba 2022-09-11 thomas if (have_match) {
3179 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3180 7e8004ba 2022-09-11 thomas
3181 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3182 7e8004ba 2022-09-11 thomas entry->id);
3183 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3184 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3185 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3186 7e8004ba 2022-09-11 thomas break;
3187 7e8004ba 2022-09-11 thomas }
3188 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3189 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3190 7e8004ba 2022-09-11 thomas
3191 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3192 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3193 7e8004ba 2022-09-11 thomas matched, entry);
3194 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3195 7e8004ba 2022-09-11 thomas }
3196 7e8004ba 2022-09-11 thomas }
3197 7e8004ba 2022-09-11 thomas
3198 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3199 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3200 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3201 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3202 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3203 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3204 7e8004ba 2022-09-11 thomas if (err)
3205 7e8004ba 2022-09-11 thomas return err;
3206 7e8004ba 2022-09-11 thomas }
3207 7e8004ba 2022-09-11 thomas
3208 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3209 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3210 7e8004ba 2022-09-11 thomas s->selected = 0;
3211 7e8004ba 2022-09-11 thomas
3212 7e8004ba 2022-09-11 thomas return NULL;
3213 7e8004ba 2022-09-11 thomas }
3214 7e8004ba 2022-09-11 thomas
3215 7e8004ba 2022-09-11 thomas static const struct got_error *
3216 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3217 60493ae3 2019-06-20 stsp {
3218 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3219 60493ae3 2019-06-20 stsp
3220 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3221 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3222 60493ae3 2019-06-20 stsp return NULL;
3223 60493ae3 2019-06-20 stsp }
3224 60493ae3 2019-06-20 stsp
3225 60493ae3 2019-06-20 stsp static const struct got_error *
3226 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3227 60493ae3 2019-06-20 stsp {
3228 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3229 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3230 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3231 60493ae3 2019-06-20 stsp
3232 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3233 f9686aa5 2020-03-27 stsp show_log_view(view);
3234 f9686aa5 2020-03-27 stsp update_panels();
3235 f9686aa5 2020-03-27 stsp doupdate();
3236 f9686aa5 2020-03-27 stsp
3237 96e2b566 2019-07-08 stsp if (s->search_entry) {
3238 13add988 2019-10-15 stsp int errcode, ch;
3239 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3240 13add988 2019-10-15 stsp if (errcode)
3241 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3242 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3243 13add988 2019-10-15 stsp ch = wgetch(view->window);
3244 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3245 13add988 2019-10-15 stsp if (errcode)
3246 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3247 13add988 2019-10-15 stsp "pthread_mutex_lock");
3248 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3249 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3250 678cbce5 2019-07-28 stsp return NULL;
3251 678cbce5 2019-07-28 stsp }
3252 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3253 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3254 96e2b566 2019-07-08 stsp else
3255 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3256 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3257 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3258 df5e841d 2022-06-23 thomas /*
3259 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3260 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3261 1f4d5162 2022-06-23 thomas * might have changed.
3262 df5e841d 2022-06-23 thomas */
3263 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3264 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3265 e7baaec8 2022-09-13 thomas else
3266 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3267 e7baaec8 2022-09-13 thomas entry);
3268 20be8d96 2019-06-21 stsp } else {
3269 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3270 20be8d96 2019-06-21 stsp }
3271 60493ae3 2019-06-20 stsp
3272 60493ae3 2019-06-20 stsp while (1) {
3273 13add988 2019-10-15 stsp int have_match = 0;
3274 13add988 2019-10-15 stsp
3275 60493ae3 2019-06-20 stsp if (entry == NULL) {
3276 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3277 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3278 f9967bca 2020-03-27 stsp view->search_next_done =
3279 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3280 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3281 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3282 f801134a 2019-06-25 stsp return NULL;
3283 60493ae3 2019-06-20 stsp }
3284 96e2b566 2019-07-08 stsp /*
3285 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3286 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3287 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3288 96e2b566 2019-07-08 stsp */
3289 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3290 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3291 60493ae3 2019-06-20 stsp }
3292 60493ae3 2019-06-20 stsp
3293 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3294 13add988 2019-10-15 stsp &view->regex);
3295 5943eee2 2019-08-13 stsp if (err)
3296 13add988 2019-10-15 stsp break;
3297 13add988 2019-10-15 stsp if (have_match) {
3298 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3299 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3300 60493ae3 2019-06-20 stsp break;
3301 60493ae3 2019-06-20 stsp }
3302 13add988 2019-10-15 stsp
3303 96e2b566 2019-07-08 stsp s->search_entry = entry;
3304 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3305 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3306 b1bf1435 2019-06-21 stsp else
3307 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3308 60493ae3 2019-06-20 stsp }
3309 60493ae3 2019-06-20 stsp
3310 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3311 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3312 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3313 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3314 60493ae3 2019-06-20 stsp if (err)
3315 60493ae3 2019-06-20 stsp return err;
3316 ead14cbe 2019-06-21 stsp cur++;
3317 ead14cbe 2019-06-21 stsp }
3318 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3319 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3320 60493ae3 2019-06-20 stsp if (err)
3321 60493ae3 2019-06-20 stsp return err;
3322 ead14cbe 2019-06-21 stsp cur--;
3323 60493ae3 2019-06-20 stsp }
3324 60493ae3 2019-06-20 stsp }
3325 60493ae3 2019-06-20 stsp
3326 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3327 96e2b566 2019-07-08 stsp
3328 60493ae3 2019-06-20 stsp return NULL;
3329 60493ae3 2019-06-20 stsp }
3330 60493ae3 2019-06-20 stsp
3331 60493ae3 2019-06-20 stsp static const struct got_error *
3332 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3333 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3334 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3335 80ddbec8 2018-04-29 stsp {
3336 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3337 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3338 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3339 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3340 1a76625f 2018-10-22 stsp int errcode;
3341 80ddbec8 2018-04-29 stsp
3342 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3343 f135c941 2020-02-20 stsp free(s->in_repo_path);
3344 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3345 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3346 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3347 f135c941 2020-02-20 stsp }
3348 ecb28ae0 2018-07-16 stsp
3349 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3350 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3351 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3352 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3353 78756c87 2020-11-24 stsp
3354 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3355 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3356 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3357 7e8004ba 2022-09-11 thomas
3358 fb2756b9 2018-08-04 stsp s->repo = repo;
3359 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3360 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3361 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3362 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3363 9cd7cbd1 2020-12-07 stsp goto done;
3364 9cd7cbd1 2020-12-07 stsp }
3365 9cd7cbd1 2020-12-07 stsp }
3366 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3367 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3368 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3369 5036bf37 2018-09-24 stsp goto done;
3370 5036bf37 2018-09-24 stsp }
3371 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3372 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3373 e5a0f69f 2018-08-18 stsp
3374 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3375 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3376 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3377 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3378 11b20872 2019-11-08 stsp if (err)
3379 11b20872 2019-11-08 stsp goto done;
3380 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3381 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3382 11b20872 2019-11-08 stsp if (err) {
3383 11b20872 2019-11-08 stsp free_colors(&s->colors);
3384 11b20872 2019-11-08 stsp goto done;
3385 11b20872 2019-11-08 stsp }
3386 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3387 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3388 11b20872 2019-11-08 stsp if (err) {
3389 11b20872 2019-11-08 stsp free_colors(&s->colors);
3390 11b20872 2019-11-08 stsp goto done;
3391 11b20872 2019-11-08 stsp }
3392 11b20872 2019-11-08 stsp }
3393 11b20872 2019-11-08 stsp
3394 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3395 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3396 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3397 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3398 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3399 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3400 1a76625f 2018-10-22 stsp
3401 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3402 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3403 1af5eddf 2022-06-23 thomas if (err)
3404 1af5eddf 2022-06-23 thomas goto done;
3405 1af5eddf 2022-06-23 thomas }
3406 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3407 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3408 7cd52833 2022-06-23 thomas if (err)
3409 7cd52833 2022-06-23 thomas goto done;
3410 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3411 b672a97a 2020-01-27 stsp !s->log_branches);
3412 1a76625f 2018-10-22 stsp if (err)
3413 1a76625f 2018-10-22 stsp goto done;
3414 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3415 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3416 c5b78334 2020-01-12 stsp if (err)
3417 c5b78334 2020-01-12 stsp goto done;
3418 1a76625f 2018-10-22 stsp
3419 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3420 1a76625f 2018-10-22 stsp if (errcode) {
3421 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3422 1a76625f 2018-10-22 stsp goto done;
3423 1a76625f 2018-10-22 stsp }
3424 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3425 7c1452c1 2020-03-26 stsp if (errcode) {
3426 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3427 7c1452c1 2020-03-26 stsp goto done;
3428 7c1452c1 2020-03-26 stsp }
3429 1a76625f 2018-10-22 stsp
3430 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3431 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3432 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3433 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3434 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3435 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3436 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3437 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3438 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3439 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3440 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3441 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3442 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3443 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3444 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3445 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3446 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3447 ba4f502b 2018-08-04 stsp done:
3448 1a76625f 2018-10-22 stsp if (err)
3449 1a76625f 2018-10-22 stsp close_log_view(view);
3450 ba4f502b 2018-08-04 stsp return err;
3451 ba4f502b 2018-08-04 stsp }
3452 ba4f502b 2018-08-04 stsp
3453 e5a0f69f 2018-08-18 stsp static const struct got_error *
3454 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3455 ba4f502b 2018-08-04 stsp {
3456 f2f6d207 2020-11-24 stsp const struct got_error *err;
3457 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3458 ba4f502b 2018-08-04 stsp
3459 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3460 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3461 2b380cc8 2018-10-24 stsp &s->thread_args);
3462 2b380cc8 2018-10-24 stsp if (errcode)
3463 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3464 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3465 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3466 f2f6d207 2020-11-24 stsp if (err)
3467 f2f6d207 2020-11-24 stsp return err;
3468 f2f6d207 2020-11-24 stsp }
3469 2b380cc8 2018-10-24 stsp }
3470 2b380cc8 2018-10-24 stsp
3471 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3472 b31f89ff 2022-07-01 thomas }
3473 b31f89ff 2022-07-01 thomas
3474 b31f89ff 2022-07-01 thomas static void
3475 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3476 b31f89ff 2022-07-01 thomas {
3477 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3478 b31f89ff 2022-07-01 thomas
3479 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3480 b31f89ff 2022-07-01 thomas return;
3481 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3482 7e8004ba 2022-09-11 thomas view->count = 0;
3483 b31f89ff 2022-07-01 thomas
3484 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3485 b31f89ff 2022-07-01 thomas || home)
3486 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3487 b31f89ff 2022-07-01 thomas
3488 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3489 b31f89ff 2022-07-01 thomas --s->selected;
3490 b31f89ff 2022-07-01 thomas else
3491 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3492 b31f89ff 2022-07-01 thomas
3493 b31f89ff 2022-07-01 thomas select_commit(s);
3494 b31f89ff 2022-07-01 thomas return;
3495 b31f89ff 2022-07-01 thomas }
3496 b31f89ff 2022-07-01 thomas
3497 b31f89ff 2022-07-01 thomas static const struct got_error *
3498 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3499 b31f89ff 2022-07-01 thomas {
3500 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3501 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3502 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3503 b31f89ff 2022-07-01 thomas
3504 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3505 7e8004ba 2022-09-11 thomas return NULL;
3506 7e8004ba 2022-09-11 thomas
3507 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3508 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3509 b31f89ff 2022-07-01 thomas return NULL;
3510 b31f89ff 2022-07-01 thomas
3511 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3512 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3513 b31f89ff 2022-07-01 thomas
3514 7ed048bd 2022-08-12 thomas if (!page) {
3515 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3516 b31f89ff 2022-07-01 thomas ++s->selected;
3517 b31f89ff 2022-07-01 thomas else
3518 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3519 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3520 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3521 7ed048bd 2022-08-12 thomas int n;
3522 7ed048bd 2022-08-12 thomas
3523 7ed048bd 2022-08-12 thomas s->selected = 0;
3524 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3525 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3526 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3527 7ed048bd 2022-08-12 thomas if (entry == NULL)
3528 7ed048bd 2022-08-12 thomas break;
3529 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3530 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3531 7ed048bd 2022-08-12 thomas }
3532 7ed048bd 2022-08-12 thomas if (n > 0)
3533 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3534 b31f89ff 2022-07-01 thomas } else {
3535 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3536 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3537 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3538 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3539 bd3f8225 2022-08-12 thomas else
3540 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3541 b31f89ff 2022-07-01 thomas }
3542 b31f89ff 2022-07-01 thomas if (err)
3543 b31f89ff 2022-07-01 thomas return err;
3544 b31f89ff 2022-07-01 thomas
3545 a5d43cac 2022-07-01 thomas /*
3546 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3547 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3548 a5d43cac 2022-07-01 thomas */
3549 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3550 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3551 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3552 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3553 25100026 2022-08-13 thomas }
3554 a5d43cac 2022-07-01 thomas
3555 b31f89ff 2022-07-01 thomas select_commit(s);
3556 b31f89ff 2022-07-01 thomas
3557 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3558 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3559 b31f89ff 2022-07-01 thomas view->count = 0;
3560 b31f89ff 2022-07-01 thomas
3561 b31f89ff 2022-07-01 thomas return NULL;
3562 e5a0f69f 2018-08-18 stsp }
3563 04cc582a 2018-08-01 stsp
3564 a5d43cac 2022-07-01 thomas static void
3565 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3566 a5d43cac 2022-07-01 thomas {
3567 24415785 2022-07-03 thomas *x = 0;
3568 24415785 2022-07-03 thomas *y = 0;
3569 24415785 2022-07-03 thomas
3570 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3571 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3572 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3573 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3574 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3575 53d2bdd3 2022-07-10 thomas else
3576 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3577 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3578 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3579 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3580 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3581 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3582 53d2bdd3 2022-07-10 thomas else
3583 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3584 53d2bdd3 2022-07-10 thomas }
3585 a5d43cac 2022-07-01 thomas }
3586 a5d43cac 2022-07-01 thomas
3587 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3588 e5a0f69f 2018-08-18 stsp static const struct got_error *
3589 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3590 a5d43cac 2022-07-01 thomas {
3591 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3592 a5d43cac 2022-07-01 thomas
3593 a5d43cac 2022-07-01 thomas view->nlines = y;
3594 64486692 2022-07-07 thomas view->ncols = COLS;
3595 a5d43cac 2022-07-01 thomas err = view_resize(view);
3596 a5d43cac 2022-07-01 thomas if (err)
3597 a5d43cac 2022-07-01 thomas return err;
3598 a5d43cac 2022-07-01 thomas
3599 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3600 a5d43cac 2022-07-01 thomas
3601 a5d43cac 2022-07-01 thomas return err;
3602 07dd3ed3 2022-08-06 thomas }
3603 07dd3ed3 2022-08-06 thomas
3604 07dd3ed3 2022-08-06 thomas static const struct got_error *
3605 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3606 07dd3ed3 2022-08-06 thomas {
3607 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3608 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3609 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3610 25100026 2022-08-13 thomas
3611 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3612 25100026 2022-08-13 thomas return NULL;
3613 07dd3ed3 2022-08-06 thomas
3614 07dd3ed3 2022-08-06 thomas g = view->gline;
3615 07dd3ed3 2022-08-06 thomas view->gline = 0;
3616 07dd3ed3 2022-08-06 thomas
3617 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3618 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3619 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3620 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3621 07dd3ed3 2022-08-06 thomas select_commit(s);
3622 07dd3ed3 2022-08-06 thomas return NULL;
3623 07dd3ed3 2022-08-06 thomas }
3624 07dd3ed3 2022-08-06 thomas
3625 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3626 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3627 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3628 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3629 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3630 07dd3ed3 2022-08-06 thomas if (err)
3631 07dd3ed3 2022-08-06 thomas return err;
3632 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3633 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3634 07dd3ed3 2022-08-06 thomas
3635 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3636 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3637 07dd3ed3 2022-08-06 thomas
3638 07dd3ed3 2022-08-06 thomas select_commit(s);
3639 07dd3ed3 2022-08-06 thomas return NULL;
3640 07dd3ed3 2022-08-06 thomas
3641 a5d43cac 2022-07-01 thomas }
3642 a5d43cac 2022-07-01 thomas
3643 a5d43cac 2022-07-01 thomas static const struct got_error *
3644 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3645 e5a0f69f 2018-08-18 stsp {
3646 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3647 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3648 7ed048bd 2022-08-12 thomas int eos, nscroll;
3649 80ddbec8 2018-04-29 stsp
3650 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3651 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3652 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3653 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3654 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3655 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3656 fb280deb 2021-08-30 stsp }
3657 c0be8933 2022-08-12 thomas if (err)
3658 c0be8933 2022-08-12 thomas return err;
3659 528dedf3 2021-08-30 stsp }
3660 068ab281 2022-07-01 thomas
3661 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3662 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3663 068ab281 2022-07-01 thomas --eos; /* border */
3664 07dd3ed3 2022-08-06 thomas
3665 07dd3ed3 2022-08-06 thomas if (view->gline)
3666 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3667 068ab281 2022-07-01 thomas
3668 528dedf3 2021-08-30 stsp switch (ch) {
3669 7e8004ba 2022-09-11 thomas case '&':
3670 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3671 7e8004ba 2022-09-11 thomas break;
3672 1e37a5c2 2019-05-12 jcs case 'q':
3673 1e37a5c2 2019-05-12 jcs s->quit = 1;
3674 05171be4 2022-06-23 thomas break;
3675 05171be4 2022-06-23 thomas case '0':
3676 05171be4 2022-06-23 thomas view->x = 0;
3677 05171be4 2022-06-23 thomas break;
3678 05171be4 2022-06-23 thomas case '$':
3679 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3680 07b0611c 2022-06-23 thomas view->count = 0;
3681 05171be4 2022-06-23 thomas break;
3682 05171be4 2022-06-23 thomas case KEY_RIGHT:
3683 05171be4 2022-06-23 thomas case 'l':
3684 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3685 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3686 07b0611c 2022-06-23 thomas else
3687 07b0611c 2022-06-23 thomas view->count = 0;
3688 1e37a5c2 2019-05-12 jcs break;
3689 05171be4 2022-06-23 thomas case KEY_LEFT:
3690 05171be4 2022-06-23 thomas case 'h':
3691 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3692 07b0611c 2022-06-23 thomas if (view->x <= 0)
3693 07b0611c 2022-06-23 thomas view->count = 0;
3694 05171be4 2022-06-23 thomas break;
3695 1e37a5c2 2019-05-12 jcs case 'k':
3696 1e37a5c2 2019-05-12 jcs case KEY_UP:
3697 1e37a5c2 2019-05-12 jcs case '<':
3698 1e37a5c2 2019-05-12 jcs case ',':
3699 f7140bf5 2021-10-17 thomas case CTRL('p'):
3700 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3701 912a3f79 2021-08-30 j break;
3702 912a3f79 2021-08-30 j case 'g':
3703 aa7a1117 2023-01-09 thomas case '=':
3704 912a3f79 2021-08-30 j case KEY_HOME:
3705 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3706 07b0611c 2022-06-23 thomas view->count = 0;
3707 1e37a5c2 2019-05-12 jcs break;
3708 70f17a53 2022-06-13 thomas case CTRL('u'):
3709 23427b14 2022-06-23 thomas case 'u':
3710 70f17a53 2022-06-13 thomas nscroll /= 2;
3711 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3712 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3713 a4292ac5 2019-05-12 jcs case CTRL('b'):
3714 1c5e5faa 2022-06-23 thomas case 'b':
3715 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3716 1e37a5c2 2019-05-12 jcs break;
3717 1e37a5c2 2019-05-12 jcs case 'j':
3718 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3719 1e37a5c2 2019-05-12 jcs case '>':
3720 1e37a5c2 2019-05-12 jcs case '.':
3721 f7140bf5 2021-10-17 thomas case CTRL('n'):
3722 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3723 912a3f79 2021-08-30 j break;
3724 f69c5a46 2022-07-19 thomas case '@':
3725 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3726 f69c5a46 2022-07-19 thomas break;
3727 912a3f79 2021-08-30 j case 'G':
3728 aa7a1117 2023-01-09 thomas case '*':
3729 912a3f79 2021-08-30 j case KEY_END: {
3730 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3731 912a3f79 2021-08-30 j * traverse them all. */
3732 07b0611c 2022-06-23 thomas view->count = 0;
3733 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3734 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3735 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3736 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3737 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3738 1e37a5c2 2019-05-12 jcs break;
3739 912a3f79 2021-08-30 j }
3740 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3741 23427b14 2022-06-23 thomas case 'd':
3742 70f17a53 2022-06-13 thomas nscroll /= 2;
3743 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3744 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3745 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3746 4c2d69cb 2022-06-23 thomas case 'f':
3747 b31f89ff 2022-07-01 thomas case ' ':
3748 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3749 1e37a5c2 2019-05-12 jcs break;
3750 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3751 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3752 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3753 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3754 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3755 2b779855 2020-12-05 naddy select_commit(s);
3756 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3757 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3758 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3759 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3760 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3761 0bf7f153 2020-12-02 naddy }
3762 1e37a5c2 2019-05-12 jcs break;
3763 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3764 444d5325 2022-07-03 thomas case '\r':
3765 07b0611c 2022-06-23 thomas view->count = 0;
3766 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3767 e5a0f69f 2018-08-18 stsp break;
3768 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3769 1e37a5c2 2019-05-12 jcs break;
3770 1be4947a 2022-07-22 thomas case 'T':
3771 07b0611c 2022-06-23 thomas view->count = 0;
3772 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3773 5036bf37 2018-09-24 stsp break;
3774 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3775 1e37a5c2 2019-05-12 jcs break;
3776 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3777 21355643 2020-12-06 stsp case CTRL('l'):
3778 21355643 2020-12-06 stsp case 'B':
3779 07b0611c 2022-06-23 thomas view->count = 0;
3780 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3781 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3782 1e37a5c2 2019-05-12 jcs break;
3783 21355643 2020-12-06 stsp err = stop_log_thread(s);
3784 74cfe85e 2020-10-20 stsp if (err)
3785 74cfe85e 2020-10-20 stsp return err;
3786 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3787 21355643 2020-12-06 stsp char *parent_path;
3788 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3789 21355643 2020-12-06 stsp if (err)
3790 21355643 2020-12-06 stsp return err;
3791 21355643 2020-12-06 stsp free(s->in_repo_path);
3792 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3793 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3794 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3795 21355643 2020-12-06 stsp struct got_object_id *start_id;
3796 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3797 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3798 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3799 466429a1 2022-11-17 thomas if (err) {
3800 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
3801 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
3802 466429a1 2022-11-17 thomas return err;
3803 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
3804 466429a1 2022-11-17 thomas free(s->head_ref_name);
3805 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
3806 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
3807 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3808 466429a1 2022-11-17 thomas &tog_refs, s->repo);
3809 466429a1 2022-11-17 thomas if (err)
3810 466429a1 2022-11-17 thomas return err;
3811 466429a1 2022-11-17 thomas }
3812 21355643 2020-12-06 stsp free(s->start_id);
3813 21355643 2020-12-06 stsp s->start_id = start_id;
3814 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3815 21355643 2020-12-06 stsp } else /* 'B' */
3816 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3817 21355643 2020-12-06 stsp
3818 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3819 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3820 bf7e79b3 2022-06-23 thomas if (err)
3821 bf7e79b3 2022-06-23 thomas return err;
3822 bf7e79b3 2022-06-23 thomas }
3823 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3824 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3825 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3826 74cfe85e 2020-10-20 stsp if (err)
3827 21355643 2020-12-06 stsp return err;
3828 51a10b52 2020-12-26 stsp tog_free_refs();
3829 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3830 ca51c541 2020-12-07 stsp if (err)
3831 ca51c541 2020-12-07 stsp return err;
3832 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3833 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3834 d01904d4 2019-06-25 stsp if (err)
3835 d01904d4 2019-06-25 stsp return err;
3836 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3837 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3838 b672a97a 2020-01-27 stsp if (err)
3839 b672a97a 2020-01-27 stsp return err;
3840 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3841 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3842 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3843 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3844 21355643 2020-12-06 stsp s->selected_entry = NULL;
3845 21355643 2020-12-06 stsp s->selected = 0;
3846 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3847 21355643 2020-12-06 stsp s->quit = 0;
3848 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3849 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3850 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3851 94ecf40d 2022-07-22 thomas view->offset = 0;
3852 d01904d4 2019-06-25 stsp break;
3853 1be4947a 2022-07-22 thomas case 'R':
3854 07b0611c 2022-06-23 thomas view->count = 0;
3855 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3856 6458efa5 2020-11-24 stsp break;
3857 1e37a5c2 2019-05-12 jcs default:
3858 07b0611c 2022-06-23 thomas view->count = 0;
3859 1e37a5c2 2019-05-12 jcs break;
3860 899d86c2 2018-05-10 stsp }
3861 e5a0f69f 2018-08-18 stsp
3862 80ddbec8 2018-04-29 stsp return err;
3863 80ddbec8 2018-04-29 stsp }
3864 80ddbec8 2018-04-29 stsp
3865 4ed7e80c 2018-05-20 stsp static const struct got_error *
3866 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3867 c2db6724 2019-01-04 stsp {
3868 c2db6724 2019-01-04 stsp const struct got_error *error;
3869 c2db6724 2019-01-04 stsp
3870 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3871 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3872 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3873 37c06ea4 2019-07-15 stsp #endif
3874 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3875 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3876 c2db6724 2019-01-04 stsp
3877 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3878 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3879 c2db6724 2019-01-04 stsp
3880 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3881 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3882 c2db6724 2019-01-04 stsp
3883 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3884 c2db6724 2019-01-04 stsp if (error != NULL)
3885 c2db6724 2019-01-04 stsp return error;
3886 c2db6724 2019-01-04 stsp
3887 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3888 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3889 c2db6724 2019-01-04 stsp
3890 c2db6724 2019-01-04 stsp return NULL;
3891 c2db6724 2019-01-04 stsp }
3892 c2db6724 2019-01-04 stsp
3893 a915003a 2019-02-05 stsp static void
3894 a915003a 2019-02-05 stsp init_curses(void)
3895 a915003a 2019-02-05 stsp {
3896 296152d1 2022-05-31 thomas /*
3897 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3898 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3899 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3900 296152d1 2022-05-31 thomas */
3901 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3902 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3903 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3904 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3905 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3906 296152d1 2022-05-31 thomas
3907 a915003a 2019-02-05 stsp initscr();
3908 a915003a 2019-02-05 stsp cbreak();
3909 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3910 a915003a 2019-02-05 stsp noecho();
3911 a915003a 2019-02-05 stsp nonl();
3912 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3913 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3914 a915003a 2019-02-05 stsp curs_set(0);
3915 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3916 6d17833f 2019-11-08 stsp start_color();
3917 6d17833f 2019-11-08 stsp use_default_colors();
3918 6d17833f 2019-11-08 stsp }
3919 a915003a 2019-02-05 stsp }
3920 a915003a 2019-02-05 stsp
3921 c2db6724 2019-01-04 stsp static const struct got_error *
3922 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3923 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3924 f135c941 2020-02-20 stsp {
3925 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3926 f135c941 2020-02-20 stsp
3927 f135c941 2020-02-20 stsp if (argc == 0) {
3928 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3929 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3930 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3931 f135c941 2020-02-20 stsp return NULL;
3932 f135c941 2020-02-20 stsp }
3933 f135c941 2020-02-20 stsp
3934 f135c941 2020-02-20 stsp if (worktree) {
3935 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3936 bfd61697 2020-11-14 stsp char *p;
3937 f135c941 2020-02-20 stsp
3938 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3939 f135c941 2020-02-20 stsp if (err)
3940 f135c941 2020-02-20 stsp return err;
3941 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3942 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3943 bfd61697 2020-11-14 stsp p) == -1) {
3944 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3945 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3946 f135c941 2020-02-20 stsp }
3947 f135c941 2020-02-20 stsp free(p);
3948 f135c941 2020-02-20 stsp } else
3949 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3950 f135c941 2020-02-20 stsp
3951 f135c941 2020-02-20 stsp return err;
3952 f135c941 2020-02-20 stsp }
3953 f135c941 2020-02-20 stsp
3954 f135c941 2020-02-20 stsp static const struct got_error *
3955 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3956 9f7d7167 2018-04-29 stsp {
3957 80ddbec8 2018-04-29 stsp const struct got_error *error;
3958 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3959 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3960 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3961 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3962 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3963 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3964 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3965 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3966 04cc582a 2018-08-01 stsp struct tog_view *view;
3967 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3968 80ddbec8 2018-04-29 stsp
3969 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3970 80ddbec8 2018-04-29 stsp switch (ch) {
3971 b672a97a 2020-01-27 stsp case 'b':
3972 b672a97a 2020-01-27 stsp log_branches = 1;
3973 b672a97a 2020-01-27 stsp break;
3974 80ddbec8 2018-04-29 stsp case 'c':
3975 80ddbec8 2018-04-29 stsp start_commit = optarg;
3976 80ddbec8 2018-04-29 stsp break;
3977 ecb28ae0 2018-07-16 stsp case 'r':
3978 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3979 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3980 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3981 9ba1d308 2019-10-21 stsp optarg);
3982 ecb28ae0 2018-07-16 stsp break;
3983 80ddbec8 2018-04-29 stsp default:
3984 17020d27 2019-03-07 stsp usage_log();
3985 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3986 80ddbec8 2018-04-29 stsp }
3987 80ddbec8 2018-04-29 stsp }
3988 80ddbec8 2018-04-29 stsp
3989 80ddbec8 2018-04-29 stsp argc -= optind;
3990 80ddbec8 2018-04-29 stsp argv += optind;
3991 80ddbec8 2018-04-29 stsp
3992 f135c941 2020-02-20 stsp if (argc > 1)
3993 f135c941 2020-02-20 stsp usage_log();
3994 963f97a1 2019-03-18 stsp
3995 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3996 7cd52833 2022-06-23 thomas if (error != NULL)
3997 7cd52833 2022-06-23 thomas goto done;
3998 7cd52833 2022-06-23 thomas
3999 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4000 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4001 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4002 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4003 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4004 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4005 c156c7a4 2020-12-18 stsp goto done;
4006 a1fbf39a 2019-08-11 stsp if (worktree)
4007 6962eb72 2020-02-20 stsp repo_path =
4008 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4009 a1fbf39a 2019-08-11 stsp else
4010 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4011 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4012 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4013 c156c7a4 2020-12-18 stsp goto done;
4014 c156c7a4 2020-12-18 stsp }
4015 ecb28ae0 2018-07-16 stsp }
4016 ecb28ae0 2018-07-16 stsp
4017 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4018 80ddbec8 2018-04-29 stsp if (error != NULL)
4019 ecb28ae0 2018-07-16 stsp goto done;
4020 80ddbec8 2018-04-29 stsp
4021 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4022 f135c941 2020-02-20 stsp repo, worktree);
4023 f135c941 2020-02-20 stsp if (error)
4024 f135c941 2020-02-20 stsp goto done;
4025 f135c941 2020-02-20 stsp
4026 f135c941 2020-02-20 stsp init_curses();
4027 f135c941 2020-02-20 stsp
4028 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4029 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4030 c02c541e 2019-03-29 stsp if (error)
4031 c02c541e 2019-03-29 stsp goto done;
4032 c02c541e 2019-03-29 stsp
4033 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4034 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4035 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4036 87670572 2020-12-26 naddy if (error)
4037 87670572 2020-12-26 naddy goto done;
4038 87670572 2020-12-26 naddy }
4039 51a10b52 2020-12-26 stsp
4040 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4041 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4042 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4043 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4044 d8f38dc4 2020-12-05 stsp if (error)
4045 d8f38dc4 2020-12-05 stsp goto done;
4046 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4047 d8f38dc4 2020-12-05 stsp } else {
4048 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4049 d8f38dc4 2020-12-05 stsp if (error == NULL)
4050 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4051 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4052 d8f38dc4 2020-12-05 stsp goto done;
4053 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4054 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4055 d8f38dc4 2020-12-05 stsp if (error)
4056 d8f38dc4 2020-12-05 stsp goto done;
4057 d8f38dc4 2020-12-05 stsp }
4058 8b473291 2019-02-21 stsp
4059 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4060 04cc582a 2018-08-01 stsp if (view == NULL) {
4061 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4062 04cc582a 2018-08-01 stsp goto done;
4063 04cc582a 2018-08-01 stsp }
4064 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4065 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4066 ba4f502b 2018-08-04 stsp if (error)
4067 ba4f502b 2018-08-04 stsp goto done;
4068 2fc00ff4 2019-08-31 stsp if (worktree) {
4069 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4070 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4071 2fc00ff4 2019-08-31 stsp worktree = NULL;
4072 2fc00ff4 2019-08-31 stsp }
4073 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4074 ecb28ae0 2018-07-16 stsp done:
4075 f135c941 2020-02-20 stsp free(in_repo_path);
4076 ecb28ae0 2018-07-16 stsp free(repo_path);
4077 ecb28ae0 2018-07-16 stsp free(cwd);
4078 899d86c2 2018-05-10 stsp free(start_id);
4079 d8f38dc4 2020-12-05 stsp free(label);
4080 d8f38dc4 2020-12-05 stsp if (ref)
4081 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4082 1d0f4054 2021-06-17 stsp if (repo) {
4083 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4084 1d0f4054 2021-06-17 stsp if (error == NULL)
4085 1d0f4054 2021-06-17 stsp error = close_err;
4086 1d0f4054 2021-06-17 stsp }
4087 ec142235 2019-03-07 stsp if (worktree)
4088 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4089 7cd52833 2022-06-23 thomas if (pack_fds) {
4090 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4091 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4092 7cd52833 2022-06-23 thomas if (error == NULL)
4093 7cd52833 2022-06-23 thomas error = pack_err;
4094 7cd52833 2022-06-23 thomas }
4095 51a10b52 2020-12-26 stsp tog_free_refs();
4096 80ddbec8 2018-04-29 stsp return error;
4097 9f7d7167 2018-04-29 stsp }
4098 9f7d7167 2018-04-29 stsp
4099 4ed7e80c 2018-05-20 stsp __dead static void
4100 9f7d7167 2018-04-29 stsp usage_diff(void)
4101 9f7d7167 2018-04-29 stsp {
4102 80ddbec8 2018-04-29 stsp endwin();
4103 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4104 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4105 9f7d7167 2018-04-29 stsp exit(1);
4106 b304db33 2018-05-20 stsp }
4107 b304db33 2018-05-20 stsp
4108 6d17833f 2019-11-08 stsp static int
4109 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4110 41605754 2020-11-12 stsp regmatch_t *regmatch)
4111 6d17833f 2019-11-08 stsp {
4112 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4113 6d17833f 2019-11-08 stsp }
4114 6d17833f 2019-11-08 stsp
4115 ef20f542 2022-06-26 thomas static struct tog_color *
4116 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4117 6d17833f 2019-11-08 stsp {
4118 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4119 6d17833f 2019-11-08 stsp
4120 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4121 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4122 6d17833f 2019-11-08 stsp return tc;
4123 6d17833f 2019-11-08 stsp }
4124 6d17833f 2019-11-08 stsp
4125 6d17833f 2019-11-08 stsp return NULL;
4126 6d17833f 2019-11-08 stsp }
4127 6d17833f 2019-11-08 stsp
4128 4ed7e80c 2018-05-20 stsp static const struct got_error *
4129 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4130 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4131 41605754 2020-11-12 stsp {
4132 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4133 666f7b10 2022-06-23 thomas char *exstr = NULL;
4134 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4135 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4136 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4137 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4138 41605754 2020-11-12 stsp
4139 41605754 2020-11-12 stsp *wtotal = 0;
4140 cbea3800 2022-06-23 thomas
4141 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4142 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4143 41605754 2020-11-12 stsp
4144 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4145 666f7b10 2022-06-23 thomas if (err)
4146 666f7b10 2022-06-23 thomas return err;
4147 666f7b10 2022-06-23 thomas
4148 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4149 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4150 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4151 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4152 666f7b10 2022-06-23 thomas goto done;
4153 666f7b10 2022-06-23 thomas }
4154 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4155 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4156 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4157 cbea3800 2022-06-23 thomas goto done;
4158 cbea3800 2022-06-23 thomas }
4159 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4160 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4161 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4162 cbea3800 2022-06-23 thomas goto done;
4163 cbea3800 2022-06-23 thomas }
4164 05171be4 2022-06-23 thomas
4165 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4166 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4167 cbea3800 2022-06-23 thomas col_tab_align, 1);
4168 cbea3800 2022-06-23 thomas if (err)
4169 cbea3800 2022-06-23 thomas goto done;
4170 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4171 05171be4 2022-06-23 thomas if (n) {
4172 cbea3800 2022-06-23 thomas free(wline);
4173 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4174 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4175 cbea3800 2022-06-23 thomas if (err)
4176 cbea3800 2022-06-23 thomas goto done;
4177 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4178 cbea3800 2022-06-23 thomas wlimit -= width;
4179 cbea3800 2022-06-23 thomas *wtotal += width;
4180 41605754 2020-11-12 stsp }
4181 41605754 2020-11-12 stsp
4182 41605754 2020-11-12 stsp if (wlimit > 0) {
4183 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4184 cbea3800 2022-06-23 thomas size_t wlen;
4185 cbea3800 2022-06-23 thomas
4186 cbea3800 2022-06-23 thomas free(wline);
4187 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4188 cbea3800 2022-06-23 thomas col_tab_align, 1);
4189 cbea3800 2022-06-23 thomas if (err)
4190 cbea3800 2022-06-23 thomas goto done;
4191 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4192 cbea3800 2022-06-23 thomas while (i < wlen) {
4193 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4194 cbea3800 2022-06-23 thomas if (width == -1) {
4195 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4196 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4197 cbea3800 2022-06-23 thomas goto done;
4198 cbea3800 2022-06-23 thomas }
4199 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4200 cbea3800 2022-06-23 thomas break;
4201 1c5e5faa 2022-06-23 thomas w += width;
4202 cbea3800 2022-06-23 thomas i++;
4203 41605754 2020-11-12 stsp }
4204 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4205 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4206 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4207 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4208 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4209 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4210 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4211 41605754 2020-11-12 stsp }
4212 41605754 2020-11-12 stsp }
4213 41605754 2020-11-12 stsp
4214 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4215 cbea3800 2022-06-23 thomas free(wline);
4216 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4217 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4218 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4219 cbea3800 2022-06-23 thomas col_tab_align, 1);
4220 cbea3800 2022-06-23 thomas if (err)
4221 cbea3800 2022-06-23 thomas goto done;
4222 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4223 cbea3800 2022-06-23 thomas } else {
4224 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4225 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4226 cbea3800 2022-06-23 thomas if (err)
4227 cbea3800 2022-06-23 thomas goto done;
4228 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4229 cbea3800 2022-06-23 thomas }
4230 cbea3800 2022-06-23 thomas *wtotal += width2;
4231 41605754 2020-11-12 stsp }
4232 cbea3800 2022-06-23 thomas done:
4233 05171be4 2022-06-23 thomas free(wline);
4234 666f7b10 2022-06-23 thomas free(exstr);
4235 cbea3800 2022-06-23 thomas free(seg0);
4236 cbea3800 2022-06-23 thomas free(seg1);
4237 cbea3800 2022-06-23 thomas free(seg2);
4238 cbea3800 2022-06-23 thomas return err;
4239 41605754 2020-11-12 stsp }
4240 07dd3ed3 2022-08-06 thomas
4241 07dd3ed3 2022-08-06 thomas static int
4242 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4243 07dd3ed3 2022-08-06 thomas {
4244 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4245 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4246 07dd3ed3 2022-08-06 thomas
4247 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4248 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4249 fc2737d5 2022-09-15 thomas
4250 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4251 fc2737d5 2022-09-15 thomas selected = first;
4252 fc2737d5 2022-09-15 thomas eof = &s->eof;
4253 fc2737d5 2022-09-15 thomas f = s->f;
4254 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4255 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4256 41605754 2020-11-12 stsp
4257 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4258 07dd3ed3 2022-08-06 thomas selected = first;
4259 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4260 07dd3ed3 2022-08-06 thomas f = s->f;
4261 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4262 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4263 07dd3ed3 2022-08-06 thomas
4264 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4265 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4266 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4267 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4268 07dd3ed3 2022-08-06 thomas } else
4269 07dd3ed3 2022-08-06 thomas return 0;
4270 07dd3ed3 2022-08-06 thomas
4271 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4272 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4273 07dd3ed3 2022-08-06 thomas return 0;
4274 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4275 07dd3ed3 2022-08-06 thomas rewind(f);
4276 07dd3ed3 2022-08-06 thomas *eof = 0;
4277 07dd3ed3 2022-08-06 thomas *first = 1;
4278 07dd3ed3 2022-08-06 thomas *lineno = 0;
4279 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4280 07dd3ed3 2022-08-06 thomas return 0;
4281 07dd3ed3 2022-08-06 thomas }
4282 07dd3ed3 2022-08-06 thomas
4283 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4284 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4285 07dd3ed3 2022-08-06 thomas view->gline = 0;
4286 07dd3ed3 2022-08-06 thomas
4287 07dd3ed3 2022-08-06 thomas return 1;
4288 07dd3ed3 2022-08-06 thomas }
4289 07dd3ed3 2022-08-06 thomas
4290 41605754 2020-11-12 stsp static const struct got_error *
4291 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4292 26ed57b2 2018-05-19 stsp {
4293 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4294 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4295 61e69b96 2018-05-20 stsp const struct got_error *err;
4296 82c78e96 2022-08-06 thomas int nprinted = 0;
4297 b304db33 2018-05-20 stsp char *line;
4298 826082fe 2020-12-10 stsp size_t linesize = 0;
4299 826082fe 2020-12-10 stsp ssize_t linelen;
4300 61e69b96 2018-05-20 stsp wchar_t *wline;
4301 e0b650dd 2018-05-20 stsp int width;
4302 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4303 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4304 fe621944 2020-11-10 stsp off_t line_offset;
4305 26ed57b2 2018-05-19 stsp
4306 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4307 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4308 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4309 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4310 fe621944 2020-11-10 stsp
4311 f7d12f7e 2018-08-01 stsp werase(view->window);
4312 a3404814 2018-09-02 stsp
4313 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4314 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4315 07dd3ed3 2022-08-06 thomas
4316 a3404814 2018-09-02 stsp if (header) {
4317 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4318 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4319 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4320 07dd3ed3 2022-08-06 thomas
4321 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4322 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4323 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4324 f91a2b48 2022-06-23 thomas 0, 0);
4325 135a2da0 2020-11-11 stsp free(line);
4326 135a2da0 2020-11-11 stsp if (err)
4327 a3404814 2018-09-02 stsp return err;
4328 a3404814 2018-09-02 stsp
4329 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4330 a3404814 2018-09-02 stsp wstandout(view->window);
4331 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4332 e54cc94a 2020-11-11 stsp free(wline);
4333 e54cc94a 2020-11-11 stsp wline = NULL;
4334 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4335 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4336 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4337 a3404814 2018-09-02 stsp wstandend(view->window);
4338 26ed57b2 2018-05-19 stsp
4339 a3404814 2018-09-02 stsp if (max_lines <= 1)
4340 a3404814 2018-09-02 stsp return NULL;
4341 a3404814 2018-09-02 stsp max_lines--;
4342 a3404814 2018-09-02 stsp }
4343 a3404814 2018-09-02 stsp
4344 89f1a395 2020-12-01 naddy s->eof = 0;
4345 05171be4 2022-06-23 thomas view->maxx = 0;
4346 826082fe 2020-12-10 stsp line = NULL;
4347 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4348 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4349 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4350 6568f0aa 2022-08-06 thomas
4351 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4352 826082fe 2020-12-10 stsp if (linelen == -1) {
4353 826082fe 2020-12-10 stsp if (feof(s->f)) {
4354 826082fe 2020-12-10 stsp s->eof = 1;
4355 826082fe 2020-12-10 stsp break;
4356 826082fe 2020-12-10 stsp }
4357 826082fe 2020-12-10 stsp free(line);
4358 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4359 61e69b96 2018-05-20 stsp }
4360 05171be4 2022-06-23 thomas
4361 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4362 07dd3ed3 2022-08-06 thomas continue;
4363 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4364 07dd3ed3 2022-08-06 thomas continue;
4365 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4366 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4367 07dd3ed3 2022-08-06 thomas
4368 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4369 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4370 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4371 cbea3800 2022-06-23 thomas if (err) {
4372 cbea3800 2022-06-23 thomas free(line);
4373 cbea3800 2022-06-23 thomas return err;
4374 cbea3800 2022-06-23 thomas }
4375 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4376 cbea3800 2022-06-23 thomas free(wline);
4377 cbea3800 2022-06-23 thomas wline = NULL;
4378 cbea3800 2022-06-23 thomas
4379 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4380 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4381 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4382 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4383 07dd3ed3 2022-08-06 thomas if (attr)
4384 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4385 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4386 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4387 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4388 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4389 41605754 2020-11-12 stsp if (err) {
4390 41605754 2020-11-12 stsp free(line);
4391 41605754 2020-11-12 stsp return err;
4392 41605754 2020-11-12 stsp }
4393 41605754 2020-11-12 stsp } else {
4394 f1c0ec19 2022-06-23 thomas int skip;
4395 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4396 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4397 f1c0ec19 2022-06-23 thomas if (err) {
4398 f1c0ec19 2022-06-23 thomas free(line);
4399 f1c0ec19 2022-06-23 thomas return err;
4400 f1c0ec19 2022-06-23 thomas }
4401 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4402 f1c0ec19 2022-06-23 thomas free(wline);
4403 41605754 2020-11-12 stsp wline = NULL;
4404 41605754 2020-11-12 stsp }
4405 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4406 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4407 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4408 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4409 07dd3ed3 2022-08-06 thomas } else {
4410 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4411 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4412 07dd3ed3 2022-08-06 thomas }
4413 07dd3ed3 2022-08-06 thomas if (attr)
4414 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4415 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4416 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4417 826082fe 2020-12-10 stsp }
4418 826082fe 2020-12-10 stsp free(line);
4419 fe621944 2020-11-10 stsp if (nprinted >= 1)
4420 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4421 89f1a395 2020-12-01 naddy (nprinted - 1);
4422 fe621944 2020-11-10 stsp else
4423 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4424 26ed57b2 2018-05-19 stsp
4425 a5d43cac 2022-07-01 thomas view_border(view);
4426 c3e9aa98 2019-05-13 jcs
4427 89f1a395 2020-12-01 naddy if (s->eof) {
4428 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4429 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4430 c3e9aa98 2019-05-13 jcs nprinted++;
4431 c3e9aa98 2019-05-13 jcs }
4432 c3e9aa98 2019-05-13 jcs
4433 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4434 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4435 c3e9aa98 2019-05-13 jcs if (err) {
4436 c3e9aa98 2019-05-13 jcs return err;
4437 c3e9aa98 2019-05-13 jcs }
4438 26ed57b2 2018-05-19 stsp
4439 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4440 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4441 e54cc94a 2020-11-11 stsp free(wline);
4442 e54cc94a 2020-11-11 stsp wline = NULL;
4443 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4444 c3e9aa98 2019-05-13 jcs }
4445 c3e9aa98 2019-05-13 jcs
4446 26ed57b2 2018-05-19 stsp return NULL;
4447 abd2672a 2018-12-23 stsp }
4448 abd2672a 2018-12-23 stsp
4449 abd2672a 2018-12-23 stsp static char *
4450 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4451 abd2672a 2018-12-23 stsp {
4452 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4453 09867e48 2019-08-13 stsp char *p, *s;
4454 09867e48 2019-08-13 stsp
4455 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4456 09867e48 2019-08-13 stsp if (tm == NULL)
4457 09867e48 2019-08-13 stsp return NULL;
4458 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4459 09867e48 2019-08-13 stsp if (s == NULL)
4460 09867e48 2019-08-13 stsp return NULL;
4461 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4462 abd2672a 2018-12-23 stsp if (p)
4463 abd2672a 2018-12-23 stsp *p = '\0';
4464 abd2672a 2018-12-23 stsp return s;
4465 9f7d7167 2018-04-29 stsp }
4466 9f7d7167 2018-04-29 stsp
4467 4ed7e80c 2018-05-20 stsp static const struct got_error *
4468 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4469 772fcad5 2023-01-07 thomas struct got_commit_object *commit, struct got_repository *repo,
4470 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg *dsa)
4471 0208f208 2020-05-05 stsp {
4472 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4473 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4474 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4475 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4476 772fcad5 2023-01-07 thomas FILE *f1 = NULL, *f2 = NULL;
4477 772fcad5 2023-01-07 thomas int fd1 = -1, fd2 = -1;
4478 0208f208 2020-05-05 stsp
4479 772fcad5 2023-01-07 thomas f1 = got_opentemp();
4480 772fcad5 2023-01-07 thomas if (f1 == NULL) {
4481 772fcad5 2023-01-07 thomas err = got_error_from_errno("got_opentemp");
4482 772fcad5 2023-01-07 thomas goto done;
4483 772fcad5 2023-01-07 thomas }
4484 772fcad5 2023-01-07 thomas f2 = got_opentemp();
4485 772fcad5 2023-01-07 thomas if (f2 == NULL) {
4486 772fcad5 2023-01-07 thomas err = got_error_from_errno("got_opentemp");
4487 772fcad5 2023-01-07 thomas goto done;
4488 772fcad5 2023-01-07 thomas }
4489 772fcad5 2023-01-07 thomas
4490 772fcad5 2023-01-07 thomas fd1 = got_opentempfd();
4491 772fcad5 2023-01-07 thomas if (fd1 == -1) {
4492 772fcad5 2023-01-07 thomas err = got_error_from_errno("got_opentempfd");
4493 772fcad5 2023-01-07 thomas goto done;
4494 772fcad5 2023-01-07 thomas }
4495 772fcad5 2023-01-07 thomas fd2 = got_opentempfd();
4496 772fcad5 2023-01-07 thomas if (fd2 == -1) {
4497 772fcad5 2023-01-07 thomas err = got_error_from_errno("got_opentempfd");
4498 772fcad5 2023-01-07 thomas goto done;
4499 772fcad5 2023-01-07 thomas }
4500 772fcad5 2023-01-07 thomas
4501 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4502 0208f208 2020-05-05 stsp if (qid != NULL) {
4503 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4504 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4505 ec242592 2022-04-22 thomas &qid->id);
4506 0208f208 2020-05-05 stsp if (err)
4507 0208f208 2020-05-05 stsp return err;
4508 0208f208 2020-05-05 stsp
4509 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4510 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4511 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4512 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4513 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4514 aa8b5dd0 2021-08-01 stsp }
4515 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4516 0208f208 2020-05-05 stsp
4517 0208f208 2020-05-05 stsp }
4518 0208f208 2020-05-05 stsp
4519 0208f208 2020-05-05 stsp if (tree_id1) {
4520 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4521 0208f208 2020-05-05 stsp if (err)
4522 0208f208 2020-05-05 stsp goto done;
4523 0208f208 2020-05-05 stsp }
4524 0208f208 2020-05-05 stsp
4525 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4526 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4527 0208f208 2020-05-05 stsp if (err)
4528 0208f208 2020-05-05 stsp goto done;
4529 0208f208 2020-05-05 stsp
4530 772fcad5 2023-01-07 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
4531 772fcad5 2023-01-07 thomas got_diff_tree_compute_diffstat, dsa, 1);
4532 0208f208 2020-05-05 stsp done:
4533 0208f208 2020-05-05 stsp if (tree1)
4534 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4535 0208f208 2020-05-05 stsp if (tree2)
4536 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4537 772fcad5 2023-01-07 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4538 772fcad5 2023-01-07 thomas err = got_error_from_errno("close");
4539 772fcad5 2023-01-07 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4540 772fcad5 2023-01-07 thomas err = got_error_from_errno("close");
4541 772fcad5 2023-01-07 thomas if (f1 && fclose(f1) == EOF && err == NULL)
4542 772fcad5 2023-01-07 thomas err = got_error_from_errno("fclose");
4543 772fcad5 2023-01-07 thomas if (f2 && fclose(f2) == EOF && err == NULL)
4544 772fcad5 2023-01-07 thomas err = got_error_from_errno("fclose");
4545 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4546 0208f208 2020-05-05 stsp return err;
4547 0208f208 2020-05-05 stsp }
4548 0208f208 2020-05-05 stsp
4549 0208f208 2020-05-05 stsp static const struct got_error *
4550 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4551 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4552 abd2672a 2018-12-23 stsp {
4553 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4554 fe621944 2020-11-10 stsp
4555 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4556 fe621944 2020-11-10 stsp if (p == NULL)
4557 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4558 82c78e96 2022-08-06 thomas *lines = p;
4559 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4560 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4561 fe621944 2020-11-10 stsp (*nlines)++;
4562 82c78e96 2022-08-06 thomas
4563 fe621944 2020-11-10 stsp return NULL;
4564 fe621944 2020-11-10 stsp }
4565 fe621944 2020-11-10 stsp
4566 fe621944 2020-11-10 stsp static const struct got_error *
4567 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4568 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4569 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4570 772fcad5 2023-01-07 thomas FILE *outfile)
4571 fe621944 2020-11-10 stsp {
4572 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4573 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4574 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4575 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4576 45d799e2 2018-12-23 stsp time_t committer_time;
4577 45d799e2 2018-12-23 stsp const char *author, *committer;
4578 8b473291 2019-02-21 stsp char *refs_str = NULL;
4579 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4580 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4581 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0, &changed_paths,
4582 772fcad5 2023-01-07 thomas ignore_ws, force_text_diff, tog_diff_algo };
4583 fe621944 2020-11-10 stsp off_t outoff = 0;
4584 fe621944 2020-11-10 stsp int n;
4585 abd2672a 2018-12-23 stsp
4586 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4587 0208f208 2020-05-05 stsp
4588 8b473291 2019-02-21 stsp if (refs) {
4589 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4590 8b473291 2019-02-21 stsp if (err)
4591 8b473291 2019-02-21 stsp return err;
4592 8b473291 2019-02-21 stsp }
4593 8b473291 2019-02-21 stsp
4594 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4595 abd2672a 2018-12-23 stsp if (err)
4596 abd2672a 2018-12-23 stsp return err;
4597 abd2672a 2018-12-23 stsp
4598 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4599 15a94983 2018-12-23 stsp if (err) {
4600 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4601 15a94983 2018-12-23 stsp goto done;
4602 15a94983 2018-12-23 stsp }
4603 abd2672a 2018-12-23 stsp
4604 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4605 fe621944 2020-11-10 stsp if (err)
4606 fe621944 2020-11-10 stsp goto done;
4607 fe621944 2020-11-10 stsp
4608 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4609 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4610 fe621944 2020-11-10 stsp if (n < 0) {
4611 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4612 abd2672a 2018-12-23 stsp goto done;
4613 abd2672a 2018-12-23 stsp }
4614 fe621944 2020-11-10 stsp outoff += n;
4615 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4616 fe621944 2020-11-10 stsp if (err)
4617 fe621944 2020-11-10 stsp goto done;
4618 fe621944 2020-11-10 stsp
4619 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4620 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4621 fe621944 2020-11-10 stsp if (n < 0) {
4622 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4623 abd2672a 2018-12-23 stsp goto done;
4624 abd2672a 2018-12-23 stsp }
4625 fe621944 2020-11-10 stsp outoff += n;
4626 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4627 fe621944 2020-11-10 stsp if (err)
4628 fe621944 2020-11-10 stsp goto done;
4629 fe621944 2020-11-10 stsp
4630 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
4631 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
4632 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
4633 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
4634 fe621944 2020-11-10 stsp if (n < 0) {
4635 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4636 fe621944 2020-11-10 stsp goto done;
4637 fe621944 2020-11-10 stsp }
4638 fe621944 2020-11-10 stsp outoff += n;
4639 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4640 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
4641 fe621944 2020-11-10 stsp if (err)
4642 fe621944 2020-11-10 stsp goto done;
4643 abd2672a 2018-12-23 stsp }
4644 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
4645 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
4646 48830929 2023-01-07 thomas if (datestr) {
4647 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
4648 fe621944 2020-11-10 stsp if (n < 0) {
4649 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4650 fe621944 2020-11-10 stsp goto done;
4651 fe621944 2020-11-10 stsp }
4652 fe621944 2020-11-10 stsp outoff += n;
4653 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4654 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
4655 fe621944 2020-11-10 stsp if (err)
4656 fe621944 2020-11-10 stsp goto done;
4657 abd2672a 2018-12-23 stsp }
4658 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4659 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4660 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4661 ac4dc263 2021-09-24 thomas int pn = 1;
4662 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4663 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4664 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4665 ac4dc263 2021-09-24 thomas if (err)
4666 ac4dc263 2021-09-24 thomas goto done;
4667 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4668 ac4dc263 2021-09-24 thomas if (n < 0) {
4669 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4670 ac4dc263 2021-09-24 thomas goto done;
4671 ac4dc263 2021-09-24 thomas }
4672 ac4dc263 2021-09-24 thomas outoff += n;
4673 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4674 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4675 ac4dc263 2021-09-24 thomas if (err)
4676 ac4dc263 2021-09-24 thomas goto done;
4677 ac4dc263 2021-09-24 thomas free(id_str);
4678 ac4dc263 2021-09-24 thomas id_str = NULL;
4679 ac4dc263 2021-09-24 thomas }
4680 ac4dc263 2021-09-24 thomas }
4681 ac4dc263 2021-09-24 thomas
4682 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4683 5943eee2 2019-08-13 stsp if (err)
4684 5943eee2 2019-08-13 stsp goto done;
4685 fe621944 2020-11-10 stsp s = logmsg;
4686 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4687 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4688 fe621944 2020-11-10 stsp if (n < 0) {
4689 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4690 fe621944 2020-11-10 stsp goto done;
4691 fe621944 2020-11-10 stsp }
4692 fe621944 2020-11-10 stsp outoff += n;
4693 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4694 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4695 fe621944 2020-11-10 stsp if (err)
4696 fe621944 2020-11-10 stsp goto done;
4697 abd2672a 2018-12-23 stsp }
4698 fe621944 2020-11-10 stsp
4699 772fcad5 2023-01-07 thomas err = get_changed_paths(&changed_paths, commit, repo, &dsa);
4700 0208f208 2020-05-05 stsp if (err)
4701 0208f208 2020-05-05 stsp goto done;
4702 772fcad5 2023-01-07 thomas
4703 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4704 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4705 772fcad5 2023-01-07 thomas int pad = dsa.max_path_len - pe->path_len + 1;
4706 772fcad5 2023-01-07 thomas
4707 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4708 772fcad5 2023-01-07 thomas pe->path, pad, ' ', dsa.add_cols + 1, cp->add,
4709 772fcad5 2023-01-07 thomas dsa.rm_cols + 1, cp->rm);
4710 fe621944 2020-11-10 stsp if (n < 0) {
4711 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4712 fe621944 2020-11-10 stsp goto done;
4713 fe621944 2020-11-10 stsp }
4714 fe621944 2020-11-10 stsp outoff += n;
4715 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4716 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4717 fe621944 2020-11-10 stsp if (err)
4718 fe621944 2020-11-10 stsp goto done;
4719 0208f208 2020-05-05 stsp }
4720 fe621944 2020-11-10 stsp
4721 0208f208 2020-05-05 stsp fputc('\n', outfile);
4722 fe621944 2020-11-10 stsp outoff++;
4723 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4724 772fcad5 2023-01-07 thomas if (err)
4725 772fcad5 2023-01-07 thomas goto done;
4726 772fcad5 2023-01-07 thomas
4727 772fcad5 2023-01-07 thomas n = fprintf(outfile,
4728 772fcad5 2023-01-07 thomas "%d file%s changed, %d insertions(+), %d deletions(-)\n",
4729 772fcad5 2023-01-07 thomas dsa.nfiles, dsa.nfiles > 1 ? "s" : "", dsa.ins, dsa.del);
4730 772fcad5 2023-01-07 thomas if (n < 0) {
4731 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
4732 772fcad5 2023-01-07 thomas goto done;
4733 772fcad5 2023-01-07 thomas }
4734 772fcad5 2023-01-07 thomas outoff += n;
4735 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4736 772fcad5 2023-01-07 thomas if (err)
4737 772fcad5 2023-01-07 thomas goto done;
4738 772fcad5 2023-01-07 thomas
4739 772fcad5 2023-01-07 thomas fputc('\n', outfile);
4740 772fcad5 2023-01-07 thomas outoff++;
4741 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4742 abd2672a 2018-12-23 stsp done:
4743 21c2d8be 2023-01-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4744 abd2672a 2018-12-23 stsp free(id_str);
4745 5943eee2 2019-08-13 stsp free(logmsg);
4746 8b473291 2019-02-21 stsp free(refs_str);
4747 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4748 7510f233 2020-08-09 stsp if (err) {
4749 82c78e96 2022-08-06 thomas free(*lines);
4750 82c78e96 2022-08-06 thomas *lines = NULL;
4751 ae6a6978 2020-08-09 stsp *nlines = 0;
4752 7510f233 2020-08-09 stsp }
4753 fe621944 2020-11-10 stsp return err;
4754 abd2672a 2018-12-23 stsp }
4755 abd2672a 2018-12-23 stsp
4756 abd2672a 2018-12-23 stsp static const struct got_error *
4757 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4758 26ed57b2 2018-05-19 stsp {
4759 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4760 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4761 15a94983 2018-12-23 stsp int obj_type;
4762 fe621944 2020-11-10 stsp
4763 82c78e96 2022-08-06 thomas free(s->lines);
4764 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4765 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4766 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4767 fe621944 2020-11-10 stsp s->nlines = 0;
4768 26ed57b2 2018-05-19 stsp
4769 511a516b 2018-05-19 stsp f = got_opentemp();
4770 48ae06ee 2018-10-18 stsp if (f == NULL) {
4771 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4772 48ae06ee 2018-10-18 stsp goto done;
4773 48ae06ee 2018-10-18 stsp }
4774 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4775 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4776 fb43ecf1 2019-02-11 stsp goto done;
4777 fb43ecf1 2019-02-11 stsp }
4778 48ae06ee 2018-10-18 stsp s->f = f;
4779 26ed57b2 2018-05-19 stsp
4780 15a94983 2018-12-23 stsp if (s->id1)
4781 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4782 15a94983 2018-12-23 stsp else
4783 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4784 15a94983 2018-12-23 stsp if (err)
4785 15a94983 2018-12-23 stsp goto done;
4786 15a94983 2018-12-23 stsp
4787 15a94983 2018-12-23 stsp switch (obj_type) {
4788 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4789 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4790 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4791 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4792 53d03f97 2023-01-10 thomas s->ignore_whitespace, s->force_text_diff, 0, NULL, s->repo,
4793 53d03f97 2023-01-10 thomas s->f);
4794 26ed57b2 2018-05-19 stsp break;
4795 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4796 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4797 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4798 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4799 53d03f97 2023-01-10 thomas s->force_text_diff, 0, NULL, s->repo, s->f);
4800 26ed57b2 2018-05-19 stsp break;
4801 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4802 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4803 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4804 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4805 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4806 abd2672a 2018-12-23 stsp
4807 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4808 abd2672a 2018-12-23 stsp if (err)
4809 3ffacbe1 2020-02-02 tracey goto done;
4810 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4811 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4812 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4813 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4814 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
4815 772fcad5 2023-01-07 thomas s->force_text_diff, s->f);
4816 f44b1f58 2020-02-02 tracey if (err)
4817 f44b1f58 2020-02-02 tracey goto done;
4818 f44b1f58 2020-02-02 tracey } else {
4819 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4820 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4821 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4822 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4823 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4824 772fcad5 2023-01-07 thomas s->ignore_whitespace,
4825 772fcad5 2023-01-07 thomas s->force_text_diff, s->f);
4826 f44b1f58 2020-02-02 tracey if (err)
4827 f44b1f58 2020-02-02 tracey goto done;
4828 f5404e4e 2020-02-02 tracey break;
4829 15a087fe 2019-02-21 stsp }
4830 abd2672a 2018-12-23 stsp }
4831 abd2672a 2018-12-23 stsp }
4832 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4833 abd2672a 2018-12-23 stsp
4834 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4835 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4836 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4837 53d03f97 2023-01-10 thomas s->force_text_diff, 0, NULL, s->repo, s->f);
4838 26ed57b2 2018-05-19 stsp break;
4839 abd2672a 2018-12-23 stsp }
4840 26ed57b2 2018-05-19 stsp default:
4841 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4842 48ae06ee 2018-10-18 stsp break;
4843 26ed57b2 2018-05-19 stsp }
4844 48ae06ee 2018-10-18 stsp done:
4845 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4846 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4847 48ae06ee 2018-10-18 stsp return err;
4848 48ae06ee 2018-10-18 stsp }
4849 26ed57b2 2018-05-19 stsp
4850 f5215bb9 2019-02-22 stsp static void
4851 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4852 f5215bb9 2019-02-22 stsp {
4853 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4854 f5215bb9 2019-02-22 stsp update_panels();
4855 f5215bb9 2019-02-22 stsp doupdate();
4856 f44b1f58 2020-02-02 tracey }
4857 f44b1f58 2020-02-02 tracey
4858 f44b1f58 2020-02-02 tracey static const struct got_error *
4859 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4860 f44b1f58 2020-02-02 tracey {
4861 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4862 f44b1f58 2020-02-02 tracey
4863 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4864 f44b1f58 2020-02-02 tracey return NULL;
4865 f44b1f58 2020-02-02 tracey }
4866 f44b1f58 2020-02-02 tracey
4867 2a7d3cd7 2022-09-17 thomas static void
4868 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4869 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
4870 f44b1f58 2020-02-02 tracey {
4871 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
4872 fc2737d5 2022-09-15 thomas
4873 2a7d3cd7 2022-09-17 thomas *f = s->f;
4874 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
4875 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
4876 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
4877 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
4878 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
4879 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
4880 fc2737d5 2022-09-15 thomas }
4881 fc2737d5 2022-09-15 thomas
4882 fc2737d5 2022-09-15 thomas static const struct got_error *
4883 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
4884 fc2737d5 2022-09-15 thomas {
4885 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4886 fc2737d5 2022-09-15 thomas FILE *f;
4887 f44b1f58 2020-02-02 tracey int lineno;
4888 78eecffc 2022-06-23 thomas char *line = NULL;
4889 826082fe 2020-12-10 stsp size_t linesize = 0;
4890 826082fe 2020-12-10 stsp ssize_t linelen;
4891 fc2737d5 2022-09-15 thomas off_t *line_offsets;
4892 fc2737d5 2022-09-15 thomas size_t nlines = 0;
4893 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
4894 f44b1f58 2020-02-02 tracey
4895 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
4896 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
4897 2a7d3cd7 2022-09-17 thomas "view search not supported");
4898 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4899 fc2737d5 2022-09-15 thomas &match, &selected);
4900 fc2737d5 2022-09-15 thomas
4901 f44b1f58 2020-02-02 tracey if (!view->searching) {
4902 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4903 f44b1f58 2020-02-02 tracey return NULL;
4904 f44b1f58 2020-02-02 tracey }
4905 f44b1f58 2020-02-02 tracey
4906 fc2737d5 2022-09-15 thomas if (*match) {
4907 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4908 fc2737d5 2022-09-15 thomas lineno = *match + 1;
4909 f44b1f58 2020-02-02 tracey else
4910 fc2737d5 2022-09-15 thomas lineno = *match - 1;
4911 4b3f9dac 2021-12-17 thomas } else
4912 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
4913 f44b1f58 2020-02-02 tracey
4914 f44b1f58 2020-02-02 tracey while (1) {
4915 f44b1f58 2020-02-02 tracey off_t offset;
4916 f44b1f58 2020-02-02 tracey
4917 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
4918 fc2737d5 2022-09-15 thomas if (*match == 0) {
4919 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4920 f44b1f58 2020-02-02 tracey break;
4921 f44b1f58 2020-02-02 tracey }
4922 f44b1f58 2020-02-02 tracey
4923 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4924 f44b1f58 2020-02-02 tracey lineno = 1;
4925 f44b1f58 2020-02-02 tracey else
4926 fc2737d5 2022-09-15 thomas lineno = nlines;
4927 f44b1f58 2020-02-02 tracey }
4928 f44b1f58 2020-02-02 tracey
4929 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
4930 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
4931 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
4932 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
4933 f44b1f58 2020-02-02 tracey free(line);
4934 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4935 f44b1f58 2020-02-02 tracey }
4936 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
4937 78eecffc 2022-06-23 thomas if (linelen != -1) {
4938 78eecffc 2022-06-23 thomas char *exstr;
4939 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4940 78eecffc 2022-06-23 thomas if (err)
4941 78eecffc 2022-06-23 thomas break;
4942 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4943 78eecffc 2022-06-23 thomas &view->regmatch)) {
4944 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4945 fc2737d5 2022-09-15 thomas *match = lineno;
4946 78eecffc 2022-06-23 thomas free(exstr);
4947 78eecffc 2022-06-23 thomas break;
4948 78eecffc 2022-06-23 thomas }
4949 78eecffc 2022-06-23 thomas free(exstr);
4950 f44b1f58 2020-02-02 tracey }
4951 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4952 f44b1f58 2020-02-02 tracey lineno++;
4953 f44b1f58 2020-02-02 tracey else
4954 f44b1f58 2020-02-02 tracey lineno--;
4955 f44b1f58 2020-02-02 tracey }
4956 826082fe 2020-12-10 stsp free(line);
4957 f44b1f58 2020-02-02 tracey
4958 fc2737d5 2022-09-15 thomas if (*match) {
4959 fc2737d5 2022-09-15 thomas *first = *match;
4960 fc2737d5 2022-09-15 thomas *selected = 1;
4961 f44b1f58 2020-02-02 tracey }
4962 f44b1f58 2020-02-02 tracey
4963 05171be4 2022-06-23 thomas return err;
4964 f5215bb9 2019-02-22 stsp }
4965 f5215bb9 2019-02-22 stsp
4966 48ae06ee 2018-10-18 stsp static const struct got_error *
4967 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4968 a0f32f33 2022-06-13 thomas {
4969 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4970 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4971 a0f32f33 2022-06-13 thomas
4972 a0f32f33 2022-06-13 thomas free(s->id1);
4973 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4974 a0f32f33 2022-06-13 thomas free(s->id2);
4975 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4976 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4977 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4978 a0f32f33 2022-06-13 thomas s->f = NULL;
4979 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4980 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4981 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4982 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4983 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4984 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4985 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4986 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4987 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4988 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4989 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4990 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4991 82c78e96 2022-08-06 thomas free(s->lines);
4992 82c78e96 2022-08-06 thomas s->lines = NULL;
4993 a0f32f33 2022-06-13 thomas s->nlines = 0;
4994 a0f32f33 2022-06-13 thomas return err;
4995 a0f32f33 2022-06-13 thomas }
4996 a0f32f33 2022-06-13 thomas
4997 a0f32f33 2022-06-13 thomas static const struct got_error *
4998 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4999 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5000 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5001 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5002 48ae06ee 2018-10-18 stsp {
5003 48ae06ee 2018-10-18 stsp const struct got_error *err;
5004 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5005 5dc9f4bc 2018-08-04 stsp
5006 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5007 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5008 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5009 a0f32f33 2022-06-13 thomas
5010 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5011 15a94983 2018-12-23 stsp int type1, type2;
5012 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5013 15a94983 2018-12-23 stsp if (err)
5014 15a94983 2018-12-23 stsp return err;
5015 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5016 15a94983 2018-12-23 stsp if (err)
5017 15a94983 2018-12-23 stsp return err;
5018 15a94983 2018-12-23 stsp
5019 15a94983 2018-12-23 stsp if (type1 != type2)
5020 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5021 15a94983 2018-12-23 stsp }
5022 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5023 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5024 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5025 f44b1f58 2020-02-02 tracey s->repo = repo;
5026 f44b1f58 2020-02-02 tracey s->id1 = id1;
5027 f44b1f58 2020-02-02 tracey s->id2 = id2;
5028 3dbaef42 2020-11-24 stsp s->label1 = label1;
5029 3dbaef42 2020-11-24 stsp s->label2 = label2;
5030 48ae06ee 2018-10-18 stsp
5031 15a94983 2018-12-23 stsp if (id1) {
5032 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5033 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5034 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5035 48ae06ee 2018-10-18 stsp } else
5036 5465d566 2020-02-01 tracey s->id1 = NULL;
5037 48ae06ee 2018-10-18 stsp
5038 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5039 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5040 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5041 a0f32f33 2022-06-13 thomas goto done;
5042 48ae06ee 2018-10-18 stsp }
5043 a0f32f33 2022-06-13 thomas
5044 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5045 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5046 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5047 9a1d7435 2022-06-23 thomas goto done;
5048 9a1d7435 2022-06-23 thomas }
5049 9a1d7435 2022-06-23 thomas
5050 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5051 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5052 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5053 a0f32f33 2022-06-13 thomas goto done;
5054 a0f32f33 2022-06-13 thomas }
5055 a0f32f33 2022-06-13 thomas
5056 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5057 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5058 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5059 19a6a6b5 2022-07-01 thomas goto done;
5060 19a6a6b5 2022-07-01 thomas }
5061 19a6a6b5 2022-07-01 thomas
5062 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5063 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5064 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5065 19a6a6b5 2022-07-01 thomas goto done;
5066 19a6a6b5 2022-07-01 thomas }
5067 19a6a6b5 2022-07-01 thomas
5068 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5069 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5070 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5071 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5072 5465d566 2020-02-01 tracey s->repo = repo;
5073 6d17833f 2019-11-08 stsp
5074 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5075 6568f0aa 2022-08-06 thomas int rc;
5076 6d17833f 2019-11-08 stsp
5077 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5078 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5079 6568f0aa 2022-08-06 thomas if (rc != ERR)
5080 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5081 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5082 6568f0aa 2022-08-06 thomas if (rc != ERR)
5083 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5084 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5085 6568f0aa 2022-08-06 thomas if (rc != ERR)
5086 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5087 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5088 6568f0aa 2022-08-06 thomas if (rc != ERR)
5089 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5090 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5091 6568f0aa 2022-08-06 thomas if (rc != ERR)
5092 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5093 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5094 6568f0aa 2022-08-06 thomas if (rc != ERR)
5095 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5096 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5097 6568f0aa 2022-08-06 thomas if (rc != ERR)
5098 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5099 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5100 6568f0aa 2022-08-06 thomas if (rc != ERR)
5101 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5102 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5103 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5104 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5105 a0f32f33 2022-06-13 thomas goto done;
5106 6568f0aa 2022-08-06 thomas }
5107 6d17833f 2019-11-08 stsp }
5108 5dc9f4bc 2018-08-04 stsp
5109 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5110 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5111 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5112 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5113 f5215bb9 2019-02-22 stsp
5114 5465d566 2020-02-01 tracey err = create_diff(s);
5115 48ae06ee 2018-10-18 stsp
5116 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5117 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5118 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5119 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5120 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5121 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5122 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5123 a0f32f33 2022-06-13 thomas done:
5124 a0f32f33 2022-06-13 thomas if (err)
5125 a0f32f33 2022-06-13 thomas close_diff_view(view);
5126 e5a0f69f 2018-08-18 stsp return err;
5127 5dc9f4bc 2018-08-04 stsp }
5128 5dc9f4bc 2018-08-04 stsp
5129 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5130 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5131 5dc9f4bc 2018-08-04 stsp {
5132 a3404814 2018-09-02 stsp const struct got_error *err;
5133 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5134 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5135 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5136 a3404814 2018-09-02 stsp
5137 a3404814 2018-09-02 stsp if (s->id1) {
5138 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5139 a3404814 2018-09-02 stsp if (err)
5140 a3404814 2018-09-02 stsp return err;
5141 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5142 3dbaef42 2020-11-24 stsp } else
5143 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5144 3dbaef42 2020-11-24 stsp
5145 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5146 a3404814 2018-09-02 stsp if (err)
5147 a3404814 2018-09-02 stsp return err;
5148 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5149 26ed57b2 2018-05-19 stsp
5150 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5151 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5152 a3404814 2018-09-02 stsp free(id_str1);
5153 a3404814 2018-09-02 stsp free(id_str2);
5154 a3404814 2018-09-02 stsp return err;
5155 a3404814 2018-09-02 stsp }
5156 a3404814 2018-09-02 stsp free(id_str1);
5157 a3404814 2018-09-02 stsp free(id_str2);
5158 a3404814 2018-09-02 stsp
5159 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5160 267bb3b8 2021-08-01 stsp free(header);
5161 267bb3b8 2021-08-01 stsp return err;
5162 15a087fe 2019-02-21 stsp }
5163 15a087fe 2019-02-21 stsp
5164 15a087fe 2019-02-21 stsp static const struct got_error *
5165 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5166 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5167 15a087fe 2019-02-21 stsp {
5168 d7a04538 2019-02-21 stsp const struct got_error *err;
5169 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5170 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5171 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5172 15a087fe 2019-02-21 stsp
5173 15a087fe 2019-02-21 stsp free(s->id2);
5174 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5175 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5176 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5177 15a087fe 2019-02-21 stsp
5178 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5179 d7a04538 2019-02-21 stsp if (err)
5180 d7a04538 2019-02-21 stsp return err;
5181 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5182 15a087fe 2019-02-21 stsp free(s->id1);
5183 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5184 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5185 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5186 15a087fe 2019-02-21 stsp return NULL;
5187 0cf4efb1 2018-09-29 stsp }
5188 0cf4efb1 2018-09-29 stsp
5189 0cf4efb1 2018-09-29 stsp static const struct got_error *
5190 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5191 adf4c9e0 2022-07-03 thomas {
5192 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5193 adf4c9e0 2022-07-03 thomas
5194 adf4c9e0 2022-07-03 thomas view->count = 0;
5195 adf4c9e0 2022-07-03 thomas wclear(view->window);
5196 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5197 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5198 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5199 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5200 adf4c9e0 2022-07-03 thomas return create_diff(s);
5201 82c78e96 2022-08-06 thomas }
5202 82c78e96 2022-08-06 thomas
5203 82c78e96 2022-08-06 thomas static void
5204 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5205 82c78e96 2022-08-06 thomas {
5206 82c78e96 2022-08-06 thomas int start, i;
5207 82c78e96 2022-08-06 thomas
5208 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5209 82c78e96 2022-08-06 thomas
5210 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5211 82c78e96 2022-08-06 thomas if (i == 0)
5212 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5213 82c78e96 2022-08-06 thomas if (--i == start)
5214 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5215 82c78e96 2022-08-06 thomas }
5216 82c78e96 2022-08-06 thomas
5217 82c78e96 2022-08-06 thomas s->selected_line = 1;
5218 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5219 adf4c9e0 2022-07-03 thomas }
5220 adf4c9e0 2022-07-03 thomas
5221 82c78e96 2022-08-06 thomas static void
5222 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5223 82c78e96 2022-08-06 thomas {
5224 82c78e96 2022-08-06 thomas int start, i;
5225 82c78e96 2022-08-06 thomas
5226 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5227 82c78e96 2022-08-06 thomas
5228 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5229 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5230 82c78e96 2022-08-06 thomas i = 0;
5231 82c78e96 2022-08-06 thomas if (++i == start)
5232 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5233 82c78e96 2022-08-06 thomas }
5234 82c78e96 2022-08-06 thomas
5235 82c78e96 2022-08-06 thomas s->selected_line = 1;
5236 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5237 82c78e96 2022-08-06 thomas }
5238 82c78e96 2022-08-06 thomas
5239 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5240 4fc71f3b 2022-07-12 thomas int, int, int);
5241 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5242 4fc71f3b 2022-07-12 thomas int, int);
5243 4fc71f3b 2022-07-12 thomas
5244 adf4c9e0 2022-07-03 thomas static const struct got_error *
5245 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5246 e5a0f69f 2018-08-18 stsp {
5247 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5248 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5249 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5250 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5251 826082fe 2020-12-10 stsp char *line = NULL;
5252 826082fe 2020-12-10 stsp size_t linesize = 0;
5253 826082fe 2020-12-10 stsp ssize_t linelen;
5254 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5255 e5a0f69f 2018-08-18 stsp
5256 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5257 82c78e96 2022-08-06 thomas
5258 e5a0f69f 2018-08-18 stsp switch (ch) {
5259 05171be4 2022-06-23 thomas case '0':
5260 05171be4 2022-06-23 thomas view->x = 0;
5261 05171be4 2022-06-23 thomas break;
5262 05171be4 2022-06-23 thomas case '$':
5263 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5264 07b0611c 2022-06-23 thomas view->count = 0;
5265 05171be4 2022-06-23 thomas break;
5266 05171be4 2022-06-23 thomas case KEY_RIGHT:
5267 05171be4 2022-06-23 thomas case 'l':
5268 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5269 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5270 07b0611c 2022-06-23 thomas else
5271 07b0611c 2022-06-23 thomas view->count = 0;
5272 05171be4 2022-06-23 thomas break;
5273 05171be4 2022-06-23 thomas case KEY_LEFT:
5274 05171be4 2022-06-23 thomas case 'h':
5275 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5276 07b0611c 2022-06-23 thomas if (view->x <= 0)
5277 07b0611c 2022-06-23 thomas view->count = 0;
5278 05171be4 2022-06-23 thomas break;
5279 64453f7e 2020-11-21 stsp case 'a':
5280 3dbaef42 2020-11-24 stsp case 'w':
5281 3dbaef42 2020-11-24 stsp if (ch == 'a')
5282 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5283 772fcad5 2023-01-07 thomas else if (ch == 'w')
5284 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5285 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5286 912a3f79 2021-08-30 j break;
5287 912a3f79 2021-08-30 j case 'g':
5288 912a3f79 2021-08-30 j case KEY_HOME:
5289 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5290 07b0611c 2022-06-23 thomas view->count = 0;
5291 64453f7e 2020-11-21 stsp break;
5292 912a3f79 2021-08-30 j case 'G':
5293 912a3f79 2021-08-30 j case KEY_END:
5294 07b0611c 2022-06-23 thomas view->count = 0;
5295 912a3f79 2021-08-30 j if (s->eof)
5296 912a3f79 2021-08-30 j break;
5297 912a3f79 2021-08-30 j
5298 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5299 912a3f79 2021-08-30 j s->eof = 1;
5300 912a3f79 2021-08-30 j break;
5301 1e37a5c2 2019-05-12 jcs case 'k':
5302 1e37a5c2 2019-05-12 jcs case KEY_UP:
5303 f7140bf5 2021-10-17 thomas case CTRL('p'):
5304 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5305 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5306 07b0611c 2022-06-23 thomas else
5307 07b0611c 2022-06-23 thomas view->count = 0;
5308 1e37a5c2 2019-05-12 jcs break;
5309 70f17a53 2022-06-13 thomas case CTRL('u'):
5310 23427b14 2022-06-23 thomas case 'u':
5311 70f17a53 2022-06-13 thomas nscroll /= 2;
5312 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5313 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5314 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5315 1c5e5faa 2022-06-23 thomas case 'b':
5316 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5317 07b0611c 2022-06-23 thomas view->count = 0;
5318 26ed57b2 2018-05-19 stsp break;
5319 07b0611c 2022-06-23 thomas }
5320 1e37a5c2 2019-05-12 jcs i = 0;
5321 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5322 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5323 1e37a5c2 2019-05-12 jcs break;
5324 1e37a5c2 2019-05-12 jcs case 'j':
5325 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5326 f7140bf5 2021-10-17 thomas case CTRL('n'):
5327 1e37a5c2 2019-05-12 jcs if (!s->eof)
5328 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5329 07b0611c 2022-06-23 thomas else
5330 07b0611c 2022-06-23 thomas view->count = 0;
5331 1e37a5c2 2019-05-12 jcs break;
5332 70f17a53 2022-06-13 thomas case CTRL('d'):
5333 23427b14 2022-06-23 thomas case 'd':
5334 70f17a53 2022-06-13 thomas nscroll /= 2;
5335 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5336 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5337 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5338 1c5e5faa 2022-06-23 thomas case 'f':
5339 1e37a5c2 2019-05-12 jcs case ' ':
5340 07b0611c 2022-06-23 thomas if (s->eof) {
5341 07b0611c 2022-06-23 thomas view->count = 0;
5342 1e37a5c2 2019-05-12 jcs break;
5343 07b0611c 2022-06-23 thomas }
5344 1e37a5c2 2019-05-12 jcs i = 0;
5345 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5346 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5347 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5348 826082fe 2020-12-10 stsp if (linelen == -1) {
5349 826082fe 2020-12-10 stsp if (feof(s->f)) {
5350 826082fe 2020-12-10 stsp s->eof = 1;
5351 826082fe 2020-12-10 stsp } else
5352 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5353 34bc9ec9 2019-02-22 stsp break;
5354 826082fe 2020-12-10 stsp }
5355 1e37a5c2 2019-05-12 jcs }
5356 826082fe 2020-12-10 stsp free(line);
5357 1e37a5c2 2019-05-12 jcs break;
5358 82c78e96 2022-08-06 thomas case '(':
5359 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5360 82c78e96 2022-08-06 thomas break;
5361 82c78e96 2022-08-06 thomas case ')':
5362 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5363 82c78e96 2022-08-06 thomas break;
5364 82c78e96 2022-08-06 thomas case '{':
5365 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5366 82c78e96 2022-08-06 thomas break;
5367 82c78e96 2022-08-06 thomas case '}':
5368 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5369 82c78e96 2022-08-06 thomas break;
5370 1e37a5c2 2019-05-12 jcs case '[':
5371 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5372 1e37a5c2 2019-05-12 jcs s->diff_context--;
5373 aa61903a 2021-12-31 thomas s->matched_line = 0;
5374 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5375 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5376 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5377 27829c9e 2020-11-21 stsp s->nlines) {
5378 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5379 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5380 27829c9e 2020-11-21 stsp }
5381 07b0611c 2022-06-23 thomas } else
5382 07b0611c 2022-06-23 thomas view->count = 0;
5383 1e37a5c2 2019-05-12 jcs break;
5384 1e37a5c2 2019-05-12 jcs case ']':
5385 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5386 1e37a5c2 2019-05-12 jcs s->diff_context++;
5387 aa61903a 2021-12-31 thomas s->matched_line = 0;
5388 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5389 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5390 07b0611c 2022-06-23 thomas } else
5391 07b0611c 2022-06-23 thomas view->count = 0;
5392 1e37a5c2 2019-05-12 jcs break;
5393 1e37a5c2 2019-05-12 jcs case '<':
5394 1e37a5c2 2019-05-12 jcs case ',':
5395 777aae21 2022-07-20 thomas case 'K':
5396 4fc71f3b 2022-07-12 thomas up = 1;
5397 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5398 4fc71f3b 2022-07-12 thomas case '>':
5399 4fc71f3b 2022-07-12 thomas case '.':
5400 777aae21 2022-07-20 thomas case 'J':
5401 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5402 07b0611c 2022-06-23 thomas view->count = 0;
5403 48ae06ee 2018-10-18 stsp break;
5404 07b0611c 2022-06-23 thomas }
5405 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5406 6524637e 2019-02-21 stsp
5407 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5408 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5409 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5410 15a087fe 2019-02-21 stsp
5411 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5412 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5413 4fc71f3b 2022-07-12 thomas if (err)
5414 4fc71f3b 2022-07-12 thomas break;
5415 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5416 15a087fe 2019-02-21 stsp
5417 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5418 4fc71f3b 2022-07-12 thomas break;
5419 15a087fe 2019-02-21 stsp
5420 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5421 4fc71f3b 2022-07-12 thomas if (err)
5422 4fc71f3b 2022-07-12 thomas break;
5423 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5424 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5425 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5426 5e224a3e 2019-02-22 stsp
5427 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5428 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5429 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5430 4fc71f3b 2022-07-12 thomas
5431 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5432 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5433 4fc71f3b 2022-07-12 thomas if (err)
5434 4fc71f3b 2022-07-12 thomas break;
5435 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5436 5a8b5076 2020-12-05 stsp
5437 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5438 4fc71f3b 2022-07-12 thomas break;
5439 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5440 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5441 4fc71f3b 2022-07-12 thomas bs->selected_line);
5442 4fc71f3b 2022-07-12 thomas if (id == NULL)
5443 4fc71f3b 2022-07-12 thomas break;
5444 15a087fe 2019-02-21 stsp
5445 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5446 4fc71f3b 2022-07-12 thomas break;
5447 15a087fe 2019-02-21 stsp
5448 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5449 4fc71f3b 2022-07-12 thomas if (err)
5450 4fc71f3b 2022-07-12 thomas break;
5451 4fc71f3b 2022-07-12 thomas }
5452 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5453 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5454 aa61903a 2021-12-31 thomas s->matched_line = 0;
5455 05171be4 2022-06-23 thomas view->x = 0;
5456 1e37a5c2 2019-05-12 jcs
5457 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5458 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5459 1e37a5c2 2019-05-12 jcs break;
5460 1e37a5c2 2019-05-12 jcs default:
5461 07b0611c 2022-06-23 thomas view->count = 0;
5462 1e37a5c2 2019-05-12 jcs break;
5463 26ed57b2 2018-05-19 stsp }
5464 e5a0f69f 2018-08-18 stsp
5465 bcbd79e2 2018-08-19 stsp return err;
5466 26ed57b2 2018-05-19 stsp }
5467 26ed57b2 2018-05-19 stsp
5468 4ed7e80c 2018-05-20 stsp static const struct got_error *
5469 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5470 9f7d7167 2018-04-29 stsp {
5471 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5472 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5473 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5474 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5475 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5476 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5477 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5478 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5479 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5480 3dbaef42 2020-11-24 stsp const char *errstr;
5481 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5482 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5483 70ac5f84 2019-03-28 stsp
5484 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5485 26ed57b2 2018-05-19 stsp switch (ch) {
5486 64453f7e 2020-11-21 stsp case 'a':
5487 64453f7e 2020-11-21 stsp force_text_diff = 1;
5488 3dbaef42 2020-11-24 stsp break;
5489 3dbaef42 2020-11-24 stsp case 'C':
5490 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5491 3dbaef42 2020-11-24 stsp &errstr);
5492 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5493 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5494 8f666e67 2022-02-12 thomas errstr, errstr);
5495 64453f7e 2020-11-21 stsp break;
5496 09b5bff8 2020-02-23 naddy case 'r':
5497 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5498 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5499 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5500 09b5bff8 2020-02-23 naddy optarg);
5501 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5502 09b5bff8 2020-02-23 naddy break;
5503 3dbaef42 2020-11-24 stsp case 'w':
5504 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5505 3dbaef42 2020-11-24 stsp break;
5506 26ed57b2 2018-05-19 stsp default:
5507 17020d27 2019-03-07 stsp usage_diff();
5508 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5509 26ed57b2 2018-05-19 stsp }
5510 26ed57b2 2018-05-19 stsp }
5511 26ed57b2 2018-05-19 stsp
5512 26ed57b2 2018-05-19 stsp argc -= optind;
5513 26ed57b2 2018-05-19 stsp argv += optind;
5514 26ed57b2 2018-05-19 stsp
5515 26ed57b2 2018-05-19 stsp if (argc == 0) {
5516 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5517 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5518 15a94983 2018-12-23 stsp id_str1 = argv[0];
5519 15a94983 2018-12-23 stsp id_str2 = argv[1];
5520 26ed57b2 2018-05-19 stsp } else
5521 26ed57b2 2018-05-19 stsp usage_diff();
5522 eb6600df 2019-01-04 stsp
5523 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5524 7cd52833 2022-06-23 thomas if (error)
5525 7cd52833 2022-06-23 thomas goto done;
5526 7cd52833 2022-06-23 thomas
5527 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5528 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5529 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5530 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5531 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5532 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5533 c156c7a4 2020-12-18 stsp goto done;
5534 a273ac94 2020-02-23 naddy if (worktree)
5535 a273ac94 2020-02-23 naddy repo_path =
5536 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5537 a273ac94 2020-02-23 naddy else
5538 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5539 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5540 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5541 c156c7a4 2020-12-18 stsp goto done;
5542 c156c7a4 2020-12-18 stsp }
5543 a273ac94 2020-02-23 naddy }
5544 a273ac94 2020-02-23 naddy
5545 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5546 eb6600df 2019-01-04 stsp if (error)
5547 eb6600df 2019-01-04 stsp goto done;
5548 26ed57b2 2018-05-19 stsp
5549 a273ac94 2020-02-23 naddy init_curses();
5550 a273ac94 2020-02-23 naddy
5551 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5552 51a10b52 2020-12-26 stsp if (error)
5553 51a10b52 2020-12-26 stsp goto done;
5554 51a10b52 2020-12-26 stsp
5555 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5556 26ed57b2 2018-05-19 stsp if (error)
5557 26ed57b2 2018-05-19 stsp goto done;
5558 26ed57b2 2018-05-19 stsp
5559 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5560 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5561 26ed57b2 2018-05-19 stsp if (error)
5562 26ed57b2 2018-05-19 stsp goto done;
5563 26ed57b2 2018-05-19 stsp
5564 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5565 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5566 26ed57b2 2018-05-19 stsp if (error)
5567 26ed57b2 2018-05-19 stsp goto done;
5568 26ed57b2 2018-05-19 stsp
5569 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5570 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5571 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5572 ea5e7bb5 2018-08-01 stsp goto done;
5573 ea5e7bb5 2018-08-01 stsp }
5574 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5575 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5576 5dc9f4bc 2018-08-04 stsp if (error)
5577 5dc9f4bc 2018-08-04 stsp goto done;
5578 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5579 26ed57b2 2018-05-19 stsp done:
5580 3dbaef42 2020-11-24 stsp free(label1);
5581 3dbaef42 2020-11-24 stsp free(label2);
5582 c02c541e 2019-03-29 stsp free(repo_path);
5583 a273ac94 2020-02-23 naddy free(cwd);
5584 1d0f4054 2021-06-17 stsp if (repo) {
5585 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5586 1d0f4054 2021-06-17 stsp if (error == NULL)
5587 1d0f4054 2021-06-17 stsp error = close_err;
5588 1d0f4054 2021-06-17 stsp }
5589 a273ac94 2020-02-23 naddy if (worktree)
5590 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5591 7cd52833 2022-06-23 thomas if (pack_fds) {
5592 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5593 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5594 7cd52833 2022-06-23 thomas if (error == NULL)
5595 7cd52833 2022-06-23 thomas error = pack_err;
5596 7cd52833 2022-06-23 thomas }
5597 51a10b52 2020-12-26 stsp tog_free_refs();
5598 26ed57b2 2018-05-19 stsp return error;
5599 9f7d7167 2018-04-29 stsp }
5600 9f7d7167 2018-04-29 stsp
5601 4ed7e80c 2018-05-20 stsp __dead static void
5602 9f7d7167 2018-04-29 stsp usage_blame(void)
5603 9f7d7167 2018-04-29 stsp {
5604 80ddbec8 2018-04-29 stsp endwin();
5605 91198554 2022-06-23 thomas fprintf(stderr,
5606 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5607 9f7d7167 2018-04-29 stsp getprogname());
5608 9f7d7167 2018-04-29 stsp exit(1);
5609 9f7d7167 2018-04-29 stsp }
5610 84451b3e 2018-07-10 stsp
5611 84451b3e 2018-07-10 stsp struct tog_blame_line {
5612 84451b3e 2018-07-10 stsp int annotated;
5613 84451b3e 2018-07-10 stsp struct got_object_id *id;
5614 84451b3e 2018-07-10 stsp };
5615 9f7d7167 2018-04-29 stsp
5616 4ed7e80c 2018-05-20 stsp static const struct got_error *
5617 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5618 84451b3e 2018-07-10 stsp {
5619 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5620 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5621 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5622 84451b3e 2018-07-10 stsp const struct got_error *err;
5623 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5624 826082fe 2020-12-10 stsp char *line = NULL;
5625 826082fe 2020-12-10 stsp size_t linesize = 0;
5626 826082fe 2020-12-10 stsp ssize_t linelen;
5627 84451b3e 2018-07-10 stsp wchar_t *wline;
5628 27a741e5 2019-09-11 stsp int width;
5629 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5630 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5631 ab089a2a 2018-07-12 stsp char *id_str;
5632 11b20872 2019-11-08 stsp struct tog_color *tc;
5633 ab089a2a 2018-07-12 stsp
5634 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5635 ab089a2a 2018-07-12 stsp if (err)
5636 ab089a2a 2018-07-12 stsp return err;
5637 84451b3e 2018-07-10 stsp
5638 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5639 f7d12f7e 2018-08-01 stsp werase(view->window);
5640 84451b3e 2018-07-10 stsp
5641 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5642 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5643 ab089a2a 2018-07-12 stsp free(id_str);
5644 ab089a2a 2018-07-12 stsp return err;
5645 ab089a2a 2018-07-12 stsp }
5646 ab089a2a 2018-07-12 stsp
5647 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5648 ab089a2a 2018-07-12 stsp free(line);
5649 2550e4c3 2018-07-13 stsp line = NULL;
5650 1cae65b4 2019-09-22 stsp if (err)
5651 1cae65b4 2019-09-22 stsp return err;
5652 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5653 a3404814 2018-09-02 stsp wstandout(view->window);
5654 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5655 11b20872 2019-11-08 stsp if (tc)
5656 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5657 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5658 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5659 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5660 11b20872 2019-11-08 stsp if (tc)
5661 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5662 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5663 a3404814 2018-09-02 stsp wstandend(view->window);
5664 2550e4c3 2018-07-13 stsp free(wline);
5665 2550e4c3 2018-07-13 stsp wline = NULL;
5666 ab089a2a 2018-07-12 stsp
5667 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5668 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5669 07dd3ed3 2022-08-06 thomas
5670 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5671 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5672 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5673 ab089a2a 2018-07-12 stsp free(id_str);
5674 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5675 ab089a2a 2018-07-12 stsp }
5676 ab089a2a 2018-07-12 stsp free(id_str);
5677 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5678 3f60a8ef 2018-07-10 stsp free(line);
5679 2550e4c3 2018-07-13 stsp line = NULL;
5680 3f60a8ef 2018-07-10 stsp if (err)
5681 3f60a8ef 2018-07-10 stsp return err;
5682 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5683 2550e4c3 2018-07-13 stsp free(wline);
5684 2550e4c3 2018-07-13 stsp wline = NULL;
5685 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5686 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5687 3f60a8ef 2018-07-10 stsp
5688 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5689 05171be4 2022-06-23 thomas view->maxx = 0;
5690 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5691 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5692 826082fe 2020-12-10 stsp if (linelen == -1) {
5693 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5694 826082fe 2020-12-10 stsp s->eof = 1;
5695 826082fe 2020-12-10 stsp break;
5696 826082fe 2020-12-10 stsp }
5697 84451b3e 2018-07-10 stsp free(line);
5698 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5699 84451b3e 2018-07-10 stsp }
5700 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5701 07dd3ed3 2022-08-06 thomas continue;
5702 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5703 826082fe 2020-12-10 stsp continue;
5704 cbea3800 2022-06-23 thomas
5705 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5706 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5707 cbea3800 2022-06-23 thomas if (err) {
5708 cbea3800 2022-06-23 thomas free(line);
5709 cbea3800 2022-06-23 thomas return err;
5710 cbea3800 2022-06-23 thomas }
5711 cbea3800 2022-06-23 thomas free(wline);
5712 cbea3800 2022-06-23 thomas wline = NULL;
5713 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5714 84451b3e 2018-07-10 stsp
5715 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5716 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5717 b700b5d6 2018-07-10 stsp
5718 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5719 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5720 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5721 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5722 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5723 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5724 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5725 8d0fe45a 2019-08-12 stsp char *id_str;
5726 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5727 91198554 2022-06-23 thomas blame_line->id);
5728 8d0fe45a 2019-08-12 stsp if (err) {
5729 8d0fe45a 2019-08-12 stsp free(line);
5730 8d0fe45a 2019-08-12 stsp return err;
5731 8d0fe45a 2019-08-12 stsp }
5732 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5733 11b20872 2019-11-08 stsp if (tc)
5734 11b20872 2019-11-08 stsp wattr_on(view->window,
5735 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5736 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5737 11b20872 2019-11-08 stsp if (tc)
5738 11b20872 2019-11-08 stsp wattr_off(view->window,
5739 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5740 8d0fe45a 2019-08-12 stsp free(id_str);
5741 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5742 8d0fe45a 2019-08-12 stsp } else {
5743 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5744 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5745 84451b3e 2018-07-10 stsp }
5746 ee41ec32 2018-07-10 stsp } else {
5747 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5748 ee41ec32 2018-07-10 stsp prev_id = NULL;
5749 ee41ec32 2018-07-10 stsp }
5750 84451b3e 2018-07-10 stsp
5751 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5752 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5753 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5754 27a741e5 2019-09-11 stsp
5755 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5756 41605754 2020-11-12 stsp width = 9;
5757 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5758 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5759 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5760 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5761 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5762 41605754 2020-11-12 stsp if (err) {
5763 41605754 2020-11-12 stsp free(line);
5764 41605754 2020-11-12 stsp return err;
5765 41605754 2020-11-12 stsp }
5766 41605754 2020-11-12 stsp width += 9;
5767 41605754 2020-11-12 stsp } else {
5768 923086fd 2022-06-23 thomas int skip;
5769 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5770 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5771 923086fd 2022-06-23 thomas if (err) {
5772 923086fd 2022-06-23 thomas free(line);
5773 923086fd 2022-06-23 thomas return err;
5774 05171be4 2022-06-23 thomas }
5775 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5776 02bce7e2 2022-06-23 thomas width += 9;
5777 41605754 2020-11-12 stsp free(wline);
5778 41605754 2020-11-12 stsp wline = NULL;
5779 41605754 2020-11-12 stsp }
5780 41605754 2020-11-12 stsp
5781 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5782 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5783 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5784 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5785 84451b3e 2018-07-10 stsp }
5786 826082fe 2020-12-10 stsp free(line);
5787 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5788 84451b3e 2018-07-10 stsp
5789 a5d43cac 2022-07-01 thomas view_border(view);
5790 84451b3e 2018-07-10 stsp
5791 84451b3e 2018-07-10 stsp return NULL;
5792 84451b3e 2018-07-10 stsp }
5793 84451b3e 2018-07-10 stsp
5794 84451b3e 2018-07-10 stsp static const struct got_error *
5795 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5796 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5797 84451b3e 2018-07-10 stsp {
5798 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5799 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5800 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5801 1a76625f 2018-10-22 stsp int errcode;
5802 84451b3e 2018-07-10 stsp
5803 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5804 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5805 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5806 84451b3e 2018-07-10 stsp
5807 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5808 1a76625f 2018-10-22 stsp if (errcode)
5809 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5810 84451b3e 2018-07-10 stsp
5811 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5812 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5813 d68a0a7d 2018-07-10 stsp goto done;
5814 d68a0a7d 2018-07-10 stsp }
5815 d68a0a7d 2018-07-10 stsp
5816 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5817 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5818 d68a0a7d 2018-07-10 stsp
5819 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5820 d68a0a7d 2018-07-10 stsp if (line->annotated)
5821 d68a0a7d 2018-07-10 stsp goto done;
5822 d68a0a7d 2018-07-10 stsp
5823 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5824 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5825 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5826 84451b3e 2018-07-10 stsp goto done;
5827 84451b3e 2018-07-10 stsp }
5828 84451b3e 2018-07-10 stsp line->annotated = 1;
5829 84451b3e 2018-07-10 stsp done:
5830 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5831 1a76625f 2018-10-22 stsp if (errcode)
5832 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5833 84451b3e 2018-07-10 stsp return err;
5834 84451b3e 2018-07-10 stsp }
5835 84451b3e 2018-07-10 stsp
5836 84451b3e 2018-07-10 stsp static void *
5837 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5838 84451b3e 2018-07-10 stsp {
5839 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5840 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5841 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5842 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5843 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5844 00a8878e 2022-07-01 thomas
5845 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5846 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5847 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5848 9117a7b7 2022-07-01 thomas
5849 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5850 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5851 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5852 9117a7b7 2022-07-01 thomas goto done;
5853 9117a7b7 2022-07-01 thomas }
5854 18430de3 2018-07-10 stsp
5855 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5856 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5857 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5858 9117a7b7 2022-07-01 thomas goto done;
5859 9117a7b7 2022-07-01 thomas }
5860 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5861 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5862 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5863 9117a7b7 2022-07-01 thomas goto done;
5864 9117a7b7 2022-07-01 thomas }
5865 9117a7b7 2022-07-01 thomas
5866 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5867 61266923 2020-01-14 stsp if (err)
5868 9117a7b7 2022-07-01 thomas goto done;
5869 61266923 2020-01-14 stsp
5870 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5871 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5872 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5873 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5874 fc06ba56 2019-08-22 stsp err = NULL;
5875 18430de3 2018-07-10 stsp
5876 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5877 9117a7b7 2022-07-01 thomas if (errcode) {
5878 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5879 9117a7b7 2022-07-01 thomas goto done;
5880 9117a7b7 2022-07-01 thomas }
5881 18430de3 2018-07-10 stsp
5882 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5883 1d0f4054 2021-06-17 stsp if (err == NULL)
5884 1d0f4054 2021-06-17 stsp err = close_err;
5885 c9beca56 2018-07-22 stsp ta->repo = NULL;
5886 c9beca56 2018-07-22 stsp *ta->complete = 1;
5887 18430de3 2018-07-10 stsp
5888 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5889 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5890 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5891 18430de3 2018-07-10 stsp
5892 9117a7b7 2022-07-01 thomas done:
5893 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5894 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5895 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5896 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5897 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5898 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5899 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5900 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5901 00a8878e 2022-07-01 thomas
5902 18430de3 2018-07-10 stsp return (void *)err;
5903 84451b3e 2018-07-10 stsp }
5904 84451b3e 2018-07-10 stsp
5905 245d91c1 2018-07-12 stsp static struct got_object_id *
5906 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5907 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5908 245d91c1 2018-07-12 stsp {
5909 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5910 8d0fe45a 2019-08-12 stsp
5911 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5912 8d0fe45a 2019-08-12 stsp return NULL;
5913 b880a918 2018-07-10 stsp
5914 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5915 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5916 4fc71f3b 2022-07-12 thomas return NULL;
5917 4fc71f3b 2022-07-12 thomas
5918 4fc71f3b 2022-07-12 thomas return line->id;
5919 4fc71f3b 2022-07-12 thomas }
5920 4fc71f3b 2022-07-12 thomas
5921 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5922 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5923 4fc71f3b 2022-07-12 thomas int lineno)
5924 4fc71f3b 2022-07-12 thomas {
5925 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5926 4fc71f3b 2022-07-12 thomas
5927 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5928 4fc71f3b 2022-07-12 thomas return NULL;
5929 4fc71f3b 2022-07-12 thomas
5930 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5931 245d91c1 2018-07-12 stsp if (!line->annotated)
5932 245d91c1 2018-07-12 stsp return NULL;
5933 245d91c1 2018-07-12 stsp
5934 245d91c1 2018-07-12 stsp return line->id;
5935 b880a918 2018-07-10 stsp }
5936 245d91c1 2018-07-12 stsp
5937 b880a918 2018-07-10 stsp static const struct got_error *
5938 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5939 a70480e0 2018-06-23 stsp {
5940 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5941 245d91c1 2018-07-12 stsp int i;
5942 245d91c1 2018-07-12 stsp
5943 245d91c1 2018-07-12 stsp if (blame->thread) {
5944 1a76625f 2018-10-22 stsp int errcode;
5945 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5946 1a76625f 2018-10-22 stsp if (errcode)
5947 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5948 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5949 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5950 1a76625f 2018-10-22 stsp if (errcode)
5951 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5952 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5953 1a76625f 2018-10-22 stsp if (errcode)
5954 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5955 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5956 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5957 245d91c1 2018-07-12 stsp err = NULL;
5958 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5959 245d91c1 2018-07-12 stsp }
5960 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5961 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5962 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5963 1d0f4054 2021-06-17 stsp if (err == NULL)
5964 1d0f4054 2021-06-17 stsp err = close_err;
5965 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5966 245d91c1 2018-07-12 stsp }
5967 245d91c1 2018-07-12 stsp if (blame->f) {
5968 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5969 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5970 245d91c1 2018-07-12 stsp blame->f = NULL;
5971 245d91c1 2018-07-12 stsp }
5972 57670559 2018-12-23 stsp if (blame->lines) {
5973 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5974 57670559 2018-12-23 stsp free(blame->lines[i].id);
5975 57670559 2018-12-23 stsp free(blame->lines);
5976 57670559 2018-12-23 stsp blame->lines = NULL;
5977 57670559 2018-12-23 stsp }
5978 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5979 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5980 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5981 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5982 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5983 7cd52833 2022-06-23 thomas if (err == NULL)
5984 7cd52833 2022-06-23 thomas err = pack_err;
5985 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5986 7cd52833 2022-06-23 thomas }
5987 245d91c1 2018-07-12 stsp return err;
5988 245d91c1 2018-07-12 stsp }
5989 245d91c1 2018-07-12 stsp
5990 245d91c1 2018-07-12 stsp static const struct got_error *
5991 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5992 fc06ba56 2019-08-22 stsp {
5993 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5994 fc06ba56 2019-08-22 stsp int *done = arg;
5995 fc06ba56 2019-08-22 stsp int errcode;
5996 fc06ba56 2019-08-22 stsp
5997 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5998 fc06ba56 2019-08-22 stsp if (errcode)
5999 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6000 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6001 fc06ba56 2019-08-22 stsp
6002 fc06ba56 2019-08-22 stsp if (*done)
6003 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6004 fc06ba56 2019-08-22 stsp
6005 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6006 fc06ba56 2019-08-22 stsp if (errcode)
6007 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6008 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6009 fc06ba56 2019-08-22 stsp
6010 fc06ba56 2019-08-22 stsp return err;
6011 fc06ba56 2019-08-22 stsp }
6012 fc06ba56 2019-08-22 stsp
6013 fc06ba56 2019-08-22 stsp static const struct got_error *
6014 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6015 245d91c1 2018-07-12 stsp {
6016 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6017 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6018 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6019 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6020 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6021 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6022 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6023 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6024 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6025 a70480e0 2018-06-23 stsp
6026 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6027 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6028 27d434c2 2018-09-15 stsp if (err)
6029 15a94983 2018-12-23 stsp return err;
6030 f4ae6ddb 2022-07-01 thomas
6031 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6032 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6033 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6034 f4ae6ddb 2022-07-01 thomas goto done;
6035 f4ae6ddb 2022-07-01 thomas }
6036 945f9229 2022-04-16 thomas
6037 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6038 945f9229 2022-04-16 thomas if (err)
6039 945f9229 2022-04-16 thomas goto done;
6040 27d434c2 2018-09-15 stsp
6041 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6042 84451b3e 2018-07-10 stsp if (err)
6043 84451b3e 2018-07-10 stsp goto done;
6044 27d434c2 2018-09-15 stsp
6045 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6046 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6047 84451b3e 2018-07-10 stsp goto done;
6048 84451b3e 2018-07-10 stsp }
6049 a70480e0 2018-06-23 stsp
6050 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6051 a70480e0 2018-06-23 stsp if (err)
6052 a70480e0 2018-06-23 stsp goto done;
6053 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6054 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6055 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6056 84451b3e 2018-07-10 stsp goto done;
6057 84451b3e 2018-07-10 stsp }
6058 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6059 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6060 1fddf795 2021-01-20 stsp if (err)
6061 1fddf795 2021-01-20 stsp goto done;
6062 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6063 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6064 84451b3e 2018-07-10 stsp goto done;
6065 1fddf795 2021-01-20 stsp }
6066 b02560ec 2019-08-19 stsp
6067 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6068 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6069 b02560ec 2019-08-19 stsp blame->nlines--;
6070 a70480e0 2018-06-23 stsp
6071 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6072 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6073 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6074 84451b3e 2018-07-10 stsp goto done;
6075 84451b3e 2018-07-10 stsp }
6076 a70480e0 2018-06-23 stsp
6077 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6078 bd24772e 2018-07-11 stsp if (err)
6079 bd24772e 2018-07-11 stsp goto done;
6080 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6081 7cd52833 2022-06-23 thomas pack_fds);
6082 7cd52833 2022-06-23 thomas if (err)
6083 7cd52833 2022-06-23 thomas goto done;
6084 bd24772e 2018-07-11 stsp
6085 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6086 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6087 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6088 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6089 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6090 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6091 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6092 245d91c1 2018-07-12 stsp goto done;
6093 245d91c1 2018-07-12 stsp }
6094 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6095 245d91c1 2018-07-12 stsp
6096 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6097 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6098 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6099 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6100 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6101 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6102 a5388363 2020-12-01 naddy s->blame_complete = 0;
6103 f5a09613 2020-12-13 naddy
6104 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6105 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6106 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6107 f5a09613 2020-12-13 naddy s->selected_line = 1;
6108 f5a09613 2020-12-13 naddy }
6109 aa61903a 2021-12-31 thomas s->matched_line = 0;
6110 245d91c1 2018-07-12 stsp
6111 245d91c1 2018-07-12 stsp done:
6112 945f9229 2022-04-16 thomas if (commit)
6113 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6114 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6115 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6116 245d91c1 2018-07-12 stsp if (blob)
6117 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6118 27d434c2 2018-09-15 stsp free(obj_id);
6119 245d91c1 2018-07-12 stsp if (err)
6120 245d91c1 2018-07-12 stsp stop_blame(blame);
6121 245d91c1 2018-07-12 stsp return err;
6122 245d91c1 2018-07-12 stsp }
6123 245d91c1 2018-07-12 stsp
6124 245d91c1 2018-07-12 stsp static const struct got_error *
6125 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6126 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6127 245d91c1 2018-07-12 stsp {
6128 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6129 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6130 dbc6a6b6 2018-07-12 stsp
6131 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6132 245d91c1 2018-07-12 stsp
6133 c4843652 2019-08-12 stsp s->path = strdup(path);
6134 c4843652 2019-08-12 stsp if (s->path == NULL)
6135 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6136 c4843652 2019-08-12 stsp
6137 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6138 c4843652 2019-08-12 stsp if (err) {
6139 c4843652 2019-08-12 stsp free(s->path);
6140 7cbe629d 2018-08-04 stsp return err;
6141 c4843652 2019-08-12 stsp }
6142 245d91c1 2018-07-12 stsp
6143 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6144 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6145 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6146 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6147 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6148 fb2756b9 2018-08-04 stsp s->repo = repo;
6149 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6150 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6151 7cbe629d 2018-08-04 stsp
6152 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6153 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6154 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6155 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6156 11b20872 2019-11-08 stsp if (err)
6157 11b20872 2019-11-08 stsp return err;
6158 11b20872 2019-11-08 stsp }
6159 11b20872 2019-11-08 stsp
6160 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6161 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6162 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6163 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6164 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6165 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6166 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6167 e5a0f69f 2018-08-18 stsp
6168 a5388363 2020-12-01 naddy return run_blame(view);
6169 7cbe629d 2018-08-04 stsp }
6170 7cbe629d 2018-08-04 stsp
6171 e5a0f69f 2018-08-18 stsp static const struct got_error *
6172 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6173 7cbe629d 2018-08-04 stsp {
6174 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6175 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6176 7cbe629d 2018-08-04 stsp
6177 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6178 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6179 e5a0f69f 2018-08-18 stsp
6180 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6181 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6182 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6183 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6184 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6185 7cbe629d 2018-08-04 stsp }
6186 e5a0f69f 2018-08-18 stsp
6187 e5a0f69f 2018-08-18 stsp free(s->path);
6188 11b20872 2019-11-08 stsp free_colors(&s->colors);
6189 e5a0f69f 2018-08-18 stsp return err;
6190 7cbe629d 2018-08-04 stsp }
6191 7cbe629d 2018-08-04 stsp
6192 7cbe629d 2018-08-04 stsp static const struct got_error *
6193 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6194 6c4c42e0 2019-06-24 stsp {
6195 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6196 6c4c42e0 2019-06-24 stsp
6197 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6198 6c4c42e0 2019-06-24 stsp return NULL;
6199 2a7d3cd7 2022-09-17 thomas }
6200 2a7d3cd7 2022-09-17 thomas
6201 2a7d3cd7 2022-09-17 thomas static void
6202 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6203 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6204 2a7d3cd7 2022-09-17 thomas {
6205 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6206 2a7d3cd7 2022-09-17 thomas
6207 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6208 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6209 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6210 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6211 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6212 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6213 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6214 6c4c42e0 2019-06-24 stsp }
6215 6c4c42e0 2019-06-24 stsp
6216 6c4c42e0 2019-06-24 stsp static const struct got_error *
6217 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6218 7cbe629d 2018-08-04 stsp {
6219 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6220 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6221 2b380cc8 2018-10-24 stsp int errcode;
6222 2b380cc8 2018-10-24 stsp
6223 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6224 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6225 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6226 2b380cc8 2018-10-24 stsp if (errcode)
6227 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6228 51fe7530 2019-08-19 stsp
6229 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6230 2b380cc8 2018-10-24 stsp }
6231 e5a0f69f 2018-08-18 stsp
6232 51fe7530 2019-08-19 stsp if (s->blame_complete)
6233 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6234 51fe7530 2019-08-19 stsp
6235 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6236 e5a0f69f 2018-08-18 stsp
6237 a5d43cac 2022-07-01 thomas view_border(view);
6238 e5a0f69f 2018-08-18 stsp return err;
6239 e5a0f69f 2018-08-18 stsp }
6240 e5a0f69f 2018-08-18 stsp
6241 e5a0f69f 2018-08-18 stsp static const struct got_error *
6242 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6243 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6244 eaeaa612 2022-07-20 thomas {
6245 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6246 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6247 eaeaa612 2022-07-20 thomas
6248 eaeaa612 2022-07-20 thomas *new_view = NULL;
6249 eaeaa612 2022-07-20 thomas
6250 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6251 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6252 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6253 eaeaa612 2022-07-20 thomas
6254 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6255 eaeaa612 2022-07-20 thomas if (err)
6256 eaeaa612 2022-07-20 thomas view_close(log_view);
6257 eaeaa612 2022-07-20 thomas else
6258 eaeaa612 2022-07-20 thomas *new_view = log_view;
6259 eaeaa612 2022-07-20 thomas
6260 eaeaa612 2022-07-20 thomas return err;
6261 eaeaa612 2022-07-20 thomas }
6262 eaeaa612 2022-07-20 thomas
6263 eaeaa612 2022-07-20 thomas static const struct got_error *
6264 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6265 e5a0f69f 2018-08-18 stsp {
6266 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6267 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6268 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6269 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6270 a5d43cac 2022-07-01 thomas
6271 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6272 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6273 a5d43cac 2022-07-01 thomas --eos; /* border */
6274 7cbe629d 2018-08-04 stsp
6275 e5a0f69f 2018-08-18 stsp switch (ch) {
6276 05171be4 2022-06-23 thomas case '0':
6277 05171be4 2022-06-23 thomas view->x = 0;
6278 05171be4 2022-06-23 thomas break;
6279 05171be4 2022-06-23 thomas case '$':
6280 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
6281 07b0611c 2022-06-23 thomas view->count = 0;
6282 05171be4 2022-06-23 thomas break;
6283 05171be4 2022-06-23 thomas case KEY_RIGHT:
6284 05171be4 2022-06-23 thomas case 'l':
6285 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
6286 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
6287 07b0611c 2022-06-23 thomas else
6288 07b0611c 2022-06-23 thomas view->count = 0;
6289 05171be4 2022-06-23 thomas break;
6290 05171be4 2022-06-23 thomas case KEY_LEFT:
6291 05171be4 2022-06-23 thomas case 'h':
6292 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
6293 07b0611c 2022-06-23 thomas if (view->x <= 0)
6294 07b0611c 2022-06-23 thomas view->count = 0;
6295 05171be4 2022-06-23 thomas break;
6296 1e37a5c2 2019-05-12 jcs case 'q':
6297 1e37a5c2 2019-05-12 jcs s->done = 1;
6298 4deef56f 2021-09-02 naddy break;
6299 4deef56f 2021-09-02 naddy case 'g':
6300 4deef56f 2021-09-02 naddy case KEY_HOME:
6301 4deef56f 2021-09-02 naddy s->selected_line = 1;
6302 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6303 07b0611c 2022-06-23 thomas view->count = 0;
6304 4deef56f 2021-09-02 naddy break;
6305 4deef56f 2021-09-02 naddy case 'G':
6306 4deef56f 2021-09-02 naddy case KEY_END:
6307 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6308 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6309 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6310 4deef56f 2021-09-02 naddy } else {
6311 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6312 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6313 4deef56f 2021-09-02 naddy }
6314 07b0611c 2022-06-23 thomas view->count = 0;
6315 1e37a5c2 2019-05-12 jcs break;
6316 1e37a5c2 2019-05-12 jcs case 'k':
6317 1e37a5c2 2019-05-12 jcs case KEY_UP:
6318 f7140bf5 2021-10-17 thomas case CTRL('p'):
6319 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6320 1e37a5c2 2019-05-12 jcs s->selected_line--;
6321 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6322 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6323 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6324 07b0611c 2022-06-23 thomas else
6325 07b0611c 2022-06-23 thomas view->count = 0;
6326 1e37a5c2 2019-05-12 jcs break;
6327 70f17a53 2022-06-13 thomas case CTRL('u'):
6328 23427b14 2022-06-23 thomas case 'u':
6329 70f17a53 2022-06-13 thomas nscroll /= 2;
6330 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6331 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6332 ea025d1d 2020-02-22 naddy case CTRL('b'):
6333 1c5e5faa 2022-06-23 thomas case 'b':
6334 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6335 07b0611c 2022-06-23 thomas if (view->count > 1)
6336 07b0611c 2022-06-23 thomas nscroll += nscroll;
6337 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6338 07b0611c 2022-06-23 thomas view->count = 0;
6339 e5a0f69f 2018-08-18 stsp break;
6340 1e37a5c2 2019-05-12 jcs }
6341 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6342 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6343 1e37a5c2 2019-05-12 jcs else
6344 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6345 1e37a5c2 2019-05-12 jcs break;
6346 1e37a5c2 2019-05-12 jcs case 'j':
6347 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6348 f7140bf5 2021-10-17 thomas case CTRL('n'):
6349 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6350 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6351 1e37a5c2 2019-05-12 jcs s->selected_line++;
6352 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6353 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6354 07b0611c 2022-06-23 thomas else
6355 07b0611c 2022-06-23 thomas view->count = 0;
6356 1e37a5c2 2019-05-12 jcs break;
6357 1c5e5faa 2022-06-23 thomas case 'c':
6358 1e37a5c2 2019-05-12 jcs case 'p': {
6359 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6360 07b0611c 2022-06-23 thomas
6361 07b0611c 2022-06-23 thomas view->count = 0;
6362 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6363 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6364 1e37a5c2 2019-05-12 jcs if (id == NULL)
6365 e5a0f69f 2018-08-18 stsp break;
6366 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6367 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6368 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6369 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6370 1e37a5c2 2019-05-12 jcs int obj_type;
6371 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6372 1e37a5c2 2019-05-12 jcs s->repo, id);
6373 e5a0f69f 2018-08-18 stsp if (err)
6374 e5a0f69f 2018-08-18 stsp break;
6375 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6376 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6377 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6378 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6379 e5a0f69f 2018-08-18 stsp break;
6380 e5a0f69f 2018-08-18 stsp }
6381 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6382 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6383 ec242592 2022-04-22 thomas s->repo, &pid->id);
6384 945f9229 2022-04-16 thomas if (err)
6385 945f9229 2022-04-16 thomas break;
6386 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6387 945f9229 2022-04-16 thomas pcommit, s->path);
6388 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6389 e5a0f69f 2018-08-18 stsp if (err) {
6390 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6391 1e37a5c2 2019-05-12 jcs err = NULL;
6392 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6393 e5a0f69f 2018-08-18 stsp break;
6394 e5a0f69f 2018-08-18 stsp }
6395 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6396 1e37a5c2 2019-05-12 jcs blob_id);
6397 1e37a5c2 2019-05-12 jcs free(blob_id);
6398 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6399 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6400 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6401 e5a0f69f 2018-08-18 stsp break;
6402 1e37a5c2 2019-05-12 jcs }
6403 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6404 ec242592 2022-04-22 thomas &pid->id);
6405 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6406 1e37a5c2 2019-05-12 jcs } else {
6407 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6408 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6409 1e37a5c2 2019-05-12 jcs break;
6410 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6411 1e37a5c2 2019-05-12 jcs id);
6412 1e37a5c2 2019-05-12 jcs }
6413 1e37a5c2 2019-05-12 jcs if (err)
6414 e5a0f69f 2018-08-18 stsp break;
6415 1e37a5c2 2019-05-12 jcs s->done = 1;
6416 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6417 1e37a5c2 2019-05-12 jcs s->done = 0;
6418 1e37a5c2 2019-05-12 jcs if (thread_err)
6419 1e37a5c2 2019-05-12 jcs break;
6420 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6421 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6422 a5388363 2020-12-01 naddy err = run_blame(view);
6423 1e37a5c2 2019-05-12 jcs if (err)
6424 1e37a5c2 2019-05-12 jcs break;
6425 1e37a5c2 2019-05-12 jcs break;
6426 1e37a5c2 2019-05-12 jcs }
6427 1c5e5faa 2022-06-23 thomas case 'C': {
6428 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6429 07b0611c 2022-06-23 thomas
6430 07b0611c 2022-06-23 thomas view->count = 0;
6431 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6432 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6433 1e37a5c2 2019-05-12 jcs break;
6434 1e37a5c2 2019-05-12 jcs s->done = 1;
6435 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6436 1e37a5c2 2019-05-12 jcs s->done = 0;
6437 1e37a5c2 2019-05-12 jcs if (thread_err)
6438 1e37a5c2 2019-05-12 jcs break;
6439 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6440 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6441 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6442 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6443 a5388363 2020-12-01 naddy err = run_blame(view);
6444 1e37a5c2 2019-05-12 jcs if (err)
6445 1e37a5c2 2019-05-12 jcs break;
6446 1e37a5c2 2019-05-12 jcs break;
6447 1e37a5c2 2019-05-12 jcs }
6448 2a31b33b 2022-07-23 thomas case 'L':
6449 eaeaa612 2022-07-20 thomas view->count = 0;
6450 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6451 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6452 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6453 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6454 eaeaa612 2022-07-20 thomas break;
6455 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6456 1e37a5c2 2019-05-12 jcs case '\r': {
6457 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6458 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6459 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6460 07b0611c 2022-06-23 thomas
6461 07b0611c 2022-06-23 thomas view->count = 0;
6462 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6463 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6464 1e37a5c2 2019-05-12 jcs if (id == NULL)
6465 1e37a5c2 2019-05-12 jcs break;
6466 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6467 1e37a5c2 2019-05-12 jcs if (err)
6468 1e37a5c2 2019-05-12 jcs break;
6469 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6470 4fc71f3b 2022-07-12 thomas if (*new_view) {
6471 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6472 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6473 4fc71f3b 2022-07-12 thomas if (err)
6474 4fc71f3b 2022-07-12 thomas break;
6475 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6476 4fc71f3b 2022-07-12 thomas } else {
6477 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6478 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6479 a5d43cac 2022-07-01 thomas
6480 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6481 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6482 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6483 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6484 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6485 4fc71f3b 2022-07-12 thomas break;
6486 4fc71f3b 2022-07-12 thomas }
6487 15a94983 2018-12-23 stsp }
6488 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6489 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6490 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6491 1e37a5c2 2019-05-12 jcs if (err) {
6492 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6493 1e37a5c2 2019-05-12 jcs break;
6494 1e37a5c2 2019-05-12 jcs }
6495 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6496 4fc71f3b 2022-07-12 thomas s->selected_line;
6497 4fc71f3b 2022-07-12 thomas if (*new_view)
6498 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6499 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6500 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6501 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6502 a5d43cac 2022-07-01 thomas if (err)
6503 a5d43cac 2022-07-01 thomas break;
6504 a5d43cac 2022-07-01 thomas }
6505 a5d43cac 2022-07-01 thomas
6506 e78dc838 2020-12-04 stsp view->focussed = 0;
6507 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6508 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6509 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6510 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6511 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6512 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6513 1e37a5c2 2019-05-12 jcs if (err)
6514 34bc9ec9 2019-02-22 stsp break;
6515 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6516 40236d76 2022-06-23 thomas if (err)
6517 40236d76 2022-06-23 thomas break;
6518 e78dc838 2020-12-04 stsp view->focus_child = 1;
6519 1e37a5c2 2019-05-12 jcs } else
6520 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6521 1e37a5c2 2019-05-12 jcs if (err)
6522 e5a0f69f 2018-08-18 stsp break;
6523 1e37a5c2 2019-05-12 jcs break;
6524 1e37a5c2 2019-05-12 jcs }
6525 70f17a53 2022-06-13 thomas case CTRL('d'):
6526 23427b14 2022-06-23 thomas case 'd':
6527 70f17a53 2022-06-13 thomas nscroll /= 2;
6528 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6529 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6530 ea025d1d 2020-02-22 naddy case CTRL('f'):
6531 1c5e5faa 2022-06-23 thomas case 'f':
6532 1e37a5c2 2019-05-12 jcs case ' ':
6533 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6534 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6535 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6536 07b0611c 2022-06-23 thomas view->count = 0;
6537 e5a0f69f 2018-08-18 stsp break;
6538 1e37a5c2 2019-05-12 jcs }
6539 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6540 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6541 70f17a53 2022-06-13 thomas s->selected_line +=
6542 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6543 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6544 1e37a5c2 2019-05-12 jcs }
6545 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6546 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6547 1e37a5c2 2019-05-12 jcs else
6548 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6549 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6550 1e37a5c2 2019-05-12 jcs break;
6551 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6552 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6553 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6554 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6555 1e37a5c2 2019-05-12 jcs }
6556 1e37a5c2 2019-05-12 jcs break;
6557 1e37a5c2 2019-05-12 jcs default:
6558 07b0611c 2022-06-23 thomas view->count = 0;
6559 1e37a5c2 2019-05-12 jcs break;
6560 a70480e0 2018-06-23 stsp }
6561 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6562 adf4c9e0 2022-07-03 thomas }
6563 adf4c9e0 2022-07-03 thomas
6564 adf4c9e0 2022-07-03 thomas static const struct got_error *
6565 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6566 adf4c9e0 2022-07-03 thomas {
6567 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6568 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6569 adf4c9e0 2022-07-03 thomas
6570 adf4c9e0 2022-07-03 thomas view->count = 0;
6571 adf4c9e0 2022-07-03 thomas s->done = 1;
6572 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6573 adf4c9e0 2022-07-03 thomas s->done = 0;
6574 adf4c9e0 2022-07-03 thomas if (err)
6575 adf4c9e0 2022-07-03 thomas return err;
6576 adf4c9e0 2022-07-03 thomas return run_blame(view);
6577 a70480e0 2018-06-23 stsp }
6578 a70480e0 2018-06-23 stsp
6579 a70480e0 2018-06-23 stsp static const struct got_error *
6580 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6581 9f7d7167 2018-04-29 stsp {
6582 a70480e0 2018-06-23 stsp const struct got_error *error;
6583 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6584 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6585 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6586 0587e10c 2020-07-23 stsp char *link_target = NULL;
6587 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6588 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6589 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6590 a70480e0 2018-06-23 stsp int ch;
6591 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6592 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6593 a70480e0 2018-06-23 stsp
6594 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6595 a70480e0 2018-06-23 stsp switch (ch) {
6596 a70480e0 2018-06-23 stsp case 'c':
6597 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6598 a70480e0 2018-06-23 stsp break;
6599 69069811 2018-08-02 stsp case 'r':
6600 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6601 69069811 2018-08-02 stsp if (repo_path == NULL)
6602 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6603 9ba1d308 2019-10-21 stsp optarg);
6604 69069811 2018-08-02 stsp break;
6605 a70480e0 2018-06-23 stsp default:
6606 17020d27 2019-03-07 stsp usage_blame();
6607 a70480e0 2018-06-23 stsp /* NOTREACHED */
6608 a70480e0 2018-06-23 stsp }
6609 a70480e0 2018-06-23 stsp }
6610 a70480e0 2018-06-23 stsp
6611 a70480e0 2018-06-23 stsp argc -= optind;
6612 a70480e0 2018-06-23 stsp argv += optind;
6613 a70480e0 2018-06-23 stsp
6614 f135c941 2020-02-20 stsp if (argc != 1)
6615 a70480e0 2018-06-23 stsp usage_blame();
6616 6962eb72 2020-02-20 stsp
6617 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6618 7cd52833 2022-06-23 thomas if (error != NULL)
6619 7cd52833 2022-06-23 thomas goto done;
6620 7cd52833 2022-06-23 thomas
6621 69069811 2018-08-02 stsp if (repo_path == NULL) {
6622 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6623 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6624 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6625 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6626 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6627 c156c7a4 2020-12-18 stsp goto done;
6628 f135c941 2020-02-20 stsp if (worktree)
6629 eb41ed75 2019-02-05 stsp repo_path =
6630 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6631 f135c941 2020-02-20 stsp else
6632 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6633 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6634 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6635 c156c7a4 2020-12-18 stsp goto done;
6636 c156c7a4 2020-12-18 stsp }
6637 f135c941 2020-02-20 stsp }
6638 a915003a 2019-02-05 stsp
6639 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6640 c02c541e 2019-03-29 stsp if (error != NULL)
6641 8e94dd5b 2019-01-04 stsp goto done;
6642 69069811 2018-08-02 stsp
6643 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6644 f135c941 2020-02-20 stsp worktree);
6645 c02c541e 2019-03-29 stsp if (error)
6646 92205607 2019-01-04 stsp goto done;
6647 69069811 2018-08-02 stsp
6648 f135c941 2020-02-20 stsp init_curses();
6649 f135c941 2020-02-20 stsp
6650 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6651 51a10b52 2020-12-26 stsp if (error)
6652 51a10b52 2020-12-26 stsp goto done;
6653 51a10b52 2020-12-26 stsp
6654 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6655 eb41ed75 2019-02-05 stsp if (error)
6656 69069811 2018-08-02 stsp goto done;
6657 a70480e0 2018-06-23 stsp
6658 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6659 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6660 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6661 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6662 a70480e0 2018-06-23 stsp if (error != NULL)
6663 66b4983c 2018-06-23 stsp goto done;
6664 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6665 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6666 a70480e0 2018-06-23 stsp } else {
6667 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6668 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6669 a70480e0 2018-06-23 stsp }
6670 a19e88aa 2018-06-23 stsp if (error != NULL)
6671 8b473291 2019-02-21 stsp goto done;
6672 8b473291 2019-02-21 stsp
6673 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6674 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6675 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6676 e1cd8fed 2018-08-01 stsp goto done;
6677 e1cd8fed 2018-08-01 stsp }
6678 0587e10c 2020-07-23 stsp
6679 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6680 945f9229 2022-04-16 thomas if (error)
6681 945f9229 2022-04-16 thomas goto done;
6682 945f9229 2022-04-16 thomas
6683 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6684 945f9229 2022-04-16 thomas commit, repo);
6685 7cbe629d 2018-08-04 stsp if (error)
6686 7cbe629d 2018-08-04 stsp goto done;
6687 0587e10c 2020-07-23 stsp
6688 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6689 78756c87 2020-11-24 stsp commit_id, repo);
6690 0587e10c 2020-07-23 stsp if (error)
6691 0587e10c 2020-07-23 stsp goto done;
6692 12314ad4 2019-08-31 stsp if (worktree) {
6693 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6694 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6695 12314ad4 2019-08-31 stsp worktree = NULL;
6696 12314ad4 2019-08-31 stsp }
6697 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6698 a70480e0 2018-06-23 stsp done:
6699 69069811 2018-08-02 stsp free(repo_path);
6700 f135c941 2020-02-20 stsp free(in_repo_path);
6701 0587e10c 2020-07-23 stsp free(link_target);
6702 69069811 2018-08-02 stsp free(cwd);
6703 a70480e0 2018-06-23 stsp free(commit_id);
6704 945f9229 2022-04-16 thomas if (commit)
6705 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6706 eb41ed75 2019-02-05 stsp if (worktree)
6707 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6708 1d0f4054 2021-06-17 stsp if (repo) {
6709 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6710 1d0f4054 2021-06-17 stsp if (error == NULL)
6711 1d0f4054 2021-06-17 stsp error = close_err;
6712 1d0f4054 2021-06-17 stsp }
6713 7cd52833 2022-06-23 thomas if (pack_fds) {
6714 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6715 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6716 7cd52833 2022-06-23 thomas if (error == NULL)
6717 7cd52833 2022-06-23 thomas error = pack_err;
6718 7cd52833 2022-06-23 thomas }
6719 51a10b52 2020-12-26 stsp tog_free_refs();
6720 a70480e0 2018-06-23 stsp return error;
6721 ffd1d5e5 2018-06-23 stsp }
6722 ffd1d5e5 2018-06-23 stsp
6723 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6724 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6725 ffd1d5e5 2018-06-23 stsp {
6726 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6727 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6728 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6729 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6730 86f4aab9 2022-09-09 thomas char *index = NULL;
6731 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6732 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6733 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6734 ffd1d5e5 2018-06-23 stsp
6735 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6736 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6737 a5d43cac 2022-07-01 thomas --limit; /* border */
6738 ffd1d5e5 2018-06-23 stsp
6739 f7d12f7e 2018-08-01 stsp werase(view->window);
6740 ffd1d5e5 2018-06-23 stsp
6741 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6742 ffd1d5e5 2018-06-23 stsp return NULL;
6743 ffd1d5e5 2018-06-23 stsp
6744 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6745 f91a2b48 2022-06-23 thomas 0, 0);
6746 ffd1d5e5 2018-06-23 stsp if (err)
6747 ffd1d5e5 2018-06-23 stsp return err;
6748 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6749 a3404814 2018-09-02 stsp wstandout(view->window);
6750 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6751 11b20872 2019-11-08 stsp if (tc)
6752 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6753 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6754 86f4aab9 2022-09-09 thomas free(wline);
6755 86f4aab9 2022-09-09 thomas wline = NULL;
6756 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6757 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6758 11b20872 2019-11-08 stsp if (tc)
6759 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6760 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6761 a3404814 2018-09-02 stsp wstandend(view->window);
6762 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6763 86f4aab9 2022-09-09 thomas return NULL;
6764 07dd3ed3 2022-08-06 thomas
6765 6f5f393a 2022-08-12 thomas i += s->selected;
6766 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6767 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6768 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6769 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6770 07dd3ed3 2022-08-06 thomas }
6771 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6772 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6773 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6774 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6775 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6776 86f4aab9 2022-09-09 thomas free(index);
6777 ce52c690 2018-06-23 stsp if (err)
6778 ce52c690 2018-06-23 stsp return err;
6779 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6780 2550e4c3 2018-07-13 stsp free(wline);
6781 2550e4c3 2018-07-13 stsp wline = NULL;
6782 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6783 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6784 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6785 ffd1d5e5 2018-06-23 stsp return NULL;
6786 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6787 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6788 a1eca9bb 2018-06-23 stsp return NULL;
6789 ffd1d5e5 2018-06-23 stsp
6790 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6791 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6792 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6793 0cf4efb1 2018-09-29 stsp if (view->focussed)
6794 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6795 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6796 ffd1d5e5 2018-06-23 stsp }
6797 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6798 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6799 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6800 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6801 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6802 ffd1d5e5 2018-06-23 stsp return NULL;
6803 ffd1d5e5 2018-06-23 stsp n = 1;
6804 ffd1d5e5 2018-06-23 stsp } else {
6805 ffd1d5e5 2018-06-23 stsp n = 0;
6806 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6807 ffd1d5e5 2018-06-23 stsp }
6808 ffd1d5e5 2018-06-23 stsp
6809 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6810 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6811 848d6979 2019-08-12 stsp const char *modestr = "";
6812 56e0773d 2019-11-28 stsp mode_t mode;
6813 1d13200f 2018-07-12 stsp
6814 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6815 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6816 56e0773d 2019-11-28 stsp
6817 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6818 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6819 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6820 1d13200f 2018-07-12 stsp if (err)
6821 638f9024 2019-05-13 stsp return got_error_from_errno(
6822 230a42bd 2019-05-11 jcs "got_object_id_str");
6823 1d13200f 2018-07-12 stsp }
6824 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6825 63c5ca5d 2019-08-24 stsp modestr = "$";
6826 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6827 0d6c6ee3 2020-05-20 stsp int i;
6828 0d6c6ee3 2020-05-20 stsp
6829 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6830 d86d3b18 2020-12-01 naddy te, s->repo);
6831 0d6c6ee3 2020-05-20 stsp if (err) {
6832 0d6c6ee3 2020-05-20 stsp free(id_str);
6833 0d6c6ee3 2020-05-20 stsp return err;
6834 0d6c6ee3 2020-05-20 stsp }
6835 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6836 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6837 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6838 0d6c6ee3 2020-05-20 stsp }
6839 848d6979 2019-08-12 stsp modestr = "@";
6840 0d6c6ee3 2020-05-20 stsp }
6841 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6842 848d6979 2019-08-12 stsp modestr = "/";
6843 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6844 848d6979 2019-08-12 stsp modestr = "*";
6845 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6846 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6847 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6848 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6849 1d13200f 2018-07-12 stsp free(id_str);
6850 0d6c6ee3 2020-05-20 stsp free(link_target);
6851 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6852 1d13200f 2018-07-12 stsp }
6853 1d13200f 2018-07-12 stsp free(id_str);
6854 0d6c6ee3 2020-05-20 stsp free(link_target);
6855 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6856 f91a2b48 2022-06-23 thomas 0, 0);
6857 ffd1d5e5 2018-06-23 stsp if (err) {
6858 ffd1d5e5 2018-06-23 stsp free(line);
6859 ffd1d5e5 2018-06-23 stsp break;
6860 ffd1d5e5 2018-06-23 stsp }
6861 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6862 0cf4efb1 2018-09-29 stsp if (view->focussed)
6863 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6864 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6865 ffd1d5e5 2018-06-23 stsp }
6866 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6867 f26dddb7 2019-11-08 stsp if (tc)
6868 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6869 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6870 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6871 f26dddb7 2019-11-08 stsp if (tc)
6872 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6873 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6874 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6875 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6876 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6877 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6878 ffd1d5e5 2018-06-23 stsp free(line);
6879 2550e4c3 2018-07-13 stsp free(wline);
6880 2550e4c3 2018-07-13 stsp wline = NULL;
6881 ffd1d5e5 2018-06-23 stsp n++;
6882 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6883 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6884 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6885 ffd1d5e5 2018-06-23 stsp break;
6886 ffd1d5e5 2018-06-23 stsp }
6887 ffd1d5e5 2018-06-23 stsp
6888 ffd1d5e5 2018-06-23 stsp return err;
6889 ffd1d5e5 2018-06-23 stsp }
6890 ffd1d5e5 2018-06-23 stsp
6891 ffd1d5e5 2018-06-23 stsp static void
6892 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6893 ffd1d5e5 2018-06-23 stsp {
6894 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6895 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6896 fa86c4bf 2020-11-29 stsp int i = 0;
6897 ffd1d5e5 2018-06-23 stsp
6898 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6899 ffd1d5e5 2018-06-23 stsp return;
6900 ffd1d5e5 2018-06-23 stsp
6901 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6902 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6903 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6904 fa86c4bf 2020-11-29 stsp if (!isroot)
6905 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6906 fa86c4bf 2020-11-29 stsp break;
6907 fa86c4bf 2020-11-29 stsp }
6908 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6909 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6910 ffd1d5e5 2018-06-23 stsp }
6911 ffd1d5e5 2018-06-23 stsp }
6912 ffd1d5e5 2018-06-23 stsp
6913 a5d43cac 2022-07-01 thomas static const struct got_error *
6914 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6915 ffd1d5e5 2018-06-23 stsp {
6916 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6917 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6918 ffd1d5e5 2018-06-23 stsp int n = 0;
6919 ffd1d5e5 2018-06-23 stsp
6920 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6921 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6922 694d3271 2020-12-01 naddy s->first_displayed_entry);
6923 694d3271 2020-12-01 naddy else
6924 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6925 56e0773d 2019-11-28 stsp
6926 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6927 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6928 07dd3ed3 2022-08-06 thomas if (last) {
6929 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6930 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6931 07dd3ed3 2022-08-06 thomas }
6932 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6933 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6934 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6935 768394f3 2019-01-24 stsp }
6936 ffd1d5e5 2018-06-23 stsp }
6937 a5d43cac 2022-07-01 thomas
6938 a5d43cac 2022-07-01 thomas return NULL;
6939 ffd1d5e5 2018-06-23 stsp }
6940 ffd1d5e5 2018-06-23 stsp
6941 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6942 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6943 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6944 ffd1d5e5 2018-06-23 stsp {
6945 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6946 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6947 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6948 ffd1d5e5 2018-06-23 stsp
6949 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6950 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6951 56e0773d 2019-11-28 stsp + 1 /* slash */;
6952 ce52c690 2018-06-23 stsp if (te)
6953 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6954 ce52c690 2018-06-23 stsp
6955 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6956 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6957 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6958 ffd1d5e5 2018-06-23 stsp
6959 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6960 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6961 d9765a41 2018-06-23 stsp while (pt) {
6962 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6963 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6964 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6965 cb2ebc8a 2018-06-23 stsp goto done;
6966 cb2ebc8a 2018-06-23 stsp }
6967 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6968 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6969 cb2ebc8a 2018-06-23 stsp goto done;
6970 cb2ebc8a 2018-06-23 stsp }
6971 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6972 ffd1d5e5 2018-06-23 stsp }
6973 ce52c690 2018-06-23 stsp if (te) {
6974 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6975 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6976 ce52c690 2018-06-23 stsp goto done;
6977 ce52c690 2018-06-23 stsp }
6978 cb2ebc8a 2018-06-23 stsp }
6979 ce52c690 2018-06-23 stsp done:
6980 ce52c690 2018-06-23 stsp if (err) {
6981 ce52c690 2018-06-23 stsp free(*path);
6982 ce52c690 2018-06-23 stsp *path = NULL;
6983 ce52c690 2018-06-23 stsp }
6984 ce52c690 2018-06-23 stsp return err;
6985 ce52c690 2018-06-23 stsp }
6986 ce52c690 2018-06-23 stsp
6987 ce52c690 2018-06-23 stsp static const struct got_error *
6988 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6989 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6990 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6991 ce52c690 2018-06-23 stsp {
6992 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6993 ce52c690 2018-06-23 stsp char *path;
6994 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6995 a0de39f3 2019-08-09 stsp
6996 a0de39f3 2019-08-09 stsp *new_view = NULL;
6997 69efd4c4 2018-07-18 stsp
6998 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6999 ce52c690 2018-06-23 stsp if (err)
7000 ce52c690 2018-06-23 stsp return err;
7001 ffd1d5e5 2018-06-23 stsp
7002 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7003 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7004 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7005 83ce39e3 2019-08-12 stsp goto done;
7006 83ce39e3 2019-08-12 stsp }
7007 cdf1ee82 2018-08-01 stsp
7008 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7009 e5a0f69f 2018-08-18 stsp if (err) {
7010 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7011 fc06ba56 2019-08-22 stsp err = NULL;
7012 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7013 e5a0f69f 2018-08-18 stsp } else
7014 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7015 83ce39e3 2019-08-12 stsp done:
7016 83ce39e3 2019-08-12 stsp free(path);
7017 69efd4c4 2018-07-18 stsp return err;
7018 69efd4c4 2018-07-18 stsp }
7019 69efd4c4 2018-07-18 stsp
7020 69efd4c4 2018-07-18 stsp static const struct got_error *
7021 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7022 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7023 69efd4c4 2018-07-18 stsp {
7024 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7025 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7026 69efd4c4 2018-07-18 stsp char *path;
7027 a0de39f3 2019-08-09 stsp
7028 a0de39f3 2019-08-09 stsp *new_view = NULL;
7029 69efd4c4 2018-07-18 stsp
7030 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7031 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7032 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7033 e5a0f69f 2018-08-18 stsp
7034 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7035 69efd4c4 2018-07-18 stsp if (err)
7036 69efd4c4 2018-07-18 stsp return err;
7037 69efd4c4 2018-07-18 stsp
7038 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7039 4e97c21c 2020-12-06 stsp path, 0);
7040 ba4f502b 2018-08-04 stsp if (err)
7041 e5a0f69f 2018-08-18 stsp view_close(log_view);
7042 e5a0f69f 2018-08-18 stsp else
7043 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7044 cb2ebc8a 2018-06-23 stsp free(path);
7045 cb2ebc8a 2018-06-23 stsp return err;
7046 ffd1d5e5 2018-06-23 stsp }
7047 ffd1d5e5 2018-06-23 stsp
7048 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7049 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7050 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7051 ffd1d5e5 2018-06-23 stsp {
7052 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7053 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7054 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7055 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7056 ffd1d5e5 2018-06-23 stsp
7057 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7058 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7059 bc573f3b 2021-07-10 stsp
7060 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7061 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7062 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7063 ffd1d5e5 2018-06-23 stsp
7064 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7065 bc573f3b 2021-07-10 stsp if (err)
7066 bc573f3b 2021-07-10 stsp goto done;
7067 bc573f3b 2021-07-10 stsp
7068 bc573f3b 2021-07-10 stsp /*
7069 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7070 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7071 bc573f3b 2021-07-10 stsp * closed on demand.
7072 bc573f3b 2021-07-10 stsp */
7073 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7074 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7075 bc573f3b 2021-07-10 stsp if (err)
7076 bc573f3b 2021-07-10 stsp goto done;
7077 bc573f3b 2021-07-10 stsp s->tree = s->root;
7078 bc573f3b 2021-07-10 stsp
7079 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7080 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7081 ffd1d5e5 2018-06-23 stsp goto done;
7082 ffd1d5e5 2018-06-23 stsp
7083 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7084 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7085 ffd1d5e5 2018-06-23 stsp goto done;
7086 ffd1d5e5 2018-06-23 stsp }
7087 ffd1d5e5 2018-06-23 stsp
7088 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7089 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7090 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7091 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7092 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7093 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7094 9cd7cbd1 2020-12-07 stsp goto done;
7095 9cd7cbd1 2020-12-07 stsp }
7096 9cd7cbd1 2020-12-07 stsp }
7097 fb2756b9 2018-08-04 stsp s->repo = repo;
7098 c0b01bdb 2019-11-08 stsp
7099 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7100 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7101 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7102 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7103 c0b01bdb 2019-11-08 stsp if (err)
7104 c0b01bdb 2019-11-08 stsp goto done;
7105 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7106 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7107 bc573f3b 2021-07-10 stsp if (err)
7108 c0b01bdb 2019-11-08 stsp goto done;
7109 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7110 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7111 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7112 bc573f3b 2021-07-10 stsp if (err)
7113 c0b01bdb 2019-11-08 stsp goto done;
7114 e5a0f69f 2018-08-18 stsp
7115 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7116 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7117 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7118 bc573f3b 2021-07-10 stsp if (err)
7119 11b20872 2019-11-08 stsp goto done;
7120 11b20872 2019-11-08 stsp
7121 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7122 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7123 bc573f3b 2021-07-10 stsp if (err)
7124 c0b01bdb 2019-11-08 stsp goto done;
7125 c0b01bdb 2019-11-08 stsp }
7126 c0b01bdb 2019-11-08 stsp
7127 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7128 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7129 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7130 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7131 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7132 ad80ab7b 2018-08-04 stsp done:
7133 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7134 bc573f3b 2021-07-10 stsp if (commit)
7135 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7136 bc573f3b 2021-07-10 stsp if (err)
7137 bc573f3b 2021-07-10 stsp close_tree_view(view);
7138 ad80ab7b 2018-08-04 stsp return err;
7139 ad80ab7b 2018-08-04 stsp }
7140 ad80ab7b 2018-08-04 stsp
7141 e5a0f69f 2018-08-18 stsp static const struct got_error *
7142 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7143 ad80ab7b 2018-08-04 stsp {
7144 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7145 ad80ab7b 2018-08-04 stsp
7146 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7147 fb2756b9 2018-08-04 stsp free(s->tree_label);
7148 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7149 6484ec90 2018-09-29 stsp free(s->commit_id);
7150 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7151 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7152 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7153 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7154 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7155 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7156 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7157 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7158 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7159 ad80ab7b 2018-08-04 stsp free(parent);
7160 ad80ab7b 2018-08-04 stsp
7161 ad80ab7b 2018-08-04 stsp }
7162 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7163 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7164 bc573f3b 2021-07-10 stsp if (s->root)
7165 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7166 7c32bd05 2019-06-22 stsp return NULL;
7167 7c32bd05 2019-06-22 stsp }
7168 7c32bd05 2019-06-22 stsp
7169 7c32bd05 2019-06-22 stsp static const struct got_error *
7170 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7171 7c32bd05 2019-06-22 stsp {
7172 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7173 7c32bd05 2019-06-22 stsp
7174 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7175 7c32bd05 2019-06-22 stsp return NULL;
7176 7c32bd05 2019-06-22 stsp }
7177 7c32bd05 2019-06-22 stsp
7178 7c32bd05 2019-06-22 stsp static int
7179 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7180 7c32bd05 2019-06-22 stsp {
7181 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7182 7c32bd05 2019-06-22 stsp
7183 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7184 56e0773d 2019-11-28 stsp 0) == 0;
7185 7c32bd05 2019-06-22 stsp }
7186 7c32bd05 2019-06-22 stsp
7187 7c32bd05 2019-06-22 stsp static const struct got_error *
7188 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7189 7c32bd05 2019-06-22 stsp {
7190 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7191 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7192 7c32bd05 2019-06-22 stsp
7193 7c32bd05 2019-06-22 stsp if (!view->searching) {
7194 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7195 7c32bd05 2019-06-22 stsp return NULL;
7196 7c32bd05 2019-06-22 stsp }
7197 7c32bd05 2019-06-22 stsp
7198 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7199 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7200 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7201 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7202 56e0773d 2019-11-28 stsp s->selected_entry);
7203 7c32bd05 2019-06-22 stsp else
7204 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7205 56e0773d 2019-11-28 stsp } else {
7206 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7207 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7208 56e0773d 2019-11-28 stsp else
7209 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7210 56e0773d 2019-11-28 stsp s->selected_entry);
7211 7c32bd05 2019-06-22 stsp }
7212 7c32bd05 2019-06-22 stsp } else {
7213 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7214 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7215 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7216 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7217 56e0773d 2019-11-28 stsp else
7218 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7219 7c32bd05 2019-06-22 stsp }
7220 7c32bd05 2019-06-22 stsp
7221 7c32bd05 2019-06-22 stsp while (1) {
7222 56e0773d 2019-11-28 stsp if (te == NULL) {
7223 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7224 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7225 ac66afb8 2019-06-24 stsp return NULL;
7226 ac66afb8 2019-06-24 stsp }
7227 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7228 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7229 56e0773d 2019-11-28 stsp else
7230 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7231 7c32bd05 2019-06-22 stsp }
7232 7c32bd05 2019-06-22 stsp
7233 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7234 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7235 56e0773d 2019-11-28 stsp s->matched_entry = te;
7236 7c32bd05 2019-06-22 stsp break;
7237 7c32bd05 2019-06-22 stsp }
7238 7c32bd05 2019-06-22 stsp
7239 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7240 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7241 56e0773d 2019-11-28 stsp else
7242 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7243 7c32bd05 2019-06-22 stsp }
7244 e5a0f69f 2018-08-18 stsp
7245 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7246 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7247 7c32bd05 2019-06-22 stsp s->selected = 0;
7248 7c32bd05 2019-06-22 stsp }
7249 7c32bd05 2019-06-22 stsp
7250 e5a0f69f 2018-08-18 stsp return NULL;
7251 ad80ab7b 2018-08-04 stsp }
7252 ad80ab7b 2018-08-04 stsp
7253 ad80ab7b 2018-08-04 stsp static const struct got_error *
7254 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7255 ad80ab7b 2018-08-04 stsp {
7256 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7257 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7258 e5a0f69f 2018-08-18 stsp char *parent_path;
7259 ad80ab7b 2018-08-04 stsp
7260 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7261 e5a0f69f 2018-08-18 stsp if (err)
7262 e5a0f69f 2018-08-18 stsp return err;
7263 ffd1d5e5 2018-06-23 stsp
7264 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7265 e5a0f69f 2018-08-18 stsp free(parent_path);
7266 669b5ffa 2018-10-07 stsp
7267 a5d43cac 2022-07-01 thomas view_border(view);
7268 e5a0f69f 2018-08-18 stsp return err;
7269 07dd3ed3 2022-08-06 thomas }
7270 07dd3ed3 2022-08-06 thomas
7271 07dd3ed3 2022-08-06 thomas static const struct got_error *
7272 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7273 07dd3ed3 2022-08-06 thomas {
7274 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7275 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7276 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7277 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7278 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7279 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7280 07dd3ed3 2022-08-06 thomas
7281 07dd3ed3 2022-08-06 thomas g = view->gline;
7282 07dd3ed3 2022-08-06 thomas view->gline = 0;
7283 07dd3ed3 2022-08-06 thomas
7284 07dd3ed3 2022-08-06 thomas if (g == 0)
7285 07dd3ed3 2022-08-06 thomas g = 1;
7286 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7287 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7288 07dd3ed3 2022-08-06 thomas
7289 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7290 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7291 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7292 07dd3ed3 2022-08-06 thomas
7293 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7294 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7295 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7296 07dd3ed3 2022-08-06 thomas }
7297 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7298 07dd3ed3 2022-08-06 thomas last += off;
7299 07dd3ed3 2022-08-06 thomas
7300 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7301 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7302 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7303 07dd3ed3 2022-08-06 thomas }
7304 07dd3ed3 2022-08-06 thomas
7305 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7306 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7307 07dd3ed3 2022-08-06 thomas i += off;
7308 07dd3ed3 2022-08-06 thomas }
7309 07dd3ed3 2022-08-06 thomas
7310 07dd3ed3 2022-08-06 thomas if (i < g) {
7311 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7312 07dd3ed3 2022-08-06 thomas if (err)
7313 07dd3ed3 2022-08-06 thomas return err;
7314 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7315 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7316 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7317 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7318 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7319 07dd3ed3 2022-08-06 thomas first += off;
7320 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7321 07dd3ed3 2022-08-06 thomas }
7322 07dd3ed3 2022-08-06 thomas } else if (i > g)
7323 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7324 07dd3ed3 2022-08-06 thomas
7325 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7326 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7327 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7328 07dd3ed3 2022-08-06 thomas
7329 07dd3ed3 2022-08-06 thomas return NULL;
7330 e5a0f69f 2018-08-18 stsp }
7331 ce52c690 2018-06-23 stsp
7332 e5a0f69f 2018-08-18 stsp static const struct got_error *
7333 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7334 e5a0f69f 2018-08-18 stsp {
7335 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7336 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7337 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7338 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7339 ffd1d5e5 2018-06-23 stsp
7340 07dd3ed3 2022-08-06 thomas if (view->gline)
7341 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7342 07dd3ed3 2022-08-06 thomas
7343 e5a0f69f 2018-08-18 stsp switch (ch) {
7344 1e37a5c2 2019-05-12 jcs case 'i':
7345 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7346 07b0611c 2022-06-23 thomas view->count = 0;
7347 1e37a5c2 2019-05-12 jcs break;
7348 1be4947a 2022-07-22 thomas case 'L':
7349 07b0611c 2022-06-23 thomas view->count = 0;
7350 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7351 ffd1d5e5 2018-06-23 stsp break;
7352 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7353 152c1c93 2020-11-29 stsp break;
7354 1be4947a 2022-07-22 thomas case 'R':
7355 07b0611c 2022-06-23 thomas view->count = 0;
7356 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7357 e4526bf5 2021-09-03 naddy break;
7358 e4526bf5 2021-09-03 naddy case 'g':
7359 aa7a1117 2023-01-09 thomas case '=':
7360 e4526bf5 2021-09-03 naddy case KEY_HOME:
7361 e4526bf5 2021-09-03 naddy s->selected = 0;
7362 07b0611c 2022-06-23 thomas view->count = 0;
7363 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7364 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7365 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7366 e4526bf5 2021-09-03 naddy else
7367 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7368 1e37a5c2 2019-05-12 jcs break;
7369 e4526bf5 2021-09-03 naddy case 'G':
7370 aa7a1117 2023-01-09 thomas case '*':
7371 a5d43cac 2022-07-01 thomas case KEY_END: {
7372 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7373 a5d43cac 2022-07-01 thomas
7374 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7375 a5d43cac 2022-07-01 thomas --eos; /* border */
7376 e4526bf5 2021-09-03 naddy s->selected = 0;
7377 07b0611c 2022-06-23 thomas view->count = 0;
7378 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7379 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7380 e4526bf5 2021-09-03 naddy if (te == NULL) {
7381 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7382 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7383 e4526bf5 2021-09-03 naddy n++;
7384 e4526bf5 2021-09-03 naddy }
7385 e4526bf5 2021-09-03 naddy break;
7386 e4526bf5 2021-09-03 naddy }
7387 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7388 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7389 e4526bf5 2021-09-03 naddy }
7390 e4526bf5 2021-09-03 naddy if (n > 0)
7391 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7392 e4526bf5 2021-09-03 naddy break;
7393 a5d43cac 2022-07-01 thomas }
7394 1e37a5c2 2019-05-12 jcs case 'k':
7395 1e37a5c2 2019-05-12 jcs case KEY_UP:
7396 f7140bf5 2021-10-17 thomas case CTRL('p'):
7397 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7398 1e37a5c2 2019-05-12 jcs s->selected--;
7399 fa86c4bf 2020-11-29 stsp break;
7400 1e37a5c2 2019-05-12 jcs }
7401 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7402 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7403 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7404 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7405 07b0611c 2022-06-23 thomas view->count = 0;
7406 1e37a5c2 2019-05-12 jcs break;
7407 70f17a53 2022-06-13 thomas case CTRL('u'):
7408 23427b14 2022-06-23 thomas case 'u':
7409 70f17a53 2022-06-13 thomas nscroll /= 2;
7410 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7411 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7412 ea025d1d 2020-02-22 naddy case CTRL('b'):
7413 1c5e5faa 2022-06-23 thomas case 'b':
7414 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7415 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7416 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7417 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7418 fa86c4bf 2020-11-29 stsp } else {
7419 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7420 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7421 fa86c4bf 2020-11-29 stsp }
7422 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7423 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7424 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7425 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7426 07b0611c 2022-06-23 thomas view->count = 0;
7427 1e37a5c2 2019-05-12 jcs break;
7428 1e37a5c2 2019-05-12 jcs case 'j':
7429 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7430 f7140bf5 2021-10-17 thomas case CTRL('n'):
7431 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7432 1e37a5c2 2019-05-12 jcs s->selected++;
7433 1e37a5c2 2019-05-12 jcs break;
7434 1e37a5c2 2019-05-12 jcs }
7435 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7436 07b0611c 2022-06-23 thomas == NULL) {
7437 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7438 07b0611c 2022-06-23 thomas view->count = 0;
7439 1e37a5c2 2019-05-12 jcs break;
7440 07b0611c 2022-06-23 thomas }
7441 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7442 1e37a5c2 2019-05-12 jcs break;
7443 70f17a53 2022-06-13 thomas case CTRL('d'):
7444 23427b14 2022-06-23 thomas case 'd':
7445 70f17a53 2022-06-13 thomas nscroll /= 2;
7446 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7447 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7448 ea025d1d 2020-02-22 naddy case CTRL('f'):
7449 1c5e5faa 2022-06-23 thomas case 'f':
7450 4c2d69cb 2022-06-23 thomas case ' ':
7451 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7452 1e37a5c2 2019-05-12 jcs == NULL) {
7453 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7454 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7455 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7456 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7457 07b0611c 2022-06-23 thomas else
7458 07b0611c 2022-06-23 thomas view->count = 0;
7459 1e37a5c2 2019-05-12 jcs break;
7460 1e37a5c2 2019-05-12 jcs }
7461 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7462 1e37a5c2 2019-05-12 jcs break;
7463 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7464 1e37a5c2 2019-05-12 jcs case '\r':
7465 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7466 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7467 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7468 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7469 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7470 07b0611c 2022-06-23 thomas view->count = 0;
7471 1e37a5c2 2019-05-12 jcs break;
7472 07b0611c 2022-06-23 thomas }
7473 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7474 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7475 1e37a5c2 2019-05-12 jcs entry);
7476 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7477 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7478 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7479 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7480 1e37a5c2 2019-05-12 jcs s->selected_entry =
7481 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7482 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7483 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7484 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7485 a5d43cac 2022-07-01 thomas if (err)
7486 a5d43cac 2022-07-01 thomas break;
7487 a5d43cac 2022-07-01 thomas }
7488 1e37a5c2 2019-05-12 jcs free(parent);
7489 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7490 56e0773d 2019-11-28 stsp s->selected_entry))) {
7491 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7492 07b0611c 2022-06-23 thomas view->count = 0;
7493 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7494 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7495 1e37a5c2 2019-05-12 jcs if (err)
7496 1e37a5c2 2019-05-12 jcs break;
7497 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7498 941e9f74 2019-05-21 stsp if (err) {
7499 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7500 1e37a5c2 2019-05-12 jcs break;
7501 1e37a5c2 2019-05-12 jcs }
7502 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7503 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7504 1e37a5c2 2019-05-12 jcs break;
7505 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7506 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7507 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7508 07b0611c 2022-06-23 thomas view->count = 0;
7509 1e37a5c2 2019-05-12 jcs break;
7510 1e37a5c2 2019-05-12 jcs default:
7511 07b0611c 2022-06-23 thomas view->count = 0;
7512 1e37a5c2 2019-05-12 jcs break;
7513 ffd1d5e5 2018-06-23 stsp }
7514 e5a0f69f 2018-08-18 stsp
7515 ffd1d5e5 2018-06-23 stsp return err;
7516 9f7d7167 2018-04-29 stsp }
7517 9f7d7167 2018-04-29 stsp
7518 ffd1d5e5 2018-06-23 stsp __dead static void
7519 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7520 ffd1d5e5 2018-06-23 stsp {
7521 ffd1d5e5 2018-06-23 stsp endwin();
7522 91198554 2022-06-23 thomas fprintf(stderr,
7523 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7524 ffd1d5e5 2018-06-23 stsp getprogname());
7525 ffd1d5e5 2018-06-23 stsp exit(1);
7526 ffd1d5e5 2018-06-23 stsp }
7527 ffd1d5e5 2018-06-23 stsp
7528 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7529 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7530 ffd1d5e5 2018-06-23 stsp {
7531 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7532 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7533 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7534 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7535 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7536 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7537 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7538 4e97c21c 2020-12-06 stsp char *label = NULL;
7539 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7540 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7541 ffd1d5e5 2018-06-23 stsp int ch;
7542 5221c383 2018-08-01 stsp struct tog_view *view;
7543 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7544 70ac5f84 2019-03-28 stsp
7545 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7546 ffd1d5e5 2018-06-23 stsp switch (ch) {
7547 ffd1d5e5 2018-06-23 stsp case 'c':
7548 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7549 74283ab8 2020-02-07 stsp break;
7550 74283ab8 2020-02-07 stsp case 'r':
7551 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7552 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7553 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7554 74283ab8 2020-02-07 stsp optarg);
7555 ffd1d5e5 2018-06-23 stsp break;
7556 ffd1d5e5 2018-06-23 stsp default:
7557 e99e2d15 2020-11-24 naddy usage_tree();
7558 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7559 ffd1d5e5 2018-06-23 stsp }
7560 ffd1d5e5 2018-06-23 stsp }
7561 ffd1d5e5 2018-06-23 stsp
7562 ffd1d5e5 2018-06-23 stsp argc -= optind;
7563 ffd1d5e5 2018-06-23 stsp argv += optind;
7564 ffd1d5e5 2018-06-23 stsp
7565 55cccc34 2020-02-20 stsp if (argc > 1)
7566 e99e2d15 2020-11-24 naddy usage_tree();
7567 7cd52833 2022-06-23 thomas
7568 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7569 7cd52833 2022-06-23 thomas if (error != NULL)
7570 7cd52833 2022-06-23 thomas goto done;
7571 74283ab8 2020-02-07 stsp
7572 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7573 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7574 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7575 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7576 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7577 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7578 c156c7a4 2020-12-18 stsp goto done;
7579 55cccc34 2020-02-20 stsp if (worktree)
7580 52185f70 2019-02-05 stsp repo_path =
7581 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7582 55cccc34 2020-02-20 stsp else
7583 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7584 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7585 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7586 c156c7a4 2020-12-18 stsp goto done;
7587 c156c7a4 2020-12-18 stsp }
7588 55cccc34 2020-02-20 stsp }
7589 a915003a 2019-02-05 stsp
7590 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7591 c02c541e 2019-03-29 stsp if (error != NULL)
7592 52185f70 2019-02-05 stsp goto done;
7593 d188b9a6 2019-01-04 stsp
7594 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7595 55cccc34 2020-02-20 stsp repo, worktree);
7596 55cccc34 2020-02-20 stsp if (error)
7597 55cccc34 2020-02-20 stsp goto done;
7598 55cccc34 2020-02-20 stsp
7599 55cccc34 2020-02-20 stsp init_curses();
7600 55cccc34 2020-02-20 stsp
7601 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7602 c02c541e 2019-03-29 stsp if (error)
7603 52185f70 2019-02-05 stsp goto done;
7604 ffd1d5e5 2018-06-23 stsp
7605 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7606 51a10b52 2020-12-26 stsp if (error)
7607 51a10b52 2020-12-26 stsp goto done;
7608 51a10b52 2020-12-26 stsp
7609 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7610 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7611 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7612 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7613 4e97c21c 2020-12-06 stsp if (error)
7614 4e97c21c 2020-12-06 stsp goto done;
7615 4e97c21c 2020-12-06 stsp head_ref_name = label;
7616 4e97c21c 2020-12-06 stsp } else {
7617 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7618 4e97c21c 2020-12-06 stsp if (error == NULL)
7619 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7620 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7621 4e97c21c 2020-12-06 stsp goto done;
7622 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7623 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7624 4e97c21c 2020-12-06 stsp if (error)
7625 4e97c21c 2020-12-06 stsp goto done;
7626 4e97c21c 2020-12-06 stsp }
7627 ffd1d5e5 2018-06-23 stsp
7628 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7629 945f9229 2022-04-16 thomas if (error)
7630 945f9229 2022-04-16 thomas goto done;
7631 945f9229 2022-04-16 thomas
7632 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7633 5221c383 2018-08-01 stsp if (view == NULL) {
7634 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7635 5221c383 2018-08-01 stsp goto done;
7636 5221c383 2018-08-01 stsp }
7637 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7638 ad80ab7b 2018-08-04 stsp if (error)
7639 ad80ab7b 2018-08-04 stsp goto done;
7640 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7641 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7642 d91faf3b 2020-12-01 naddy in_repo_path);
7643 55cccc34 2020-02-20 stsp if (error)
7644 55cccc34 2020-02-20 stsp goto done;
7645 55cccc34 2020-02-20 stsp }
7646 55cccc34 2020-02-20 stsp
7647 55cccc34 2020-02-20 stsp if (worktree) {
7648 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7649 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7650 55cccc34 2020-02-20 stsp worktree = NULL;
7651 55cccc34 2020-02-20 stsp }
7652 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7653 ffd1d5e5 2018-06-23 stsp done:
7654 52185f70 2019-02-05 stsp free(repo_path);
7655 e4a0e26d 2020-02-20 stsp free(cwd);
7656 ffd1d5e5 2018-06-23 stsp free(commit_id);
7657 4e97c21c 2020-12-06 stsp free(label);
7658 486cd271 2020-12-06 stsp if (ref)
7659 486cd271 2020-12-06 stsp got_ref_close(ref);
7660 1d0f4054 2021-06-17 stsp if (repo) {
7661 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7662 1d0f4054 2021-06-17 stsp if (error == NULL)
7663 1d0f4054 2021-06-17 stsp error = close_err;
7664 1d0f4054 2021-06-17 stsp }
7665 7cd52833 2022-06-23 thomas if (pack_fds) {
7666 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7667 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7668 7cd52833 2022-06-23 thomas if (error == NULL)
7669 7cd52833 2022-06-23 thomas error = pack_err;
7670 7cd52833 2022-06-23 thomas }
7671 51a10b52 2020-12-26 stsp tog_free_refs();
7672 ffd1d5e5 2018-06-23 stsp return error;
7673 6458efa5 2020-11-24 stsp }
7674 6458efa5 2020-11-24 stsp
7675 6458efa5 2020-11-24 stsp static const struct got_error *
7676 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7677 6458efa5 2020-11-24 stsp {
7678 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7679 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7680 6458efa5 2020-11-24 stsp
7681 6458efa5 2020-11-24 stsp s->nrefs = 0;
7682 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7683 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7684 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7685 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7686 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7687 6458efa5 2020-11-24 stsp continue;
7688 6458efa5 2020-11-24 stsp
7689 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7690 6458efa5 2020-11-24 stsp if (re == NULL)
7691 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7692 6458efa5 2020-11-24 stsp
7693 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7694 8924d611 2020-12-26 stsp if (re->ref == NULL)
7695 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7696 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7697 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7698 6458efa5 2020-11-24 stsp }
7699 6458efa5 2020-11-24 stsp
7700 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7701 6458efa5 2020-11-24 stsp return NULL;
7702 6458efa5 2020-11-24 stsp }
7703 6458efa5 2020-11-24 stsp
7704 ef20f542 2022-06-26 thomas static void
7705 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7706 6458efa5 2020-11-24 stsp {
7707 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7708 6458efa5 2020-11-24 stsp
7709 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7710 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7711 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7712 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7713 6458efa5 2020-11-24 stsp free(re);
7714 6458efa5 2020-11-24 stsp }
7715 6458efa5 2020-11-24 stsp }
7716 6458efa5 2020-11-24 stsp
7717 6458efa5 2020-11-24 stsp static const struct got_error *
7718 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7719 6458efa5 2020-11-24 stsp {
7720 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7721 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7722 6458efa5 2020-11-24 stsp
7723 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7724 6458efa5 2020-11-24 stsp s->repo = repo;
7725 6458efa5 2020-11-24 stsp
7726 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7727 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7728 6458efa5 2020-11-24 stsp
7729 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7730 6458efa5 2020-11-24 stsp if (err)
7731 6458efa5 2020-11-24 stsp return err;
7732 34ba6917 2020-11-30 stsp
7733 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7734 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7735 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7736 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7737 6458efa5 2020-11-24 stsp if (err)
7738 6458efa5 2020-11-24 stsp goto done;
7739 6458efa5 2020-11-24 stsp
7740 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7741 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7742 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7743 6458efa5 2020-11-24 stsp if (err)
7744 6458efa5 2020-11-24 stsp goto done;
7745 6458efa5 2020-11-24 stsp
7746 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7747 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7748 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7749 2183bbf6 2022-01-23 thomas if (err)
7750 2183bbf6 2022-01-23 thomas goto done;
7751 2183bbf6 2022-01-23 thomas
7752 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7753 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7754 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7755 6458efa5 2020-11-24 stsp if (err)
7756 6458efa5 2020-11-24 stsp goto done;
7757 6458efa5 2020-11-24 stsp }
7758 6458efa5 2020-11-24 stsp
7759 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7760 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7761 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7762 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7763 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7764 6458efa5 2020-11-24 stsp done:
7765 6458efa5 2020-11-24 stsp if (err)
7766 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7767 6458efa5 2020-11-24 stsp return err;
7768 6458efa5 2020-11-24 stsp }
7769 6458efa5 2020-11-24 stsp
7770 6458efa5 2020-11-24 stsp static const struct got_error *
7771 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7772 6458efa5 2020-11-24 stsp {
7773 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7774 6458efa5 2020-11-24 stsp
7775 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7776 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7777 6458efa5 2020-11-24 stsp
7778 6458efa5 2020-11-24 stsp return NULL;
7779 9f7d7167 2018-04-29 stsp }
7780 ce5b7c56 2019-07-09 stsp
7781 6458efa5 2020-11-24 stsp static const struct got_error *
7782 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7783 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7784 6458efa5 2020-11-24 stsp {
7785 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7786 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7787 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7788 6458efa5 2020-11-24 stsp int obj_type;
7789 6458efa5 2020-11-24 stsp
7790 c42c9805 2020-11-24 stsp *commit_id = NULL;
7791 6458efa5 2020-11-24 stsp
7792 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7793 6458efa5 2020-11-24 stsp if (err)
7794 6458efa5 2020-11-24 stsp return err;
7795 6458efa5 2020-11-24 stsp
7796 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7797 6458efa5 2020-11-24 stsp if (err)
7798 6458efa5 2020-11-24 stsp goto done;
7799 6458efa5 2020-11-24 stsp
7800 6458efa5 2020-11-24 stsp switch (obj_type) {
7801 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7802 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7803 6458efa5 2020-11-24 stsp break;
7804 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7805 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7806 6458efa5 2020-11-24 stsp if (err)
7807 6458efa5 2020-11-24 stsp goto done;
7808 c42c9805 2020-11-24 stsp free(obj_id);
7809 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7810 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7811 c42c9805 2020-11-24 stsp if (err)
7812 6458efa5 2020-11-24 stsp goto done;
7813 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7814 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7815 c42c9805 2020-11-24 stsp goto done;
7816 c42c9805 2020-11-24 stsp }
7817 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7818 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7819 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7820 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7821 c42c9805 2020-11-24 stsp goto done;
7822 c42c9805 2020-11-24 stsp }
7823 6458efa5 2020-11-24 stsp break;
7824 6458efa5 2020-11-24 stsp default:
7825 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7826 c42c9805 2020-11-24 stsp break;
7827 6458efa5 2020-11-24 stsp }
7828 6458efa5 2020-11-24 stsp
7829 c42c9805 2020-11-24 stsp done:
7830 c42c9805 2020-11-24 stsp if (tag)
7831 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7832 c42c9805 2020-11-24 stsp if (err) {
7833 c42c9805 2020-11-24 stsp free(*commit_id);
7834 c42c9805 2020-11-24 stsp *commit_id = NULL;
7835 c42c9805 2020-11-24 stsp }
7836 c42c9805 2020-11-24 stsp return err;
7837 c42c9805 2020-11-24 stsp }
7838 c42c9805 2020-11-24 stsp
7839 c42c9805 2020-11-24 stsp static const struct got_error *
7840 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7841 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7842 c42c9805 2020-11-24 stsp {
7843 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7844 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7845 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7846 c42c9805 2020-11-24 stsp
7847 c42c9805 2020-11-24 stsp *new_view = NULL;
7848 c42c9805 2020-11-24 stsp
7849 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7850 c42c9805 2020-11-24 stsp if (err) {
7851 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7852 c42c9805 2020-11-24 stsp return err;
7853 c42c9805 2020-11-24 stsp else
7854 c42c9805 2020-11-24 stsp return NULL;
7855 c42c9805 2020-11-24 stsp }
7856 c42c9805 2020-11-24 stsp
7857 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7858 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7859 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7860 6458efa5 2020-11-24 stsp goto done;
7861 6458efa5 2020-11-24 stsp }
7862 6458efa5 2020-11-24 stsp
7863 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7864 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7865 6458efa5 2020-11-24 stsp done:
7866 6458efa5 2020-11-24 stsp if (err)
7867 6458efa5 2020-11-24 stsp view_close(log_view);
7868 6458efa5 2020-11-24 stsp else
7869 6458efa5 2020-11-24 stsp *new_view = log_view;
7870 c42c9805 2020-11-24 stsp free(commit_id);
7871 6458efa5 2020-11-24 stsp return err;
7872 6458efa5 2020-11-24 stsp }
7873 6458efa5 2020-11-24 stsp
7874 ce5b7c56 2019-07-09 stsp static void
7875 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7876 6458efa5 2020-11-24 stsp {
7877 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7878 34ba6917 2020-11-30 stsp int i = 0;
7879 6458efa5 2020-11-24 stsp
7880 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7881 6458efa5 2020-11-24 stsp return;
7882 6458efa5 2020-11-24 stsp
7883 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7884 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7885 34ba6917 2020-11-30 stsp if (re == NULL)
7886 34ba6917 2020-11-30 stsp break;
7887 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7888 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7889 6458efa5 2020-11-24 stsp }
7890 6458efa5 2020-11-24 stsp }
7891 6458efa5 2020-11-24 stsp
7892 a5d43cac 2022-07-01 thomas static const struct got_error *
7893 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7894 6458efa5 2020-11-24 stsp {
7895 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7896 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7897 6458efa5 2020-11-24 stsp int n = 0;
7898 6458efa5 2020-11-24 stsp
7899 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7900 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7901 6458efa5 2020-11-24 stsp else
7902 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7903 6458efa5 2020-11-24 stsp
7904 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7905 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7906 07dd3ed3 2022-08-06 thomas if (last) {
7907 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7908 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7909 07dd3ed3 2022-08-06 thomas }
7910 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7911 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7912 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7913 6458efa5 2020-11-24 stsp }
7914 6458efa5 2020-11-24 stsp }
7915 a5d43cac 2022-07-01 thomas
7916 a5d43cac 2022-07-01 thomas return NULL;
7917 6458efa5 2020-11-24 stsp }
7918 6458efa5 2020-11-24 stsp
7919 6458efa5 2020-11-24 stsp static const struct got_error *
7920 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7921 6458efa5 2020-11-24 stsp {
7922 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7923 6458efa5 2020-11-24 stsp
7924 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7925 6458efa5 2020-11-24 stsp return NULL;
7926 6458efa5 2020-11-24 stsp }
7927 6458efa5 2020-11-24 stsp
7928 6458efa5 2020-11-24 stsp static int
7929 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7930 6458efa5 2020-11-24 stsp {
7931 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7932 6458efa5 2020-11-24 stsp
7933 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7934 6458efa5 2020-11-24 stsp 0) == 0;
7935 6458efa5 2020-11-24 stsp }
7936 6458efa5 2020-11-24 stsp
7937 6458efa5 2020-11-24 stsp static const struct got_error *
7938 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7939 6458efa5 2020-11-24 stsp {
7940 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7941 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7942 6458efa5 2020-11-24 stsp
7943 6458efa5 2020-11-24 stsp if (!view->searching) {
7944 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7945 6458efa5 2020-11-24 stsp return NULL;
7946 6458efa5 2020-11-24 stsp }
7947 6458efa5 2020-11-24 stsp
7948 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7949 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7950 6458efa5 2020-11-24 stsp if (s->selected_entry)
7951 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7952 6458efa5 2020-11-24 stsp else
7953 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7954 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7955 6458efa5 2020-11-24 stsp } else {
7956 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7957 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7958 6458efa5 2020-11-24 stsp else
7959 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7960 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7961 6458efa5 2020-11-24 stsp }
7962 6458efa5 2020-11-24 stsp } else {
7963 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7964 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7965 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7966 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7967 6458efa5 2020-11-24 stsp else
7968 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7969 6458efa5 2020-11-24 stsp }
7970 6458efa5 2020-11-24 stsp
7971 6458efa5 2020-11-24 stsp while (1) {
7972 6458efa5 2020-11-24 stsp if (re == NULL) {
7973 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7974 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7975 6458efa5 2020-11-24 stsp return NULL;
7976 6458efa5 2020-11-24 stsp }
7977 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7978 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7979 6458efa5 2020-11-24 stsp else
7980 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7981 6458efa5 2020-11-24 stsp }
7982 6458efa5 2020-11-24 stsp
7983 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7984 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7985 6458efa5 2020-11-24 stsp s->matched_entry = re;
7986 6458efa5 2020-11-24 stsp break;
7987 6458efa5 2020-11-24 stsp }
7988 6458efa5 2020-11-24 stsp
7989 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7990 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7991 6458efa5 2020-11-24 stsp else
7992 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7993 6458efa5 2020-11-24 stsp }
7994 6458efa5 2020-11-24 stsp
7995 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7996 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7997 6458efa5 2020-11-24 stsp s->selected = 0;
7998 6458efa5 2020-11-24 stsp }
7999 6458efa5 2020-11-24 stsp
8000 6458efa5 2020-11-24 stsp return NULL;
8001 6458efa5 2020-11-24 stsp }
8002 6458efa5 2020-11-24 stsp
8003 6458efa5 2020-11-24 stsp static const struct got_error *
8004 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8005 6458efa5 2020-11-24 stsp {
8006 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8007 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8008 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8009 6458efa5 2020-11-24 stsp char *line = NULL;
8010 6458efa5 2020-11-24 stsp wchar_t *wline;
8011 6458efa5 2020-11-24 stsp struct tog_color *tc;
8012 6458efa5 2020-11-24 stsp int width, n;
8013 6458efa5 2020-11-24 stsp int limit = view->nlines;
8014 6458efa5 2020-11-24 stsp
8015 6458efa5 2020-11-24 stsp werase(view->window);
8016 6458efa5 2020-11-24 stsp
8017 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8018 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8019 a5d43cac 2022-07-01 thomas --limit; /* border */
8020 6458efa5 2020-11-24 stsp
8021 6458efa5 2020-11-24 stsp if (limit == 0)
8022 6458efa5 2020-11-24 stsp return NULL;
8023 6458efa5 2020-11-24 stsp
8024 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8025 6458efa5 2020-11-24 stsp
8026 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8027 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8028 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8029 6458efa5 2020-11-24 stsp
8030 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8031 6458efa5 2020-11-24 stsp if (err) {
8032 6458efa5 2020-11-24 stsp free(line);
8033 6458efa5 2020-11-24 stsp return err;
8034 6458efa5 2020-11-24 stsp }
8035 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8036 6458efa5 2020-11-24 stsp wstandout(view->window);
8037 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8038 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8039 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8040 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8041 6458efa5 2020-11-24 stsp wstandend(view->window);
8042 6458efa5 2020-11-24 stsp free(wline);
8043 6458efa5 2020-11-24 stsp wline = NULL;
8044 6458efa5 2020-11-24 stsp free(line);
8045 6458efa5 2020-11-24 stsp line = NULL;
8046 6458efa5 2020-11-24 stsp if (--limit <= 0)
8047 6458efa5 2020-11-24 stsp return NULL;
8048 6458efa5 2020-11-24 stsp
8049 6458efa5 2020-11-24 stsp n = 0;
8050 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8051 6458efa5 2020-11-24 stsp char *line = NULL;
8052 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8053 6458efa5 2020-11-24 stsp
8054 84227eb1 2022-06-23 thomas if (s->show_date) {
8055 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8056 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8057 84227eb1 2022-06-23 thomas struct got_object_id *id;
8058 84227eb1 2022-06-23 thomas struct tm tm;
8059 84227eb1 2022-06-23 thomas time_t t;
8060 84227eb1 2022-06-23 thomas
8061 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8062 84227eb1 2022-06-23 thomas if (err)
8063 84227eb1 2022-06-23 thomas return err;
8064 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8065 84227eb1 2022-06-23 thomas if (err) {
8066 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8067 84227eb1 2022-06-23 thomas free(id);
8068 84227eb1 2022-06-23 thomas return err;
8069 84227eb1 2022-06-23 thomas }
8070 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8071 84227eb1 2022-06-23 thomas id);
8072 84227eb1 2022-06-23 thomas if (err) {
8073 84227eb1 2022-06-23 thomas free(id);
8074 84227eb1 2022-06-23 thomas return err;
8075 84227eb1 2022-06-23 thomas }
8076 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8077 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8078 84227eb1 2022-06-23 thomas } else {
8079 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8080 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8081 84227eb1 2022-06-23 thomas }
8082 84227eb1 2022-06-23 thomas free(id);
8083 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8084 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8085 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8086 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8087 84227eb1 2022-06-23 thomas }
8088 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8089 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8090 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8091 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8092 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8093 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8094 6458efa5 2020-11-24 stsp struct got_object_id *id;
8095 6458efa5 2020-11-24 stsp char *id_str;
8096 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8097 6458efa5 2020-11-24 stsp if (err)
8098 6458efa5 2020-11-24 stsp return err;
8099 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8100 6458efa5 2020-11-24 stsp if (err) {
8101 6458efa5 2020-11-24 stsp free(id);
8102 6458efa5 2020-11-24 stsp return err;
8103 6458efa5 2020-11-24 stsp }
8104 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8105 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8106 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8107 6458efa5 2020-11-24 stsp free(id);
8108 6458efa5 2020-11-24 stsp free(id_str);
8109 6458efa5 2020-11-24 stsp return err;
8110 6458efa5 2020-11-24 stsp }
8111 6458efa5 2020-11-24 stsp free(id);
8112 6458efa5 2020-11-24 stsp free(id_str);
8113 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8114 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8115 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8116 6458efa5 2020-11-24 stsp
8117 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8118 f91a2b48 2022-06-23 thomas 0, 0);
8119 6458efa5 2020-11-24 stsp if (err) {
8120 6458efa5 2020-11-24 stsp free(line);
8121 6458efa5 2020-11-24 stsp return err;
8122 6458efa5 2020-11-24 stsp }
8123 6458efa5 2020-11-24 stsp if (n == s->selected) {
8124 6458efa5 2020-11-24 stsp if (view->focussed)
8125 6458efa5 2020-11-24 stsp wstandout(view->window);
8126 6458efa5 2020-11-24 stsp s->selected_entry = re;
8127 6458efa5 2020-11-24 stsp }
8128 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8129 6458efa5 2020-11-24 stsp if (tc)
8130 6458efa5 2020-11-24 stsp wattr_on(view->window,
8131 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8132 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8133 6458efa5 2020-11-24 stsp if (tc)
8134 6458efa5 2020-11-24 stsp wattr_off(view->window,
8135 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8136 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8137 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8138 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8139 6458efa5 2020-11-24 stsp wstandend(view->window);
8140 6458efa5 2020-11-24 stsp free(line);
8141 6458efa5 2020-11-24 stsp free(wline);
8142 6458efa5 2020-11-24 stsp wline = NULL;
8143 6458efa5 2020-11-24 stsp n++;
8144 6458efa5 2020-11-24 stsp s->ndisplayed++;
8145 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8146 6458efa5 2020-11-24 stsp
8147 6458efa5 2020-11-24 stsp limit--;
8148 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8149 6458efa5 2020-11-24 stsp }
8150 6458efa5 2020-11-24 stsp
8151 a5d43cac 2022-07-01 thomas view_border(view);
8152 6458efa5 2020-11-24 stsp return err;
8153 6458efa5 2020-11-24 stsp }
8154 6458efa5 2020-11-24 stsp
8155 6458efa5 2020-11-24 stsp static const struct got_error *
8156 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8157 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8158 c42c9805 2020-11-24 stsp {
8159 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8160 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8161 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8162 c42c9805 2020-11-24 stsp
8163 c42c9805 2020-11-24 stsp *new_view = NULL;
8164 c42c9805 2020-11-24 stsp
8165 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8166 c42c9805 2020-11-24 stsp if (err) {
8167 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8168 c42c9805 2020-11-24 stsp return err;
8169 c42c9805 2020-11-24 stsp else
8170 c42c9805 2020-11-24 stsp return NULL;
8171 c42c9805 2020-11-24 stsp }
8172 c42c9805 2020-11-24 stsp
8173 c42c9805 2020-11-24 stsp
8174 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8175 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8176 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8177 c42c9805 2020-11-24 stsp goto done;
8178 c42c9805 2020-11-24 stsp }
8179 c42c9805 2020-11-24 stsp
8180 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8181 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8182 c42c9805 2020-11-24 stsp if (err)
8183 c42c9805 2020-11-24 stsp goto done;
8184 c42c9805 2020-11-24 stsp
8185 c42c9805 2020-11-24 stsp *new_view = tree_view;
8186 c42c9805 2020-11-24 stsp done:
8187 c42c9805 2020-11-24 stsp free(commit_id);
8188 c42c9805 2020-11-24 stsp return err;
8189 07dd3ed3 2022-08-06 thomas }
8190 07dd3ed3 2022-08-06 thomas
8191 07dd3ed3 2022-08-06 thomas static const struct got_error *
8192 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8193 07dd3ed3 2022-08-06 thomas {
8194 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8195 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8196 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8197 07dd3ed3 2022-08-06 thomas
8198 07dd3ed3 2022-08-06 thomas g = view->gline;
8199 07dd3ed3 2022-08-06 thomas view->gline = 0;
8200 07dd3ed3 2022-08-06 thomas
8201 07dd3ed3 2022-08-06 thomas if (g == 0)
8202 07dd3ed3 2022-08-06 thomas g = 1;
8203 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8204 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8205 07dd3ed3 2022-08-06 thomas
8206 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8207 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8208 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8209 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8210 07dd3ed3 2022-08-06 thomas return NULL;
8211 07dd3ed3 2022-08-06 thomas }
8212 07dd3ed3 2022-08-06 thomas
8213 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8214 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8215 07dd3ed3 2022-08-06 thomas if (err)
8216 07dd3ed3 2022-08-06 thomas return err;
8217 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8218 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8219 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8220 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8221 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8222 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8223 07dd3ed3 2022-08-06 thomas
8224 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8225 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8226 07dd3ed3 2022-08-06 thomas
8227 07dd3ed3 2022-08-06 thomas return NULL;
8228 07dd3ed3 2022-08-06 thomas
8229 c42c9805 2020-11-24 stsp }
8230 07dd3ed3 2022-08-06 thomas
8231 c42c9805 2020-11-24 stsp static const struct got_error *
8232 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8233 6458efa5 2020-11-24 stsp {
8234 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8235 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8236 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8237 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8238 6458efa5 2020-11-24 stsp
8239 07dd3ed3 2022-08-06 thomas if (view->gline)
8240 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8241 07dd3ed3 2022-08-06 thomas
8242 6458efa5 2020-11-24 stsp switch (ch) {
8243 6458efa5 2020-11-24 stsp case 'i':
8244 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8245 07b0611c 2022-06-23 thomas view->count = 0;
8246 3bfadbd4 2021-11-20 thomas break;
8247 84227eb1 2022-06-23 thomas case 'm':
8248 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8249 07b0611c 2022-06-23 thomas view->count = 0;
8250 84227eb1 2022-06-23 thomas break;
8251 98182bd0 2021-11-20 thomas case 'o':
8252 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8253 07b0611c 2022-06-23 thomas view->count = 0;
8254 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8255 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8256 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8257 c591440f 2021-11-20 thomas if (err)
8258 c591440f 2021-11-20 thomas break;
8259 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8260 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8261 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8262 3bfadbd4 2021-11-20 thomas if (err)
8263 3bfadbd4 2021-11-20 thomas break;
8264 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8265 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8266 6458efa5 2020-11-24 stsp break;
8267 6458efa5 2020-11-24 stsp case KEY_ENTER:
8268 6458efa5 2020-11-24 stsp case '\r':
8269 07b0611c 2022-06-23 thomas view->count = 0;
8270 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8271 a5d43cac 2022-07-01 thomas break;
8272 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8273 6458efa5 2020-11-24 stsp break;
8274 1be4947a 2022-07-22 thomas case 'T':
8275 07b0611c 2022-06-23 thomas view->count = 0;
8276 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8277 c42c9805 2020-11-24 stsp break;
8278 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8279 c42c9805 2020-11-24 stsp break;
8280 e4526bf5 2021-09-03 naddy case 'g':
8281 aa7a1117 2023-01-09 thomas case '=':
8282 e4526bf5 2021-09-03 naddy case KEY_HOME:
8283 e4526bf5 2021-09-03 naddy s->selected = 0;
8284 07b0611c 2022-06-23 thomas view->count = 0;
8285 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8286 e4526bf5 2021-09-03 naddy break;
8287 e4526bf5 2021-09-03 naddy case 'G':
8288 aa7a1117 2023-01-09 thomas case '*':
8289 a5d43cac 2022-07-01 thomas case KEY_END: {
8290 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8291 a5d43cac 2022-07-01 thomas
8292 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8293 a5d43cac 2022-07-01 thomas --eos; /* border */
8294 e4526bf5 2021-09-03 naddy s->selected = 0;
8295 07b0611c 2022-06-23 thomas view->count = 0;
8296 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8297 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8298 e4526bf5 2021-09-03 naddy if (re == NULL)
8299 e4526bf5 2021-09-03 naddy break;
8300 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8301 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8302 e4526bf5 2021-09-03 naddy }
8303 e4526bf5 2021-09-03 naddy if (n > 0)
8304 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8305 e4526bf5 2021-09-03 naddy break;
8306 a5d43cac 2022-07-01 thomas }
8307 6458efa5 2020-11-24 stsp case 'k':
8308 6458efa5 2020-11-24 stsp case KEY_UP:
8309 f7140bf5 2021-10-17 thomas case CTRL('p'):
8310 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8311 6458efa5 2020-11-24 stsp s->selected--;
8312 6458efa5 2020-11-24 stsp break;
8313 34ba6917 2020-11-30 stsp }
8314 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8315 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8316 07b0611c 2022-06-23 thomas view->count = 0;
8317 6458efa5 2020-11-24 stsp break;
8318 70f17a53 2022-06-13 thomas case CTRL('u'):
8319 23427b14 2022-06-23 thomas case 'u':
8320 70f17a53 2022-06-13 thomas nscroll /= 2;
8321 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8322 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8323 6458efa5 2020-11-24 stsp case CTRL('b'):
8324 1c5e5faa 2022-06-23 thomas case 'b':
8325 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8326 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8327 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8328 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8329 07b0611c 2022-06-23 thomas view->count = 0;
8330 6458efa5 2020-11-24 stsp break;
8331 6458efa5 2020-11-24 stsp case 'j':
8332 6458efa5 2020-11-24 stsp case KEY_DOWN:
8333 f7140bf5 2021-10-17 thomas case CTRL('n'):
8334 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8335 6458efa5 2020-11-24 stsp s->selected++;
8336 6458efa5 2020-11-24 stsp break;
8337 6458efa5 2020-11-24 stsp }
8338 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8339 6458efa5 2020-11-24 stsp /* can't scroll any further */
8340 07b0611c 2022-06-23 thomas view->count = 0;
8341 6458efa5 2020-11-24 stsp break;
8342 07b0611c 2022-06-23 thomas }
8343 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8344 6458efa5 2020-11-24 stsp break;
8345 70f17a53 2022-06-13 thomas case CTRL('d'):
8346 23427b14 2022-06-23 thomas case 'd':
8347 70f17a53 2022-06-13 thomas nscroll /= 2;
8348 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8349 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8350 6458efa5 2020-11-24 stsp case CTRL('f'):
8351 1c5e5faa 2022-06-23 thomas case 'f':
8352 4c2d69cb 2022-06-23 thomas case ' ':
8353 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8354 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8355 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8356 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8357 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8358 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8359 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8360 07b0611c 2022-06-23 thomas view->count = 0;
8361 6458efa5 2020-11-24 stsp break;
8362 6458efa5 2020-11-24 stsp }
8363 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8364 6458efa5 2020-11-24 stsp break;
8365 6458efa5 2020-11-24 stsp case CTRL('l'):
8366 07b0611c 2022-06-23 thomas view->count = 0;
8367 8924d611 2020-12-26 stsp tog_free_refs();
8368 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8369 8924d611 2020-12-26 stsp if (err)
8370 8924d611 2020-12-26 stsp break;
8371 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8372 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8373 6458efa5 2020-11-24 stsp break;
8374 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8375 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8376 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8377 6458efa5 2020-11-24 stsp break;
8378 6458efa5 2020-11-24 stsp default:
8379 07b0611c 2022-06-23 thomas view->count = 0;
8380 6458efa5 2020-11-24 stsp break;
8381 6458efa5 2020-11-24 stsp }
8382 6458efa5 2020-11-24 stsp
8383 6458efa5 2020-11-24 stsp return err;
8384 6458efa5 2020-11-24 stsp }
8385 6458efa5 2020-11-24 stsp
8386 6458efa5 2020-11-24 stsp __dead static void
8387 6458efa5 2020-11-24 stsp usage_ref(void)
8388 6458efa5 2020-11-24 stsp {
8389 6458efa5 2020-11-24 stsp endwin();
8390 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8391 6458efa5 2020-11-24 stsp getprogname());
8392 6458efa5 2020-11-24 stsp exit(1);
8393 6458efa5 2020-11-24 stsp }
8394 6458efa5 2020-11-24 stsp
8395 6458efa5 2020-11-24 stsp static const struct got_error *
8396 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8397 6458efa5 2020-11-24 stsp {
8398 6458efa5 2020-11-24 stsp const struct got_error *error;
8399 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8400 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8401 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8402 6458efa5 2020-11-24 stsp int ch;
8403 6458efa5 2020-11-24 stsp struct tog_view *view;
8404 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8405 6458efa5 2020-11-24 stsp
8406 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8407 6458efa5 2020-11-24 stsp switch (ch) {
8408 6458efa5 2020-11-24 stsp case 'r':
8409 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8410 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8411 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8412 6458efa5 2020-11-24 stsp optarg);
8413 6458efa5 2020-11-24 stsp break;
8414 6458efa5 2020-11-24 stsp default:
8415 e99e2d15 2020-11-24 naddy usage_ref();
8416 6458efa5 2020-11-24 stsp /* NOTREACHED */
8417 6458efa5 2020-11-24 stsp }
8418 6458efa5 2020-11-24 stsp }
8419 6458efa5 2020-11-24 stsp
8420 6458efa5 2020-11-24 stsp argc -= optind;
8421 6458efa5 2020-11-24 stsp argv += optind;
8422 6458efa5 2020-11-24 stsp
8423 6458efa5 2020-11-24 stsp if (argc > 1)
8424 e99e2d15 2020-11-24 naddy usage_ref();
8425 7cd52833 2022-06-23 thomas
8426 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8427 7cd52833 2022-06-23 thomas if (error != NULL)
8428 7cd52833 2022-06-23 thomas goto done;
8429 6458efa5 2020-11-24 stsp
8430 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8431 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8432 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8433 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8434 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8435 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8436 c156c7a4 2020-12-18 stsp goto done;
8437 6458efa5 2020-11-24 stsp if (worktree)
8438 6458efa5 2020-11-24 stsp repo_path =
8439 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8440 6458efa5 2020-11-24 stsp else
8441 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8442 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8443 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8444 c156c7a4 2020-12-18 stsp goto done;
8445 c156c7a4 2020-12-18 stsp }
8446 6458efa5 2020-11-24 stsp }
8447 6458efa5 2020-11-24 stsp
8448 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8449 6458efa5 2020-11-24 stsp if (error != NULL)
8450 6458efa5 2020-11-24 stsp goto done;
8451 6458efa5 2020-11-24 stsp
8452 6458efa5 2020-11-24 stsp init_curses();
8453 6458efa5 2020-11-24 stsp
8454 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8455 51a10b52 2020-12-26 stsp if (error)
8456 51a10b52 2020-12-26 stsp goto done;
8457 51a10b52 2020-12-26 stsp
8458 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8459 6458efa5 2020-11-24 stsp if (error)
8460 6458efa5 2020-11-24 stsp goto done;
8461 6458efa5 2020-11-24 stsp
8462 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8463 6458efa5 2020-11-24 stsp if (view == NULL) {
8464 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8465 6458efa5 2020-11-24 stsp goto done;
8466 6458efa5 2020-11-24 stsp }
8467 6458efa5 2020-11-24 stsp
8468 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8469 6458efa5 2020-11-24 stsp if (error)
8470 6458efa5 2020-11-24 stsp goto done;
8471 6458efa5 2020-11-24 stsp
8472 6458efa5 2020-11-24 stsp if (worktree) {
8473 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8474 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8475 6458efa5 2020-11-24 stsp worktree = NULL;
8476 6458efa5 2020-11-24 stsp }
8477 6458efa5 2020-11-24 stsp error = view_loop(view);
8478 6458efa5 2020-11-24 stsp done:
8479 6458efa5 2020-11-24 stsp free(repo_path);
8480 6458efa5 2020-11-24 stsp free(cwd);
8481 1d0f4054 2021-06-17 stsp if (repo) {
8482 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8483 1d0f4054 2021-06-17 stsp if (close_err)
8484 1d0f4054 2021-06-17 stsp error = close_err;
8485 1d0f4054 2021-06-17 stsp }
8486 7cd52833 2022-06-23 thomas if (pack_fds) {
8487 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8488 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8489 7cd52833 2022-06-23 thomas if (error == NULL)
8490 7cd52833 2022-06-23 thomas error = pack_err;
8491 7cd52833 2022-06-23 thomas }
8492 51a10b52 2020-12-26 stsp tog_free_refs();
8493 6458efa5 2020-11-24 stsp return error;
8494 6458efa5 2020-11-24 stsp }
8495 6458efa5 2020-11-24 stsp
8496 fc2737d5 2022-09-15 thomas static const struct got_error*
8497 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8498 fc2737d5 2022-09-15 thomas const char *str)
8499 fc2737d5 2022-09-15 thomas {
8500 fc2737d5 2022-09-15 thomas size_t len;
8501 fc2737d5 2022-09-15 thomas
8502 fc2737d5 2022-09-15 thomas if (win == NULL)
8503 fc2737d5 2022-09-15 thomas win = stdscr;
8504 fc2737d5 2022-09-15 thomas
8505 fc2737d5 2022-09-15 thomas len = strlen(str);
8506 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8507 fc2737d5 2022-09-15 thomas
8508 fc2737d5 2022-09-15 thomas if (focus)
8509 fc2737d5 2022-09-15 thomas wstandout(win);
8510 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8511 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8512 fc2737d5 2022-09-15 thomas if (focus)
8513 fc2737d5 2022-09-15 thomas wstandend(win);
8514 fc2737d5 2022-09-15 thomas
8515 fc2737d5 2022-09-15 thomas return NULL;
8516 fc2737d5 2022-09-15 thomas }
8517 fc2737d5 2022-09-15 thomas
8518 2a31b33b 2022-07-23 thomas static const struct got_error *
8519 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8520 fc2737d5 2022-09-15 thomas {
8521 fc2737d5 2022-09-15 thomas off_t *p;
8522 fc2737d5 2022-09-15 thomas
8523 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8524 fc2737d5 2022-09-15 thomas if (p == NULL) {
8525 fc2737d5 2022-09-15 thomas free(*line_offsets);
8526 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8527 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8528 fc2737d5 2022-09-15 thomas }
8529 fc2737d5 2022-09-15 thomas
8530 fc2737d5 2022-09-15 thomas *line_offsets = p;
8531 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8532 fc2737d5 2022-09-15 thomas ++(*nlines);
8533 fc2737d5 2022-09-15 thomas return NULL;
8534 fc2737d5 2022-09-15 thomas }
8535 fc2737d5 2022-09-15 thomas
8536 fc2737d5 2022-09-15 thomas static const struct got_error *
8537 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8538 fc2737d5 2022-09-15 thomas {
8539 fc2737d5 2022-09-15 thomas *ret = 0;
8540 fc2737d5 2022-09-15 thomas
8541 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8542 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8543 fc2737d5 2022-09-15 thomas size_t len = 1;
8544 fc2737d5 2022-09-15 thomas
8545 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8546 fc2737d5 2022-09-15 thomas continue;
8547 fc2737d5 2022-09-15 thomas
8548 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8549 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8550 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8551 fc2737d5 2022-09-15 thomas
8552 fc2737d5 2022-09-15 thomas len += strlen(t);
8553 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8554 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8555 fc2737d5 2022-09-15 thomas free(t0);
8556 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8557 fc2737d5 2022-09-15 thomas }
8558 fc2737d5 2022-09-15 thomas
8559 fc2737d5 2022-09-15 thomas return NULL;
8560 fc2737d5 2022-09-15 thomas }
8561 fc2737d5 2022-09-15 thomas
8562 fc2737d5 2022-09-15 thomas /*
8563 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8564 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8565 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8566 fc2737d5 2022-09-15 thomas */
8567 fc2737d5 2022-09-15 thomas static const struct got_error *
8568 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8569 fc2737d5 2022-09-15 thomas {
8570 fc2737d5 2022-09-15 thomas int n, len = width;
8571 fc2737d5 2022-09-15 thomas
8572 fc2737d5 2022-09-15 thomas if (km->keys) {
8573 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8574 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8575 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8576 30e77f6a 2022-09-18 thomas };
8577 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8578 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8579 fc2737d5 2022-09-15 thomas
8580 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8581 fc2737d5 2022-09-15 thomas
8582 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8583 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8584 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8585 fc2737d5 2022-09-15 thomas
8586 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8587 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8588 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8589 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8590 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8591 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8592 fc2737d5 2022-09-15 thomas if (n < 0) {
8593 fc2737d5 2022-09-15 thomas free(t0);
8594 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8595 fc2737d5 2022-09-15 thomas }
8596 fc2737d5 2022-09-15 thomas first = 0;
8597 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8598 fc2737d5 2022-09-15 thomas *off += n;
8599 fc2737d5 2022-09-15 thomas }
8600 fc2737d5 2022-09-15 thomas free(t0);
8601 fc2737d5 2022-09-15 thomas }
8602 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8603 fc2737d5 2022-09-15 thomas if (n < 0)
8604 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8605 fc2737d5 2022-09-15 thomas *off += n;
8606 fc2737d5 2022-09-15 thomas
8607 fc2737d5 2022-09-15 thomas return NULL;
8608 fc2737d5 2022-09-15 thomas }
8609 fc2737d5 2022-09-15 thomas
8610 fc2737d5 2022-09-15 thomas static const struct got_error *
8611 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8612 fc2737d5 2022-09-15 thomas {
8613 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8614 fc2737d5 2022-09-15 thomas off_t off = 0;
8615 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8616 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8617 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8618 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8619 fc2737d5 2022-09-15 thomas GENERATE_HELP
8620 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8621 fc2737d5 2022-09-15 thomas #undef KEY_
8622 fc2737d5 2022-09-15 thomas };
8623 fc2737d5 2022-09-15 thomas
8624 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8625 fc2737d5 2022-09-15 thomas if (err)
8626 fc2737d5 2022-09-15 thomas return err;
8627 fc2737d5 2022-09-15 thomas
8628 fc2737d5 2022-09-15 thomas n = nitems(km);
8629 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8630 fc2737d5 2022-09-15 thomas if (err)
8631 fc2737d5 2022-09-15 thomas return err;
8632 fc2737d5 2022-09-15 thomas
8633 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
8634 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
8635 fc2737d5 2022-09-15 thomas show = s->all;
8636 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
8637 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
8638 fc2737d5 2022-09-15 thomas show = 1;
8639 fc2737d5 2022-09-15 thomas }
8640 fc2737d5 2022-09-15 thomas if (show) {
8641 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
8642 fc2737d5 2022-09-15 thomas if (err)
8643 fc2737d5 2022-09-15 thomas return err;
8644 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8645 fc2737d5 2022-09-15 thomas if (err)
8646 fc2737d5 2022-09-15 thomas return err;
8647 fc2737d5 2022-09-15 thomas }
8648 fc2737d5 2022-09-15 thomas }
8649 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
8650 fc2737d5 2022-09-15 thomas ++off;
8651 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8652 fc2737d5 2022-09-15 thomas return err;
8653 fc2737d5 2022-09-15 thomas }
8654 fc2737d5 2022-09-15 thomas
8655 fc2737d5 2022-09-15 thomas static const struct got_error *
8656 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
8657 fc2737d5 2022-09-15 thomas {
8658 fc2737d5 2022-09-15 thomas FILE *f;
8659 fc2737d5 2022-09-15 thomas const struct got_error *err;
8660 fc2737d5 2022-09-15 thomas
8661 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8662 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8663 fc2737d5 2022-09-15 thomas s->nlines = 0;
8664 fc2737d5 2022-09-15 thomas
8665 fc2737d5 2022-09-15 thomas f = got_opentemp();
8666 fc2737d5 2022-09-15 thomas if (f == NULL)
8667 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
8668 fc2737d5 2022-09-15 thomas s->f = f;
8669 fc2737d5 2022-09-15 thomas
8670 fc2737d5 2022-09-15 thomas err = format_help(s);
8671 fc2737d5 2022-09-15 thomas if (err)
8672 fc2737d5 2022-09-15 thomas return err;
8673 fc2737d5 2022-09-15 thomas
8674 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
8675 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
8676 fc2737d5 2022-09-15 thomas
8677 fc2737d5 2022-09-15 thomas return NULL;
8678 fc2737d5 2022-09-15 thomas }
8679 fc2737d5 2022-09-15 thomas
8680 fc2737d5 2022-09-15 thomas static const struct got_error *
8681 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
8682 fc2737d5 2022-09-15 thomas {
8683 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
8684 fc2737d5 2022-09-15 thomas return NULL;
8685 fc2737d5 2022-09-15 thomas }
8686 fc2737d5 2022-09-15 thomas
8687 2a7d3cd7 2022-09-17 thomas static void
8688 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8689 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
8690 2a7d3cd7 2022-09-17 thomas {
8691 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
8692 2a7d3cd7 2022-09-17 thomas
8693 2a7d3cd7 2022-09-17 thomas *f = s->f;
8694 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
8695 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
8696 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
8697 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
8698 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
8699 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
8700 2a7d3cd7 2022-09-17 thomas }
8701 2a7d3cd7 2022-09-17 thomas
8702 fc2737d5 2022-09-15 thomas static const struct got_error *
8703 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
8704 fc2737d5 2022-09-15 thomas {
8705 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8706 fc2737d5 2022-09-15 thomas const struct got_error *err;
8707 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
8708 fc2737d5 2022-09-15 thomas wchar_t *wline;
8709 fc2737d5 2022-09-15 thomas char *line;
8710 fc2737d5 2022-09-15 thomas ssize_t linelen;
8711 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8712 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
8713 fc2737d5 2022-09-15 thomas int eos = view->nlines;
8714 fc2737d5 2022-09-15 thomas
8715 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8716 fc2737d5 2022-09-15 thomas --eos; /* account for border */
8717 fc2737d5 2022-09-15 thomas
8718 fc2737d5 2022-09-15 thomas s->lineno = 0;
8719 fc2737d5 2022-09-15 thomas rewind(s->f);
8720 fc2737d5 2022-09-15 thomas werase(view->window);
8721 fc2737d5 2022-09-15 thomas
8722 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
8723 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
8724 fc2737d5 2022-09-15 thomas
8725 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
8726 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
8727 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
8728 fc2737d5 2022-09-15 thomas if (err)
8729 fc2737d5 2022-09-15 thomas return err;
8730 fc2737d5 2022-09-15 thomas if (eos <= 1)
8731 fc2737d5 2022-09-15 thomas return NULL;
8732 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
8733 fc2737d5 2022-09-15 thomas eos -= 2;
8734 fc2737d5 2022-09-15 thomas
8735 fc2737d5 2022-09-15 thomas s->eof = 0;
8736 fc2737d5 2022-09-15 thomas view->maxx = 0;
8737 fc2737d5 2022-09-15 thomas line = NULL;
8738 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
8739 fc2737d5 2022-09-15 thomas attr_t attr = 0;
8740 fc2737d5 2022-09-15 thomas
8741 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8742 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8743 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
8744 fc2737d5 2022-09-15 thomas free(line);
8745 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
8746 fc2737d5 2022-09-15 thomas }
8747 fc2737d5 2022-09-15 thomas s->eof = 1;
8748 fc2737d5 2022-09-15 thomas break;
8749 fc2737d5 2022-09-15 thomas }
8750 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
8751 fc2737d5 2022-09-15 thomas continue;
8752 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8753 fc2737d5 2022-09-15 thomas continue;
8754 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
8755 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
8756 fc2737d5 2022-09-15 thomas
8757 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8758 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
8759 fc2737d5 2022-09-15 thomas if (err) {
8760 fc2737d5 2022-09-15 thomas free(line);
8761 fc2737d5 2022-09-15 thomas return err;
8762 fc2737d5 2022-09-15 thomas }
8763 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
8764 fc2737d5 2022-09-15 thomas free(wline);
8765 fc2737d5 2022-09-15 thomas wline = NULL;
8766 fc2737d5 2022-09-15 thomas
8767 fc2737d5 2022-09-15 thomas if (attr)
8768 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
8769 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
8770 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8771 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
8772 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
8773 fc2737d5 2022-09-15 thomas if (err) {
8774 fc2737d5 2022-09-15 thomas free(line);
8775 fc2737d5 2022-09-15 thomas return err;
8776 fc2737d5 2022-09-15 thomas }
8777 fc2737d5 2022-09-15 thomas } else {
8778 fc2737d5 2022-09-15 thomas int skip;
8779 fc2737d5 2022-09-15 thomas
8780 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
8781 fc2737d5 2022-09-15 thomas view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8782 fc2737d5 2022-09-15 thomas if (err) {
8783 fc2737d5 2022-09-15 thomas free(line);
8784 fc2737d5 2022-09-15 thomas return err;
8785 fc2737d5 2022-09-15 thomas }
8786 fc2737d5 2022-09-15 thomas rc = waddwstr(view->window, &wline[skip]);
8787 fc2737d5 2022-09-15 thomas free(wline);
8788 fc2737d5 2022-09-15 thomas wline = NULL;
8789 fc2737d5 2022-09-15 thomas if (rc == ERR)
8790 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "waddwstr");
8791 fc2737d5 2022-09-15 thomas }
8792 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
8793 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
8794 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
8795 fc2737d5 2022-09-15 thomas } else {
8796 fc2737d5 2022-09-15 thomas if (width <= view->ncols)
8797 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
8798 fc2737d5 2022-09-15 thomas }
8799 fc2737d5 2022-09-15 thomas if (attr)
8800 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
8801 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
8802 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
8803 fc2737d5 2022-09-15 thomas }
8804 fc2737d5 2022-09-15 thomas free(line);
8805 fc2737d5 2022-09-15 thomas if (nprinted > 0)
8806 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8807 fc2737d5 2022-09-15 thomas else
8808 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
8809 fc2737d5 2022-09-15 thomas
8810 fc2737d5 2022-09-15 thomas view_border(view);
8811 fc2737d5 2022-09-15 thomas
8812 fc2737d5 2022-09-15 thomas if (s->eof) {
8813 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
8814 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
8815 fc2737d5 2022-09-15 thomas view->ncols - 1);
8816 fc2737d5 2022-09-15 thomas if (rc == ERR)
8817 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8818 fc2737d5 2022-09-15 thomas } else {
8819 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
8820 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
8821 fc2737d5 2022-09-15 thomas wstandout(view->window);
8822 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
8823 fc2737d5 2022-09-15 thomas view->ncols - 1);
8824 fc2737d5 2022-09-15 thomas if (rc == ERR)
8825 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8826 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
8827 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
8828 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
8829 fc2737d5 2022-09-15 thomas if (rc == ERR)
8830 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
8831 fc2737d5 2022-09-15 thomas }
8832 fc2737d5 2022-09-15 thomas wstandend(view->window);
8833 fc2737d5 2022-09-15 thomas }
8834 fc2737d5 2022-09-15 thomas
8835 fc2737d5 2022-09-15 thomas return NULL;
8836 fc2737d5 2022-09-15 thomas }
8837 fc2737d5 2022-09-15 thomas
8838 fc2737d5 2022-09-15 thomas static const struct got_error *
8839 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8840 fc2737d5 2022-09-15 thomas {
8841 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8842 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8843 fc2737d5 2022-09-15 thomas char *line = NULL;
8844 fc2737d5 2022-09-15 thomas ssize_t linelen;
8845 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8846 fc2737d5 2022-09-15 thomas int eos, nscroll;
8847 fc2737d5 2022-09-15 thomas
8848 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
8849 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8850 fc2737d5 2022-09-15 thomas --eos; /* border */
8851 fc2737d5 2022-09-15 thomas
8852 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
8853 fc2737d5 2022-09-15 thomas
8854 fc2737d5 2022-09-15 thomas switch (ch) {
8855 fc2737d5 2022-09-15 thomas case '0':
8856 fc2737d5 2022-09-15 thomas view->x = 0;
8857 fc2737d5 2022-09-15 thomas break;
8858 fc2737d5 2022-09-15 thomas case '$':
8859 fc2737d5 2022-09-15 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
8860 fc2737d5 2022-09-15 thomas view->count = 0;
8861 fc2737d5 2022-09-15 thomas break;
8862 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
8863 fc2737d5 2022-09-15 thomas case 'l':
8864 fc2737d5 2022-09-15 thomas if (view->x + view->ncols / 3 < view->maxx)
8865 fc2737d5 2022-09-15 thomas view->x += 2;
8866 fc2737d5 2022-09-15 thomas else
8867 fc2737d5 2022-09-15 thomas view->count = 0;
8868 fc2737d5 2022-09-15 thomas break;
8869 fc2737d5 2022-09-15 thomas case KEY_LEFT:
8870 fc2737d5 2022-09-15 thomas case 'h':
8871 fc2737d5 2022-09-15 thomas view->x -= MIN(view->x, 2);
8872 fc2737d5 2022-09-15 thomas if (view->x <= 0)
8873 fc2737d5 2022-09-15 thomas view->count = 0;
8874 fc2737d5 2022-09-15 thomas break;
8875 fc2737d5 2022-09-15 thomas case 'g':
8876 fc2737d5 2022-09-15 thomas case KEY_HOME:
8877 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8878 fc2737d5 2022-09-15 thomas view->count = 0;
8879 fc2737d5 2022-09-15 thomas break;
8880 fc2737d5 2022-09-15 thomas case 'G':
8881 fc2737d5 2022-09-15 thomas case KEY_END:
8882 fc2737d5 2022-09-15 thomas view->count = 0;
8883 fc2737d5 2022-09-15 thomas if (s->eof)
8884 fc2737d5 2022-09-15 thomas break;
8885 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
8886 fc2737d5 2022-09-15 thomas s->eof = 1;
8887 fc2737d5 2022-09-15 thomas break;
8888 fc2737d5 2022-09-15 thomas case 'k':
8889 fc2737d5 2022-09-15 thomas case KEY_UP:
8890 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
8891 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
8892 fc2737d5 2022-09-15 thomas else
8893 fc2737d5 2022-09-15 thomas view->count = 0;
8894 fc2737d5 2022-09-15 thomas break;
8895 fc2737d5 2022-09-15 thomas case CTRL('u'):
8896 fc2737d5 2022-09-15 thomas case 'u':
8897 fc2737d5 2022-09-15 thomas nscroll /= 2;
8898 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8899 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
8900 fc2737d5 2022-09-15 thomas case CTRL('b'):
8901 fc2737d5 2022-09-15 thomas case 'b':
8902 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
8903 fc2737d5 2022-09-15 thomas view->count = 0;
8904 fc2737d5 2022-09-15 thomas break;
8905 fc2737d5 2022-09-15 thomas }
8906 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
8907 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
8908 fc2737d5 2022-09-15 thomas break;
8909 fc2737d5 2022-09-15 thomas case 'j':
8910 fc2737d5 2022-09-15 thomas case KEY_DOWN:
8911 fc2737d5 2022-09-15 thomas case CTRL('n'):
8912 fc2737d5 2022-09-15 thomas if (!s->eof)
8913 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
8914 fc2737d5 2022-09-15 thomas else
8915 fc2737d5 2022-09-15 thomas view->count = 0;
8916 fc2737d5 2022-09-15 thomas break;
8917 fc2737d5 2022-09-15 thomas case CTRL('d'):
8918 fc2737d5 2022-09-15 thomas case 'd':
8919 fc2737d5 2022-09-15 thomas nscroll /= 2;
8920 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8921 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
8922 fc2737d5 2022-09-15 thomas case CTRL('f'):
8923 fc2737d5 2022-09-15 thomas case 'f':
8924 fc2737d5 2022-09-15 thomas case ' ':
8925 fc2737d5 2022-09-15 thomas if (s->eof) {
8926 fc2737d5 2022-09-15 thomas view->count = 0;
8927 fc2737d5 2022-09-15 thomas break;
8928 fc2737d5 2022-09-15 thomas }
8929 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
8930 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8931 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
8932 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8933 fc2737d5 2022-09-15 thomas if (feof(s->f))
8934 fc2737d5 2022-09-15 thomas s->eof = 1;
8935 fc2737d5 2022-09-15 thomas else
8936 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
8937 fc2737d5 2022-09-15 thomas break;
8938 fc2737d5 2022-09-15 thomas }
8939 fc2737d5 2022-09-15 thomas }
8940 fc2737d5 2022-09-15 thomas free(line);
8941 fc2737d5 2022-09-15 thomas break;
8942 fc2737d5 2022-09-15 thomas default:
8943 fc2737d5 2022-09-15 thomas view->count = 0;
8944 fc2737d5 2022-09-15 thomas break;
8945 fc2737d5 2022-09-15 thomas }
8946 fc2737d5 2022-09-15 thomas
8947 fc2737d5 2022-09-15 thomas return err;
8948 fc2737d5 2022-09-15 thomas }
8949 fc2737d5 2022-09-15 thomas
8950 fc2737d5 2022-09-15 thomas static const struct got_error *
8951 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
8952 fc2737d5 2022-09-15 thomas {
8953 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8954 fc2737d5 2022-09-15 thomas
8955 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8956 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8957 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
8958 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8959 fc2737d5 2022-09-15 thomas
8960 fc2737d5 2022-09-15 thomas return NULL;
8961 fc2737d5 2022-09-15 thomas }
8962 fc2737d5 2022-09-15 thomas
8963 fc2737d5 2022-09-15 thomas static const struct got_error *
8964 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
8965 fc2737d5 2022-09-15 thomas {
8966 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8967 fc2737d5 2022-09-15 thomas
8968 fc2737d5 2022-09-15 thomas
8969 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
8970 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8971 fc2737d5 2022-09-15 thomas
8972 fc2737d5 2022-09-15 thomas wclear(view->window);
8973 fc2737d5 2022-09-15 thomas view->count = 0;
8974 fc2737d5 2022-09-15 thomas view->x = 0;
8975 fc2737d5 2022-09-15 thomas s->all = !s->all;
8976 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8977 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
8978 fc2737d5 2022-09-15 thomas s->matched_line = 0;
8979 fc2737d5 2022-09-15 thomas
8980 fc2737d5 2022-09-15 thomas return create_help(s);
8981 fc2737d5 2022-09-15 thomas }
8982 fc2737d5 2022-09-15 thomas
8983 fc2737d5 2022-09-15 thomas static const struct got_error *
8984 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
8985 fc2737d5 2022-09-15 thomas {
8986 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8987 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8988 fc2737d5 2022-09-15 thomas
8989 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
8990 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8991 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
8992 fc2737d5 2022-09-15 thomas s->selected_line = 1;
8993 fc2737d5 2022-09-15 thomas
8994 fc2737d5 2022-09-15 thomas view->show = show_help_view;
8995 fc2737d5 2022-09-15 thomas view->input = input_help_view;
8996 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
8997 fc2737d5 2022-09-15 thomas view->close = close_help_view;
8998 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
8999 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9000 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9001 fc2737d5 2022-09-15 thomas
9002 fc2737d5 2022-09-15 thomas err = create_help(s);
9003 fc2737d5 2022-09-15 thomas return err;
9004 fc2737d5 2022-09-15 thomas }
9005 fc2737d5 2022-09-15 thomas
9006 fc2737d5 2022-09-15 thomas static const struct got_error *
9007 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9008 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9009 2a31b33b 2022-07-23 thomas {
9010 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9011 2a31b33b 2022-07-23 thomas
9012 2a31b33b 2022-07-23 thomas *new_view = NULL;
9013 2a31b33b 2022-07-23 thomas
9014 2a31b33b 2022-07-23 thomas switch (request) {
9015 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9016 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9017 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9018 2a31b33b 2022-07-23 thomas
9019 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9020 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9021 2a31b33b 2022-07-23 thomas view, s->repo);
9022 2a31b33b 2022-07-23 thomas } else
9023 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9024 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9025 2a31b33b 2022-07-23 thomas break;
9026 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9027 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9028 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9029 2a31b33b 2022-07-23 thomas
9030 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9031 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9032 2a31b33b 2022-07-23 thomas s->repo);
9033 2a31b33b 2022-07-23 thomas } else
9034 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9035 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9036 2a31b33b 2022-07-23 thomas break;
9037 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9038 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9039 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9040 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9041 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9042 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9043 2a31b33b 2022-07-23 thomas &view->state.tree);
9044 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9045 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9046 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9047 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9048 2a31b33b 2022-07-23 thomas else
9049 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9050 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9051 2a31b33b 2022-07-23 thomas break;
9052 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9053 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9054 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9055 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9056 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9057 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9058 2a31b33b 2022-07-23 thomas view->state.log.repo);
9059 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9060 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9061 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9062 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9063 2a31b33b 2022-07-23 thomas else
9064 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9065 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9066 2a31b33b 2022-07-23 thomas break;
9067 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9068 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9069 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9070 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9071 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9072 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9073 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9074 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9075 2a31b33b 2022-07-23 thomas else
9076 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9077 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9078 2a31b33b 2022-07-23 thomas if (err)
9079 2a31b33b 2022-07-23 thomas view_close(*new_view);
9080 2a31b33b 2022-07-23 thomas break;
9081 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9082 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9083 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9084 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9085 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9086 fc2737d5 2022-09-15 thomas if (err)
9087 fc2737d5 2022-09-15 thomas view_close(*new_view);
9088 fc2737d5 2022-09-15 thomas break;
9089 2a31b33b 2022-07-23 thomas default:
9090 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9091 2a31b33b 2022-07-23 thomas }
9092 2a31b33b 2022-07-23 thomas
9093 2a31b33b 2022-07-23 thomas return err;
9094 2a31b33b 2022-07-23 thomas }
9095 2a31b33b 2022-07-23 thomas
9096 a5d43cac 2022-07-01 thomas /*
9097 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9098 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9099 a5d43cac 2022-07-01 thomas */
9100 6458efa5 2020-11-24 stsp static void
9101 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9102 a5d43cac 2022-07-01 thomas {
9103 a5d43cac 2022-07-01 thomas switch (view->type) {
9104 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9105 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9106 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9107 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9108 a5d43cac 2022-07-01 thomas 1);
9109 a5d43cac 2022-07-01 thomas break;
9110 a5d43cac 2022-07-01 thomas }
9111 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9112 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9113 a5d43cac 2022-07-01 thomas else
9114 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9115 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9116 a5d43cac 2022-07-01 thomas break;
9117 a5d43cac 2022-07-01 thomas }
9118 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9119 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9120 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9121 a5d43cac 2022-07-01 thomas break;
9122 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9123 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9124 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9125 a5d43cac 2022-07-01 thomas break;
9126 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9127 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9128 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9129 a5d43cac 2022-07-01 thomas break;
9130 a5d43cac 2022-07-01 thomas default:
9131 a5d43cac 2022-07-01 thomas break;
9132 a5d43cac 2022-07-01 thomas }
9133 a5d43cac 2022-07-01 thomas
9134 a5d43cac 2022-07-01 thomas view->offset = 0;
9135 a5d43cac 2022-07-01 thomas }
9136 a5d43cac 2022-07-01 thomas
9137 a5d43cac 2022-07-01 thomas /*
9138 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9139 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9140 a5d43cac 2022-07-01 thomas */
9141 a5d43cac 2022-07-01 thomas static const struct got_error *
9142 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9143 a5d43cac 2022-07-01 thomas {
9144 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9145 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9146 a5d43cac 2022-07-01 thomas int *selected = NULL;
9147 a5d43cac 2022-07-01 thomas int header, offset;
9148 a5d43cac 2022-07-01 thomas
9149 a5d43cac 2022-07-01 thomas switch (view->type) {
9150 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9151 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9152 a5d43cac 2022-07-01 thomas header = 3;
9153 a5d43cac 2022-07-01 thomas scrolld = NULL;
9154 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9155 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9156 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9157 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9158 a5d43cac 2022-07-01 thomas view->offset = offset;
9159 a5d43cac 2022-07-01 thomas }
9160 a5d43cac 2022-07-01 thomas break;
9161 a5d43cac 2022-07-01 thomas }
9162 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9163 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9164 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9165 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9166 a5d43cac 2022-07-01 thomas selected = &s->selected;
9167 a5d43cac 2022-07-01 thomas break;
9168 a5d43cac 2022-07-01 thomas }
9169 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9170 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9171 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9172 a5d43cac 2022-07-01 thomas header = 3;
9173 a5d43cac 2022-07-01 thomas selected = &s->selected;
9174 a5d43cac 2022-07-01 thomas break;
9175 a5d43cac 2022-07-01 thomas }
9176 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9177 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9178 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9179 a5d43cac 2022-07-01 thomas header = 5;
9180 a5d43cac 2022-07-01 thomas selected = &s->selected;
9181 a5d43cac 2022-07-01 thomas break;
9182 a5d43cac 2022-07-01 thomas }
9183 a5d43cac 2022-07-01 thomas default:
9184 a5d43cac 2022-07-01 thomas selected = NULL;
9185 a5d43cac 2022-07-01 thomas scrolld = NULL;
9186 a5d43cac 2022-07-01 thomas header = 0;
9187 a5d43cac 2022-07-01 thomas break;
9188 a5d43cac 2022-07-01 thomas }
9189 a5d43cac 2022-07-01 thomas
9190 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9191 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9192 a5d43cac 2022-07-01 thomas view->offset = offset;
9193 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9194 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9195 a5d43cac 2022-07-01 thomas *selected -= offset;
9196 a5d43cac 2022-07-01 thomas }
9197 a5d43cac 2022-07-01 thomas }
9198 a5d43cac 2022-07-01 thomas
9199 a5d43cac 2022-07-01 thomas return err;
9200 a5d43cac 2022-07-01 thomas }
9201 a5d43cac 2022-07-01 thomas
9202 a5d43cac 2022-07-01 thomas static void
9203 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9204 ce5b7c56 2019-07-09 stsp {
9205 6059809a 2020-12-17 stsp size_t i;
9206 9f7d7167 2018-04-29 stsp
9207 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9208 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9209 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9210 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9211 ce5b7c56 2019-07-09 stsp }
9212 6879ba42 2020-10-01 naddy fputc('\n', fp);
9213 ce5b7c56 2019-07-09 stsp }
9214 ce5b7c56 2019-07-09 stsp
9215 4ed7e80c 2018-05-20 stsp __dead static void
9216 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9217 9f7d7167 2018-04-29 stsp {
9218 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9219 6879ba42 2020-10-01 naddy
9220 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9221 6879ba42 2020-10-01 naddy getprogname());
9222 6879ba42 2020-10-01 naddy if (hflag) {
9223 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9224 6879ba42 2020-10-01 naddy list_commands(fp);
9225 ee85c5e8 2020-02-29 stsp }
9226 6879ba42 2020-10-01 naddy exit(status);
9227 9f7d7167 2018-04-29 stsp }
9228 9f7d7167 2018-04-29 stsp
9229 c2301be8 2018-04-30 stsp static char **
9230 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9231 c2301be8 2018-04-30 stsp {
9232 ee85c5e8 2020-02-29 stsp va_list ap;
9233 c2301be8 2018-04-30 stsp char **argv;
9234 ee85c5e8 2020-02-29 stsp int i;
9235 c2301be8 2018-04-30 stsp
9236 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9237 ee85c5e8 2020-02-29 stsp
9238 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9239 c2301be8 2018-04-30 stsp if (argv == NULL)
9240 c2301be8 2018-04-30 stsp err(1, "calloc");
9241 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9242 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9243 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9244 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9245 c2301be8 2018-04-30 stsp }
9246 c2301be8 2018-04-30 stsp
9247 ee85c5e8 2020-02-29 stsp va_end(ap);
9248 c2301be8 2018-04-30 stsp return argv;
9249 ee85c5e8 2020-02-29 stsp }
9250 ee85c5e8 2020-02-29 stsp
9251 ee85c5e8 2020-02-29 stsp /*
9252 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9253 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9254 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9255 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9256 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9257 ee85c5e8 2020-02-29 stsp */
9258 ee85c5e8 2020-02-29 stsp static const struct got_error *
9259 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9260 ee85c5e8 2020-02-29 stsp {
9261 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9262 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9263 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9264 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9265 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9266 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9267 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9268 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9269 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9270 84de9106 2020-12-26 stsp
9271 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9272 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9273 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9274 ee85c5e8 2020-02-29 stsp
9275 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9276 7cd52833 2022-06-23 thomas if (error != NULL)
9277 7cd52833 2022-06-23 thomas goto done;
9278 7cd52833 2022-06-23 thomas
9279 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9280 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9281 ee85c5e8 2020-02-29 stsp goto done;
9282 ee85c5e8 2020-02-29 stsp
9283 ee85c5e8 2020-02-29 stsp if (worktree)
9284 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9285 ee85c5e8 2020-02-29 stsp else
9286 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9287 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9288 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9289 ee85c5e8 2020-02-29 stsp goto done;
9290 ee85c5e8 2020-02-29 stsp }
9291 ee85c5e8 2020-02-29 stsp
9292 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9293 ee85c5e8 2020-02-29 stsp if (error != NULL)
9294 ee85c5e8 2020-02-29 stsp goto done;
9295 ee85c5e8 2020-02-29 stsp
9296 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9297 ee85c5e8 2020-02-29 stsp repo, worktree);
9298 ee85c5e8 2020-02-29 stsp if (error)
9299 ee85c5e8 2020-02-29 stsp goto done;
9300 ee85c5e8 2020-02-29 stsp
9301 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9302 84de9106 2020-12-26 stsp if (error)
9303 84de9106 2020-12-26 stsp goto done;
9304 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9305 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9306 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9307 ee85c5e8 2020-02-29 stsp if (error)
9308 ee85c5e8 2020-02-29 stsp goto done;
9309 ee85c5e8 2020-02-29 stsp
9310 ee85c5e8 2020-02-29 stsp if (worktree) {
9311 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9312 ee85c5e8 2020-02-29 stsp worktree = NULL;
9313 ee85c5e8 2020-02-29 stsp }
9314 ee85c5e8 2020-02-29 stsp
9315 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9316 945f9229 2022-04-16 thomas if (error)
9317 945f9229 2022-04-16 thomas goto done;
9318 945f9229 2022-04-16 thomas
9319 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9320 ee85c5e8 2020-02-29 stsp if (error) {
9321 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9322 ee85c5e8 2020-02-29 stsp goto done;
9323 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9324 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9325 6879ba42 2020-10-01 naddy usage(1, 1);
9326 ee85c5e8 2020-02-29 stsp /* not reached */
9327 ee85c5e8 2020-02-29 stsp }
9328 ee85c5e8 2020-02-29 stsp
9329 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9330 ee85c5e8 2020-02-29 stsp if (error)
9331 ee85c5e8 2020-02-29 stsp goto done;
9332 ee85c5e8 2020-02-29 stsp
9333 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9334 ee85c5e8 2020-02-29 stsp argc = 4;
9335 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9336 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9337 ee85c5e8 2020-02-29 stsp done:
9338 1d0f4054 2021-06-17 stsp if (repo) {
9339 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9340 1d0f4054 2021-06-17 stsp if (error == NULL)
9341 1d0f4054 2021-06-17 stsp error = close_err;
9342 1d0f4054 2021-06-17 stsp }
9343 945f9229 2022-04-16 thomas if (commit)
9344 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9345 ee85c5e8 2020-02-29 stsp if (worktree)
9346 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9347 7cd52833 2022-06-23 thomas if (pack_fds) {
9348 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9349 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9350 7cd52833 2022-06-23 thomas if (error == NULL)
9351 7cd52833 2022-06-23 thomas error = pack_err;
9352 7cd52833 2022-06-23 thomas }
9353 ee85c5e8 2020-02-29 stsp free(id);
9354 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9355 ee85c5e8 2020-02-29 stsp free(commit_id);
9356 ee85c5e8 2020-02-29 stsp free(cwd);
9357 ee85c5e8 2020-02-29 stsp free(repo_path);
9358 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9359 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9360 ee85c5e8 2020-02-29 stsp int i;
9361 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9362 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9363 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9364 ee85c5e8 2020-02-29 stsp }
9365 87670572 2020-12-26 naddy tog_free_refs();
9366 ee85c5e8 2020-02-29 stsp return error;
9367 c2301be8 2018-04-30 stsp }
9368 c2301be8 2018-04-30 stsp
9369 9f7d7167 2018-04-29 stsp int
9370 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9371 9f7d7167 2018-04-29 stsp {
9372 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9373 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9374 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9375 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9376 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9377 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9378 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9379 83cd27f8 2020-01-13 stsp };
9380 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9381 9f7d7167 2018-04-29 stsp
9382 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9383 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9384 cacf1690 2022-09-06 thomas
9385 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9386 9f7d7167 2018-04-29 stsp
9387 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9388 9f7d7167 2018-04-29 stsp switch (ch) {
9389 9f7d7167 2018-04-29 stsp case 'h':
9390 9f7d7167 2018-04-29 stsp hflag = 1;
9391 9f7d7167 2018-04-29 stsp break;
9392 53ccebc2 2019-07-30 stsp case 'V':
9393 53ccebc2 2019-07-30 stsp Vflag = 1;
9394 53ccebc2 2019-07-30 stsp break;
9395 9f7d7167 2018-04-29 stsp default:
9396 6879ba42 2020-10-01 naddy usage(hflag, 1);
9397 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9398 9f7d7167 2018-04-29 stsp }
9399 9f7d7167 2018-04-29 stsp }
9400 9f7d7167 2018-04-29 stsp
9401 9f7d7167 2018-04-29 stsp argc -= optind;
9402 9f7d7167 2018-04-29 stsp argv += optind;
9403 9814e6a3 2020-09-27 naddy optind = 1;
9404 c2301be8 2018-04-30 stsp optreset = 1;
9405 9f7d7167 2018-04-29 stsp
9406 53ccebc2 2019-07-30 stsp if (Vflag) {
9407 53ccebc2 2019-07-30 stsp got_version_print_str();
9408 6879ba42 2020-10-01 naddy return 0;
9409 53ccebc2 2019-07-30 stsp }
9410 4010e238 2020-12-04 stsp
9411 4010e238 2020-12-04 stsp #ifndef PROFILE
9412 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9413 4010e238 2020-12-04 stsp NULL) == -1)
9414 4010e238 2020-12-04 stsp err(1, "pledge");
9415 4010e238 2020-12-04 stsp #endif
9416 53ccebc2 2019-07-30 stsp
9417 c2301be8 2018-04-30 stsp if (argc == 0) {
9418 f29d3e89 2018-06-23 stsp if (hflag)
9419 6879ba42 2020-10-01 naddy usage(hflag, 0);
9420 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9421 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9422 c2301be8 2018-04-30 stsp argc = 1;
9423 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9424 c2301be8 2018-04-30 stsp } else {
9425 6059809a 2020-12-17 stsp size_t i;
9426 9f7d7167 2018-04-29 stsp
9427 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9428 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9429 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9430 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9431 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9432 9f7d7167 2018-04-29 stsp break;
9433 9f7d7167 2018-04-29 stsp }
9434 9f7d7167 2018-04-29 stsp }
9435 ee85c5e8 2020-02-29 stsp }
9436 3642c4c6 2019-07-09 stsp
9437 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9438 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9439 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9440 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9441 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9442 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9443 adf4c9e0 2022-07-03 thomas }
9444 adf4c9e0 2022-07-03 thomas
9445 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9446 ee85c5e8 2020-02-29 stsp if (argc != 1)
9447 6879ba42 2020-10-01 naddy usage(0, 1);
9448 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9449 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9450 ee85c5e8 2020-02-29 stsp } else {
9451 ee85c5e8 2020-02-29 stsp if (hflag)
9452 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9453 ee85c5e8 2020-02-29 stsp else
9454 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9455 9f7d7167 2018-04-29 stsp }
9456 9f7d7167 2018-04-29 stsp
9457 9f7d7167 2018-04-29 stsp endwin();
9458 b46c1e04 2020-09-20 naddy putchar('\n');
9459 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9460 a2f4a359 2020-02-28 stsp int i;
9461 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9462 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9463 a2f4a359 2020-02-28 stsp free(cmd_argv);
9464 a2f4a359 2020-02-28 stsp }
9465 a2f4a359 2020-02-28 stsp
9466 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9467 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9468 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9469 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9470 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9471 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9472 9f7d7167 2018-04-29 stsp return 0;
9473 9f7d7167 2018-04-29 stsp }