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