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