Blame


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