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