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