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