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