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 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2425 1a76625f 2018-10-22 stsp break;
2426 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2427 1e37a5c2 2019-05-12 jcs s->selected--;
2428 1144d21a 2019-06-21 stsp else
2429 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2430 912a3f79 2021-08-30 j select_commit(s);
2431 912a3f79 2021-08-30 j break;
2432 912a3f79 2021-08-30 j case 'g':
2433 912a3f79 2021-08-30 j case KEY_HOME:
2434 912a3f79 2021-08-30 j s->selected = 0;
2435 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2436 2b779855 2020-12-05 naddy select_commit(s);
2437 1e37a5c2 2019-05-12 jcs break;
2438 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2439 a4292ac5 2019-05-12 jcs case CTRL('b'):
2440 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2441 e5a0f69f 2018-08-18 stsp break;
2442 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2443 1e37a5c2 2019-05-12 jcs s->selected = 0;
2444 2b779855 2020-12-05 naddy else
2445 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2446 2b779855 2020-12-05 naddy select_commit(s);
2447 1e37a5c2 2019-05-12 jcs break;
2448 1e37a5c2 2019-05-12 jcs case 'j':
2449 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2450 1e37a5c2 2019-05-12 jcs case '>':
2451 1e37a5c2 2019-05-12 jcs case '.':
2452 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2453 e5a0f69f 2018-08-18 stsp break;
2454 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2455 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2456 1e37a5c2 2019-05-12 jcs s->selected++;
2457 2b779855 2020-12-05 naddy else {
2458 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2459 2b779855 2020-12-05 naddy if (err)
2460 2b779855 2020-12-05 naddy break;
2461 912a3f79 2021-08-30 j }
2462 912a3f79 2021-08-30 j select_commit(s);
2463 912a3f79 2021-08-30 j break;
2464 912a3f79 2021-08-30 j case 'G':
2465 912a3f79 2021-08-30 j case KEY_END: {
2466 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2467 912a3f79 2021-08-30 j * traverse them all. */
2468 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2469 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2470 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2471 80ddbec8 2018-04-29 stsp }
2472 912a3f79 2021-08-30 j
2473 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2474 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2475 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2476 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2477 f3bc9f1d 2021-09-05 naddy break;
2478 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2479 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2480 f3bc9f1d 2021-09-05 naddy }
2481 f3bc9f1d 2021-09-05 naddy if (n > 0)
2482 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2483 2b779855 2020-12-05 naddy select_commit(s);
2484 1e37a5c2 2019-05-12 jcs break;
2485 912a3f79 2021-08-30 j }
2486 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2487 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2488 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2489 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2490 1e37a5c2 2019-05-12 jcs if (first == NULL)
2491 e5a0f69f 2018-08-18 stsp break;
2492 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2493 1cae65b4 2019-09-22 stsp if (err)
2494 1cae65b4 2019-09-22 stsp break;
2495 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2496 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2497 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2498 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2499 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2500 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2501 1e37a5c2 2019-05-12 jcs }
2502 2b779855 2020-12-05 naddy select_commit(s);
2503 1e37a5c2 2019-05-12 jcs break;
2504 1e37a5c2 2019-05-12 jcs }
2505 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2506 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2507 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2508 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2509 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2510 2b779855 2020-12-05 naddy select_commit(s);
2511 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2512 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2513 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2514 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2515 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2516 0bf7f153 2020-12-02 naddy }
2517 1e37a5c2 2019-05-12 jcs break;
2518 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2519 87c7274c 2019-05-12 jcs case ' ':
2520 1e37a5c2 2019-05-12 jcs case '\r':
2521 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2522 e5a0f69f 2018-08-18 stsp break;
2523 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2524 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2525 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2526 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2527 78756c87 2020-11-24 stsp view, s->repo);
2528 1e37a5c2 2019-05-12 jcs if (err)
2529 1e37a5c2 2019-05-12 jcs break;
2530 e78dc838 2020-12-04 stsp view->focussed = 0;
2531 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2532 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2533 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2534 f7013a22 2018-10-24 stsp if (err)
2535 1e37a5c2 2019-05-12 jcs return err;
2536 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2537 e78dc838 2020-12-04 stsp view->focus_child = 1;
2538 1e37a5c2 2019-05-12 jcs } else
2539 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2540 1e37a5c2 2019-05-12 jcs break;
2541 1e37a5c2 2019-05-12 jcs case 't':
2542 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2543 5036bf37 2018-09-24 stsp break;
2544 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2545 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2546 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2547 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2548 4e97c21c 2020-12-06 stsp s->repo);
2549 1e37a5c2 2019-05-12 jcs if (err)
2550 e5a0f69f 2018-08-18 stsp break;
2551 e78dc838 2020-12-04 stsp view->focussed = 0;
2552 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2553 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2554 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2555 1e37a5c2 2019-05-12 jcs if (err)
2556 1e37a5c2 2019-05-12 jcs return err;
2557 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2558 e78dc838 2020-12-04 stsp view->focus_child = 1;
2559 1e37a5c2 2019-05-12 jcs } else
2560 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2561 1e37a5c2 2019-05-12 jcs break;
2562 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2563 21355643 2020-12-06 stsp case CTRL('l'):
2564 21355643 2020-12-06 stsp case 'B':
2565 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2566 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2567 1e37a5c2 2019-05-12 jcs break;
2568 21355643 2020-12-06 stsp err = stop_log_thread(s);
2569 74cfe85e 2020-10-20 stsp if (err)
2570 74cfe85e 2020-10-20 stsp return err;
2571 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2572 21355643 2020-12-06 stsp char *parent_path;
2573 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2574 21355643 2020-12-06 stsp if (err)
2575 21355643 2020-12-06 stsp return err;
2576 21355643 2020-12-06 stsp free(s->in_repo_path);
2577 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2578 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2579 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2580 21355643 2020-12-06 stsp struct got_object_id *start_id;
2581 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2582 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2583 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2584 21355643 2020-12-06 stsp if (err)
2585 21355643 2020-12-06 stsp return err;
2586 21355643 2020-12-06 stsp free(s->start_id);
2587 21355643 2020-12-06 stsp s->start_id = start_id;
2588 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2589 21355643 2020-12-06 stsp } else /* 'B' */
2590 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2591 21355643 2020-12-06 stsp
2592 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2593 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2594 74cfe85e 2020-10-20 stsp if (err)
2595 21355643 2020-12-06 stsp return err;
2596 51a10b52 2020-12-26 stsp tog_free_refs();
2597 51a10b52 2020-12-26 stsp err = tog_load_refs(s->repo);
2598 ca51c541 2020-12-07 stsp if (err)
2599 ca51c541 2020-12-07 stsp return err;
2600 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2601 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2602 d01904d4 2019-06-25 stsp if (err)
2603 d01904d4 2019-06-25 stsp return err;
2604 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2605 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2606 b672a97a 2020-01-27 stsp if (err)
2607 b672a97a 2020-01-27 stsp return err;
2608 21355643 2020-12-06 stsp free_commits(&s->commits);
2609 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2610 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2611 21355643 2020-12-06 stsp s->selected_entry = NULL;
2612 21355643 2020-12-06 stsp s->selected = 0;
2613 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2614 21355643 2020-12-06 stsp s->quit = 0;
2615 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2616 d01904d4 2019-06-25 stsp break;
2617 6458efa5 2020-11-24 stsp case 'r':
2618 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2619 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2620 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2621 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2622 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2623 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2624 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2625 6458efa5 2020-11-24 stsp if (err) {
2626 6458efa5 2020-11-24 stsp view_close(ref_view);
2627 6458efa5 2020-11-24 stsp return err;
2628 6458efa5 2020-11-24 stsp }
2629 e78dc838 2020-12-04 stsp view->focussed = 0;
2630 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2631 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2632 6458efa5 2020-11-24 stsp err = view_close_child(view);
2633 6458efa5 2020-11-24 stsp if (err)
2634 6458efa5 2020-11-24 stsp return err;
2635 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2636 e78dc838 2020-12-04 stsp view->focus_child = 1;
2637 6458efa5 2020-11-24 stsp } else
2638 6458efa5 2020-11-24 stsp *new_view = ref_view;
2639 6458efa5 2020-11-24 stsp break;
2640 1e37a5c2 2019-05-12 jcs default:
2641 1e37a5c2 2019-05-12 jcs break;
2642 899d86c2 2018-05-10 stsp }
2643 e5a0f69f 2018-08-18 stsp
2644 80ddbec8 2018-04-29 stsp return err;
2645 80ddbec8 2018-04-29 stsp }
2646 80ddbec8 2018-04-29 stsp
2647 4ed7e80c 2018-05-20 stsp static const struct got_error *
2648 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2649 c2db6724 2019-01-04 stsp {
2650 c2db6724 2019-01-04 stsp const struct got_error *error;
2651 c2db6724 2019-01-04 stsp
2652 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2653 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2654 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2655 37c06ea4 2019-07-15 stsp #endif
2656 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2657 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2658 c2db6724 2019-01-04 stsp
2659 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2660 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2661 c2db6724 2019-01-04 stsp
2662 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2663 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2664 c2db6724 2019-01-04 stsp
2665 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2666 c2db6724 2019-01-04 stsp if (error != NULL)
2667 c2db6724 2019-01-04 stsp return error;
2668 c2db6724 2019-01-04 stsp
2669 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2670 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2671 c2db6724 2019-01-04 stsp
2672 c2db6724 2019-01-04 stsp return NULL;
2673 c2db6724 2019-01-04 stsp }
2674 c2db6724 2019-01-04 stsp
2675 a915003a 2019-02-05 stsp static void
2676 a915003a 2019-02-05 stsp init_curses(void)
2677 a915003a 2019-02-05 stsp {
2678 a915003a 2019-02-05 stsp initscr();
2679 a915003a 2019-02-05 stsp cbreak();
2680 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2681 a915003a 2019-02-05 stsp noecho();
2682 a915003a 2019-02-05 stsp nonl();
2683 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2684 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2685 a915003a 2019-02-05 stsp curs_set(0);
2686 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2687 6d17833f 2019-11-08 stsp start_color();
2688 6d17833f 2019-11-08 stsp use_default_colors();
2689 6d17833f 2019-11-08 stsp }
2690 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2691 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2692 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2693 a915003a 2019-02-05 stsp }
2694 a915003a 2019-02-05 stsp
2695 c2db6724 2019-01-04 stsp static const struct got_error *
2696 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2697 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2698 f135c941 2020-02-20 stsp {
2699 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2700 f135c941 2020-02-20 stsp
2701 f135c941 2020-02-20 stsp if (argc == 0) {
2702 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2703 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2704 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2705 f135c941 2020-02-20 stsp return NULL;
2706 f135c941 2020-02-20 stsp }
2707 f135c941 2020-02-20 stsp
2708 f135c941 2020-02-20 stsp if (worktree) {
2709 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2710 bfd61697 2020-11-14 stsp char *p;
2711 f135c941 2020-02-20 stsp
2712 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2713 f135c941 2020-02-20 stsp if (err)
2714 f135c941 2020-02-20 stsp return err;
2715 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2716 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2717 bfd61697 2020-11-14 stsp p) == -1) {
2718 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2719 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2720 f135c941 2020-02-20 stsp }
2721 f135c941 2020-02-20 stsp free(p);
2722 f135c941 2020-02-20 stsp } else
2723 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2724 f135c941 2020-02-20 stsp
2725 f135c941 2020-02-20 stsp return err;
2726 f135c941 2020-02-20 stsp }
2727 f135c941 2020-02-20 stsp
2728 f135c941 2020-02-20 stsp static const struct got_error *
2729 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2730 9f7d7167 2018-04-29 stsp {
2731 80ddbec8 2018-04-29 stsp const struct got_error *error;
2732 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2733 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2734 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2735 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2736 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2737 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2738 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2739 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2740 04cc582a 2018-08-01 stsp struct tog_view *view;
2741 80ddbec8 2018-04-29 stsp
2742 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2743 80ddbec8 2018-04-29 stsp switch (ch) {
2744 b672a97a 2020-01-27 stsp case 'b':
2745 b672a97a 2020-01-27 stsp log_branches = 1;
2746 b672a97a 2020-01-27 stsp break;
2747 80ddbec8 2018-04-29 stsp case 'c':
2748 80ddbec8 2018-04-29 stsp start_commit = optarg;
2749 80ddbec8 2018-04-29 stsp break;
2750 ecb28ae0 2018-07-16 stsp case 'r':
2751 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2752 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2753 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2754 9ba1d308 2019-10-21 stsp optarg);
2755 ecb28ae0 2018-07-16 stsp break;
2756 80ddbec8 2018-04-29 stsp default:
2757 17020d27 2019-03-07 stsp usage_log();
2758 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2759 80ddbec8 2018-04-29 stsp }
2760 80ddbec8 2018-04-29 stsp }
2761 80ddbec8 2018-04-29 stsp
2762 80ddbec8 2018-04-29 stsp argc -= optind;
2763 80ddbec8 2018-04-29 stsp argv += optind;
2764 80ddbec8 2018-04-29 stsp
2765 f135c941 2020-02-20 stsp if (argc > 1)
2766 f135c941 2020-02-20 stsp usage_log();
2767 963f97a1 2019-03-18 stsp
2768 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2769 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2770 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2771 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2772 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2773 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2774 c156c7a4 2020-12-18 stsp goto done;
2775 a1fbf39a 2019-08-11 stsp if (worktree)
2776 6962eb72 2020-02-20 stsp repo_path =
2777 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2778 a1fbf39a 2019-08-11 stsp else
2779 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2780 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2781 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2782 c156c7a4 2020-12-18 stsp goto done;
2783 c156c7a4 2020-12-18 stsp }
2784 ecb28ae0 2018-07-16 stsp }
2785 ecb28ae0 2018-07-16 stsp
2786 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2787 80ddbec8 2018-04-29 stsp if (error != NULL)
2788 ecb28ae0 2018-07-16 stsp goto done;
2789 80ddbec8 2018-04-29 stsp
2790 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2791 f135c941 2020-02-20 stsp repo, worktree);
2792 f135c941 2020-02-20 stsp if (error)
2793 f135c941 2020-02-20 stsp goto done;
2794 f135c941 2020-02-20 stsp
2795 f135c941 2020-02-20 stsp init_curses();
2796 f135c941 2020-02-20 stsp
2797 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2798 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2799 c02c541e 2019-03-29 stsp if (error)
2800 c02c541e 2019-03-29 stsp goto done;
2801 c02c541e 2019-03-29 stsp
2802 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2803 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2804 87670572 2020-12-26 naddy error = tog_load_refs(repo);
2805 87670572 2020-12-26 naddy if (error)
2806 87670572 2020-12-26 naddy goto done;
2807 87670572 2020-12-26 naddy }
2808 51a10b52 2020-12-26 stsp
2809 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2810 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2811 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2812 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2813 d8f38dc4 2020-12-05 stsp if (error)
2814 d8f38dc4 2020-12-05 stsp goto done;
2815 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2816 d8f38dc4 2020-12-05 stsp } else {
2817 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2818 d8f38dc4 2020-12-05 stsp if (error == NULL)
2819 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2820 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2821 d8f38dc4 2020-12-05 stsp goto done;
2822 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2823 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2824 d8f38dc4 2020-12-05 stsp if (error)
2825 d8f38dc4 2020-12-05 stsp goto done;
2826 d8f38dc4 2020-12-05 stsp }
2827 8b473291 2019-02-21 stsp
2828 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2829 04cc582a 2018-08-01 stsp if (view == NULL) {
2830 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2831 04cc582a 2018-08-01 stsp goto done;
2832 04cc582a 2018-08-01 stsp }
2833 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2834 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2835 ba4f502b 2018-08-04 stsp if (error)
2836 ba4f502b 2018-08-04 stsp goto done;
2837 2fc00ff4 2019-08-31 stsp if (worktree) {
2838 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2839 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2840 2fc00ff4 2019-08-31 stsp worktree = NULL;
2841 2fc00ff4 2019-08-31 stsp }
2842 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2843 ecb28ae0 2018-07-16 stsp done:
2844 f135c941 2020-02-20 stsp free(in_repo_path);
2845 ecb28ae0 2018-07-16 stsp free(repo_path);
2846 ecb28ae0 2018-07-16 stsp free(cwd);
2847 899d86c2 2018-05-10 stsp free(start_id);
2848 d8f38dc4 2020-12-05 stsp free(label);
2849 d8f38dc4 2020-12-05 stsp if (ref)
2850 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2851 1d0f4054 2021-06-17 stsp if (repo) {
2852 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2853 1d0f4054 2021-06-17 stsp if (error == NULL)
2854 1d0f4054 2021-06-17 stsp error = close_err;
2855 1d0f4054 2021-06-17 stsp }
2856 ec142235 2019-03-07 stsp if (worktree)
2857 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2858 51a10b52 2020-12-26 stsp tog_free_refs();
2859 80ddbec8 2018-04-29 stsp return error;
2860 9f7d7167 2018-04-29 stsp }
2861 9f7d7167 2018-04-29 stsp
2862 4ed7e80c 2018-05-20 stsp __dead static void
2863 9f7d7167 2018-04-29 stsp usage_diff(void)
2864 9f7d7167 2018-04-29 stsp {
2865 80ddbec8 2018-04-29 stsp endwin();
2866 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2867 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2868 9f7d7167 2018-04-29 stsp exit(1);
2869 b304db33 2018-05-20 stsp }
2870 b304db33 2018-05-20 stsp
2871 6d17833f 2019-11-08 stsp static int
2872 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2873 41605754 2020-11-12 stsp regmatch_t *regmatch)
2874 6d17833f 2019-11-08 stsp {
2875 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2876 6d17833f 2019-11-08 stsp }
2877 6d17833f 2019-11-08 stsp
2878 f26dddb7 2019-11-08 stsp struct tog_color *
2879 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2880 6d17833f 2019-11-08 stsp {
2881 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2882 6d17833f 2019-11-08 stsp
2883 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2884 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2885 6d17833f 2019-11-08 stsp return tc;
2886 6d17833f 2019-11-08 stsp }
2887 6d17833f 2019-11-08 stsp
2888 6d17833f 2019-11-08 stsp return NULL;
2889 6d17833f 2019-11-08 stsp }
2890 6d17833f 2019-11-08 stsp
2891 4ed7e80c 2018-05-20 stsp static const struct got_error *
2892 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2893 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2894 41605754 2020-11-12 stsp {
2895 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2896 41605754 2020-11-12 stsp wchar_t *wline;
2897 41605754 2020-11-12 stsp int width;
2898 41605754 2020-11-12 stsp char *s;
2899 41605754 2020-11-12 stsp
2900 41605754 2020-11-12 stsp *wtotal = 0;
2901 41605754 2020-11-12 stsp
2902 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2903 41605754 2020-11-12 stsp if (s == NULL)
2904 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2905 41605754 2020-11-12 stsp
2906 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2907 41605754 2020-11-12 stsp if (err) {
2908 41605754 2020-11-12 stsp free(s);
2909 41605754 2020-11-12 stsp return err;
2910 41605754 2020-11-12 stsp }
2911 41605754 2020-11-12 stsp waddwstr(window, wline);
2912 41605754 2020-11-12 stsp free(wline);
2913 41605754 2020-11-12 stsp free(s);
2914 41605754 2020-11-12 stsp wlimit -= width;
2915 41605754 2020-11-12 stsp *wtotal += width;
2916 41605754 2020-11-12 stsp
2917 41605754 2020-11-12 stsp if (wlimit > 0) {
2918 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2919 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2920 41605754 2020-11-12 stsp if (s == NULL) {
2921 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2922 41605754 2020-11-12 stsp free(s);
2923 41605754 2020-11-12 stsp return err;
2924 41605754 2020-11-12 stsp }
2925 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2926 41605754 2020-11-12 stsp if (err) {
2927 41605754 2020-11-12 stsp free(s);
2928 41605754 2020-11-12 stsp return err;
2929 41605754 2020-11-12 stsp }
2930 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2931 41605754 2020-11-12 stsp waddwstr(window, wline);
2932 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2933 41605754 2020-11-12 stsp free(wline);
2934 41605754 2020-11-12 stsp free(s);
2935 41605754 2020-11-12 stsp wlimit -= width;
2936 41605754 2020-11-12 stsp *wtotal += width;
2937 41605754 2020-11-12 stsp }
2938 41605754 2020-11-12 stsp
2939 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2940 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2941 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2942 41605754 2020-11-12 stsp if (err)
2943 41605754 2020-11-12 stsp return err;
2944 41605754 2020-11-12 stsp waddwstr(window, wline);
2945 41605754 2020-11-12 stsp free(wline);
2946 41605754 2020-11-12 stsp *wtotal += width;
2947 41605754 2020-11-12 stsp }
2948 41605754 2020-11-12 stsp
2949 41605754 2020-11-12 stsp return NULL;
2950 41605754 2020-11-12 stsp }
2951 41605754 2020-11-12 stsp
2952 41605754 2020-11-12 stsp static const struct got_error *
2953 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2954 26ed57b2 2018-05-19 stsp {
2955 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2956 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2957 61e69b96 2018-05-20 stsp const struct got_error *err;
2958 fe621944 2020-11-10 stsp int nprinted = 0;
2959 b304db33 2018-05-20 stsp char *line;
2960 826082fe 2020-12-10 stsp size_t linesize = 0;
2961 826082fe 2020-12-10 stsp ssize_t linelen;
2962 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2963 61e69b96 2018-05-20 stsp wchar_t *wline;
2964 e0b650dd 2018-05-20 stsp int width;
2965 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2966 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2967 fe621944 2020-11-10 stsp off_t line_offset;
2968 26ed57b2 2018-05-19 stsp
2969 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2970 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2971 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2972 fe621944 2020-11-10 stsp
2973 f7d12f7e 2018-08-01 stsp werase(view->window);
2974 a3404814 2018-09-02 stsp
2975 a3404814 2018-09-02 stsp if (header) {
2976 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2977 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2978 135a2da0 2020-11-11 stsp header) == -1)
2979 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2980 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2981 135a2da0 2020-11-11 stsp free(line);
2982 135a2da0 2020-11-11 stsp if (err)
2983 a3404814 2018-09-02 stsp return err;
2984 a3404814 2018-09-02 stsp
2985 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2986 a3404814 2018-09-02 stsp wstandout(view->window);
2987 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2988 e54cc94a 2020-11-11 stsp free(wline);
2989 e54cc94a 2020-11-11 stsp wline = NULL;
2990 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2991 a3404814 2018-09-02 stsp wstandend(view->window);
2992 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2993 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2994 26ed57b2 2018-05-19 stsp
2995 a3404814 2018-09-02 stsp if (max_lines <= 1)
2996 a3404814 2018-09-02 stsp return NULL;
2997 a3404814 2018-09-02 stsp max_lines--;
2998 a3404814 2018-09-02 stsp }
2999 a3404814 2018-09-02 stsp
3000 89f1a395 2020-12-01 naddy s->eof = 0;
3001 826082fe 2020-12-10 stsp line = NULL;
3002 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3003 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3004 826082fe 2020-12-10 stsp if (linelen == -1) {
3005 826082fe 2020-12-10 stsp if (feof(s->f)) {
3006 826082fe 2020-12-10 stsp s->eof = 1;
3007 826082fe 2020-12-10 stsp break;
3008 826082fe 2020-12-10 stsp }
3009 826082fe 2020-12-10 stsp free(line);
3010 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3011 61e69b96 2018-05-20 stsp }
3012 6d17833f 2019-11-08 stsp
3013 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3014 f26dddb7 2019-11-08 stsp if (tc)
3015 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3016 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3017 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3018 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3019 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3020 41605754 2020-11-12 stsp view->window, regmatch);
3021 41605754 2020-11-12 stsp if (err) {
3022 41605754 2020-11-12 stsp free(line);
3023 41605754 2020-11-12 stsp return err;
3024 41605754 2020-11-12 stsp }
3025 41605754 2020-11-12 stsp } else {
3026 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3027 41605754 2020-11-12 stsp if (err) {
3028 41605754 2020-11-12 stsp free(line);
3029 41605754 2020-11-12 stsp return err;
3030 41605754 2020-11-12 stsp }
3031 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3032 41605754 2020-11-12 stsp free(wline);
3033 41605754 2020-11-12 stsp wline = NULL;
3034 41605754 2020-11-12 stsp }
3035 f26dddb7 2019-11-08 stsp if (tc)
3036 6d17833f 2019-11-08 stsp wattr_off(view->window,
3037 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3038 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3039 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3040 fe621944 2020-11-10 stsp nprinted++;
3041 826082fe 2020-12-10 stsp }
3042 826082fe 2020-12-10 stsp free(line);
3043 fe621944 2020-11-10 stsp if (nprinted >= 1)
3044 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3045 89f1a395 2020-12-01 naddy (nprinted - 1);
3046 fe621944 2020-11-10 stsp else
3047 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3048 26ed57b2 2018-05-19 stsp
3049 1a57306a 2018-09-02 stsp view_vborder(view);
3050 c3e9aa98 2019-05-13 jcs
3051 89f1a395 2020-12-01 naddy if (s->eof) {
3052 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3053 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3054 c3e9aa98 2019-05-13 jcs nprinted++;
3055 c3e9aa98 2019-05-13 jcs }
3056 c3e9aa98 2019-05-13 jcs
3057 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3058 c3e9aa98 2019-05-13 jcs if (err) {
3059 c3e9aa98 2019-05-13 jcs return err;
3060 c3e9aa98 2019-05-13 jcs }
3061 26ed57b2 2018-05-19 stsp
3062 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3063 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3064 e54cc94a 2020-11-11 stsp free(wline);
3065 e54cc94a 2020-11-11 stsp wline = NULL;
3066 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3067 c3e9aa98 2019-05-13 jcs }
3068 c3e9aa98 2019-05-13 jcs
3069 26ed57b2 2018-05-19 stsp return NULL;
3070 abd2672a 2018-12-23 stsp }
3071 abd2672a 2018-12-23 stsp
3072 abd2672a 2018-12-23 stsp static char *
3073 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3074 abd2672a 2018-12-23 stsp {
3075 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3076 09867e48 2019-08-13 stsp char *p, *s;
3077 09867e48 2019-08-13 stsp
3078 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3079 09867e48 2019-08-13 stsp if (tm == NULL)
3080 09867e48 2019-08-13 stsp return NULL;
3081 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3082 09867e48 2019-08-13 stsp if (s == NULL)
3083 09867e48 2019-08-13 stsp return NULL;
3084 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3085 abd2672a 2018-12-23 stsp if (p)
3086 abd2672a 2018-12-23 stsp *p = '\0';
3087 abd2672a 2018-12-23 stsp return s;
3088 9f7d7167 2018-04-29 stsp }
3089 9f7d7167 2018-04-29 stsp
3090 4ed7e80c 2018-05-20 stsp static const struct got_error *
3091 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3092 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3093 0208f208 2020-05-05 stsp {
3094 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3095 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3096 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3097 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3098 0208f208 2020-05-05 stsp
3099 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3100 0208f208 2020-05-05 stsp if (qid != NULL) {
3101 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3102 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3103 0208f208 2020-05-05 stsp qid->id);
3104 0208f208 2020-05-05 stsp if (err)
3105 0208f208 2020-05-05 stsp return err;
3106 0208f208 2020-05-05 stsp
3107 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3108 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3109 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3110 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3111 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3112 aa8b5dd0 2021-08-01 stsp }
3113 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3114 0208f208 2020-05-05 stsp
3115 0208f208 2020-05-05 stsp }
3116 0208f208 2020-05-05 stsp
3117 0208f208 2020-05-05 stsp if (tree_id1) {
3118 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3119 0208f208 2020-05-05 stsp if (err)
3120 0208f208 2020-05-05 stsp goto done;
3121 0208f208 2020-05-05 stsp }
3122 0208f208 2020-05-05 stsp
3123 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3124 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3125 0208f208 2020-05-05 stsp if (err)
3126 0208f208 2020-05-05 stsp goto done;
3127 0208f208 2020-05-05 stsp
3128 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3129 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3130 0208f208 2020-05-05 stsp done:
3131 0208f208 2020-05-05 stsp if (tree1)
3132 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3133 0208f208 2020-05-05 stsp if (tree2)
3134 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3135 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3136 0208f208 2020-05-05 stsp return err;
3137 0208f208 2020-05-05 stsp }
3138 0208f208 2020-05-05 stsp
3139 0208f208 2020-05-05 stsp static const struct got_error *
3140 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3141 abd2672a 2018-12-23 stsp {
3142 fe621944 2020-11-10 stsp off_t *p;
3143 fe621944 2020-11-10 stsp
3144 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3145 fe621944 2020-11-10 stsp if (p == NULL)
3146 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3147 fe621944 2020-11-10 stsp *line_offsets = p;
3148 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3149 fe621944 2020-11-10 stsp (*nlines)++;
3150 fe621944 2020-11-10 stsp return NULL;
3151 fe621944 2020-11-10 stsp }
3152 fe621944 2020-11-10 stsp
3153 fe621944 2020-11-10 stsp static const struct got_error *
3154 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3155 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3156 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3157 fe621944 2020-11-10 stsp {
3158 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3159 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3160 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3161 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3162 45d799e2 2018-12-23 stsp time_t committer_time;
3163 45d799e2 2018-12-23 stsp const char *author, *committer;
3164 8b473291 2019-02-21 stsp char *refs_str = NULL;
3165 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3166 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3167 fe621944 2020-11-10 stsp off_t outoff = 0;
3168 fe621944 2020-11-10 stsp int n;
3169 abd2672a 2018-12-23 stsp
3170 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3171 0208f208 2020-05-05 stsp
3172 8b473291 2019-02-21 stsp if (refs) {
3173 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3174 8b473291 2019-02-21 stsp if (err)
3175 8b473291 2019-02-21 stsp return err;
3176 8b473291 2019-02-21 stsp }
3177 8b473291 2019-02-21 stsp
3178 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3179 abd2672a 2018-12-23 stsp if (err)
3180 abd2672a 2018-12-23 stsp return err;
3181 abd2672a 2018-12-23 stsp
3182 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3183 15a94983 2018-12-23 stsp if (err) {
3184 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3185 15a94983 2018-12-23 stsp goto done;
3186 15a94983 2018-12-23 stsp }
3187 abd2672a 2018-12-23 stsp
3188 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3189 fe621944 2020-11-10 stsp if (err)
3190 fe621944 2020-11-10 stsp goto done;
3191 fe621944 2020-11-10 stsp
3192 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3193 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3194 fe621944 2020-11-10 stsp if (n < 0) {
3195 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3196 abd2672a 2018-12-23 stsp goto done;
3197 abd2672a 2018-12-23 stsp }
3198 fe621944 2020-11-10 stsp outoff += n;
3199 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3200 fe621944 2020-11-10 stsp if (err)
3201 fe621944 2020-11-10 stsp goto done;
3202 fe621944 2020-11-10 stsp
3203 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3204 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3205 fe621944 2020-11-10 stsp if (n < 0) {
3206 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3207 abd2672a 2018-12-23 stsp goto done;
3208 abd2672a 2018-12-23 stsp }
3209 fe621944 2020-11-10 stsp outoff += n;
3210 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3211 fe621944 2020-11-10 stsp if (err)
3212 fe621944 2020-11-10 stsp goto done;
3213 fe621944 2020-11-10 stsp
3214 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3215 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3216 fe621944 2020-11-10 stsp if (datestr) {
3217 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3218 fe621944 2020-11-10 stsp if (n < 0) {
3219 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3220 fe621944 2020-11-10 stsp goto done;
3221 fe621944 2020-11-10 stsp }
3222 fe621944 2020-11-10 stsp outoff += n;
3223 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3224 fe621944 2020-11-10 stsp if (err)
3225 fe621944 2020-11-10 stsp goto done;
3226 abd2672a 2018-12-23 stsp }
3227 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3228 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3229 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3230 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3231 fe621944 2020-11-10 stsp if (n < 0) {
3232 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3233 fe621944 2020-11-10 stsp goto done;
3234 fe621944 2020-11-10 stsp }
3235 fe621944 2020-11-10 stsp outoff += n;
3236 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3237 fe621944 2020-11-10 stsp if (err)
3238 fe621944 2020-11-10 stsp goto done;
3239 abd2672a 2018-12-23 stsp }
3240 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3241 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3242 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3243 ac4dc263 2021-09-24 thomas int pn = 1;
3244 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3245 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3246 ac4dc263 2021-09-24 thomas err = got_object_id_str(&id_str, qid->id);
3247 ac4dc263 2021-09-24 thomas if (err)
3248 ac4dc263 2021-09-24 thomas goto done;
3249 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3250 ac4dc263 2021-09-24 thomas if (n < 0) {
3251 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3252 ac4dc263 2021-09-24 thomas goto done;
3253 ac4dc263 2021-09-24 thomas }
3254 ac4dc263 2021-09-24 thomas outoff += n;
3255 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3256 ac4dc263 2021-09-24 thomas if (err)
3257 ac4dc263 2021-09-24 thomas goto done;
3258 ac4dc263 2021-09-24 thomas free(id_str);
3259 ac4dc263 2021-09-24 thomas id_str = NULL;
3260 ac4dc263 2021-09-24 thomas }
3261 ac4dc263 2021-09-24 thomas }
3262 ac4dc263 2021-09-24 thomas
3263 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3264 5943eee2 2019-08-13 stsp if (err)
3265 5943eee2 2019-08-13 stsp goto done;
3266 fe621944 2020-11-10 stsp s = logmsg;
3267 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3268 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3269 fe621944 2020-11-10 stsp if (n < 0) {
3270 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3271 fe621944 2020-11-10 stsp goto done;
3272 fe621944 2020-11-10 stsp }
3273 fe621944 2020-11-10 stsp outoff += n;
3274 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3275 fe621944 2020-11-10 stsp if (err)
3276 fe621944 2020-11-10 stsp goto done;
3277 abd2672a 2018-12-23 stsp }
3278 fe621944 2020-11-10 stsp
3279 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3280 0208f208 2020-05-05 stsp if (err)
3281 0208f208 2020-05-05 stsp goto done;
3282 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3283 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3284 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3285 fe621944 2020-11-10 stsp if (n < 0) {
3286 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3287 fe621944 2020-11-10 stsp goto done;
3288 fe621944 2020-11-10 stsp }
3289 fe621944 2020-11-10 stsp outoff += n;
3290 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3291 fe621944 2020-11-10 stsp if (err)
3292 fe621944 2020-11-10 stsp goto done;
3293 0208f208 2020-05-05 stsp free((char *)pe->path);
3294 0208f208 2020-05-05 stsp free(pe->data);
3295 0208f208 2020-05-05 stsp }
3296 fe621944 2020-11-10 stsp
3297 0208f208 2020-05-05 stsp fputc('\n', outfile);
3298 fe621944 2020-11-10 stsp outoff++;
3299 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3300 abd2672a 2018-12-23 stsp done:
3301 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3302 abd2672a 2018-12-23 stsp free(id_str);
3303 5943eee2 2019-08-13 stsp free(logmsg);
3304 8b473291 2019-02-21 stsp free(refs_str);
3305 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3306 7510f233 2020-08-09 stsp if (err) {
3307 ae6a6978 2020-08-09 stsp free(*line_offsets);
3308 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3309 ae6a6978 2020-08-09 stsp *nlines = 0;
3310 7510f233 2020-08-09 stsp }
3311 fe621944 2020-11-10 stsp return err;
3312 abd2672a 2018-12-23 stsp }
3313 abd2672a 2018-12-23 stsp
3314 abd2672a 2018-12-23 stsp static const struct got_error *
3315 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3316 26ed57b2 2018-05-19 stsp {
3317 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3318 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3319 15a94983 2018-12-23 stsp int obj_type;
3320 fe621944 2020-11-10 stsp
3321 fe621944 2020-11-10 stsp free(s->line_offsets);
3322 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3323 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3324 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3325 fe621944 2020-11-10 stsp s->nlines = 0;
3326 26ed57b2 2018-05-19 stsp
3327 511a516b 2018-05-19 stsp f = got_opentemp();
3328 48ae06ee 2018-10-18 stsp if (f == NULL) {
3329 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3330 48ae06ee 2018-10-18 stsp goto done;
3331 48ae06ee 2018-10-18 stsp }
3332 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3333 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3334 fb43ecf1 2019-02-11 stsp goto done;
3335 fb43ecf1 2019-02-11 stsp }
3336 48ae06ee 2018-10-18 stsp s->f = f;
3337 26ed57b2 2018-05-19 stsp
3338 15a94983 2018-12-23 stsp if (s->id1)
3339 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3340 15a94983 2018-12-23 stsp else
3341 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3342 15a94983 2018-12-23 stsp if (err)
3343 15a94983 2018-12-23 stsp goto done;
3344 15a94983 2018-12-23 stsp
3345 15a94983 2018-12-23 stsp switch (obj_type) {
3346 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3347 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3348 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3349 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3350 26ed57b2 2018-05-19 stsp break;
3351 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3352 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3353 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3354 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3355 26ed57b2 2018-05-19 stsp break;
3356 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3357 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3358 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3359 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3360 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3361 abd2672a 2018-12-23 stsp
3362 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3363 abd2672a 2018-12-23 stsp if (err)
3364 3ffacbe1 2020-02-02 tracey goto done;
3365 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3366 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3367 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3368 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3369 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3370 f44b1f58 2020-02-02 tracey if (err)
3371 f44b1f58 2020-02-02 tracey goto done;
3372 f44b1f58 2020-02-02 tracey } else {
3373 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3374 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3375 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3376 fe621944 2020-11-10 stsp err = write_commit_info(
3377 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3378 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3379 f44b1f58 2020-02-02 tracey if (err)
3380 f44b1f58 2020-02-02 tracey goto done;
3381 f5404e4e 2020-02-02 tracey break;
3382 15a087fe 2019-02-21 stsp }
3383 abd2672a 2018-12-23 stsp }
3384 abd2672a 2018-12-23 stsp }
3385 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3386 abd2672a 2018-12-23 stsp
3387 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3388 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3389 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3390 26ed57b2 2018-05-19 stsp break;
3391 abd2672a 2018-12-23 stsp }
3392 26ed57b2 2018-05-19 stsp default:
3393 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3394 48ae06ee 2018-10-18 stsp break;
3395 26ed57b2 2018-05-19 stsp }
3396 f44b1f58 2020-02-02 tracey if (err)
3397 f44b1f58 2020-02-02 tracey goto done;
3398 48ae06ee 2018-10-18 stsp done:
3399 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3400 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3401 48ae06ee 2018-10-18 stsp return err;
3402 48ae06ee 2018-10-18 stsp }
3403 26ed57b2 2018-05-19 stsp
3404 f5215bb9 2019-02-22 stsp static void
3405 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3406 f5215bb9 2019-02-22 stsp {
3407 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3408 f5215bb9 2019-02-22 stsp update_panels();
3409 f5215bb9 2019-02-22 stsp doupdate();
3410 f44b1f58 2020-02-02 tracey }
3411 f44b1f58 2020-02-02 tracey
3412 f44b1f58 2020-02-02 tracey static const struct got_error *
3413 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3414 f44b1f58 2020-02-02 tracey {
3415 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3416 f44b1f58 2020-02-02 tracey
3417 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3418 f44b1f58 2020-02-02 tracey return NULL;
3419 f44b1f58 2020-02-02 tracey }
3420 f44b1f58 2020-02-02 tracey
3421 f44b1f58 2020-02-02 tracey static const struct got_error *
3422 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3423 f44b1f58 2020-02-02 tracey {
3424 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3425 f44b1f58 2020-02-02 tracey int lineno;
3426 826082fe 2020-12-10 stsp char *line = NULL;
3427 826082fe 2020-12-10 stsp size_t linesize = 0;
3428 826082fe 2020-12-10 stsp ssize_t linelen;
3429 f44b1f58 2020-02-02 tracey
3430 f44b1f58 2020-02-02 tracey if (!view->searching) {
3431 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3432 f44b1f58 2020-02-02 tracey return NULL;
3433 f44b1f58 2020-02-02 tracey }
3434 f44b1f58 2020-02-02 tracey
3435 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3436 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3437 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3438 f44b1f58 2020-02-02 tracey else
3439 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3440 f44b1f58 2020-02-02 tracey } else {
3441 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3442 f44b1f58 2020-02-02 tracey lineno = 1;
3443 f44b1f58 2020-02-02 tracey else
3444 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3445 f44b1f58 2020-02-02 tracey }
3446 f44b1f58 2020-02-02 tracey
3447 f44b1f58 2020-02-02 tracey while (1) {
3448 f44b1f58 2020-02-02 tracey off_t offset;
3449 f44b1f58 2020-02-02 tracey
3450 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3451 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3452 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3453 f44b1f58 2020-02-02 tracey break;
3454 f44b1f58 2020-02-02 tracey }
3455 f44b1f58 2020-02-02 tracey
3456 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3457 f44b1f58 2020-02-02 tracey lineno = 1;
3458 f44b1f58 2020-02-02 tracey else
3459 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3460 f44b1f58 2020-02-02 tracey }
3461 f44b1f58 2020-02-02 tracey
3462 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3463 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3464 f44b1f58 2020-02-02 tracey free(line);
3465 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3466 f44b1f58 2020-02-02 tracey }
3467 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3468 826082fe 2020-12-10 stsp if (linelen != -1 &&
3469 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3470 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3471 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3472 f44b1f58 2020-02-02 tracey break;
3473 f44b1f58 2020-02-02 tracey }
3474 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3475 f44b1f58 2020-02-02 tracey lineno++;
3476 f44b1f58 2020-02-02 tracey else
3477 f44b1f58 2020-02-02 tracey lineno--;
3478 f44b1f58 2020-02-02 tracey }
3479 826082fe 2020-12-10 stsp free(line);
3480 f44b1f58 2020-02-02 tracey
3481 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3482 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3483 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3484 f44b1f58 2020-02-02 tracey }
3485 f44b1f58 2020-02-02 tracey
3486 f44b1f58 2020-02-02 tracey return NULL;
3487 f5215bb9 2019-02-22 stsp }
3488 f5215bb9 2019-02-22 stsp
3489 48ae06ee 2018-10-18 stsp static const struct got_error *
3490 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3491 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3492 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3493 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3494 48ae06ee 2018-10-18 stsp {
3495 48ae06ee 2018-10-18 stsp const struct got_error *err;
3496 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3497 5dc9f4bc 2018-08-04 stsp
3498 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3499 15a94983 2018-12-23 stsp int type1, type2;
3500 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3501 15a94983 2018-12-23 stsp if (err)
3502 15a94983 2018-12-23 stsp return err;
3503 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3504 15a94983 2018-12-23 stsp if (err)
3505 15a94983 2018-12-23 stsp return err;
3506 15a94983 2018-12-23 stsp
3507 15a94983 2018-12-23 stsp if (type1 != type2)
3508 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3509 15a94983 2018-12-23 stsp }
3510 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3511 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3512 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3513 f44b1f58 2020-02-02 tracey s->repo = repo;
3514 f44b1f58 2020-02-02 tracey s->id1 = id1;
3515 f44b1f58 2020-02-02 tracey s->id2 = id2;
3516 3dbaef42 2020-11-24 stsp s->label1 = label1;
3517 3dbaef42 2020-11-24 stsp s->label2 = label2;
3518 48ae06ee 2018-10-18 stsp
3519 15a94983 2018-12-23 stsp if (id1) {
3520 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3521 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3522 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3523 48ae06ee 2018-10-18 stsp } else
3524 5465d566 2020-02-01 tracey s->id1 = NULL;
3525 48ae06ee 2018-10-18 stsp
3526 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3527 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3528 5465d566 2020-02-01 tracey free(s->id1);
3529 5465d566 2020-02-01 tracey s->id1 = NULL;
3530 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3531 48ae06ee 2018-10-18 stsp }
3532 5465d566 2020-02-01 tracey s->f = NULL;
3533 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3534 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3535 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3536 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3537 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3538 5465d566 2020-02-01 tracey s->log_view = log_view;
3539 5465d566 2020-02-01 tracey s->repo = repo;
3540 6d17833f 2019-11-08 stsp
3541 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3542 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3543 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3544 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3545 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3546 6d17833f 2019-11-08 stsp if (err)
3547 6d17833f 2019-11-08 stsp return err;
3548 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3549 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3550 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3551 6d17833f 2019-11-08 stsp if (err) {
3552 5465d566 2020-02-01 tracey free_colors(&s->colors);
3553 6d17833f 2019-11-08 stsp return err;
3554 6d17833f 2019-11-08 stsp }
3555 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3556 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3557 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3558 6d17833f 2019-11-08 stsp if (err) {
3559 5465d566 2020-02-01 tracey free_colors(&s->colors);
3560 6d17833f 2019-11-08 stsp return err;
3561 6d17833f 2019-11-08 stsp }
3562 6d17833f 2019-11-08 stsp
3563 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3564 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3565 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3566 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3567 6d17833f 2019-11-08 stsp if (err) {
3568 5465d566 2020-02-01 tracey free_colors(&s->colors);
3569 6d17833f 2019-11-08 stsp return err;
3570 6d17833f 2019-11-08 stsp }
3571 11b20872 2019-11-08 stsp
3572 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3573 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3574 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3575 11b20872 2019-11-08 stsp if (err) {
3576 5465d566 2020-02-01 tracey free_colors(&s->colors);
3577 11b20872 2019-11-08 stsp return err;
3578 11b20872 2019-11-08 stsp }
3579 11b20872 2019-11-08 stsp
3580 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3581 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3582 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3583 11b20872 2019-11-08 stsp if (err) {
3584 5465d566 2020-02-01 tracey free_colors(&s->colors);
3585 11b20872 2019-11-08 stsp return err;
3586 11b20872 2019-11-08 stsp }
3587 6d17833f 2019-11-08 stsp }
3588 5dc9f4bc 2018-08-04 stsp
3589 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3590 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3591 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3592 f5215bb9 2019-02-22 stsp
3593 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3594 fe621944 2020-11-10 stsp s->nlines = 0;
3595 5465d566 2020-02-01 tracey err = create_diff(s);
3596 48ae06ee 2018-10-18 stsp if (err) {
3597 5465d566 2020-02-01 tracey free(s->id1);
3598 5465d566 2020-02-01 tracey s->id1 = NULL;
3599 5465d566 2020-02-01 tracey free(s->id2);
3600 5465d566 2020-02-01 tracey s->id2 = NULL;
3601 78756c87 2020-11-24 stsp free_colors(&s->colors);
3602 48ae06ee 2018-10-18 stsp return err;
3603 48ae06ee 2018-10-18 stsp }
3604 48ae06ee 2018-10-18 stsp
3605 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3606 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3607 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3608 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3609 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3610 e5a0f69f 2018-08-18 stsp
3611 5dc9f4bc 2018-08-04 stsp return NULL;
3612 5dc9f4bc 2018-08-04 stsp }
3613 5dc9f4bc 2018-08-04 stsp
3614 e5a0f69f 2018-08-18 stsp static const struct got_error *
3615 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3616 5dc9f4bc 2018-08-04 stsp {
3617 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3618 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3619 5465d566 2020-02-01 tracey
3620 5465d566 2020-02-01 tracey free(s->id1);
3621 5465d566 2020-02-01 tracey s->id1 = NULL;
3622 5465d566 2020-02-01 tracey free(s->id2);
3623 5465d566 2020-02-01 tracey s->id2 = NULL;
3624 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3625 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3626 5465d566 2020-02-01 tracey free_colors(&s->colors);
3627 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3628 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3629 fe621944 2020-11-10 stsp s->nlines = 0;
3630 e5a0f69f 2018-08-18 stsp return err;
3631 5dc9f4bc 2018-08-04 stsp }
3632 5dc9f4bc 2018-08-04 stsp
3633 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3634 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3635 5dc9f4bc 2018-08-04 stsp {
3636 a3404814 2018-09-02 stsp const struct got_error *err;
3637 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3638 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3639 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3640 a3404814 2018-09-02 stsp
3641 a3404814 2018-09-02 stsp if (s->id1) {
3642 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3643 a3404814 2018-09-02 stsp if (err)
3644 a3404814 2018-09-02 stsp return err;
3645 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3646 3dbaef42 2020-11-24 stsp } else
3647 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3648 3dbaef42 2020-11-24 stsp
3649 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3650 a3404814 2018-09-02 stsp if (err)
3651 a3404814 2018-09-02 stsp return err;
3652 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3653 26ed57b2 2018-05-19 stsp
3654 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3655 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3656 a3404814 2018-09-02 stsp free(id_str1);
3657 a3404814 2018-09-02 stsp free(id_str2);
3658 a3404814 2018-09-02 stsp return err;
3659 a3404814 2018-09-02 stsp }
3660 a3404814 2018-09-02 stsp free(id_str1);
3661 a3404814 2018-09-02 stsp free(id_str2);
3662 a3404814 2018-09-02 stsp
3663 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3664 267bb3b8 2021-08-01 stsp free(header);
3665 267bb3b8 2021-08-01 stsp return err;
3666 15a087fe 2019-02-21 stsp }
3667 15a087fe 2019-02-21 stsp
3668 15a087fe 2019-02-21 stsp static const struct got_error *
3669 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3670 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3671 15a087fe 2019-02-21 stsp {
3672 d7a04538 2019-02-21 stsp const struct got_error *err;
3673 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3674 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3675 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3676 15a087fe 2019-02-21 stsp
3677 15a087fe 2019-02-21 stsp free(s->id2);
3678 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3679 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3680 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3681 15a087fe 2019-02-21 stsp
3682 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3683 d7a04538 2019-02-21 stsp if (err)
3684 d7a04538 2019-02-21 stsp return err;
3685 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3686 15a087fe 2019-02-21 stsp free(s->id1);
3687 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3688 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3689 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3690 15a087fe 2019-02-21 stsp return NULL;
3691 0cf4efb1 2018-09-29 stsp }
3692 0cf4efb1 2018-09-29 stsp
3693 0cf4efb1 2018-09-29 stsp static const struct got_error *
3694 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3695 e5a0f69f 2018-08-18 stsp {
3696 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3697 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3698 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3699 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3700 826082fe 2020-12-10 stsp char *line = NULL;
3701 826082fe 2020-12-10 stsp size_t linesize = 0;
3702 826082fe 2020-12-10 stsp ssize_t linelen;
3703 e5a0f69f 2018-08-18 stsp int i;
3704 e5a0f69f 2018-08-18 stsp
3705 e5a0f69f 2018-08-18 stsp switch (ch) {
3706 64453f7e 2020-11-21 stsp case 'a':
3707 3dbaef42 2020-11-24 stsp case 'w':
3708 3dbaef42 2020-11-24 stsp if (ch == 'a')
3709 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3710 3dbaef42 2020-11-24 stsp if (ch == 'w')
3711 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3712 64453f7e 2020-11-21 stsp wclear(view->window);
3713 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3714 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3715 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3716 64453f7e 2020-11-21 stsp err = create_diff(s);
3717 912a3f79 2021-08-30 j break;
3718 912a3f79 2021-08-30 j case 'g':
3719 912a3f79 2021-08-30 j case KEY_HOME:
3720 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3721 64453f7e 2020-11-21 stsp break;
3722 912a3f79 2021-08-30 j case 'G':
3723 912a3f79 2021-08-30 j case KEY_END:
3724 912a3f79 2021-08-30 j if (s->eof)
3725 912a3f79 2021-08-30 j break;
3726 912a3f79 2021-08-30 j
3727 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3728 912a3f79 2021-08-30 j s->eof = 1;
3729 912a3f79 2021-08-30 j break;
3730 1e37a5c2 2019-05-12 jcs case 'k':
3731 1e37a5c2 2019-05-12 jcs case KEY_UP:
3732 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3733 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3734 1e37a5c2 2019-05-12 jcs break;
3735 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3736 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3737 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3738 26ed57b2 2018-05-19 stsp break;
3739 1e37a5c2 2019-05-12 jcs i = 0;
3740 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3741 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3742 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3743 1e37a5c2 2019-05-12 jcs break;
3744 1e37a5c2 2019-05-12 jcs case 'j':
3745 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3746 1e37a5c2 2019-05-12 jcs if (!s->eof)
3747 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3748 1e37a5c2 2019-05-12 jcs break;
3749 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3750 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3751 1e37a5c2 2019-05-12 jcs case ' ':
3752 00ba99a7 2019-05-12 jcs if (s->eof)
3753 1e37a5c2 2019-05-12 jcs break;
3754 1e37a5c2 2019-05-12 jcs i = 0;
3755 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3756 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3757 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3758 826082fe 2020-12-10 stsp if (linelen == -1) {
3759 826082fe 2020-12-10 stsp if (feof(s->f)) {
3760 826082fe 2020-12-10 stsp s->eof = 1;
3761 826082fe 2020-12-10 stsp } else
3762 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3763 34bc9ec9 2019-02-22 stsp break;
3764 826082fe 2020-12-10 stsp }
3765 1e37a5c2 2019-05-12 jcs }
3766 826082fe 2020-12-10 stsp free(line);
3767 1e37a5c2 2019-05-12 jcs break;
3768 1e37a5c2 2019-05-12 jcs case '[':
3769 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3770 1e37a5c2 2019-05-12 jcs s->diff_context--;
3771 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3772 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3773 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3774 27829c9e 2020-11-21 stsp s->nlines) {
3775 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3776 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3777 27829c9e 2020-11-21 stsp }
3778 1e37a5c2 2019-05-12 jcs }
3779 1e37a5c2 2019-05-12 jcs break;
3780 1e37a5c2 2019-05-12 jcs case ']':
3781 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3782 1e37a5c2 2019-05-12 jcs s->diff_context++;
3783 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3784 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3785 1e37a5c2 2019-05-12 jcs }
3786 1e37a5c2 2019-05-12 jcs break;
3787 1e37a5c2 2019-05-12 jcs case '<':
3788 1e37a5c2 2019-05-12 jcs case ',':
3789 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3790 48ae06ee 2018-10-18 stsp break;
3791 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3792 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3793 6524637e 2019-02-21 stsp
3794 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3795 1e37a5c2 2019-05-12 jcs if (err)
3796 1e37a5c2 2019-05-12 jcs break;
3797 15a087fe 2019-02-21 stsp
3798 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3799 5a8b5076 2020-12-05 stsp break;
3800 5a8b5076 2020-12-05 stsp
3801 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3802 1e37a5c2 2019-05-12 jcs if (err)
3803 1e37a5c2 2019-05-12 jcs break;
3804 15a087fe 2019-02-21 stsp
3805 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3806 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3807 15a087fe 2019-02-21 stsp
3808 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3809 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3810 1e37a5c2 2019-05-12 jcs break;
3811 1e37a5c2 2019-05-12 jcs case '>':
3812 1e37a5c2 2019-05-12 jcs case '.':
3813 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3814 15a087fe 2019-02-21 stsp break;
3815 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3816 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3817 5e224a3e 2019-02-22 stsp
3818 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3819 1e37a5c2 2019-05-12 jcs if (err)
3820 5a8b5076 2020-12-05 stsp break;
3821 5a8b5076 2020-12-05 stsp
3822 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3823 1e37a5c2 2019-05-12 jcs break;
3824 15a087fe 2019-02-21 stsp
3825 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3826 1e37a5c2 2019-05-12 jcs if (err)
3827 1e37a5c2 2019-05-12 jcs break;
3828 15a087fe 2019-02-21 stsp
3829 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3830 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3831 1e37a5c2 2019-05-12 jcs
3832 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3833 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3834 1e37a5c2 2019-05-12 jcs break;
3835 1e37a5c2 2019-05-12 jcs default:
3836 1e37a5c2 2019-05-12 jcs break;
3837 26ed57b2 2018-05-19 stsp }
3838 e5a0f69f 2018-08-18 stsp
3839 bcbd79e2 2018-08-19 stsp return err;
3840 26ed57b2 2018-05-19 stsp }
3841 26ed57b2 2018-05-19 stsp
3842 4ed7e80c 2018-05-20 stsp static const struct got_error *
3843 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3844 9f7d7167 2018-04-29 stsp {
3845 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3846 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3847 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3848 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3849 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3850 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3851 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3852 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3853 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3854 3dbaef42 2020-11-24 stsp const char *errstr;
3855 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3856 70ac5f84 2019-03-28 stsp
3857 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3858 26ed57b2 2018-05-19 stsp switch (ch) {
3859 64453f7e 2020-11-21 stsp case 'a':
3860 64453f7e 2020-11-21 stsp force_text_diff = 1;
3861 3dbaef42 2020-11-24 stsp break;
3862 3dbaef42 2020-11-24 stsp case 'C':
3863 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3864 3dbaef42 2020-11-24 stsp &errstr);
3865 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3866 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3867 64453f7e 2020-11-21 stsp break;
3868 09b5bff8 2020-02-23 naddy case 'r':
3869 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3870 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3871 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3872 09b5bff8 2020-02-23 naddy optarg);
3873 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3874 09b5bff8 2020-02-23 naddy break;
3875 3dbaef42 2020-11-24 stsp case 'w':
3876 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3877 3dbaef42 2020-11-24 stsp break;
3878 26ed57b2 2018-05-19 stsp default:
3879 17020d27 2019-03-07 stsp usage_diff();
3880 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3881 26ed57b2 2018-05-19 stsp }
3882 26ed57b2 2018-05-19 stsp }
3883 26ed57b2 2018-05-19 stsp
3884 26ed57b2 2018-05-19 stsp argc -= optind;
3885 26ed57b2 2018-05-19 stsp argv += optind;
3886 26ed57b2 2018-05-19 stsp
3887 26ed57b2 2018-05-19 stsp if (argc == 0) {
3888 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3889 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3890 15a94983 2018-12-23 stsp id_str1 = argv[0];
3891 15a94983 2018-12-23 stsp id_str2 = argv[1];
3892 26ed57b2 2018-05-19 stsp } else
3893 26ed57b2 2018-05-19 stsp usage_diff();
3894 eb6600df 2019-01-04 stsp
3895 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3896 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3897 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3898 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3899 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3900 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3901 c156c7a4 2020-12-18 stsp goto done;
3902 a273ac94 2020-02-23 naddy if (worktree)
3903 a273ac94 2020-02-23 naddy repo_path =
3904 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3905 a273ac94 2020-02-23 naddy else
3906 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3907 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3908 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3909 c156c7a4 2020-12-18 stsp goto done;
3910 c156c7a4 2020-12-18 stsp }
3911 a273ac94 2020-02-23 naddy }
3912 a273ac94 2020-02-23 naddy
3913 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3914 eb6600df 2019-01-04 stsp if (error)
3915 eb6600df 2019-01-04 stsp goto done;
3916 26ed57b2 2018-05-19 stsp
3917 a273ac94 2020-02-23 naddy init_curses();
3918 a273ac94 2020-02-23 naddy
3919 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3920 51a10b52 2020-12-26 stsp if (error)
3921 51a10b52 2020-12-26 stsp goto done;
3922 51a10b52 2020-12-26 stsp
3923 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
3924 26ed57b2 2018-05-19 stsp if (error)
3925 26ed57b2 2018-05-19 stsp goto done;
3926 26ed57b2 2018-05-19 stsp
3927 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3928 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3929 26ed57b2 2018-05-19 stsp if (error)
3930 26ed57b2 2018-05-19 stsp goto done;
3931 26ed57b2 2018-05-19 stsp
3932 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3933 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3934 26ed57b2 2018-05-19 stsp if (error)
3935 26ed57b2 2018-05-19 stsp goto done;
3936 26ed57b2 2018-05-19 stsp
3937 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3938 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3939 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3940 ea5e7bb5 2018-08-01 stsp goto done;
3941 ea5e7bb5 2018-08-01 stsp }
3942 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3943 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3944 5dc9f4bc 2018-08-04 stsp if (error)
3945 5dc9f4bc 2018-08-04 stsp goto done;
3946 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3947 26ed57b2 2018-05-19 stsp done:
3948 3dbaef42 2020-11-24 stsp free(label1);
3949 3dbaef42 2020-11-24 stsp free(label2);
3950 c02c541e 2019-03-29 stsp free(repo_path);
3951 a273ac94 2020-02-23 naddy free(cwd);
3952 1d0f4054 2021-06-17 stsp if (repo) {
3953 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3954 1d0f4054 2021-06-17 stsp if (error == NULL)
3955 1d0f4054 2021-06-17 stsp error = close_err;
3956 1d0f4054 2021-06-17 stsp }
3957 a273ac94 2020-02-23 naddy if (worktree)
3958 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3959 51a10b52 2020-12-26 stsp tog_free_refs();
3960 26ed57b2 2018-05-19 stsp return error;
3961 9f7d7167 2018-04-29 stsp }
3962 9f7d7167 2018-04-29 stsp
3963 4ed7e80c 2018-05-20 stsp __dead static void
3964 9f7d7167 2018-04-29 stsp usage_blame(void)
3965 9f7d7167 2018-04-29 stsp {
3966 80ddbec8 2018-04-29 stsp endwin();
3967 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3968 9f7d7167 2018-04-29 stsp getprogname());
3969 9f7d7167 2018-04-29 stsp exit(1);
3970 9f7d7167 2018-04-29 stsp }
3971 84451b3e 2018-07-10 stsp
3972 84451b3e 2018-07-10 stsp struct tog_blame_line {
3973 84451b3e 2018-07-10 stsp int annotated;
3974 84451b3e 2018-07-10 stsp struct got_object_id *id;
3975 84451b3e 2018-07-10 stsp };
3976 9f7d7167 2018-04-29 stsp
3977 4ed7e80c 2018-05-20 stsp static const struct got_error *
3978 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3979 84451b3e 2018-07-10 stsp {
3980 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3981 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3982 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3983 84451b3e 2018-07-10 stsp const struct got_error *err;
3984 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3985 826082fe 2020-12-10 stsp char *line = NULL;
3986 826082fe 2020-12-10 stsp size_t linesize = 0;
3987 826082fe 2020-12-10 stsp ssize_t linelen;
3988 84451b3e 2018-07-10 stsp wchar_t *wline;
3989 27a741e5 2019-09-11 stsp int width;
3990 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3991 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3992 ab089a2a 2018-07-12 stsp char *id_str;
3993 11b20872 2019-11-08 stsp struct tog_color *tc;
3994 ab089a2a 2018-07-12 stsp
3995 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3996 ab089a2a 2018-07-12 stsp if (err)
3997 ab089a2a 2018-07-12 stsp return err;
3998 84451b3e 2018-07-10 stsp
3999 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4000 f7d12f7e 2018-08-01 stsp werase(view->window);
4001 84451b3e 2018-07-10 stsp
4002 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4003 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4004 ab089a2a 2018-07-12 stsp free(id_str);
4005 ab089a2a 2018-07-12 stsp return err;
4006 ab089a2a 2018-07-12 stsp }
4007 ab089a2a 2018-07-12 stsp
4008 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4009 ab089a2a 2018-07-12 stsp free(line);
4010 2550e4c3 2018-07-13 stsp line = NULL;
4011 1cae65b4 2019-09-22 stsp if (err)
4012 1cae65b4 2019-09-22 stsp return err;
4013 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4014 a3404814 2018-09-02 stsp wstandout(view->window);
4015 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4016 11b20872 2019-11-08 stsp if (tc)
4017 11b20872 2019-11-08 stsp wattr_on(view->window,
4018 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4019 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4020 11b20872 2019-11-08 stsp if (tc)
4021 11b20872 2019-11-08 stsp wattr_off(view->window,
4022 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4023 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4024 a3404814 2018-09-02 stsp wstandend(view->window);
4025 2550e4c3 2018-07-13 stsp free(wline);
4026 2550e4c3 2018-07-13 stsp wline = NULL;
4027 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4028 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4029 ab089a2a 2018-07-12 stsp
4030 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4031 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4032 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4033 ab089a2a 2018-07-12 stsp free(id_str);
4034 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4035 ab089a2a 2018-07-12 stsp }
4036 ab089a2a 2018-07-12 stsp free(id_str);
4037 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4038 3f60a8ef 2018-07-10 stsp free(line);
4039 2550e4c3 2018-07-13 stsp line = NULL;
4040 3f60a8ef 2018-07-10 stsp if (err)
4041 3f60a8ef 2018-07-10 stsp return err;
4042 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4043 2550e4c3 2018-07-13 stsp free(wline);
4044 2550e4c3 2018-07-13 stsp wline = NULL;
4045 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4046 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4047 3f60a8ef 2018-07-10 stsp
4048 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4049 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4050 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4051 826082fe 2020-12-10 stsp if (linelen == -1) {
4052 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4053 826082fe 2020-12-10 stsp s->eof = 1;
4054 826082fe 2020-12-10 stsp break;
4055 826082fe 2020-12-10 stsp }
4056 84451b3e 2018-07-10 stsp free(line);
4057 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4058 84451b3e 2018-07-10 stsp }
4059 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4060 826082fe 2020-12-10 stsp continue;
4061 84451b3e 2018-07-10 stsp
4062 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4063 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4064 b700b5d6 2018-07-10 stsp
4065 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4066 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4067 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4068 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4069 27a741e5 2019-09-11 stsp !(view->focussed &&
4070 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4071 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4072 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4073 8d0fe45a 2019-08-12 stsp char *id_str;
4074 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4075 8d0fe45a 2019-08-12 stsp if (err) {
4076 8d0fe45a 2019-08-12 stsp free(line);
4077 8d0fe45a 2019-08-12 stsp return err;
4078 8d0fe45a 2019-08-12 stsp }
4079 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4080 11b20872 2019-11-08 stsp if (tc)
4081 11b20872 2019-11-08 stsp wattr_on(view->window,
4082 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4083 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4084 11b20872 2019-11-08 stsp if (tc)
4085 11b20872 2019-11-08 stsp wattr_off(view->window,
4086 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4087 8d0fe45a 2019-08-12 stsp free(id_str);
4088 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4089 8d0fe45a 2019-08-12 stsp } else {
4090 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4091 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4092 84451b3e 2018-07-10 stsp }
4093 ee41ec32 2018-07-10 stsp } else {
4094 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4095 ee41ec32 2018-07-10 stsp prev_id = NULL;
4096 ee41ec32 2018-07-10 stsp }
4097 84451b3e 2018-07-10 stsp
4098 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4099 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4100 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4101 27a741e5 2019-09-11 stsp
4102 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4103 41605754 2020-11-12 stsp width = 9;
4104 41605754 2020-11-12 stsp wline = wcsdup(L"");
4105 41605754 2020-11-12 stsp if (wline == NULL) {
4106 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4107 41605754 2020-11-12 stsp free(line);
4108 41605754 2020-11-12 stsp return err;
4109 41605754 2020-11-12 stsp }
4110 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4111 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4112 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4113 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4114 41605754 2020-11-12 stsp view->window, regmatch);
4115 41605754 2020-11-12 stsp if (err) {
4116 41605754 2020-11-12 stsp free(line);
4117 41605754 2020-11-12 stsp return err;
4118 41605754 2020-11-12 stsp }
4119 41605754 2020-11-12 stsp width += 9;
4120 41605754 2020-11-12 stsp } else {
4121 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4122 41605754 2020-11-12 stsp view->ncols - 9, 9);
4123 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4124 41605754 2020-11-12 stsp free(wline);
4125 41605754 2020-11-12 stsp wline = NULL;
4126 41605754 2020-11-12 stsp width += 9;
4127 41605754 2020-11-12 stsp }
4128 41605754 2020-11-12 stsp
4129 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4130 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4131 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4132 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4133 84451b3e 2018-07-10 stsp }
4134 826082fe 2020-12-10 stsp free(line);
4135 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4136 84451b3e 2018-07-10 stsp
4137 1a57306a 2018-09-02 stsp view_vborder(view);
4138 84451b3e 2018-07-10 stsp
4139 84451b3e 2018-07-10 stsp return NULL;
4140 84451b3e 2018-07-10 stsp }
4141 84451b3e 2018-07-10 stsp
4142 84451b3e 2018-07-10 stsp static const struct got_error *
4143 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4144 84451b3e 2018-07-10 stsp {
4145 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4146 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4147 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4148 1a76625f 2018-10-22 stsp int errcode;
4149 84451b3e 2018-07-10 stsp
4150 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4151 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4152 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4153 84451b3e 2018-07-10 stsp
4154 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4155 1a76625f 2018-10-22 stsp if (errcode)
4156 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4157 84451b3e 2018-07-10 stsp
4158 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4159 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4160 d68a0a7d 2018-07-10 stsp goto done;
4161 d68a0a7d 2018-07-10 stsp }
4162 d68a0a7d 2018-07-10 stsp
4163 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4164 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4165 d68a0a7d 2018-07-10 stsp
4166 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4167 d68a0a7d 2018-07-10 stsp if (line->annotated)
4168 d68a0a7d 2018-07-10 stsp goto done;
4169 d68a0a7d 2018-07-10 stsp
4170 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4171 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4172 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4173 84451b3e 2018-07-10 stsp goto done;
4174 84451b3e 2018-07-10 stsp }
4175 84451b3e 2018-07-10 stsp line->annotated = 1;
4176 84451b3e 2018-07-10 stsp done:
4177 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4178 1a76625f 2018-10-22 stsp if (errcode)
4179 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4180 84451b3e 2018-07-10 stsp return err;
4181 84451b3e 2018-07-10 stsp }
4182 84451b3e 2018-07-10 stsp
4183 84451b3e 2018-07-10 stsp static void *
4184 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4185 84451b3e 2018-07-10 stsp {
4186 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4187 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4188 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4189 1a76625f 2018-10-22 stsp int errcode;
4190 18430de3 2018-07-10 stsp
4191 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4192 61266923 2020-01-14 stsp if (err)
4193 61266923 2020-01-14 stsp return (void *)err;
4194 61266923 2020-01-14 stsp
4195 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4196 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4197 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4198 fc06ba56 2019-08-22 stsp err = NULL;
4199 18430de3 2018-07-10 stsp
4200 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4201 1a76625f 2018-10-22 stsp if (errcode)
4202 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4203 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4204 18430de3 2018-07-10 stsp
4205 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4206 1d0f4054 2021-06-17 stsp if (err == NULL)
4207 1d0f4054 2021-06-17 stsp err = close_err;
4208 c9beca56 2018-07-22 stsp ta->repo = NULL;
4209 c9beca56 2018-07-22 stsp *ta->complete = 1;
4210 18430de3 2018-07-10 stsp
4211 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4212 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4213 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4214 18430de3 2018-07-10 stsp
4215 18430de3 2018-07-10 stsp return (void *)err;
4216 84451b3e 2018-07-10 stsp }
4217 84451b3e 2018-07-10 stsp
4218 245d91c1 2018-07-12 stsp static struct got_object_id *
4219 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4220 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4221 245d91c1 2018-07-12 stsp {
4222 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4223 8d0fe45a 2019-08-12 stsp
4224 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4225 8d0fe45a 2019-08-12 stsp return NULL;
4226 b880a918 2018-07-10 stsp
4227 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4228 245d91c1 2018-07-12 stsp if (!line->annotated)
4229 245d91c1 2018-07-12 stsp return NULL;
4230 245d91c1 2018-07-12 stsp
4231 245d91c1 2018-07-12 stsp return line->id;
4232 b880a918 2018-07-10 stsp }
4233 245d91c1 2018-07-12 stsp
4234 b880a918 2018-07-10 stsp static const struct got_error *
4235 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4236 a70480e0 2018-06-23 stsp {
4237 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4238 245d91c1 2018-07-12 stsp int i;
4239 245d91c1 2018-07-12 stsp
4240 245d91c1 2018-07-12 stsp if (blame->thread) {
4241 1a76625f 2018-10-22 stsp int errcode;
4242 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4243 1a76625f 2018-10-22 stsp if (errcode)
4244 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4245 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4246 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4247 1a76625f 2018-10-22 stsp if (errcode)
4248 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4249 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4250 1a76625f 2018-10-22 stsp if (errcode)
4251 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4252 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4253 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4254 245d91c1 2018-07-12 stsp err = NULL;
4255 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4256 245d91c1 2018-07-12 stsp }
4257 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4258 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4259 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4260 1d0f4054 2021-06-17 stsp if (err == NULL)
4261 1d0f4054 2021-06-17 stsp err = close_err;
4262 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4263 245d91c1 2018-07-12 stsp }
4264 245d91c1 2018-07-12 stsp if (blame->f) {
4265 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4266 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4267 245d91c1 2018-07-12 stsp blame->f = NULL;
4268 245d91c1 2018-07-12 stsp }
4269 57670559 2018-12-23 stsp if (blame->lines) {
4270 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4271 57670559 2018-12-23 stsp free(blame->lines[i].id);
4272 57670559 2018-12-23 stsp free(blame->lines);
4273 57670559 2018-12-23 stsp blame->lines = NULL;
4274 57670559 2018-12-23 stsp }
4275 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4276 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4277 245d91c1 2018-07-12 stsp
4278 245d91c1 2018-07-12 stsp return err;
4279 245d91c1 2018-07-12 stsp }
4280 245d91c1 2018-07-12 stsp
4281 245d91c1 2018-07-12 stsp static const struct got_error *
4282 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4283 fc06ba56 2019-08-22 stsp {
4284 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4285 fc06ba56 2019-08-22 stsp int *done = arg;
4286 fc06ba56 2019-08-22 stsp int errcode;
4287 fc06ba56 2019-08-22 stsp
4288 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4289 fc06ba56 2019-08-22 stsp if (errcode)
4290 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4291 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4292 fc06ba56 2019-08-22 stsp
4293 fc06ba56 2019-08-22 stsp if (*done)
4294 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4295 fc06ba56 2019-08-22 stsp
4296 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4297 fc06ba56 2019-08-22 stsp if (errcode)
4298 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4299 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4300 fc06ba56 2019-08-22 stsp
4301 fc06ba56 2019-08-22 stsp return err;
4302 fc06ba56 2019-08-22 stsp }
4303 fc06ba56 2019-08-22 stsp
4304 fc06ba56 2019-08-22 stsp static const struct got_error *
4305 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4306 245d91c1 2018-07-12 stsp {
4307 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4308 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4309 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4310 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4311 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4312 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4313 15a94983 2018-12-23 stsp int obj_type;
4314 a70480e0 2018-06-23 stsp
4315 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4316 a5388363 2020-12-01 naddy s->path);
4317 27d434c2 2018-09-15 stsp if (err)
4318 15a94983 2018-12-23 stsp return err;
4319 27d434c2 2018-09-15 stsp
4320 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4321 84451b3e 2018-07-10 stsp if (err)
4322 84451b3e 2018-07-10 stsp goto done;
4323 27d434c2 2018-09-15 stsp
4324 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4325 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4326 84451b3e 2018-07-10 stsp goto done;
4327 84451b3e 2018-07-10 stsp }
4328 a70480e0 2018-06-23 stsp
4329 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4330 a70480e0 2018-06-23 stsp if (err)
4331 a70480e0 2018-06-23 stsp goto done;
4332 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4333 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4334 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4335 84451b3e 2018-07-10 stsp goto done;
4336 84451b3e 2018-07-10 stsp }
4337 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4338 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4339 1fddf795 2021-01-20 stsp if (err)
4340 1fddf795 2021-01-20 stsp goto done;
4341 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4342 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4343 84451b3e 2018-07-10 stsp goto done;
4344 1fddf795 2021-01-20 stsp }
4345 b02560ec 2019-08-19 stsp
4346 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4347 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4348 b02560ec 2019-08-19 stsp blame->nlines--;
4349 a70480e0 2018-06-23 stsp
4350 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4351 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4352 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4353 84451b3e 2018-07-10 stsp goto done;
4354 84451b3e 2018-07-10 stsp }
4355 a70480e0 2018-06-23 stsp
4356 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4357 bd24772e 2018-07-11 stsp if (err)
4358 bd24772e 2018-07-11 stsp goto done;
4359 bd24772e 2018-07-11 stsp
4360 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4361 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4362 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4363 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4364 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4365 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4366 245d91c1 2018-07-12 stsp goto done;
4367 245d91c1 2018-07-12 stsp }
4368 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4369 245d91c1 2018-07-12 stsp
4370 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4371 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4372 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4373 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4374 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4375 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4376 a5388363 2020-12-01 naddy s->blame_complete = 0;
4377 f5a09613 2020-12-13 naddy
4378 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4379 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4380 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4381 f5a09613 2020-12-13 naddy s->selected_line = 1;
4382 f5a09613 2020-12-13 naddy }
4383 245d91c1 2018-07-12 stsp
4384 245d91c1 2018-07-12 stsp done:
4385 245d91c1 2018-07-12 stsp if (blob)
4386 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4387 27d434c2 2018-09-15 stsp free(obj_id);
4388 245d91c1 2018-07-12 stsp if (err)
4389 245d91c1 2018-07-12 stsp stop_blame(blame);
4390 245d91c1 2018-07-12 stsp return err;
4391 245d91c1 2018-07-12 stsp }
4392 245d91c1 2018-07-12 stsp
4393 245d91c1 2018-07-12 stsp static const struct got_error *
4394 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4395 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4396 245d91c1 2018-07-12 stsp {
4397 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4398 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4399 dbc6a6b6 2018-07-12 stsp
4400 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4401 245d91c1 2018-07-12 stsp
4402 c4843652 2019-08-12 stsp s->path = strdup(path);
4403 c4843652 2019-08-12 stsp if (s->path == NULL)
4404 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4405 c4843652 2019-08-12 stsp
4406 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4407 c4843652 2019-08-12 stsp if (err) {
4408 c4843652 2019-08-12 stsp free(s->path);
4409 7cbe629d 2018-08-04 stsp return err;
4410 c4843652 2019-08-12 stsp }
4411 245d91c1 2018-07-12 stsp
4412 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4413 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4414 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4415 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4416 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4417 fb2756b9 2018-08-04 stsp s->repo = repo;
4418 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4419 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4420 7cbe629d 2018-08-04 stsp
4421 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4422 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4423 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4424 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4425 11b20872 2019-11-08 stsp if (err)
4426 11b20872 2019-11-08 stsp return err;
4427 11b20872 2019-11-08 stsp }
4428 11b20872 2019-11-08 stsp
4429 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4430 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4431 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4432 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4433 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4434 e5a0f69f 2018-08-18 stsp
4435 a5388363 2020-12-01 naddy return run_blame(view);
4436 7cbe629d 2018-08-04 stsp }
4437 7cbe629d 2018-08-04 stsp
4438 e5a0f69f 2018-08-18 stsp static const struct got_error *
4439 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4440 7cbe629d 2018-08-04 stsp {
4441 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4442 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4443 7cbe629d 2018-08-04 stsp
4444 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4445 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4446 e5a0f69f 2018-08-18 stsp
4447 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4448 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4449 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4450 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4451 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4452 7cbe629d 2018-08-04 stsp }
4453 e5a0f69f 2018-08-18 stsp
4454 e5a0f69f 2018-08-18 stsp free(s->path);
4455 11b20872 2019-11-08 stsp free_colors(&s->colors);
4456 e5a0f69f 2018-08-18 stsp
4457 e5a0f69f 2018-08-18 stsp return err;
4458 7cbe629d 2018-08-04 stsp }
4459 7cbe629d 2018-08-04 stsp
4460 7cbe629d 2018-08-04 stsp static const struct got_error *
4461 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4462 6c4c42e0 2019-06-24 stsp {
4463 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4464 6c4c42e0 2019-06-24 stsp
4465 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4466 6c4c42e0 2019-06-24 stsp return NULL;
4467 6c4c42e0 2019-06-24 stsp }
4468 6c4c42e0 2019-06-24 stsp
4469 6c4c42e0 2019-06-24 stsp static const struct got_error *
4470 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4471 6c4c42e0 2019-06-24 stsp {
4472 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4473 6c4c42e0 2019-06-24 stsp int lineno;
4474 826082fe 2020-12-10 stsp char *line = NULL;
4475 826082fe 2020-12-10 stsp size_t linesize = 0;
4476 826082fe 2020-12-10 stsp ssize_t linelen;
4477 6c4c42e0 2019-06-24 stsp
4478 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4479 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4480 6c4c42e0 2019-06-24 stsp return NULL;
4481 6c4c42e0 2019-06-24 stsp }
4482 6c4c42e0 2019-06-24 stsp
4483 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4484 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4485 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4486 6c4c42e0 2019-06-24 stsp else
4487 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4488 6c4c42e0 2019-06-24 stsp } else {
4489 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4490 6c4c42e0 2019-06-24 stsp lineno = 1;
4491 6c4c42e0 2019-06-24 stsp else
4492 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4493 6c4c42e0 2019-06-24 stsp }
4494 6c4c42e0 2019-06-24 stsp
4495 6c4c42e0 2019-06-24 stsp while (1) {
4496 6c4c42e0 2019-06-24 stsp off_t offset;
4497 6c4c42e0 2019-06-24 stsp
4498 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4499 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4500 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4501 6c4c42e0 2019-06-24 stsp break;
4502 6c4c42e0 2019-06-24 stsp }
4503 2246482e 2019-06-25 stsp
4504 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4505 6c4c42e0 2019-06-24 stsp lineno = 1;
4506 6c4c42e0 2019-06-24 stsp else
4507 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4508 6c4c42e0 2019-06-24 stsp }
4509 6c4c42e0 2019-06-24 stsp
4510 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4511 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4512 6c4c42e0 2019-06-24 stsp free(line);
4513 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4514 6c4c42e0 2019-06-24 stsp }
4515 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4516 826082fe 2020-12-10 stsp if (linelen != -1 &&
4517 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4518 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4519 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4520 6c4c42e0 2019-06-24 stsp break;
4521 6c4c42e0 2019-06-24 stsp }
4522 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4523 6c4c42e0 2019-06-24 stsp lineno++;
4524 6c4c42e0 2019-06-24 stsp else
4525 6c4c42e0 2019-06-24 stsp lineno--;
4526 6c4c42e0 2019-06-24 stsp }
4527 826082fe 2020-12-10 stsp free(line);
4528 6c4c42e0 2019-06-24 stsp
4529 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4530 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4531 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4532 6c4c42e0 2019-06-24 stsp }
4533 6c4c42e0 2019-06-24 stsp
4534 6c4c42e0 2019-06-24 stsp return NULL;
4535 6c4c42e0 2019-06-24 stsp }
4536 6c4c42e0 2019-06-24 stsp
4537 6c4c42e0 2019-06-24 stsp static const struct got_error *
4538 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4539 7cbe629d 2018-08-04 stsp {
4540 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4541 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4542 2b380cc8 2018-10-24 stsp int errcode;
4543 2b380cc8 2018-10-24 stsp
4544 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4545 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4546 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4547 2b380cc8 2018-10-24 stsp if (errcode)
4548 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4549 51fe7530 2019-08-19 stsp
4550 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4551 2b380cc8 2018-10-24 stsp }
4552 e5a0f69f 2018-08-18 stsp
4553 51fe7530 2019-08-19 stsp if (s->blame_complete)
4554 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4555 51fe7530 2019-08-19 stsp
4556 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4557 e5a0f69f 2018-08-18 stsp
4558 669b5ffa 2018-10-07 stsp view_vborder(view);
4559 e5a0f69f 2018-08-18 stsp return err;
4560 e5a0f69f 2018-08-18 stsp }
4561 e5a0f69f 2018-08-18 stsp
4562 e5a0f69f 2018-08-18 stsp static const struct got_error *
4563 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4564 e5a0f69f 2018-08-18 stsp {
4565 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4566 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4567 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4568 669b5ffa 2018-10-07 stsp int begin_x = 0;
4569 7cbe629d 2018-08-04 stsp
4570 e5a0f69f 2018-08-18 stsp switch (ch) {
4571 1e37a5c2 2019-05-12 jcs case 'q':
4572 1e37a5c2 2019-05-12 jcs s->done = 1;
4573 4deef56f 2021-09-02 naddy break;
4574 4deef56f 2021-09-02 naddy case 'g':
4575 4deef56f 2021-09-02 naddy case KEY_HOME:
4576 4deef56f 2021-09-02 naddy s->selected_line = 1;
4577 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4578 4deef56f 2021-09-02 naddy break;
4579 4deef56f 2021-09-02 naddy case 'G':
4580 4deef56f 2021-09-02 naddy case KEY_END:
4581 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4582 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4583 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4584 4deef56f 2021-09-02 naddy } else {
4585 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4586 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4587 4deef56f 2021-09-02 naddy (view->nlines - 3);
4588 4deef56f 2021-09-02 naddy }
4589 1e37a5c2 2019-05-12 jcs break;
4590 1e37a5c2 2019-05-12 jcs case 'k':
4591 1e37a5c2 2019-05-12 jcs case KEY_UP:
4592 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4593 1e37a5c2 2019-05-12 jcs s->selected_line--;
4594 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4595 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4596 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4597 1e37a5c2 2019-05-12 jcs break;
4598 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4599 ea025d1d 2020-02-22 naddy case CTRL('b'):
4600 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4601 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4602 e5a0f69f 2018-08-18 stsp break;
4603 1e37a5c2 2019-05-12 jcs }
4604 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4605 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4606 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4607 1e37a5c2 2019-05-12 jcs else
4608 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4609 1e37a5c2 2019-05-12 jcs break;
4610 1e37a5c2 2019-05-12 jcs case 'j':
4611 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4612 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4613 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4614 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4615 1e37a5c2 2019-05-12 jcs s->selected_line++;
4616 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4617 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4618 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4619 1e37a5c2 2019-05-12 jcs break;
4620 1e37a5c2 2019-05-12 jcs case 'b':
4621 1e37a5c2 2019-05-12 jcs case 'p': {
4622 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4623 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4624 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4625 1e37a5c2 2019-05-12 jcs if (id == NULL)
4626 e5a0f69f 2018-08-18 stsp break;
4627 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4628 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4629 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4630 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4631 1e37a5c2 2019-05-12 jcs int obj_type;
4632 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4633 1e37a5c2 2019-05-12 jcs s->repo, id);
4634 e5a0f69f 2018-08-18 stsp if (err)
4635 e5a0f69f 2018-08-18 stsp break;
4636 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4637 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4638 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4639 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4640 e5a0f69f 2018-08-18 stsp break;
4641 e5a0f69f 2018-08-18 stsp }
4642 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4643 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4644 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4645 e5a0f69f 2018-08-18 stsp if (err) {
4646 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4647 1e37a5c2 2019-05-12 jcs err = NULL;
4648 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4649 e5a0f69f 2018-08-18 stsp break;
4650 e5a0f69f 2018-08-18 stsp }
4651 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4652 1e37a5c2 2019-05-12 jcs blob_id);
4653 1e37a5c2 2019-05-12 jcs free(blob_id);
4654 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4655 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4656 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4657 e5a0f69f 2018-08-18 stsp break;
4658 1e37a5c2 2019-05-12 jcs }
4659 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4660 1e37a5c2 2019-05-12 jcs pid->id);
4661 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4662 1e37a5c2 2019-05-12 jcs } else {
4663 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4664 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4665 1e37a5c2 2019-05-12 jcs break;
4666 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4667 1e37a5c2 2019-05-12 jcs id);
4668 1e37a5c2 2019-05-12 jcs }
4669 1e37a5c2 2019-05-12 jcs if (err)
4670 e5a0f69f 2018-08-18 stsp break;
4671 1e37a5c2 2019-05-12 jcs s->done = 1;
4672 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4673 1e37a5c2 2019-05-12 jcs s->done = 0;
4674 1e37a5c2 2019-05-12 jcs if (thread_err)
4675 1e37a5c2 2019-05-12 jcs break;
4676 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4677 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4678 a5388363 2020-12-01 naddy err = run_blame(view);
4679 1e37a5c2 2019-05-12 jcs if (err)
4680 1e37a5c2 2019-05-12 jcs break;
4681 1e37a5c2 2019-05-12 jcs break;
4682 1e37a5c2 2019-05-12 jcs }
4683 1e37a5c2 2019-05-12 jcs case 'B': {
4684 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4685 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4686 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4687 1e37a5c2 2019-05-12 jcs break;
4688 1e37a5c2 2019-05-12 jcs s->done = 1;
4689 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4690 1e37a5c2 2019-05-12 jcs s->done = 0;
4691 1e37a5c2 2019-05-12 jcs if (thread_err)
4692 1e37a5c2 2019-05-12 jcs break;
4693 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4694 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4695 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4696 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4697 a5388363 2020-12-01 naddy err = run_blame(view);
4698 1e37a5c2 2019-05-12 jcs if (err)
4699 1e37a5c2 2019-05-12 jcs break;
4700 1e37a5c2 2019-05-12 jcs break;
4701 1e37a5c2 2019-05-12 jcs }
4702 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4703 1e37a5c2 2019-05-12 jcs case '\r': {
4704 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4705 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4706 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4707 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4708 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4709 1e37a5c2 2019-05-12 jcs if (id == NULL)
4710 1e37a5c2 2019-05-12 jcs break;
4711 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4712 1e37a5c2 2019-05-12 jcs if (err)
4713 1e37a5c2 2019-05-12 jcs break;
4714 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4715 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4716 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4717 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4718 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4719 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4720 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4721 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4722 1e37a5c2 2019-05-12 jcs break;
4723 15a94983 2018-12-23 stsp }
4724 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4725 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4726 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4727 1e37a5c2 2019-05-12 jcs if (err) {
4728 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4729 1e37a5c2 2019-05-12 jcs break;
4730 1e37a5c2 2019-05-12 jcs }
4731 e78dc838 2020-12-04 stsp view->focussed = 0;
4732 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4733 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4734 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4735 1e37a5c2 2019-05-12 jcs if (err)
4736 34bc9ec9 2019-02-22 stsp break;
4737 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4738 e78dc838 2020-12-04 stsp view->focus_child = 1;
4739 1e37a5c2 2019-05-12 jcs } else
4740 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4741 1e37a5c2 2019-05-12 jcs if (err)
4742 e5a0f69f 2018-08-18 stsp break;
4743 1e37a5c2 2019-05-12 jcs break;
4744 1e37a5c2 2019-05-12 jcs }
4745 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4746 ea025d1d 2020-02-22 naddy case CTRL('f'):
4747 1e37a5c2 2019-05-12 jcs case ' ':
4748 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4749 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4750 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4751 e5a0f69f 2018-08-18 stsp break;
4752 1e37a5c2 2019-05-12 jcs }
4753 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4754 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4755 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4756 1e37a5c2 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 + view->nlines - 2
4760 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4761 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4762 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4763 1e37a5c2 2019-05-12 jcs else
4764 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4765 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4766 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4767 1e37a5c2 2019-05-12 jcs break;
4768 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4769 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4770 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4771 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4772 1e37a5c2 2019-05-12 jcs }
4773 1e37a5c2 2019-05-12 jcs break;
4774 1e37a5c2 2019-05-12 jcs default:
4775 1e37a5c2 2019-05-12 jcs break;
4776 a70480e0 2018-06-23 stsp }
4777 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4778 a70480e0 2018-06-23 stsp }
4779 a70480e0 2018-06-23 stsp
4780 a70480e0 2018-06-23 stsp static const struct got_error *
4781 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4782 9f7d7167 2018-04-29 stsp {
4783 a70480e0 2018-06-23 stsp const struct got_error *error;
4784 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4785 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4786 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4787 0587e10c 2020-07-23 stsp char *link_target = NULL;
4788 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4789 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4790 a70480e0 2018-06-23 stsp int ch;
4791 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4792 a70480e0 2018-06-23 stsp
4793 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4794 a70480e0 2018-06-23 stsp switch (ch) {
4795 a70480e0 2018-06-23 stsp case 'c':
4796 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4797 a70480e0 2018-06-23 stsp break;
4798 69069811 2018-08-02 stsp case 'r':
4799 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4800 69069811 2018-08-02 stsp if (repo_path == NULL)
4801 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4802 9ba1d308 2019-10-21 stsp optarg);
4803 69069811 2018-08-02 stsp break;
4804 a70480e0 2018-06-23 stsp default:
4805 17020d27 2019-03-07 stsp usage_blame();
4806 a70480e0 2018-06-23 stsp /* NOTREACHED */
4807 a70480e0 2018-06-23 stsp }
4808 a70480e0 2018-06-23 stsp }
4809 a70480e0 2018-06-23 stsp
4810 a70480e0 2018-06-23 stsp argc -= optind;
4811 a70480e0 2018-06-23 stsp argv += optind;
4812 a70480e0 2018-06-23 stsp
4813 f135c941 2020-02-20 stsp if (argc != 1)
4814 a70480e0 2018-06-23 stsp usage_blame();
4815 6962eb72 2020-02-20 stsp
4816 69069811 2018-08-02 stsp if (repo_path == NULL) {
4817 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4818 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4819 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4820 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4821 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4822 c156c7a4 2020-12-18 stsp goto done;
4823 f135c941 2020-02-20 stsp if (worktree)
4824 eb41ed75 2019-02-05 stsp repo_path =
4825 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4826 f135c941 2020-02-20 stsp else
4827 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4828 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4829 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4830 c156c7a4 2020-12-18 stsp goto done;
4831 c156c7a4 2020-12-18 stsp }
4832 f135c941 2020-02-20 stsp }
4833 a915003a 2019-02-05 stsp
4834 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4835 c02c541e 2019-03-29 stsp if (error != NULL)
4836 8e94dd5b 2019-01-04 stsp goto done;
4837 69069811 2018-08-02 stsp
4838 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4839 f135c941 2020-02-20 stsp worktree);
4840 c02c541e 2019-03-29 stsp if (error)
4841 92205607 2019-01-04 stsp goto done;
4842 69069811 2018-08-02 stsp
4843 f135c941 2020-02-20 stsp init_curses();
4844 f135c941 2020-02-20 stsp
4845 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4846 51a10b52 2020-12-26 stsp if (error)
4847 51a10b52 2020-12-26 stsp goto done;
4848 51a10b52 2020-12-26 stsp
4849 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
4850 eb41ed75 2019-02-05 stsp if (error)
4851 69069811 2018-08-02 stsp goto done;
4852 a70480e0 2018-06-23 stsp
4853 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4854 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4855 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4856 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4857 a70480e0 2018-06-23 stsp if (error != NULL)
4858 66b4983c 2018-06-23 stsp goto done;
4859 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4860 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4861 a70480e0 2018-06-23 stsp } else {
4862 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4863 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4864 a70480e0 2018-06-23 stsp }
4865 a19e88aa 2018-06-23 stsp if (error != NULL)
4866 8b473291 2019-02-21 stsp goto done;
4867 8b473291 2019-02-21 stsp
4868 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4869 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4870 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4871 e1cd8fed 2018-08-01 stsp goto done;
4872 e1cd8fed 2018-08-01 stsp }
4873 0587e10c 2020-07-23 stsp
4874 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4875 0587e10c 2020-07-23 stsp commit_id, repo);
4876 7cbe629d 2018-08-04 stsp if (error)
4877 7cbe629d 2018-08-04 stsp goto done;
4878 0587e10c 2020-07-23 stsp
4879 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4880 78756c87 2020-11-24 stsp commit_id, repo);
4881 0587e10c 2020-07-23 stsp if (error)
4882 0587e10c 2020-07-23 stsp goto done;
4883 12314ad4 2019-08-31 stsp if (worktree) {
4884 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4885 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4886 12314ad4 2019-08-31 stsp worktree = NULL;
4887 12314ad4 2019-08-31 stsp }
4888 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4889 a70480e0 2018-06-23 stsp done:
4890 69069811 2018-08-02 stsp free(repo_path);
4891 f135c941 2020-02-20 stsp free(in_repo_path);
4892 0587e10c 2020-07-23 stsp free(link_target);
4893 69069811 2018-08-02 stsp free(cwd);
4894 a70480e0 2018-06-23 stsp free(commit_id);
4895 eb41ed75 2019-02-05 stsp if (worktree)
4896 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4897 1d0f4054 2021-06-17 stsp if (repo) {
4898 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4899 1d0f4054 2021-06-17 stsp if (error == NULL)
4900 1d0f4054 2021-06-17 stsp error = close_err;
4901 1d0f4054 2021-06-17 stsp }
4902 51a10b52 2020-12-26 stsp tog_free_refs();
4903 a70480e0 2018-06-23 stsp return error;
4904 ffd1d5e5 2018-06-23 stsp }
4905 ffd1d5e5 2018-06-23 stsp
4906 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4907 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4908 ffd1d5e5 2018-06-23 stsp {
4909 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4910 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4911 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4912 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4913 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4914 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4915 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4916 ffd1d5e5 2018-06-23 stsp
4917 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4918 ffd1d5e5 2018-06-23 stsp
4919 f7d12f7e 2018-08-01 stsp werase(view->window);
4920 ffd1d5e5 2018-06-23 stsp
4921 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4922 ffd1d5e5 2018-06-23 stsp return NULL;
4923 ffd1d5e5 2018-06-23 stsp
4924 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4925 ffd1d5e5 2018-06-23 stsp if (err)
4926 ffd1d5e5 2018-06-23 stsp return err;
4927 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4928 a3404814 2018-09-02 stsp wstandout(view->window);
4929 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4930 11b20872 2019-11-08 stsp if (tc)
4931 11b20872 2019-11-08 stsp wattr_on(view->window,
4932 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4933 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4934 11b20872 2019-11-08 stsp if (tc)
4935 11b20872 2019-11-08 stsp wattr_off(view->window,
4936 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4937 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4938 a3404814 2018-09-02 stsp wstandend(view->window);
4939 2550e4c3 2018-07-13 stsp free(wline);
4940 2550e4c3 2018-07-13 stsp wline = NULL;
4941 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4942 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4943 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4944 ffd1d5e5 2018-06-23 stsp return NULL;
4945 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4946 ce52c690 2018-06-23 stsp if (err)
4947 ce52c690 2018-06-23 stsp return err;
4948 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4949 2550e4c3 2018-07-13 stsp free(wline);
4950 2550e4c3 2018-07-13 stsp wline = NULL;
4951 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4952 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4953 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4954 ffd1d5e5 2018-06-23 stsp return NULL;
4955 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4956 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4957 a1eca9bb 2018-06-23 stsp return NULL;
4958 ffd1d5e5 2018-06-23 stsp
4959 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4960 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4961 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4962 0cf4efb1 2018-09-29 stsp if (view->focussed)
4963 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4964 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4965 ffd1d5e5 2018-06-23 stsp }
4966 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4967 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4968 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4969 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4970 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4971 ffd1d5e5 2018-06-23 stsp return NULL;
4972 ffd1d5e5 2018-06-23 stsp n = 1;
4973 ffd1d5e5 2018-06-23 stsp } else {
4974 ffd1d5e5 2018-06-23 stsp n = 0;
4975 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4976 ffd1d5e5 2018-06-23 stsp }
4977 ffd1d5e5 2018-06-23 stsp
4978 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4979 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4980 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4981 848d6979 2019-08-12 stsp const char *modestr = "";
4982 56e0773d 2019-11-28 stsp mode_t mode;
4983 1d13200f 2018-07-12 stsp
4984 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4985 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4986 56e0773d 2019-11-28 stsp
4987 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4988 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4989 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4990 1d13200f 2018-07-12 stsp if (err)
4991 638f9024 2019-05-13 stsp return got_error_from_errno(
4992 230a42bd 2019-05-11 jcs "got_object_id_str");
4993 1d13200f 2018-07-12 stsp }
4994 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4995 63c5ca5d 2019-08-24 stsp modestr = "$";
4996 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4997 0d6c6ee3 2020-05-20 stsp int i;
4998 0d6c6ee3 2020-05-20 stsp
4999 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5000 d86d3b18 2020-12-01 naddy te, s->repo);
5001 0d6c6ee3 2020-05-20 stsp if (err) {
5002 0d6c6ee3 2020-05-20 stsp free(id_str);
5003 0d6c6ee3 2020-05-20 stsp return err;
5004 0d6c6ee3 2020-05-20 stsp }
5005 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5006 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5007 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5008 0d6c6ee3 2020-05-20 stsp }
5009 848d6979 2019-08-12 stsp modestr = "@";
5010 0d6c6ee3 2020-05-20 stsp }
5011 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5012 848d6979 2019-08-12 stsp modestr = "/";
5013 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5014 848d6979 2019-08-12 stsp modestr = "*";
5015 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5016 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5017 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5018 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5019 1d13200f 2018-07-12 stsp free(id_str);
5020 0d6c6ee3 2020-05-20 stsp free(link_target);
5021 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5022 1d13200f 2018-07-12 stsp }
5023 1d13200f 2018-07-12 stsp free(id_str);
5024 0d6c6ee3 2020-05-20 stsp free(link_target);
5025 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5026 ffd1d5e5 2018-06-23 stsp if (err) {
5027 ffd1d5e5 2018-06-23 stsp free(line);
5028 ffd1d5e5 2018-06-23 stsp break;
5029 ffd1d5e5 2018-06-23 stsp }
5030 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5031 0cf4efb1 2018-09-29 stsp if (view->focussed)
5032 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5033 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5034 ffd1d5e5 2018-06-23 stsp }
5035 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5036 f26dddb7 2019-11-08 stsp if (tc)
5037 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5038 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5039 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5040 f26dddb7 2019-11-08 stsp if (tc)
5041 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5042 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5043 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5044 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5045 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5046 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5047 ffd1d5e5 2018-06-23 stsp free(line);
5048 2550e4c3 2018-07-13 stsp free(wline);
5049 2550e4c3 2018-07-13 stsp wline = NULL;
5050 ffd1d5e5 2018-06-23 stsp n++;
5051 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5052 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5053 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5054 ffd1d5e5 2018-06-23 stsp break;
5055 ffd1d5e5 2018-06-23 stsp }
5056 ffd1d5e5 2018-06-23 stsp
5057 ffd1d5e5 2018-06-23 stsp return err;
5058 ffd1d5e5 2018-06-23 stsp }
5059 ffd1d5e5 2018-06-23 stsp
5060 ffd1d5e5 2018-06-23 stsp static void
5061 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5062 ffd1d5e5 2018-06-23 stsp {
5063 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5064 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5065 fa86c4bf 2020-11-29 stsp int i = 0;
5066 ffd1d5e5 2018-06-23 stsp
5067 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5068 ffd1d5e5 2018-06-23 stsp return;
5069 ffd1d5e5 2018-06-23 stsp
5070 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5071 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5072 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5073 fa86c4bf 2020-11-29 stsp if (!isroot)
5074 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5075 fa86c4bf 2020-11-29 stsp break;
5076 fa86c4bf 2020-11-29 stsp }
5077 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5078 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5079 ffd1d5e5 2018-06-23 stsp }
5080 ffd1d5e5 2018-06-23 stsp }
5081 ffd1d5e5 2018-06-23 stsp
5082 fa86c4bf 2020-11-29 stsp static void
5083 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5084 ffd1d5e5 2018-06-23 stsp {
5085 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5086 ffd1d5e5 2018-06-23 stsp int n = 0;
5087 ffd1d5e5 2018-06-23 stsp
5088 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5089 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5090 694d3271 2020-12-01 naddy s->first_displayed_entry);
5091 694d3271 2020-12-01 naddy else
5092 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5093 56e0773d 2019-11-28 stsp
5094 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5095 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5096 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5097 768394f3 2019-01-24 stsp if (last) {
5098 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5099 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5100 768394f3 2019-01-24 stsp }
5101 ffd1d5e5 2018-06-23 stsp }
5102 ffd1d5e5 2018-06-23 stsp }
5103 ffd1d5e5 2018-06-23 stsp
5104 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5105 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5106 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5107 ffd1d5e5 2018-06-23 stsp {
5108 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5109 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5110 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5111 ffd1d5e5 2018-06-23 stsp
5112 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5113 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5114 56e0773d 2019-11-28 stsp + 1 /* slash */;
5115 ce52c690 2018-06-23 stsp if (te)
5116 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5117 ce52c690 2018-06-23 stsp
5118 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5119 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5120 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5121 ffd1d5e5 2018-06-23 stsp
5122 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5123 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5124 d9765a41 2018-06-23 stsp while (pt) {
5125 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5126 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5127 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5128 cb2ebc8a 2018-06-23 stsp goto done;
5129 cb2ebc8a 2018-06-23 stsp }
5130 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5131 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5132 cb2ebc8a 2018-06-23 stsp goto done;
5133 cb2ebc8a 2018-06-23 stsp }
5134 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5135 ffd1d5e5 2018-06-23 stsp }
5136 ce52c690 2018-06-23 stsp if (te) {
5137 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5138 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5139 ce52c690 2018-06-23 stsp goto done;
5140 ce52c690 2018-06-23 stsp }
5141 cb2ebc8a 2018-06-23 stsp }
5142 ce52c690 2018-06-23 stsp done:
5143 ce52c690 2018-06-23 stsp if (err) {
5144 ce52c690 2018-06-23 stsp free(*path);
5145 ce52c690 2018-06-23 stsp *path = NULL;
5146 ce52c690 2018-06-23 stsp }
5147 ce52c690 2018-06-23 stsp return err;
5148 ce52c690 2018-06-23 stsp }
5149 ce52c690 2018-06-23 stsp
5150 ce52c690 2018-06-23 stsp static const struct got_error *
5151 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5152 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5153 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5154 ce52c690 2018-06-23 stsp {
5155 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5156 ce52c690 2018-06-23 stsp char *path;
5157 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5158 a0de39f3 2019-08-09 stsp
5159 a0de39f3 2019-08-09 stsp *new_view = NULL;
5160 69efd4c4 2018-07-18 stsp
5161 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5162 ce52c690 2018-06-23 stsp if (err)
5163 ce52c690 2018-06-23 stsp return err;
5164 ffd1d5e5 2018-06-23 stsp
5165 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5166 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5167 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5168 83ce39e3 2019-08-12 stsp goto done;
5169 83ce39e3 2019-08-12 stsp }
5170 cdf1ee82 2018-08-01 stsp
5171 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5172 e5a0f69f 2018-08-18 stsp if (err) {
5173 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5174 fc06ba56 2019-08-22 stsp err = NULL;
5175 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5176 e5a0f69f 2018-08-18 stsp } else
5177 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5178 83ce39e3 2019-08-12 stsp done:
5179 83ce39e3 2019-08-12 stsp free(path);
5180 69efd4c4 2018-07-18 stsp return err;
5181 69efd4c4 2018-07-18 stsp }
5182 69efd4c4 2018-07-18 stsp
5183 69efd4c4 2018-07-18 stsp static const struct got_error *
5184 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5185 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5186 69efd4c4 2018-07-18 stsp {
5187 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5188 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5189 69efd4c4 2018-07-18 stsp char *path;
5190 a0de39f3 2019-08-09 stsp
5191 a0de39f3 2019-08-09 stsp *new_view = NULL;
5192 69efd4c4 2018-07-18 stsp
5193 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5194 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5195 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5196 e5a0f69f 2018-08-18 stsp
5197 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5198 69efd4c4 2018-07-18 stsp if (err)
5199 69efd4c4 2018-07-18 stsp return err;
5200 69efd4c4 2018-07-18 stsp
5201 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5202 4e97c21c 2020-12-06 stsp path, 0);
5203 ba4f502b 2018-08-04 stsp if (err)
5204 e5a0f69f 2018-08-18 stsp view_close(log_view);
5205 e5a0f69f 2018-08-18 stsp else
5206 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5207 cb2ebc8a 2018-06-23 stsp free(path);
5208 cb2ebc8a 2018-06-23 stsp return err;
5209 ffd1d5e5 2018-06-23 stsp }
5210 ffd1d5e5 2018-06-23 stsp
5211 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5212 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5213 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5214 ffd1d5e5 2018-06-23 stsp {
5215 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5216 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5217 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5218 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5219 ffd1d5e5 2018-06-23 stsp
5220 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5221 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5222 bc573f3b 2021-07-10 stsp
5223 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5224 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5225 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5226 ffd1d5e5 2018-06-23 stsp
5227 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5228 bc573f3b 2021-07-10 stsp if (err)
5229 bc573f3b 2021-07-10 stsp goto done;
5230 bc573f3b 2021-07-10 stsp
5231 bc573f3b 2021-07-10 stsp /*
5232 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5233 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5234 bc573f3b 2021-07-10 stsp * closed on demand.
5235 bc573f3b 2021-07-10 stsp */
5236 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5237 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5238 bc573f3b 2021-07-10 stsp if (err)
5239 bc573f3b 2021-07-10 stsp goto done;
5240 bc573f3b 2021-07-10 stsp s->tree = s->root;
5241 bc573f3b 2021-07-10 stsp
5242 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5243 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5244 ffd1d5e5 2018-06-23 stsp goto done;
5245 ffd1d5e5 2018-06-23 stsp
5246 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5247 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5248 ffd1d5e5 2018-06-23 stsp goto done;
5249 ffd1d5e5 2018-06-23 stsp }
5250 ffd1d5e5 2018-06-23 stsp
5251 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5252 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5253 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5254 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5255 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5256 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5257 9cd7cbd1 2020-12-07 stsp goto done;
5258 9cd7cbd1 2020-12-07 stsp }
5259 9cd7cbd1 2020-12-07 stsp }
5260 fb2756b9 2018-08-04 stsp s->repo = repo;
5261 c0b01bdb 2019-11-08 stsp
5262 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5263 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5264 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5265 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5266 c0b01bdb 2019-11-08 stsp if (err)
5267 c0b01bdb 2019-11-08 stsp goto done;
5268 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5269 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5270 bc573f3b 2021-07-10 stsp if (err)
5271 c0b01bdb 2019-11-08 stsp goto done;
5272 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5273 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5274 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5275 bc573f3b 2021-07-10 stsp if (err)
5276 c0b01bdb 2019-11-08 stsp goto done;
5277 e5a0f69f 2018-08-18 stsp
5278 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5279 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5280 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5281 bc573f3b 2021-07-10 stsp if (err)
5282 11b20872 2019-11-08 stsp goto done;
5283 11b20872 2019-11-08 stsp
5284 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5285 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5286 bc573f3b 2021-07-10 stsp if (err)
5287 c0b01bdb 2019-11-08 stsp goto done;
5288 c0b01bdb 2019-11-08 stsp }
5289 c0b01bdb 2019-11-08 stsp
5290 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5291 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5292 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5293 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5294 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5295 ad80ab7b 2018-08-04 stsp done:
5296 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5297 bc573f3b 2021-07-10 stsp if (commit)
5298 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5299 bc573f3b 2021-07-10 stsp if (err)
5300 bc573f3b 2021-07-10 stsp close_tree_view(view);
5301 ad80ab7b 2018-08-04 stsp return err;
5302 ad80ab7b 2018-08-04 stsp }
5303 ad80ab7b 2018-08-04 stsp
5304 e5a0f69f 2018-08-18 stsp static const struct got_error *
5305 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5306 ad80ab7b 2018-08-04 stsp {
5307 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5308 ad80ab7b 2018-08-04 stsp
5309 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5310 fb2756b9 2018-08-04 stsp free(s->tree_label);
5311 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5312 6484ec90 2018-09-29 stsp free(s->commit_id);
5313 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5314 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5315 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5316 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5317 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5318 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5319 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5320 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5321 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5322 ad80ab7b 2018-08-04 stsp free(parent);
5323 ad80ab7b 2018-08-04 stsp
5324 ad80ab7b 2018-08-04 stsp }
5325 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5326 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5327 bc573f3b 2021-07-10 stsp if (s->root)
5328 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5329 7c32bd05 2019-06-22 stsp return NULL;
5330 7c32bd05 2019-06-22 stsp }
5331 7c32bd05 2019-06-22 stsp
5332 7c32bd05 2019-06-22 stsp static const struct got_error *
5333 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5334 7c32bd05 2019-06-22 stsp {
5335 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5336 7c32bd05 2019-06-22 stsp
5337 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5338 7c32bd05 2019-06-22 stsp return NULL;
5339 7c32bd05 2019-06-22 stsp }
5340 7c32bd05 2019-06-22 stsp
5341 7c32bd05 2019-06-22 stsp static int
5342 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5343 7c32bd05 2019-06-22 stsp {
5344 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5345 7c32bd05 2019-06-22 stsp
5346 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5347 56e0773d 2019-11-28 stsp 0) == 0;
5348 7c32bd05 2019-06-22 stsp }
5349 7c32bd05 2019-06-22 stsp
5350 7c32bd05 2019-06-22 stsp static const struct got_error *
5351 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5352 7c32bd05 2019-06-22 stsp {
5353 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5354 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5355 7c32bd05 2019-06-22 stsp
5356 7c32bd05 2019-06-22 stsp if (!view->searching) {
5357 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5358 7c32bd05 2019-06-22 stsp return NULL;
5359 7c32bd05 2019-06-22 stsp }
5360 7c32bd05 2019-06-22 stsp
5361 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5362 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5363 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5364 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5365 56e0773d 2019-11-28 stsp s->selected_entry);
5366 7c32bd05 2019-06-22 stsp else
5367 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5368 56e0773d 2019-11-28 stsp } else {
5369 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5370 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5371 56e0773d 2019-11-28 stsp else
5372 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5373 56e0773d 2019-11-28 stsp s->selected_entry);
5374 7c32bd05 2019-06-22 stsp }
5375 7c32bd05 2019-06-22 stsp } else {
5376 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5377 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5378 56e0773d 2019-11-28 stsp else
5379 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5380 7c32bd05 2019-06-22 stsp }
5381 7c32bd05 2019-06-22 stsp
5382 7c32bd05 2019-06-22 stsp while (1) {
5383 56e0773d 2019-11-28 stsp if (te == NULL) {
5384 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5385 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5386 ac66afb8 2019-06-24 stsp return NULL;
5387 ac66afb8 2019-06-24 stsp }
5388 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5389 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5390 56e0773d 2019-11-28 stsp else
5391 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5392 7c32bd05 2019-06-22 stsp }
5393 7c32bd05 2019-06-22 stsp
5394 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5395 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5396 56e0773d 2019-11-28 stsp s->matched_entry = te;
5397 7c32bd05 2019-06-22 stsp break;
5398 7c32bd05 2019-06-22 stsp }
5399 7c32bd05 2019-06-22 stsp
5400 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5401 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5402 56e0773d 2019-11-28 stsp else
5403 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5404 7c32bd05 2019-06-22 stsp }
5405 e5a0f69f 2018-08-18 stsp
5406 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5407 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5408 7c32bd05 2019-06-22 stsp s->selected = 0;
5409 7c32bd05 2019-06-22 stsp }
5410 7c32bd05 2019-06-22 stsp
5411 e5a0f69f 2018-08-18 stsp return NULL;
5412 ad80ab7b 2018-08-04 stsp }
5413 ad80ab7b 2018-08-04 stsp
5414 ad80ab7b 2018-08-04 stsp static const struct got_error *
5415 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5416 ad80ab7b 2018-08-04 stsp {
5417 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5418 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5419 e5a0f69f 2018-08-18 stsp char *parent_path;
5420 ad80ab7b 2018-08-04 stsp
5421 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5422 e5a0f69f 2018-08-18 stsp if (err)
5423 e5a0f69f 2018-08-18 stsp return err;
5424 ffd1d5e5 2018-06-23 stsp
5425 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5426 e5a0f69f 2018-08-18 stsp free(parent_path);
5427 669b5ffa 2018-10-07 stsp
5428 669b5ffa 2018-10-07 stsp view_vborder(view);
5429 e5a0f69f 2018-08-18 stsp return err;
5430 e5a0f69f 2018-08-18 stsp }
5431 ce52c690 2018-06-23 stsp
5432 e5a0f69f 2018-08-18 stsp static const struct got_error *
5433 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5434 e5a0f69f 2018-08-18 stsp {
5435 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5436 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5437 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5438 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5439 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5440 ffd1d5e5 2018-06-23 stsp
5441 e5a0f69f 2018-08-18 stsp switch (ch) {
5442 1e37a5c2 2019-05-12 jcs case 'i':
5443 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5444 1e37a5c2 2019-05-12 jcs break;
5445 1e37a5c2 2019-05-12 jcs case 'l':
5446 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5447 ffd1d5e5 2018-06-23 stsp break;
5448 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5449 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5450 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5451 e78dc838 2020-12-04 stsp view->focussed = 0;
5452 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5453 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5454 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5455 1e37a5c2 2019-05-12 jcs if (err)
5456 1e37a5c2 2019-05-12 jcs return err;
5457 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5458 e78dc838 2020-12-04 stsp view->focus_child = 1;
5459 1e37a5c2 2019-05-12 jcs } else
5460 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5461 152c1c93 2020-11-29 stsp break;
5462 152c1c93 2020-11-29 stsp case 'r':
5463 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5464 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5465 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5466 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5467 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5468 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5469 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5470 152c1c93 2020-11-29 stsp if (err) {
5471 152c1c93 2020-11-29 stsp view_close(ref_view);
5472 152c1c93 2020-11-29 stsp return err;
5473 152c1c93 2020-11-29 stsp }
5474 e78dc838 2020-12-04 stsp view->focussed = 0;
5475 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5476 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5477 152c1c93 2020-11-29 stsp err = view_close_child(view);
5478 152c1c93 2020-11-29 stsp if (err)
5479 152c1c93 2020-11-29 stsp return err;
5480 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5481 e78dc838 2020-12-04 stsp view->focus_child = 1;
5482 152c1c93 2020-11-29 stsp } else
5483 152c1c93 2020-11-29 stsp *new_view = ref_view;
5484 e4526bf5 2021-09-03 naddy break;
5485 e4526bf5 2021-09-03 naddy case 'g':
5486 e4526bf5 2021-09-03 naddy case KEY_HOME:
5487 e4526bf5 2021-09-03 naddy s->selected = 0;
5488 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5489 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5490 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5491 e4526bf5 2021-09-03 naddy else
5492 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5493 1e37a5c2 2019-05-12 jcs break;
5494 e4526bf5 2021-09-03 naddy case 'G':
5495 e4526bf5 2021-09-03 naddy case KEY_END:
5496 e4526bf5 2021-09-03 naddy s->selected = 0;
5497 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5498 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5499 e4526bf5 2021-09-03 naddy if (te == NULL) {
5500 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5501 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5502 e4526bf5 2021-09-03 naddy n++;
5503 e4526bf5 2021-09-03 naddy }
5504 e4526bf5 2021-09-03 naddy break;
5505 e4526bf5 2021-09-03 naddy }
5506 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5507 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5508 e4526bf5 2021-09-03 naddy }
5509 e4526bf5 2021-09-03 naddy if (n > 0)
5510 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5511 e4526bf5 2021-09-03 naddy break;
5512 1e37a5c2 2019-05-12 jcs case 'k':
5513 1e37a5c2 2019-05-12 jcs case KEY_UP:
5514 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5515 1e37a5c2 2019-05-12 jcs s->selected--;
5516 fa86c4bf 2020-11-29 stsp break;
5517 1e37a5c2 2019-05-12 jcs }
5518 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5519 1e37a5c2 2019-05-12 jcs break;
5520 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5521 ea025d1d 2020-02-22 naddy case CTRL('b'):
5522 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5523 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5524 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5525 fa86c4bf 2020-11-29 stsp s->selected = 0;
5526 fa86c4bf 2020-11-29 stsp } else {
5527 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5528 fa86c4bf 2020-11-29 stsp s->selected = 0;
5529 fa86c4bf 2020-11-29 stsp }
5530 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5531 1e37a5c2 2019-05-12 jcs break;
5532 1e37a5c2 2019-05-12 jcs case 'j':
5533 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5534 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5535 1e37a5c2 2019-05-12 jcs s->selected++;
5536 1e37a5c2 2019-05-12 jcs break;
5537 1e37a5c2 2019-05-12 jcs }
5538 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5539 56e0773d 2019-11-28 stsp == NULL)
5540 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5541 1e37a5c2 2019-05-12 jcs break;
5542 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5543 1e37a5c2 2019-05-12 jcs break;
5544 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5545 ea025d1d 2020-02-22 naddy case CTRL('f'):
5546 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5547 1e37a5c2 2019-05-12 jcs == NULL) {
5548 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5549 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5550 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5551 1e37a5c2 2019-05-12 jcs break;
5552 1e37a5c2 2019-05-12 jcs }
5553 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5554 1e37a5c2 2019-05-12 jcs break;
5555 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5556 1e37a5c2 2019-05-12 jcs case '\r':
5557 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5558 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5559 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5560 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5561 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5562 1e37a5c2 2019-05-12 jcs break;
5563 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5564 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5565 1e37a5c2 2019-05-12 jcs entry);
5566 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5567 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5568 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5569 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5570 1e37a5c2 2019-05-12 jcs s->selected_entry =
5571 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5572 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5573 1e37a5c2 2019-05-12 jcs free(parent);
5574 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5575 56e0773d 2019-11-28 stsp s->selected_entry))) {
5576 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5577 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5578 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5579 1e37a5c2 2019-05-12 jcs if (err)
5580 1e37a5c2 2019-05-12 jcs break;
5581 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5582 941e9f74 2019-05-21 stsp if (err) {
5583 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5584 1e37a5c2 2019-05-12 jcs break;
5585 1e37a5c2 2019-05-12 jcs }
5586 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5587 56e0773d 2019-11-28 stsp s->selected_entry))) {
5588 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5589 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5590 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5591 1e37a5c2 2019-05-12 jcs
5592 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5593 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5594 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5595 1e37a5c2 2019-05-12 jcs if (err)
5596 1e37a5c2 2019-05-12 jcs break;
5597 e78dc838 2020-12-04 stsp view->focussed = 0;
5598 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5599 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5600 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5601 669b5ffa 2018-10-07 stsp if (err)
5602 669b5ffa 2018-10-07 stsp return err;
5603 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5604 e78dc838 2020-12-04 stsp view->focus_child = 1;
5605 669b5ffa 2018-10-07 stsp } else
5606 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5607 1e37a5c2 2019-05-12 jcs }
5608 1e37a5c2 2019-05-12 jcs break;
5609 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5610 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5611 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5612 1e37a5c2 2019-05-12 jcs break;
5613 1e37a5c2 2019-05-12 jcs default:
5614 1e37a5c2 2019-05-12 jcs break;
5615 ffd1d5e5 2018-06-23 stsp }
5616 e5a0f69f 2018-08-18 stsp
5617 ffd1d5e5 2018-06-23 stsp return err;
5618 9f7d7167 2018-04-29 stsp }
5619 9f7d7167 2018-04-29 stsp
5620 ffd1d5e5 2018-06-23 stsp __dead static void
5621 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5622 ffd1d5e5 2018-06-23 stsp {
5623 ffd1d5e5 2018-06-23 stsp endwin();
5624 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5625 ffd1d5e5 2018-06-23 stsp getprogname());
5626 ffd1d5e5 2018-06-23 stsp exit(1);
5627 ffd1d5e5 2018-06-23 stsp }
5628 ffd1d5e5 2018-06-23 stsp
5629 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5630 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5631 ffd1d5e5 2018-06-23 stsp {
5632 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5633 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5634 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5635 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5636 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5637 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5638 4e97c21c 2020-12-06 stsp char *label = NULL;
5639 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5640 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5641 ffd1d5e5 2018-06-23 stsp int ch;
5642 5221c383 2018-08-01 stsp struct tog_view *view;
5643 70ac5f84 2019-03-28 stsp
5644 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5645 ffd1d5e5 2018-06-23 stsp switch (ch) {
5646 ffd1d5e5 2018-06-23 stsp case 'c':
5647 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5648 74283ab8 2020-02-07 stsp break;
5649 74283ab8 2020-02-07 stsp case 'r':
5650 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5651 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5652 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5653 74283ab8 2020-02-07 stsp optarg);
5654 ffd1d5e5 2018-06-23 stsp break;
5655 ffd1d5e5 2018-06-23 stsp default:
5656 e99e2d15 2020-11-24 naddy usage_tree();
5657 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5658 ffd1d5e5 2018-06-23 stsp }
5659 ffd1d5e5 2018-06-23 stsp }
5660 ffd1d5e5 2018-06-23 stsp
5661 ffd1d5e5 2018-06-23 stsp argc -= optind;
5662 ffd1d5e5 2018-06-23 stsp argv += optind;
5663 ffd1d5e5 2018-06-23 stsp
5664 55cccc34 2020-02-20 stsp if (argc > 1)
5665 e99e2d15 2020-11-24 naddy usage_tree();
5666 74283ab8 2020-02-07 stsp
5667 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5668 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5669 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5670 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5671 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5672 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5673 c156c7a4 2020-12-18 stsp goto done;
5674 55cccc34 2020-02-20 stsp if (worktree)
5675 52185f70 2019-02-05 stsp repo_path =
5676 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5677 55cccc34 2020-02-20 stsp else
5678 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5679 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5680 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5681 c156c7a4 2020-12-18 stsp goto done;
5682 c156c7a4 2020-12-18 stsp }
5683 55cccc34 2020-02-20 stsp }
5684 a915003a 2019-02-05 stsp
5685 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5686 c02c541e 2019-03-29 stsp if (error != NULL)
5687 52185f70 2019-02-05 stsp goto done;
5688 d188b9a6 2019-01-04 stsp
5689 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5690 55cccc34 2020-02-20 stsp repo, worktree);
5691 55cccc34 2020-02-20 stsp if (error)
5692 55cccc34 2020-02-20 stsp goto done;
5693 55cccc34 2020-02-20 stsp
5694 55cccc34 2020-02-20 stsp init_curses();
5695 55cccc34 2020-02-20 stsp
5696 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5697 c02c541e 2019-03-29 stsp if (error)
5698 52185f70 2019-02-05 stsp goto done;
5699 ffd1d5e5 2018-06-23 stsp
5700 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
5701 51a10b52 2020-12-26 stsp if (error)
5702 51a10b52 2020-12-26 stsp goto done;
5703 51a10b52 2020-12-26 stsp
5704 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5705 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5706 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5707 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5708 4e97c21c 2020-12-06 stsp if (error)
5709 4e97c21c 2020-12-06 stsp goto done;
5710 4e97c21c 2020-12-06 stsp head_ref_name = label;
5711 4e97c21c 2020-12-06 stsp } else {
5712 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5713 4e97c21c 2020-12-06 stsp if (error == NULL)
5714 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5715 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5716 4e97c21c 2020-12-06 stsp goto done;
5717 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5718 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5719 4e97c21c 2020-12-06 stsp if (error)
5720 4e97c21c 2020-12-06 stsp goto done;
5721 4e97c21c 2020-12-06 stsp }
5722 ffd1d5e5 2018-06-23 stsp
5723 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5724 5221c383 2018-08-01 stsp if (view == NULL) {
5725 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5726 5221c383 2018-08-01 stsp goto done;
5727 5221c383 2018-08-01 stsp }
5728 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5729 ad80ab7b 2018-08-04 stsp if (error)
5730 ad80ab7b 2018-08-04 stsp goto done;
5731 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5732 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5733 d91faf3b 2020-12-01 naddy in_repo_path);
5734 55cccc34 2020-02-20 stsp if (error)
5735 55cccc34 2020-02-20 stsp goto done;
5736 55cccc34 2020-02-20 stsp }
5737 55cccc34 2020-02-20 stsp
5738 55cccc34 2020-02-20 stsp if (worktree) {
5739 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5740 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5741 55cccc34 2020-02-20 stsp worktree = NULL;
5742 55cccc34 2020-02-20 stsp }
5743 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5744 ffd1d5e5 2018-06-23 stsp done:
5745 52185f70 2019-02-05 stsp free(repo_path);
5746 e4a0e26d 2020-02-20 stsp free(cwd);
5747 ffd1d5e5 2018-06-23 stsp free(commit_id);
5748 4e97c21c 2020-12-06 stsp free(label);
5749 486cd271 2020-12-06 stsp if (ref)
5750 486cd271 2020-12-06 stsp got_ref_close(ref);
5751 1d0f4054 2021-06-17 stsp if (repo) {
5752 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5753 1d0f4054 2021-06-17 stsp if (error == NULL)
5754 1d0f4054 2021-06-17 stsp error = close_err;
5755 1d0f4054 2021-06-17 stsp }
5756 51a10b52 2020-12-26 stsp tog_free_refs();
5757 ffd1d5e5 2018-06-23 stsp return error;
5758 6458efa5 2020-11-24 stsp }
5759 6458efa5 2020-11-24 stsp
5760 6458efa5 2020-11-24 stsp static const struct got_error *
5761 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5762 6458efa5 2020-11-24 stsp {
5763 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5764 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5765 6458efa5 2020-11-24 stsp
5766 6458efa5 2020-11-24 stsp s->nrefs = 0;
5767 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5768 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5769 6458efa5 2020-11-24 stsp continue;
5770 6458efa5 2020-11-24 stsp
5771 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5772 6458efa5 2020-11-24 stsp if (re == NULL)
5773 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5774 6458efa5 2020-11-24 stsp
5775 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5776 8924d611 2020-12-26 stsp if (re->ref == NULL)
5777 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5778 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5779 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5780 6458efa5 2020-11-24 stsp }
5781 6458efa5 2020-11-24 stsp
5782 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5783 6458efa5 2020-11-24 stsp return NULL;
5784 6458efa5 2020-11-24 stsp }
5785 6458efa5 2020-11-24 stsp
5786 6458efa5 2020-11-24 stsp void
5787 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5788 6458efa5 2020-11-24 stsp {
5789 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5790 6458efa5 2020-11-24 stsp
5791 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5792 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5793 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5794 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5795 6458efa5 2020-11-24 stsp free(re);
5796 6458efa5 2020-11-24 stsp }
5797 6458efa5 2020-11-24 stsp }
5798 6458efa5 2020-11-24 stsp
5799 6458efa5 2020-11-24 stsp static const struct got_error *
5800 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5801 6458efa5 2020-11-24 stsp {
5802 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5803 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5804 6458efa5 2020-11-24 stsp
5805 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5806 6458efa5 2020-11-24 stsp s->repo = repo;
5807 6458efa5 2020-11-24 stsp
5808 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5809 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5810 6458efa5 2020-11-24 stsp
5811 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5812 6458efa5 2020-11-24 stsp if (err)
5813 6458efa5 2020-11-24 stsp return err;
5814 34ba6917 2020-11-30 stsp
5815 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5816 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5817 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5818 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5819 6458efa5 2020-11-24 stsp if (err)
5820 6458efa5 2020-11-24 stsp goto done;
5821 6458efa5 2020-11-24 stsp
5822 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5823 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5824 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5825 6458efa5 2020-11-24 stsp if (err)
5826 6458efa5 2020-11-24 stsp goto done;
5827 6458efa5 2020-11-24 stsp
5828 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5829 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5830 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5831 6458efa5 2020-11-24 stsp if (err)
5832 6458efa5 2020-11-24 stsp goto done;
5833 6458efa5 2020-11-24 stsp }
5834 6458efa5 2020-11-24 stsp
5835 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5836 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5837 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5838 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5839 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5840 6458efa5 2020-11-24 stsp done:
5841 6458efa5 2020-11-24 stsp if (err)
5842 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5843 6458efa5 2020-11-24 stsp return err;
5844 6458efa5 2020-11-24 stsp }
5845 6458efa5 2020-11-24 stsp
5846 6458efa5 2020-11-24 stsp static const struct got_error *
5847 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5848 6458efa5 2020-11-24 stsp {
5849 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5850 6458efa5 2020-11-24 stsp
5851 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5852 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5853 6458efa5 2020-11-24 stsp
5854 6458efa5 2020-11-24 stsp return NULL;
5855 9f7d7167 2018-04-29 stsp }
5856 ce5b7c56 2019-07-09 stsp
5857 6458efa5 2020-11-24 stsp static const struct got_error *
5858 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5859 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5860 6458efa5 2020-11-24 stsp {
5861 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5862 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5863 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5864 6458efa5 2020-11-24 stsp int obj_type;
5865 6458efa5 2020-11-24 stsp
5866 c42c9805 2020-11-24 stsp *commit_id = NULL;
5867 6458efa5 2020-11-24 stsp
5868 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5869 6458efa5 2020-11-24 stsp if (err)
5870 6458efa5 2020-11-24 stsp return err;
5871 6458efa5 2020-11-24 stsp
5872 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5873 6458efa5 2020-11-24 stsp if (err)
5874 6458efa5 2020-11-24 stsp goto done;
5875 6458efa5 2020-11-24 stsp
5876 6458efa5 2020-11-24 stsp switch (obj_type) {
5877 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5878 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5879 6458efa5 2020-11-24 stsp break;
5880 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5881 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5882 6458efa5 2020-11-24 stsp if (err)
5883 6458efa5 2020-11-24 stsp goto done;
5884 c42c9805 2020-11-24 stsp free(obj_id);
5885 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5886 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5887 c42c9805 2020-11-24 stsp if (err)
5888 6458efa5 2020-11-24 stsp goto done;
5889 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5890 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5891 c42c9805 2020-11-24 stsp goto done;
5892 c42c9805 2020-11-24 stsp }
5893 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5894 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5895 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5896 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5897 c42c9805 2020-11-24 stsp goto done;
5898 c42c9805 2020-11-24 stsp }
5899 6458efa5 2020-11-24 stsp break;
5900 6458efa5 2020-11-24 stsp default:
5901 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5902 c42c9805 2020-11-24 stsp break;
5903 6458efa5 2020-11-24 stsp }
5904 6458efa5 2020-11-24 stsp
5905 c42c9805 2020-11-24 stsp done:
5906 c42c9805 2020-11-24 stsp if (tag)
5907 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5908 c42c9805 2020-11-24 stsp if (err) {
5909 c42c9805 2020-11-24 stsp free(*commit_id);
5910 c42c9805 2020-11-24 stsp *commit_id = NULL;
5911 c42c9805 2020-11-24 stsp }
5912 c42c9805 2020-11-24 stsp return err;
5913 c42c9805 2020-11-24 stsp }
5914 c42c9805 2020-11-24 stsp
5915 c42c9805 2020-11-24 stsp static const struct got_error *
5916 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5917 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5918 c42c9805 2020-11-24 stsp {
5919 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5920 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5921 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5922 c42c9805 2020-11-24 stsp
5923 c42c9805 2020-11-24 stsp *new_view = NULL;
5924 c42c9805 2020-11-24 stsp
5925 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5926 c42c9805 2020-11-24 stsp if (err) {
5927 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5928 c42c9805 2020-11-24 stsp return err;
5929 c42c9805 2020-11-24 stsp else
5930 c42c9805 2020-11-24 stsp return NULL;
5931 c42c9805 2020-11-24 stsp }
5932 c42c9805 2020-11-24 stsp
5933 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5934 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5935 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5936 6458efa5 2020-11-24 stsp goto done;
5937 6458efa5 2020-11-24 stsp }
5938 6458efa5 2020-11-24 stsp
5939 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5940 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5941 6458efa5 2020-11-24 stsp done:
5942 6458efa5 2020-11-24 stsp if (err)
5943 6458efa5 2020-11-24 stsp view_close(log_view);
5944 6458efa5 2020-11-24 stsp else
5945 6458efa5 2020-11-24 stsp *new_view = log_view;
5946 c42c9805 2020-11-24 stsp free(commit_id);
5947 6458efa5 2020-11-24 stsp return err;
5948 6458efa5 2020-11-24 stsp }
5949 6458efa5 2020-11-24 stsp
5950 ce5b7c56 2019-07-09 stsp static void
5951 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5952 6458efa5 2020-11-24 stsp {
5953 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5954 34ba6917 2020-11-30 stsp int i = 0;
5955 6458efa5 2020-11-24 stsp
5956 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5957 6458efa5 2020-11-24 stsp return;
5958 6458efa5 2020-11-24 stsp
5959 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5960 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5961 34ba6917 2020-11-30 stsp if (re == NULL)
5962 34ba6917 2020-11-30 stsp break;
5963 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5964 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5965 6458efa5 2020-11-24 stsp }
5966 6458efa5 2020-11-24 stsp }
5967 6458efa5 2020-11-24 stsp
5968 34ba6917 2020-11-30 stsp static void
5969 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5970 6458efa5 2020-11-24 stsp {
5971 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5972 6458efa5 2020-11-24 stsp int n = 0;
5973 6458efa5 2020-11-24 stsp
5974 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5975 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5976 6458efa5 2020-11-24 stsp else
5977 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5978 6458efa5 2020-11-24 stsp
5979 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5980 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5981 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5982 6458efa5 2020-11-24 stsp if (last) {
5983 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5984 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5985 6458efa5 2020-11-24 stsp }
5986 6458efa5 2020-11-24 stsp }
5987 6458efa5 2020-11-24 stsp }
5988 6458efa5 2020-11-24 stsp
5989 6458efa5 2020-11-24 stsp static const struct got_error *
5990 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5991 6458efa5 2020-11-24 stsp {
5992 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5993 6458efa5 2020-11-24 stsp
5994 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5995 6458efa5 2020-11-24 stsp return NULL;
5996 6458efa5 2020-11-24 stsp }
5997 6458efa5 2020-11-24 stsp
5998 6458efa5 2020-11-24 stsp static int
5999 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6000 6458efa5 2020-11-24 stsp {
6001 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6002 6458efa5 2020-11-24 stsp
6003 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6004 6458efa5 2020-11-24 stsp 0) == 0;
6005 6458efa5 2020-11-24 stsp }
6006 6458efa5 2020-11-24 stsp
6007 6458efa5 2020-11-24 stsp static const struct got_error *
6008 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6009 6458efa5 2020-11-24 stsp {
6010 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6011 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6012 6458efa5 2020-11-24 stsp
6013 6458efa5 2020-11-24 stsp if (!view->searching) {
6014 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6015 6458efa5 2020-11-24 stsp return NULL;
6016 6458efa5 2020-11-24 stsp }
6017 6458efa5 2020-11-24 stsp
6018 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6019 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6020 6458efa5 2020-11-24 stsp if (s->selected_entry)
6021 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6022 6458efa5 2020-11-24 stsp else
6023 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6024 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6025 6458efa5 2020-11-24 stsp } else {
6026 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6027 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6028 6458efa5 2020-11-24 stsp else
6029 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6030 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6031 6458efa5 2020-11-24 stsp }
6032 6458efa5 2020-11-24 stsp } else {
6033 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6034 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6035 6458efa5 2020-11-24 stsp else
6036 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6037 6458efa5 2020-11-24 stsp }
6038 6458efa5 2020-11-24 stsp
6039 6458efa5 2020-11-24 stsp while (1) {
6040 6458efa5 2020-11-24 stsp if (re == NULL) {
6041 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6042 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6043 6458efa5 2020-11-24 stsp return NULL;
6044 6458efa5 2020-11-24 stsp }
6045 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6046 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6047 6458efa5 2020-11-24 stsp else
6048 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6049 6458efa5 2020-11-24 stsp }
6050 6458efa5 2020-11-24 stsp
6051 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6052 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6053 6458efa5 2020-11-24 stsp s->matched_entry = re;
6054 6458efa5 2020-11-24 stsp break;
6055 6458efa5 2020-11-24 stsp }
6056 6458efa5 2020-11-24 stsp
6057 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6058 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6059 6458efa5 2020-11-24 stsp else
6060 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6061 6458efa5 2020-11-24 stsp }
6062 6458efa5 2020-11-24 stsp
6063 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6064 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6065 6458efa5 2020-11-24 stsp s->selected = 0;
6066 6458efa5 2020-11-24 stsp }
6067 6458efa5 2020-11-24 stsp
6068 6458efa5 2020-11-24 stsp return NULL;
6069 6458efa5 2020-11-24 stsp }
6070 6458efa5 2020-11-24 stsp
6071 6458efa5 2020-11-24 stsp static const struct got_error *
6072 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6073 6458efa5 2020-11-24 stsp {
6074 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6075 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6076 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6077 6458efa5 2020-11-24 stsp char *line = NULL;
6078 6458efa5 2020-11-24 stsp wchar_t *wline;
6079 6458efa5 2020-11-24 stsp struct tog_color *tc;
6080 6458efa5 2020-11-24 stsp int width, n;
6081 6458efa5 2020-11-24 stsp int limit = view->nlines;
6082 6458efa5 2020-11-24 stsp
6083 6458efa5 2020-11-24 stsp werase(view->window);
6084 6458efa5 2020-11-24 stsp
6085 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6086 6458efa5 2020-11-24 stsp
6087 6458efa5 2020-11-24 stsp if (limit == 0)
6088 6458efa5 2020-11-24 stsp return NULL;
6089 6458efa5 2020-11-24 stsp
6090 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6091 6458efa5 2020-11-24 stsp
6092 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6093 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6094 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6095 6458efa5 2020-11-24 stsp
6096 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6097 6458efa5 2020-11-24 stsp if (err) {
6098 6458efa5 2020-11-24 stsp free(line);
6099 6458efa5 2020-11-24 stsp return err;
6100 6458efa5 2020-11-24 stsp }
6101 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6102 6458efa5 2020-11-24 stsp wstandout(view->window);
6103 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6104 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6105 6458efa5 2020-11-24 stsp wstandend(view->window);
6106 6458efa5 2020-11-24 stsp free(wline);
6107 6458efa5 2020-11-24 stsp wline = NULL;
6108 6458efa5 2020-11-24 stsp free(line);
6109 6458efa5 2020-11-24 stsp line = NULL;
6110 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6111 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6112 6458efa5 2020-11-24 stsp if (--limit <= 0)
6113 6458efa5 2020-11-24 stsp return NULL;
6114 6458efa5 2020-11-24 stsp
6115 6458efa5 2020-11-24 stsp n = 0;
6116 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6117 6458efa5 2020-11-24 stsp char *line = NULL;
6118 6458efa5 2020-11-24 stsp
6119 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6120 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6121 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6122 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6123 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6124 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6125 6458efa5 2020-11-24 stsp struct got_object_id *id;
6126 6458efa5 2020-11-24 stsp char *id_str;
6127 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6128 6458efa5 2020-11-24 stsp if (err)
6129 6458efa5 2020-11-24 stsp return err;
6130 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6131 6458efa5 2020-11-24 stsp if (err) {
6132 6458efa5 2020-11-24 stsp free(id);
6133 6458efa5 2020-11-24 stsp return err;
6134 6458efa5 2020-11-24 stsp }
6135 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6136 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6137 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6138 6458efa5 2020-11-24 stsp free(id);
6139 6458efa5 2020-11-24 stsp free(id_str);
6140 6458efa5 2020-11-24 stsp return err;
6141 6458efa5 2020-11-24 stsp }
6142 6458efa5 2020-11-24 stsp free(id);
6143 6458efa5 2020-11-24 stsp free(id_str);
6144 6458efa5 2020-11-24 stsp } else {
6145 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6146 6458efa5 2020-11-24 stsp if (line == NULL)
6147 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6148 6458efa5 2020-11-24 stsp }
6149 6458efa5 2020-11-24 stsp
6150 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6151 6458efa5 2020-11-24 stsp if (err) {
6152 6458efa5 2020-11-24 stsp free(line);
6153 6458efa5 2020-11-24 stsp return err;
6154 6458efa5 2020-11-24 stsp }
6155 6458efa5 2020-11-24 stsp if (n == s->selected) {
6156 6458efa5 2020-11-24 stsp if (view->focussed)
6157 6458efa5 2020-11-24 stsp wstandout(view->window);
6158 6458efa5 2020-11-24 stsp s->selected_entry = re;
6159 6458efa5 2020-11-24 stsp }
6160 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6161 6458efa5 2020-11-24 stsp if (tc)
6162 6458efa5 2020-11-24 stsp wattr_on(view->window,
6163 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6164 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6165 6458efa5 2020-11-24 stsp if (tc)
6166 6458efa5 2020-11-24 stsp wattr_off(view->window,
6167 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6168 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6169 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6170 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6171 6458efa5 2020-11-24 stsp wstandend(view->window);
6172 6458efa5 2020-11-24 stsp free(line);
6173 6458efa5 2020-11-24 stsp free(wline);
6174 6458efa5 2020-11-24 stsp wline = NULL;
6175 6458efa5 2020-11-24 stsp n++;
6176 6458efa5 2020-11-24 stsp s->ndisplayed++;
6177 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6178 6458efa5 2020-11-24 stsp
6179 6458efa5 2020-11-24 stsp limit--;
6180 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6181 6458efa5 2020-11-24 stsp }
6182 6458efa5 2020-11-24 stsp
6183 6458efa5 2020-11-24 stsp view_vborder(view);
6184 6458efa5 2020-11-24 stsp return err;
6185 6458efa5 2020-11-24 stsp }
6186 6458efa5 2020-11-24 stsp
6187 6458efa5 2020-11-24 stsp static const struct got_error *
6188 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6189 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6190 c42c9805 2020-11-24 stsp {
6191 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6192 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6193 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6194 c42c9805 2020-11-24 stsp
6195 c42c9805 2020-11-24 stsp *new_view = NULL;
6196 c42c9805 2020-11-24 stsp
6197 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6198 c42c9805 2020-11-24 stsp if (err) {
6199 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6200 c42c9805 2020-11-24 stsp return err;
6201 c42c9805 2020-11-24 stsp else
6202 c42c9805 2020-11-24 stsp return NULL;
6203 c42c9805 2020-11-24 stsp }
6204 c42c9805 2020-11-24 stsp
6205 c42c9805 2020-11-24 stsp
6206 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6207 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6208 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6209 c42c9805 2020-11-24 stsp goto done;
6210 c42c9805 2020-11-24 stsp }
6211 c42c9805 2020-11-24 stsp
6212 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6213 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6214 c42c9805 2020-11-24 stsp if (err)
6215 c42c9805 2020-11-24 stsp goto done;
6216 c42c9805 2020-11-24 stsp
6217 c42c9805 2020-11-24 stsp *new_view = tree_view;
6218 c42c9805 2020-11-24 stsp done:
6219 c42c9805 2020-11-24 stsp free(commit_id);
6220 c42c9805 2020-11-24 stsp return err;
6221 c42c9805 2020-11-24 stsp }
6222 c42c9805 2020-11-24 stsp static const struct got_error *
6223 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6224 6458efa5 2020-11-24 stsp {
6225 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6226 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6227 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6228 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6229 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6230 6458efa5 2020-11-24 stsp
6231 6458efa5 2020-11-24 stsp switch (ch) {
6232 6458efa5 2020-11-24 stsp case 'i':
6233 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6234 6458efa5 2020-11-24 stsp break;
6235 6458efa5 2020-11-24 stsp case KEY_ENTER:
6236 6458efa5 2020-11-24 stsp case '\r':
6237 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6238 6458efa5 2020-11-24 stsp break;
6239 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6240 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6241 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6242 6458efa5 2020-11-24 stsp s->repo);
6243 e78dc838 2020-12-04 stsp view->focussed = 0;
6244 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6245 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6246 6458efa5 2020-11-24 stsp err = view_close_child(view);
6247 6458efa5 2020-11-24 stsp if (err)
6248 6458efa5 2020-11-24 stsp return err;
6249 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6250 e78dc838 2020-12-04 stsp view->focus_child = 1;
6251 6458efa5 2020-11-24 stsp } else
6252 6458efa5 2020-11-24 stsp *new_view = log_view;
6253 6458efa5 2020-11-24 stsp break;
6254 c42c9805 2020-11-24 stsp case 't':
6255 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6256 c42c9805 2020-11-24 stsp break;
6257 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6258 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6259 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6260 c42c9805 2020-11-24 stsp s->repo);
6261 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6262 c42c9805 2020-11-24 stsp break;
6263 e78dc838 2020-12-04 stsp view->focussed = 0;
6264 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6265 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6266 c42c9805 2020-11-24 stsp err = view_close_child(view);
6267 c42c9805 2020-11-24 stsp if (err)
6268 c42c9805 2020-11-24 stsp return err;
6269 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6270 e78dc838 2020-12-04 stsp view->focus_child = 1;
6271 c42c9805 2020-11-24 stsp } else
6272 c42c9805 2020-11-24 stsp *new_view = tree_view;
6273 c42c9805 2020-11-24 stsp break;
6274 e4526bf5 2021-09-03 naddy case 'g':
6275 e4526bf5 2021-09-03 naddy case KEY_HOME:
6276 e4526bf5 2021-09-03 naddy s->selected = 0;
6277 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6278 e4526bf5 2021-09-03 naddy break;
6279 e4526bf5 2021-09-03 naddy case 'G':
6280 e4526bf5 2021-09-03 naddy case KEY_END:
6281 e4526bf5 2021-09-03 naddy s->selected = 0;
6282 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6283 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6284 e4526bf5 2021-09-03 naddy if (re == NULL)
6285 e4526bf5 2021-09-03 naddy break;
6286 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6287 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6288 e4526bf5 2021-09-03 naddy }
6289 e4526bf5 2021-09-03 naddy if (n > 0)
6290 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6291 e4526bf5 2021-09-03 naddy break;
6292 6458efa5 2020-11-24 stsp case 'k':
6293 6458efa5 2020-11-24 stsp case KEY_UP:
6294 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6295 6458efa5 2020-11-24 stsp s->selected--;
6296 6458efa5 2020-11-24 stsp break;
6297 34ba6917 2020-11-30 stsp }
6298 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6299 6458efa5 2020-11-24 stsp break;
6300 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6301 6458efa5 2020-11-24 stsp case CTRL('b'):
6302 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6303 34ba6917 2020-11-30 stsp s->selected = 0;
6304 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6305 6458efa5 2020-11-24 stsp break;
6306 6458efa5 2020-11-24 stsp case 'j':
6307 6458efa5 2020-11-24 stsp case KEY_DOWN:
6308 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6309 6458efa5 2020-11-24 stsp s->selected++;
6310 6458efa5 2020-11-24 stsp break;
6311 6458efa5 2020-11-24 stsp }
6312 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6313 6458efa5 2020-11-24 stsp /* can't scroll any further */
6314 6458efa5 2020-11-24 stsp break;
6315 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6316 6458efa5 2020-11-24 stsp break;
6317 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6318 6458efa5 2020-11-24 stsp case CTRL('f'):
6319 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6320 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6321 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6322 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6323 6458efa5 2020-11-24 stsp break;
6324 6458efa5 2020-11-24 stsp }
6325 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6326 6458efa5 2020-11-24 stsp break;
6327 6458efa5 2020-11-24 stsp case CTRL('l'):
6328 8924d611 2020-12-26 stsp tog_free_refs();
6329 8924d611 2020-12-26 stsp err = tog_load_refs(s->repo);
6330 8924d611 2020-12-26 stsp if (err)
6331 8924d611 2020-12-26 stsp break;
6332 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6333 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6334 6458efa5 2020-11-24 stsp break;
6335 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6336 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6337 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6338 6458efa5 2020-11-24 stsp break;
6339 6458efa5 2020-11-24 stsp default:
6340 6458efa5 2020-11-24 stsp break;
6341 6458efa5 2020-11-24 stsp }
6342 6458efa5 2020-11-24 stsp
6343 6458efa5 2020-11-24 stsp return err;
6344 6458efa5 2020-11-24 stsp }
6345 6458efa5 2020-11-24 stsp
6346 6458efa5 2020-11-24 stsp __dead static void
6347 6458efa5 2020-11-24 stsp usage_ref(void)
6348 6458efa5 2020-11-24 stsp {
6349 6458efa5 2020-11-24 stsp endwin();
6350 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6351 6458efa5 2020-11-24 stsp getprogname());
6352 6458efa5 2020-11-24 stsp exit(1);
6353 6458efa5 2020-11-24 stsp }
6354 6458efa5 2020-11-24 stsp
6355 6458efa5 2020-11-24 stsp static const struct got_error *
6356 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6357 6458efa5 2020-11-24 stsp {
6358 6458efa5 2020-11-24 stsp const struct got_error *error;
6359 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6360 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6361 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6362 6458efa5 2020-11-24 stsp int ch;
6363 6458efa5 2020-11-24 stsp struct tog_view *view;
6364 6458efa5 2020-11-24 stsp
6365 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6366 6458efa5 2020-11-24 stsp switch (ch) {
6367 6458efa5 2020-11-24 stsp case 'r':
6368 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6369 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6370 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6371 6458efa5 2020-11-24 stsp optarg);
6372 6458efa5 2020-11-24 stsp break;
6373 6458efa5 2020-11-24 stsp default:
6374 e99e2d15 2020-11-24 naddy usage_ref();
6375 6458efa5 2020-11-24 stsp /* NOTREACHED */
6376 6458efa5 2020-11-24 stsp }
6377 6458efa5 2020-11-24 stsp }
6378 6458efa5 2020-11-24 stsp
6379 6458efa5 2020-11-24 stsp argc -= optind;
6380 6458efa5 2020-11-24 stsp argv += optind;
6381 6458efa5 2020-11-24 stsp
6382 6458efa5 2020-11-24 stsp if (argc > 1)
6383 e99e2d15 2020-11-24 naddy usage_ref();
6384 6458efa5 2020-11-24 stsp
6385 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6386 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6387 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6388 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6389 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6390 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6391 c156c7a4 2020-12-18 stsp goto done;
6392 6458efa5 2020-11-24 stsp if (worktree)
6393 6458efa5 2020-11-24 stsp repo_path =
6394 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6395 6458efa5 2020-11-24 stsp else
6396 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6397 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6398 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6399 c156c7a4 2020-12-18 stsp goto done;
6400 c156c7a4 2020-12-18 stsp }
6401 6458efa5 2020-11-24 stsp }
6402 6458efa5 2020-11-24 stsp
6403 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6404 6458efa5 2020-11-24 stsp if (error != NULL)
6405 6458efa5 2020-11-24 stsp goto done;
6406 6458efa5 2020-11-24 stsp
6407 6458efa5 2020-11-24 stsp init_curses();
6408 6458efa5 2020-11-24 stsp
6409 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6410 51a10b52 2020-12-26 stsp if (error)
6411 51a10b52 2020-12-26 stsp goto done;
6412 51a10b52 2020-12-26 stsp
6413 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
6414 6458efa5 2020-11-24 stsp if (error)
6415 6458efa5 2020-11-24 stsp goto done;
6416 6458efa5 2020-11-24 stsp
6417 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6418 6458efa5 2020-11-24 stsp if (view == NULL) {
6419 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6420 6458efa5 2020-11-24 stsp goto done;
6421 6458efa5 2020-11-24 stsp }
6422 6458efa5 2020-11-24 stsp
6423 6458efa5 2020-11-24 stsp error = open_ref_view(view, 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 if (worktree) {
6428 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6429 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6430 6458efa5 2020-11-24 stsp worktree = NULL;
6431 6458efa5 2020-11-24 stsp }
6432 6458efa5 2020-11-24 stsp error = view_loop(view);
6433 6458efa5 2020-11-24 stsp done:
6434 6458efa5 2020-11-24 stsp free(repo_path);
6435 6458efa5 2020-11-24 stsp free(cwd);
6436 1d0f4054 2021-06-17 stsp if (repo) {
6437 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6438 1d0f4054 2021-06-17 stsp if (close_err)
6439 1d0f4054 2021-06-17 stsp error = close_err;
6440 1d0f4054 2021-06-17 stsp }
6441 51a10b52 2020-12-26 stsp tog_free_refs();
6442 6458efa5 2020-11-24 stsp return error;
6443 6458efa5 2020-11-24 stsp }
6444 6458efa5 2020-11-24 stsp
6445 6458efa5 2020-11-24 stsp static void
6446 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6447 ce5b7c56 2019-07-09 stsp {
6448 6059809a 2020-12-17 stsp size_t i;
6449 9f7d7167 2018-04-29 stsp
6450 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6451 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6452 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6453 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6454 ce5b7c56 2019-07-09 stsp }
6455 6879ba42 2020-10-01 naddy fputc('\n', fp);
6456 ce5b7c56 2019-07-09 stsp }
6457 ce5b7c56 2019-07-09 stsp
6458 4ed7e80c 2018-05-20 stsp __dead static void
6459 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6460 9f7d7167 2018-04-29 stsp {
6461 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6462 6879ba42 2020-10-01 naddy
6463 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6464 6879ba42 2020-10-01 naddy getprogname());
6465 6879ba42 2020-10-01 naddy if (hflag) {
6466 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6467 6879ba42 2020-10-01 naddy list_commands(fp);
6468 ee85c5e8 2020-02-29 stsp }
6469 6879ba42 2020-10-01 naddy exit(status);
6470 9f7d7167 2018-04-29 stsp }
6471 9f7d7167 2018-04-29 stsp
6472 c2301be8 2018-04-30 stsp static char **
6473 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6474 c2301be8 2018-04-30 stsp {
6475 ee85c5e8 2020-02-29 stsp va_list ap;
6476 c2301be8 2018-04-30 stsp char **argv;
6477 ee85c5e8 2020-02-29 stsp int i;
6478 c2301be8 2018-04-30 stsp
6479 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6480 ee85c5e8 2020-02-29 stsp
6481 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6482 c2301be8 2018-04-30 stsp if (argv == NULL)
6483 c2301be8 2018-04-30 stsp err(1, "calloc");
6484 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6485 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6486 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6487 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6488 c2301be8 2018-04-30 stsp }
6489 c2301be8 2018-04-30 stsp
6490 ee85c5e8 2020-02-29 stsp va_end(ap);
6491 c2301be8 2018-04-30 stsp return argv;
6492 ee85c5e8 2020-02-29 stsp }
6493 ee85c5e8 2020-02-29 stsp
6494 ee85c5e8 2020-02-29 stsp /*
6495 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6496 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6497 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6498 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6499 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6500 ee85c5e8 2020-02-29 stsp */
6501 ee85c5e8 2020-02-29 stsp static const struct got_error *
6502 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6503 ee85c5e8 2020-02-29 stsp {
6504 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6505 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6506 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6507 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6508 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6509 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6510 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6511 84de9106 2020-12-26 stsp
6512 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6513 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6514 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6515 ee85c5e8 2020-02-29 stsp
6516 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6517 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6518 ee85c5e8 2020-02-29 stsp goto done;
6519 ee85c5e8 2020-02-29 stsp
6520 ee85c5e8 2020-02-29 stsp if (worktree)
6521 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6522 ee85c5e8 2020-02-29 stsp else
6523 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6524 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6525 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6526 ee85c5e8 2020-02-29 stsp goto done;
6527 ee85c5e8 2020-02-29 stsp }
6528 ee85c5e8 2020-02-29 stsp
6529 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6530 ee85c5e8 2020-02-29 stsp if (error != NULL)
6531 ee85c5e8 2020-02-29 stsp goto done;
6532 ee85c5e8 2020-02-29 stsp
6533 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6534 ee85c5e8 2020-02-29 stsp repo, worktree);
6535 ee85c5e8 2020-02-29 stsp if (error)
6536 ee85c5e8 2020-02-29 stsp goto done;
6537 ee85c5e8 2020-02-29 stsp
6538 87670572 2020-12-26 naddy error = tog_load_refs(repo);
6539 84de9106 2020-12-26 stsp if (error)
6540 84de9106 2020-12-26 stsp goto done;
6541 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6542 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6543 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6544 ee85c5e8 2020-02-29 stsp if (error)
6545 ee85c5e8 2020-02-29 stsp goto done;
6546 ee85c5e8 2020-02-29 stsp
6547 ee85c5e8 2020-02-29 stsp if (worktree) {
6548 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6549 ee85c5e8 2020-02-29 stsp worktree = NULL;
6550 ee85c5e8 2020-02-29 stsp }
6551 ee85c5e8 2020-02-29 stsp
6552 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6553 ee85c5e8 2020-02-29 stsp if (error) {
6554 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6555 ee85c5e8 2020-02-29 stsp goto done;
6556 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6557 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6558 6879ba42 2020-10-01 naddy usage(1, 1);
6559 ee85c5e8 2020-02-29 stsp /* not reached */
6560 ee85c5e8 2020-02-29 stsp }
6561 ee85c5e8 2020-02-29 stsp
6562 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6563 1d0f4054 2021-06-17 stsp if (error == NULL)
6564 1d0f4054 2021-06-17 stsp error = close_err;
6565 ee85c5e8 2020-02-29 stsp repo = NULL;
6566 ee85c5e8 2020-02-29 stsp
6567 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6568 ee85c5e8 2020-02-29 stsp if (error)
6569 ee85c5e8 2020-02-29 stsp goto done;
6570 ee85c5e8 2020-02-29 stsp
6571 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6572 ee85c5e8 2020-02-29 stsp argc = 4;
6573 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6574 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6575 ee85c5e8 2020-02-29 stsp done:
6576 1d0f4054 2021-06-17 stsp if (repo) {
6577 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6578 1d0f4054 2021-06-17 stsp if (error == NULL)
6579 1d0f4054 2021-06-17 stsp error = close_err;
6580 1d0f4054 2021-06-17 stsp }
6581 ee85c5e8 2020-02-29 stsp if (worktree)
6582 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6583 ee85c5e8 2020-02-29 stsp free(id);
6584 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6585 ee85c5e8 2020-02-29 stsp free(commit_id);
6586 ee85c5e8 2020-02-29 stsp free(cwd);
6587 ee85c5e8 2020-02-29 stsp free(repo_path);
6588 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6589 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6590 ee85c5e8 2020-02-29 stsp int i;
6591 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6592 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6593 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6594 ee85c5e8 2020-02-29 stsp }
6595 87670572 2020-12-26 naddy tog_free_refs();
6596 ee85c5e8 2020-02-29 stsp return error;
6597 c2301be8 2018-04-30 stsp }
6598 c2301be8 2018-04-30 stsp
6599 9f7d7167 2018-04-29 stsp int
6600 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6601 9f7d7167 2018-04-29 stsp {
6602 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6603 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6604 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6605 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6606 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6607 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6608 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6609 83cd27f8 2020-01-13 stsp };
6610 9f7d7167 2018-04-29 stsp
6611 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6612 9f7d7167 2018-04-29 stsp
6613 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6614 9f7d7167 2018-04-29 stsp switch (ch) {
6615 9f7d7167 2018-04-29 stsp case 'h':
6616 9f7d7167 2018-04-29 stsp hflag = 1;
6617 9f7d7167 2018-04-29 stsp break;
6618 53ccebc2 2019-07-30 stsp case 'V':
6619 53ccebc2 2019-07-30 stsp Vflag = 1;
6620 53ccebc2 2019-07-30 stsp break;
6621 9f7d7167 2018-04-29 stsp default:
6622 6879ba42 2020-10-01 naddy usage(hflag, 1);
6623 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6624 9f7d7167 2018-04-29 stsp }
6625 9f7d7167 2018-04-29 stsp }
6626 9f7d7167 2018-04-29 stsp
6627 9f7d7167 2018-04-29 stsp argc -= optind;
6628 9f7d7167 2018-04-29 stsp argv += optind;
6629 9814e6a3 2020-09-27 naddy optind = 1;
6630 c2301be8 2018-04-30 stsp optreset = 1;
6631 9f7d7167 2018-04-29 stsp
6632 53ccebc2 2019-07-30 stsp if (Vflag) {
6633 53ccebc2 2019-07-30 stsp got_version_print_str();
6634 6879ba42 2020-10-01 naddy return 0;
6635 53ccebc2 2019-07-30 stsp }
6636 4010e238 2020-12-04 stsp
6637 4010e238 2020-12-04 stsp #ifndef PROFILE
6638 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6639 4010e238 2020-12-04 stsp NULL) == -1)
6640 4010e238 2020-12-04 stsp err(1, "pledge");
6641 4010e238 2020-12-04 stsp #endif
6642 53ccebc2 2019-07-30 stsp
6643 c2301be8 2018-04-30 stsp if (argc == 0) {
6644 f29d3e89 2018-06-23 stsp if (hflag)
6645 6879ba42 2020-10-01 naddy usage(hflag, 0);
6646 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6647 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6648 c2301be8 2018-04-30 stsp argc = 1;
6649 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6650 c2301be8 2018-04-30 stsp } else {
6651 6059809a 2020-12-17 stsp size_t i;
6652 9f7d7167 2018-04-29 stsp
6653 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6654 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6655 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6656 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6657 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6658 9f7d7167 2018-04-29 stsp break;
6659 9f7d7167 2018-04-29 stsp }
6660 9f7d7167 2018-04-29 stsp }
6661 ee85c5e8 2020-02-29 stsp }
6662 3642c4c6 2019-07-09 stsp
6663 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6664 ee85c5e8 2020-02-29 stsp if (argc != 1)
6665 6879ba42 2020-10-01 naddy usage(0, 1);
6666 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6667 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6668 ee85c5e8 2020-02-29 stsp } else {
6669 ee85c5e8 2020-02-29 stsp if (hflag)
6670 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6671 ee85c5e8 2020-02-29 stsp else
6672 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6673 9f7d7167 2018-04-29 stsp }
6674 9f7d7167 2018-04-29 stsp
6675 9f7d7167 2018-04-29 stsp endwin();
6676 b46c1e04 2020-09-20 naddy putchar('\n');
6677 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6678 a2f4a359 2020-02-28 stsp int i;
6679 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6680 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6681 a2f4a359 2020-02-28 stsp free(cmd_argv);
6682 a2f4a359 2020-02-28 stsp }
6683 a2f4a359 2020-02-28 stsp
6684 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6685 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6686 9f7d7167 2018-04-29 stsp return 0;
6687 9f7d7167 2018-04-29 stsp }