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