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 99db9666 2018-05-07 stsp }
1453 99db9666 2018-05-07 stsp
1454 99db9666 2018-05-07 stsp static const struct got_error *
1455 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1456 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1457 99db9666 2018-05-07 stsp {
1458 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1459 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1460 99db9666 2018-05-07 stsp
1461 9ba79e04 2018-06-11 stsp *head_id = NULL;
1462 899d86c2 2018-05-10 stsp
1463 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1464 99db9666 2018-05-07 stsp if (err)
1465 99db9666 2018-05-07 stsp return err;
1466 99db9666 2018-05-07 stsp
1467 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1468 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1469 9ba79e04 2018-06-11 stsp if (err) {
1470 9ba79e04 2018-06-11 stsp *head_id = NULL;
1471 99db9666 2018-05-07 stsp return err;
1472 0553a4e3 2018-04-30 stsp }
1473 80ddbec8 2018-04-29 stsp
1474 9ba79e04 2018-06-11 stsp return NULL;
1475 0553a4e3 2018-04-30 stsp }
1476 0553a4e3 2018-04-30 stsp
1477 0553a4e3 2018-04-30 stsp static const struct got_error *
1478 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1479 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1480 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1481 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1482 11b20872 2019-11-08 stsp struct tog_colors *colors)
1483 0553a4e3 2018-04-30 stsp {
1484 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1485 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1486 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1487 60493ae3 2019-06-20 stsp int width;
1488 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1489 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1490 8b473291 2019-02-21 stsp char *refs_str = NULL;
1491 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1492 11b20872 2019-11-08 stsp struct tog_color *tc;
1493 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1494 0553a4e3 2018-04-30 stsp
1495 e0d42f60 2018-07-22 stsp entry = first;
1496 e0d42f60 2018-07-22 stsp ncommits = 0;
1497 e0d42f60 2018-07-22 stsp while (entry) {
1498 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1499 e0d42f60 2018-07-22 stsp *selected = entry;
1500 e0d42f60 2018-07-22 stsp break;
1501 e0d42f60 2018-07-22 stsp }
1502 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1503 e0d42f60 2018-07-22 stsp ncommits++;
1504 e0d42f60 2018-07-22 stsp }
1505 e0d42f60 2018-07-22 stsp
1506 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1507 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1508 1a76625f 2018-10-22 stsp if (err)
1509 ecb28ae0 2018-07-16 stsp return err;
1510 8b473291 2019-02-21 stsp if (refs) {
1511 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1512 52b5abe1 2019-08-13 stsp s->repo);
1513 8b473291 2019-02-21 stsp if (err)
1514 8b473291 2019-02-21 stsp goto done;
1515 8b473291 2019-02-21 stsp }
1516 867c6645 2018-07-10 stsp }
1517 359bfafd 2019-02-22 stsp
1518 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1519 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1520 1a76625f 2018-10-22 stsp
1521 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1522 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1523 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1524 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1525 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1526 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1527 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1528 8b473291 2019-02-21 stsp goto done;
1529 8b473291 2019-02-21 stsp }
1530 1a76625f 2018-10-22 stsp
1531 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1532 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1533 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1534 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1535 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1536 1a76625f 2018-10-22 stsp header = NULL;
1537 1a76625f 2018-10-22 stsp goto done;
1538 1a76625f 2018-10-22 stsp }
1539 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1540 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1541 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1542 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1543 1a76625f 2018-10-22 stsp header = NULL;
1544 1a76625f 2018-10-22 stsp goto done;
1545 ecb28ae0 2018-07-16 stsp }
1546 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1547 1a76625f 2018-10-22 stsp if (err)
1548 1a76625f 2018-10-22 stsp goto done;
1549 867c6645 2018-07-10 stsp
1550 2814baeb 2018-08-01 stsp werase(view->window);
1551 867c6645 2018-07-10 stsp
1552 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1553 a3404814 2018-09-02 stsp wstandout(view->window);
1554 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1555 11b20872 2019-11-08 stsp if (tc)
1556 11b20872 2019-11-08 stsp wattr_on(view->window,
1557 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1558 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1559 11b20872 2019-11-08 stsp if (tc)
1560 11b20872 2019-11-08 stsp wattr_off(view->window,
1561 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1562 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1563 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1564 1a76625f 2018-10-22 stsp width++;
1565 1a76625f 2018-10-22 stsp }
1566 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1567 a3404814 2018-09-02 stsp wstandend(view->window);
1568 ecb28ae0 2018-07-16 stsp free(wline);
1569 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1570 1a76625f 2018-10-22 stsp goto done;
1571 0553a4e3 2018-04-30 stsp
1572 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1573 899d86c2 2018-05-10 stsp entry = first;
1574 5813d178 2019-03-09 stsp ncommits = 0;
1575 5813d178 2019-03-09 stsp while (entry) {
1576 5813d178 2019-03-09 stsp char *author;
1577 5813d178 2019-03-09 stsp wchar_t *wauthor;
1578 5813d178 2019-03-09 stsp int width;
1579 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1580 5813d178 2019-03-09 stsp break;
1581 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1582 5813d178 2019-03-09 stsp if (author == NULL) {
1583 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1584 5813d178 2019-03-09 stsp goto done;
1585 5813d178 2019-03-09 stsp }
1586 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1587 27a741e5 2019-09-11 stsp date_display_cols);
1588 5813d178 2019-03-09 stsp if (author_cols < width)
1589 5813d178 2019-03-09 stsp author_cols = width;
1590 5813d178 2019-03-09 stsp free(wauthor);
1591 5813d178 2019-03-09 stsp free(author);
1592 7ca04879 2019-10-19 stsp ncommits++;
1593 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1594 5813d178 2019-03-09 stsp }
1595 5813d178 2019-03-09 stsp
1596 5813d178 2019-03-09 stsp entry = first;
1597 899d86c2 2018-05-10 stsp *last = first;
1598 867c6645 2018-07-10 stsp ncommits = 0;
1599 899d86c2 2018-05-10 stsp while (entry) {
1600 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1601 899d86c2 2018-05-10 stsp break;
1602 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1603 2814baeb 2018-08-01 stsp wstandout(view->window);
1604 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1605 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1606 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1607 2814baeb 2018-08-01 stsp wstandend(view->window);
1608 0553a4e3 2018-04-30 stsp if (err)
1609 60493ae3 2019-06-20 stsp goto done;
1610 0553a4e3 2018-04-30 stsp ncommits++;
1611 899d86c2 2018-05-10 stsp *last = entry;
1612 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1613 80ddbec8 2018-04-29 stsp }
1614 80ddbec8 2018-04-29 stsp
1615 1a57306a 2018-09-02 stsp view_vborder(view);
1616 1a76625f 2018-10-22 stsp done:
1617 1a76625f 2018-10-22 stsp free(id_str);
1618 8b473291 2019-02-21 stsp free(refs_str);
1619 1a76625f 2018-10-22 stsp free(ncommits_str);
1620 1a76625f 2018-10-22 stsp free(header);
1621 80ddbec8 2018-04-29 stsp return err;
1622 9f7d7167 2018-04-29 stsp }
1623 07b55e75 2018-05-10 stsp
1624 07b55e75 2018-05-10 stsp static void
1625 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1626 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1627 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1628 07b55e75 2018-05-10 stsp {
1629 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1630 07b55e75 2018-05-10 stsp int nscrolled = 0;
1631 07b55e75 2018-05-10 stsp
1632 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1633 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1634 07b55e75 2018-05-10 stsp return;
1635 9f7d7167 2018-04-29 stsp
1636 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1637 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1638 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1639 07b55e75 2018-05-10 stsp if (entry) {
1640 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1641 07b55e75 2018-05-10 stsp nscrolled++;
1642 07b55e75 2018-05-10 stsp }
1643 07b55e75 2018-05-10 stsp }
1644 aa075928 2018-05-10 stsp }
1645 aa075928 2018-05-10 stsp
1646 aa075928 2018-05-10 stsp static const struct got_error *
1647 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1648 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1649 aa075928 2018-05-10 stsp {
1650 5e224a3e 2019-02-22 stsp int errcode;
1651 8a42fca8 2019-02-22 stsp int max_wait = 20;
1652 8a42fca8 2019-02-22 stsp
1653 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1654 aa075928 2018-05-10 stsp
1655 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1656 5e224a3e 2019-02-22 stsp if (*log_complete)
1657 5e224a3e 2019-02-22 stsp break;
1658 b295e71b 2019-02-22 stsp
1659 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1660 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1661 7aafa0d1 2019-02-22 stsp if (errcode)
1662 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1663 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1664 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1665 7aafa0d1 2019-02-22 stsp if (errcode)
1666 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1667 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1668 7aafa0d1 2019-02-22 stsp pthread_yield();
1669 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1670 7aafa0d1 2019-02-22 stsp if (errcode)
1671 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1672 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1673 5e224a3e 2019-02-22 stsp
1674 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1675 5e224a3e 2019-02-22 stsp /*
1676 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1677 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1678 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1679 5e224a3e 2019-02-22 stsp * with few commits in history.
1680 5e224a3e 2019-02-22 stsp */
1681 7aafa0d1 2019-02-22 stsp return NULL;
1682 7aafa0d1 2019-02-22 stsp }
1683 5e224a3e 2019-02-22 stsp }
1684 5e224a3e 2019-02-22 stsp
1685 5e224a3e 2019-02-22 stsp return NULL;
1686 5e224a3e 2019-02-22 stsp }
1687 5e224a3e 2019-02-22 stsp
1688 5e224a3e 2019-02-22 stsp static const struct got_error *
1689 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1690 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1691 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1692 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1693 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1694 5e224a3e 2019-02-22 stsp {
1695 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1696 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1697 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1698 5e224a3e 2019-02-22 stsp
1699 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1700 5e224a3e 2019-02-22 stsp return NULL;
1701 5e224a3e 2019-02-22 stsp
1702 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1703 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1704 08ebd0a9 2019-02-22 stsp /*
1705 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1706 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1707 08ebd0a9 2019-02-22 stsp */
1708 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1709 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1710 5e224a3e 2019-02-22 stsp need_commits);
1711 5e224a3e 2019-02-22 stsp if (err)
1712 5e224a3e 2019-02-22 stsp return err;
1713 7aafa0d1 2019-02-22 stsp }
1714 b295e71b 2019-02-22 stsp
1715 7aafa0d1 2019-02-22 stsp do {
1716 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1717 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1718 88048b54 2019-02-21 stsp break;
1719 88048b54 2019-02-21 stsp
1720 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1721 aa075928 2018-05-10 stsp
1722 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1723 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1724 dd0a52c1 2018-05-20 stsp break;
1725 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1726 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1727 aa075928 2018-05-10 stsp
1728 dd0a52c1 2018-05-20 stsp return err;
1729 07b55e75 2018-05-10 stsp }
1730 4a7f7875 2018-05-10 stsp
1731 cd0acaa7 2018-05-20 stsp static const struct got_error *
1732 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1733 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1734 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1735 8b473291 2019-02-21 stsp struct got_repository *repo)
1736 cd0acaa7 2018-05-20 stsp {
1737 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1738 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1739 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1740 cd0acaa7 2018-05-20 stsp
1741 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1742 15a94983 2018-12-23 stsp if (diff_view == NULL)
1743 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1744 ea5e7bb5 2018-08-01 stsp
1745 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1746 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1747 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1748 e5a0f69f 2018-08-18 stsp if (err == NULL)
1749 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1750 cd0acaa7 2018-05-20 stsp return err;
1751 4a7f7875 2018-05-10 stsp }
1752 4a7f7875 2018-05-10 stsp
1753 80ddbec8 2018-04-29 stsp static const struct got_error *
1754 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1755 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1756 9343a5fb 2018-06-23 stsp {
1757 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1758 941e9f74 2019-05-21 stsp
1759 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1760 941e9f74 2019-05-21 stsp if (parent == NULL)
1761 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1762 941e9f74 2019-05-21 stsp
1763 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1764 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1765 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1766 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1767 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1768 941e9f74 2019-05-21 stsp s->tree = subtree;
1769 941e9f74 2019-05-21 stsp s->selected = 0;
1770 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1771 941e9f74 2019-05-21 stsp return NULL;
1772 941e9f74 2019-05-21 stsp }
1773 941e9f74 2019-05-21 stsp
1774 941e9f74 2019-05-21 stsp
1775 941e9f74 2019-05-21 stsp static const struct got_error *
1776 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1777 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1778 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1779 941e9f74 2019-05-21 stsp {
1780 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1781 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1782 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1783 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1784 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1785 941e9f74 2019-05-21 stsp const char *p;
1786 9343a5fb 2018-06-23 stsp
1787 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1788 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1789 9343a5fb 2018-06-23 stsp if (err)
1790 9343a5fb 2018-06-23 stsp return err;
1791 9343a5fb 2018-06-23 stsp
1792 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1793 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1794 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1795 e5a0f69f 2018-08-18 stsp
1796 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1797 941e9f74 2019-05-21 stsp if (err) {
1798 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1799 941e9f74 2019-05-21 stsp return err;
1800 941e9f74 2019-05-21 stsp }
1801 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1802 941e9f74 2019-05-21 stsp
1803 941e9f74 2019-05-21 stsp *new_view = tree_view;
1804 941e9f74 2019-05-21 stsp
1805 33cbf02b 2020-01-12 stsp if (got_path_is_root_dir(path))
1806 33cbf02b 2020-01-12 stsp return NULL;
1807 33cbf02b 2020-01-12 stsp
1808 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1809 941e9f74 2019-05-21 stsp p = path;
1810 941e9f74 2019-05-21 stsp while (*p) {
1811 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1812 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1813 56e0773d 2019-11-28 stsp char *te_name;
1814 33cbf02b 2020-01-12 stsp
1815 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1816 33cbf02b 2020-01-12 stsp p++;
1817 941e9f74 2019-05-21 stsp
1818 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1819 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1820 941e9f74 2019-05-21 stsp if (slash == NULL)
1821 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1822 33cbf02b 2020-01-12 stsp else
1823 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1824 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1825 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1826 56e0773d 2019-11-28 stsp break;
1827 941e9f74 2019-05-21 stsp }
1828 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1829 56e0773d 2019-11-28 stsp if (te == NULL) {
1830 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1831 56e0773d 2019-11-28 stsp free(te_name);
1832 941e9f74 2019-05-21 stsp break;
1833 941e9f74 2019-05-21 stsp }
1834 56e0773d 2019-11-28 stsp free(te_name);
1835 56e0773d 2019-11-28 stsp s->selected_entry = te;
1836 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1837 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1838 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1839 941e9f74 2019-05-21 stsp
1840 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1841 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1842 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1843 67409a31 2019-05-24 stsp s->selected = 0;
1844 b03c880f 2019-05-21 stsp break;
1845 67409a31 2019-05-24 stsp }
1846 b03c880f 2019-05-21 stsp
1847 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1848 941e9f74 2019-05-21 stsp if (slash)
1849 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1850 941e9f74 2019-05-21 stsp else
1851 941e9f74 2019-05-21 stsp subpath = strdup(path);
1852 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1853 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1854 941e9f74 2019-05-21 stsp break;
1855 941e9f74 2019-05-21 stsp }
1856 941e9f74 2019-05-21 stsp
1857 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1858 941e9f74 2019-05-21 stsp subpath);
1859 941e9f74 2019-05-21 stsp if (err)
1860 941e9f74 2019-05-21 stsp break;
1861 941e9f74 2019-05-21 stsp
1862 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1863 941e9f74 2019-05-21 stsp free(tree_id);
1864 941e9f74 2019-05-21 stsp if (err)
1865 941e9f74 2019-05-21 stsp break;
1866 941e9f74 2019-05-21 stsp
1867 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1868 941e9f74 2019-05-21 stsp if (err) {
1869 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1870 941e9f74 2019-05-21 stsp break;
1871 941e9f74 2019-05-21 stsp }
1872 941e9f74 2019-05-21 stsp if (slash == NULL)
1873 941e9f74 2019-05-21 stsp break;
1874 941e9f74 2019-05-21 stsp free(subpath);
1875 941e9f74 2019-05-21 stsp subpath = NULL;
1876 941e9f74 2019-05-21 stsp p = slash;
1877 941e9f74 2019-05-21 stsp }
1878 941e9f74 2019-05-21 stsp
1879 941e9f74 2019-05-21 stsp free(subpath);
1880 1a76625f 2018-10-22 stsp return err;
1881 61266923 2020-01-14 stsp }
1882 61266923 2020-01-14 stsp
1883 61266923 2020-01-14 stsp static const struct got_error *
1884 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1885 61266923 2020-01-14 stsp {
1886 61266923 2020-01-14 stsp sigset_t sigset;
1887 61266923 2020-01-14 stsp int errcode;
1888 61266923 2020-01-14 stsp
1889 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1890 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1891 61266923 2020-01-14 stsp
1892 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1893 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1894 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1895 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1896 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1897 61266923 2020-01-14 stsp
1898 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1899 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1900 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1901 61266923 2020-01-14 stsp
1902 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1903 61266923 2020-01-14 stsp if (errcode)
1904 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1905 61266923 2020-01-14 stsp
1906 61266923 2020-01-14 stsp return NULL;
1907 1a76625f 2018-10-22 stsp }
1908 1a76625f 2018-10-22 stsp
1909 1a76625f 2018-10-22 stsp static void *
1910 1a76625f 2018-10-22 stsp log_thread(void *arg)
1911 1a76625f 2018-10-22 stsp {
1912 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1913 1a76625f 2018-10-22 stsp int errcode = 0;
1914 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1915 1a76625f 2018-10-22 stsp int done = 0;
1916 61266923 2020-01-14 stsp
1917 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1918 61266923 2020-01-14 stsp if (err)
1919 61266923 2020-01-14 stsp return (void *)err;
1920 1a76625f 2018-10-22 stsp
1921 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1922 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1923 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1924 13add988 2019-10-15 stsp a->regex);
1925 1a76625f 2018-10-22 stsp if (err) {
1926 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1927 1a76625f 2018-10-22 stsp return (void *)err;
1928 1a76625f 2018-10-22 stsp err = NULL;
1929 1a76625f 2018-10-22 stsp done = 1;
1930 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1931 1a76625f 2018-10-22 stsp a->commits_needed--;
1932 1a76625f 2018-10-22 stsp
1933 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1934 3abe8080 2019-04-10 stsp if (errcode) {
1935 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1936 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1937 3abe8080 2019-04-10 stsp break;
1938 3abe8080 2019-04-10 stsp } else if (*a->quit)
1939 1a76625f 2018-10-22 stsp done = 1;
1940 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1941 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1942 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1943 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1944 1a76625f 2018-10-22 stsp }
1945 1a76625f 2018-10-22 stsp
1946 1a76625f 2018-10-22 stsp if (done)
1947 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1948 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1949 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1950 1a76625f 2018-10-22 stsp &tog_mutex);
1951 1a76625f 2018-10-22 stsp if (errcode)
1952 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1953 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1954 1a76625f 2018-10-22 stsp }
1955 1a76625f 2018-10-22 stsp
1956 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1957 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1958 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1959 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1960 1a76625f 2018-10-22 stsp }
1961 3abe8080 2019-04-10 stsp a->log_complete = 1;
1962 1a76625f 2018-10-22 stsp return (void *)err;
1963 1a76625f 2018-10-22 stsp }
1964 1a76625f 2018-10-22 stsp
1965 1a76625f 2018-10-22 stsp static const struct got_error *
1966 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1967 1a76625f 2018-10-22 stsp {
1968 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1969 1a76625f 2018-10-22 stsp int errcode;
1970 1a76625f 2018-10-22 stsp
1971 1a76625f 2018-10-22 stsp if (s->thread) {
1972 1a76625f 2018-10-22 stsp s->quit = 1;
1973 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1974 1a76625f 2018-10-22 stsp if (errcode)
1975 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1976 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1977 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1978 1a76625f 2018-10-22 stsp if (errcode)
1979 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1980 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1981 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1982 1a76625f 2018-10-22 stsp if (errcode)
1983 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1984 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1985 1a76625f 2018-10-22 stsp if (errcode)
1986 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1987 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1988 1a76625f 2018-10-22 stsp s->thread = NULL;
1989 1a76625f 2018-10-22 stsp }
1990 1a76625f 2018-10-22 stsp
1991 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1992 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1993 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1994 1a76625f 2018-10-22 stsp
1995 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1996 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1997 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1998 1a76625f 2018-10-22 stsp }
1999 1a76625f 2018-10-22 stsp
2000 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2001 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2002 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2003 1a76625f 2018-10-22 stsp }
2004 1a76625f 2018-10-22 stsp
2005 9343a5fb 2018-06-23 stsp return err;
2006 9343a5fb 2018-06-23 stsp }
2007 9343a5fb 2018-06-23 stsp
2008 9343a5fb 2018-06-23 stsp static const struct got_error *
2009 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2010 1a76625f 2018-10-22 stsp {
2011 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2012 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2013 1a76625f 2018-10-22 stsp
2014 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2015 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2016 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2017 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2018 1a76625f 2018-10-22 stsp free(s->start_id);
2019 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2020 1a76625f 2018-10-22 stsp return err;
2021 1a76625f 2018-10-22 stsp }
2022 1a76625f 2018-10-22 stsp
2023 1a76625f 2018-10-22 stsp static const struct got_error *
2024 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2025 60493ae3 2019-06-20 stsp {
2026 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2027 60493ae3 2019-06-20 stsp
2028 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2029 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2030 60493ae3 2019-06-20 stsp return NULL;
2031 60493ae3 2019-06-20 stsp }
2032 60493ae3 2019-06-20 stsp
2033 60493ae3 2019-06-20 stsp static const struct got_error *
2034 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2035 60493ae3 2019-06-20 stsp {
2036 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2037 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2038 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2039 60493ae3 2019-06-20 stsp
2040 60493ae3 2019-06-20 stsp if (!view->searching) {
2041 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2042 60493ae3 2019-06-20 stsp return NULL;
2043 60493ae3 2019-06-20 stsp }
2044 60493ae3 2019-06-20 stsp
2045 96e2b566 2019-07-08 stsp if (s->search_entry) {
2046 13add988 2019-10-15 stsp int errcode, ch;
2047 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2048 13add988 2019-10-15 stsp if (errcode)
2049 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2050 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2051 13add988 2019-10-15 stsp ch = wgetch(view->window);
2052 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2053 13add988 2019-10-15 stsp if (errcode)
2054 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2055 13add988 2019-10-15 stsp "pthread_mutex_lock");
2056 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2057 678cbce5 2019-07-28 stsp view->search_next_done = 1;
2058 678cbce5 2019-07-28 stsp return NULL;
2059 678cbce5 2019-07-28 stsp }
2060 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2061 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2062 96e2b566 2019-07-08 stsp else
2063 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2064 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2065 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2066 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2067 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2068 b1bf1435 2019-06-21 stsp else
2069 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
2070 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2071 20be8d96 2019-06-21 stsp } else {
2072 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2073 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2074 20be8d96 2019-06-21 stsp else
2075 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2076 20be8d96 2019-06-21 stsp }
2077 60493ae3 2019-06-20 stsp
2078 60493ae3 2019-06-20 stsp while (1) {
2079 13add988 2019-10-15 stsp int have_match = 0;
2080 13add988 2019-10-15 stsp
2081 60493ae3 2019-06-20 stsp if (entry == NULL) {
2082 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2083 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2084 f801134a 2019-06-25 stsp view->search_next_done = 1;
2085 f801134a 2019-06-25 stsp return NULL;
2086 60493ae3 2019-06-20 stsp }
2087 96e2b566 2019-07-08 stsp /*
2088 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2089 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2090 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2091 96e2b566 2019-07-08 stsp */
2092 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2093 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
2094 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2095 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2096 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
2097 60493ae3 2019-06-20 stsp }
2098 60493ae3 2019-06-20 stsp
2099 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2100 13add988 2019-10-15 stsp &view->regex);
2101 5943eee2 2019-08-13 stsp if (err)
2102 13add988 2019-10-15 stsp break;
2103 13add988 2019-10-15 stsp if (have_match) {
2104 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2105 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2106 60493ae3 2019-06-20 stsp break;
2107 60493ae3 2019-06-20 stsp }
2108 13add988 2019-10-15 stsp
2109 96e2b566 2019-07-08 stsp s->search_entry = entry;
2110 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2111 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2112 b1bf1435 2019-06-21 stsp else
2113 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2114 60493ae3 2019-06-20 stsp }
2115 60493ae3 2019-06-20 stsp
2116 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2117 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2118 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2119 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2120 60493ae3 2019-06-20 stsp if (err)
2121 60493ae3 2019-06-20 stsp return err;
2122 ead14cbe 2019-06-21 stsp cur++;
2123 ead14cbe 2019-06-21 stsp }
2124 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2125 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2126 60493ae3 2019-06-20 stsp if (err)
2127 60493ae3 2019-06-20 stsp return err;
2128 ead14cbe 2019-06-21 stsp cur--;
2129 60493ae3 2019-06-20 stsp }
2130 60493ae3 2019-06-20 stsp }
2131 60493ae3 2019-06-20 stsp
2132 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2133 96e2b566 2019-07-08 stsp
2134 60493ae3 2019-06-20 stsp return NULL;
2135 60493ae3 2019-06-20 stsp }
2136 60493ae3 2019-06-20 stsp
2137 60493ae3 2019-06-20 stsp static const struct got_error *
2138 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2139 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2140 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
2141 80ddbec8 2018-04-29 stsp {
2142 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2143 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2144 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2145 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2146 1a76625f 2018-10-22 stsp int errcode;
2147 80ddbec8 2018-04-29 stsp
2148 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2149 ecb28ae0 2018-07-16 stsp if (err != NULL)
2150 ecb28ae0 2018-07-16 stsp goto done;
2151 ecb28ae0 2018-07-16 stsp
2152 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2153 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2154 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2155 9ba79e04 2018-06-11 stsp
2156 8b473291 2019-02-21 stsp s->refs = refs;
2157 fb2756b9 2018-08-04 stsp s->repo = repo;
2158 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2159 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2160 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2161 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2162 5036bf37 2018-09-24 stsp goto done;
2163 5036bf37 2018-09-24 stsp }
2164 e5a0f69f 2018-08-18 stsp
2165 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2166 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2167 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2168 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2169 11b20872 2019-11-08 stsp if (err)
2170 11b20872 2019-11-08 stsp goto done;
2171 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2172 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2173 11b20872 2019-11-08 stsp if (err) {
2174 11b20872 2019-11-08 stsp free_colors(&s->colors);
2175 11b20872 2019-11-08 stsp goto done;
2176 11b20872 2019-11-08 stsp }
2177 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2178 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2179 11b20872 2019-11-08 stsp if (err) {
2180 11b20872 2019-11-08 stsp free_colors(&s->colors);
2181 11b20872 2019-11-08 stsp goto done;
2182 11b20872 2019-11-08 stsp }
2183 11b20872 2019-11-08 stsp }
2184 11b20872 2019-11-08 stsp
2185 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2186 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2187 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2188 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2189 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2190 1a76625f 2018-10-22 stsp
2191 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2192 1a76625f 2018-10-22 stsp if (err)
2193 1a76625f 2018-10-22 stsp goto done;
2194 3d509237 2020-01-04 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2195 1a76625f 2018-10-22 stsp if (err)
2196 1a76625f 2018-10-22 stsp goto done;
2197 c5b78334 2020-01-12 stsp err = got_commit_graph_iter_start(thread_graph,
2198 c5b78334 2020-01-12 stsp s->start_id, s->repo, NULL, NULL);
2199 c5b78334 2020-01-12 stsp if (err)
2200 c5b78334 2020-01-12 stsp goto done;
2201 1a76625f 2018-10-22 stsp
2202 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2203 1a76625f 2018-10-22 stsp if (errcode) {
2204 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2205 1a76625f 2018-10-22 stsp goto done;
2206 1a76625f 2018-10-22 stsp }
2207 1a76625f 2018-10-22 stsp
2208 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2209 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2210 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2211 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2212 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2213 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2214 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2215 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2216 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2217 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2218 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2219 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2220 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2221 ba4f502b 2018-08-04 stsp done:
2222 1a76625f 2018-10-22 stsp if (err)
2223 1a76625f 2018-10-22 stsp close_log_view(view);
2224 ba4f502b 2018-08-04 stsp return err;
2225 ba4f502b 2018-08-04 stsp }
2226 ba4f502b 2018-08-04 stsp
2227 e5a0f69f 2018-08-18 stsp static const struct got_error *
2228 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2229 ba4f502b 2018-08-04 stsp {
2230 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2231 ba4f502b 2018-08-04 stsp
2232 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2233 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2234 2b380cc8 2018-10-24 stsp &s->thread_args);
2235 2b380cc8 2018-10-24 stsp if (errcode)
2236 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2237 2b380cc8 2018-10-24 stsp }
2238 2b380cc8 2018-10-24 stsp
2239 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2240 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2241 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2242 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2243 e5a0f69f 2018-08-18 stsp }
2244 04cc582a 2018-08-01 stsp
2245 e5a0f69f 2018-08-18 stsp static const struct got_error *
2246 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2247 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2248 e5a0f69f 2018-08-18 stsp {
2249 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2250 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2251 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2252 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2253 669b5ffa 2018-10-07 stsp int begin_x = 0;
2254 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2255 80ddbec8 2018-04-29 stsp
2256 e5a0f69f 2018-08-18 stsp switch (ch) {
2257 1e37a5c2 2019-05-12 jcs case 'q':
2258 1e37a5c2 2019-05-12 jcs s->quit = 1;
2259 1e37a5c2 2019-05-12 jcs break;
2260 1e37a5c2 2019-05-12 jcs case 'k':
2261 1e37a5c2 2019-05-12 jcs case KEY_UP:
2262 1e37a5c2 2019-05-12 jcs case '<':
2263 1e37a5c2 2019-05-12 jcs case ',':
2264 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2265 1a76625f 2018-10-22 stsp break;
2266 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2267 1e37a5c2 2019-05-12 jcs s->selected--;
2268 1144d21a 2019-06-21 stsp else
2269 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2270 1144d21a 2019-06-21 stsp &s->commits);
2271 1e37a5c2 2019-05-12 jcs break;
2272 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2273 a4292ac5 2019-05-12 jcs case CTRL('b'):
2274 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2275 e5a0f69f 2018-08-18 stsp break;
2276 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2277 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2278 1e37a5c2 2019-05-12 jcs s->selected = 0;
2279 e5a0f69f 2018-08-18 stsp break;
2280 1e37a5c2 2019-05-12 jcs }
2281 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2282 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2283 1e37a5c2 2019-05-12 jcs break;
2284 1e37a5c2 2019-05-12 jcs case 'j':
2285 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2286 1e37a5c2 2019-05-12 jcs case '>':
2287 1e37a5c2 2019-05-12 jcs case '.':
2288 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2289 e5a0f69f 2018-08-18 stsp break;
2290 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2291 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2292 1e37a5c2 2019-05-12 jcs s->selected++;
2293 1e37a5c2 2019-05-12 jcs break;
2294 80ddbec8 2018-04-29 stsp }
2295 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2296 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2297 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2298 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2299 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2300 1e37a5c2 2019-05-12 jcs break;
2301 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2302 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2303 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2304 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2305 1e37a5c2 2019-05-12 jcs if (first == NULL)
2306 e5a0f69f 2018-08-18 stsp break;
2307 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2308 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2309 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2310 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2311 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2312 1cae65b4 2019-09-22 stsp if (err)
2313 1cae65b4 2019-09-22 stsp break;
2314 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2315 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2316 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2317 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2318 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2319 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2320 1e37a5c2 2019-05-12 jcs }
2321 1e37a5c2 2019-05-12 jcs err = NULL;
2322 1e37a5c2 2019-05-12 jcs break;
2323 1e37a5c2 2019-05-12 jcs }
2324 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2325 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2326 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2327 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2328 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2329 1e37a5c2 2019-05-12 jcs break;
2330 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2331 87c7274c 2019-05-12 jcs case ' ':
2332 1e37a5c2 2019-05-12 jcs case '\r':
2333 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2334 e5a0f69f 2018-08-18 stsp break;
2335 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2336 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2337 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2338 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2339 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2340 1e37a5c2 2019-05-12 jcs if (err)
2341 1e37a5c2 2019-05-12 jcs 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 f7013a22 2018-10-24 stsp if (err)
2345 1e37a5c2 2019-05-12 jcs return err;
2346 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2347 1e37a5c2 2019-05-12 jcs if (err) {
2348 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2349 f7013a22 2018-10-24 stsp break;
2350 5036bf37 2018-09-24 stsp }
2351 1e37a5c2 2019-05-12 jcs *focus_view = diff_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 = diff_view;
2355 1e37a5c2 2019-05-12 jcs break;
2356 1e37a5c2 2019-05-12 jcs case 't':
2357 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2358 5036bf37 2018-09-24 stsp break;
2359 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2360 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2361 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2362 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2363 1e37a5c2 2019-05-12 jcs if (err)
2364 e5a0f69f 2018-08-18 stsp break;
2365 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2366 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2367 1e37a5c2 2019-05-12 jcs if (err)
2368 1e37a5c2 2019-05-12 jcs return err;
2369 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2370 1e37a5c2 2019-05-12 jcs if (err) {
2371 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2372 1e37a5c2 2019-05-12 jcs break;
2373 1e37a5c2 2019-05-12 jcs }
2374 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2375 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2376 1e37a5c2 2019-05-12 jcs } else
2377 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2378 1e37a5c2 2019-05-12 jcs break;
2379 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2380 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2381 1e37a5c2 2019-05-12 jcs break;
2382 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2383 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2384 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2385 1e37a5c2 2019-05-12 jcs if (err)
2386 1e37a5c2 2019-05-12 jcs return err;
2387 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2388 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2389 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2390 638f9024 2019-05-13 stsp return got_error_from_errno(
2391 1e37a5c2 2019-05-12 jcs "view_open");
2392 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2393 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2394 1e37a5c2 2019-05-12 jcs if (err)
2395 1e37a5c2 2019-05-12 jcs return err;;
2396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2397 1e37a5c2 2019-05-12 jcs *new_view = lv;
2398 1e37a5c2 2019-05-12 jcs else {
2399 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2400 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2401 1e37a5c2 2019-05-12 jcs }
2402 1e37a5c2 2019-05-12 jcs return NULL;
2403 1e37a5c2 2019-05-12 jcs }
2404 1e37a5c2 2019-05-12 jcs break;
2405 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2406 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2407 d01904d4 2019-06-25 stsp if (err)
2408 d01904d4 2019-06-25 stsp return err;
2409 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2410 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2411 d01904d4 2019-06-25 stsp if (lv == NULL)
2412 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2413 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2414 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2415 ef129c5e 2019-08-03 stsp if (err) {
2416 ef129c5e 2019-08-03 stsp view_close(lv);
2417 d01904d4 2019-06-25 stsp return err;
2418 ef129c5e 2019-08-03 stsp }
2419 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2420 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2421 e6b23735 2019-10-04 stsp free(start_id);
2422 e6b23735 2019-10-04 stsp view_close(lv);
2423 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2424 e6b23735 2019-10-04 stsp }
2425 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2426 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2427 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2428 e6b23735 2019-10-04 stsp if (err) {
2429 d01904d4 2019-06-25 stsp free(start_id);
2430 ef129c5e 2019-08-03 stsp view_close(lv);
2431 50284065 2019-10-04 stsp return err;
2432 d01904d4 2019-06-25 stsp }
2433 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2434 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2435 ef129c5e 2019-08-03 stsp if (err) {
2436 ef129c5e 2019-08-03 stsp free(start_id);
2437 ef129c5e 2019-08-03 stsp view_close(lv);
2438 d01904d4 2019-06-25 stsp return err;;
2439 ef129c5e 2019-08-03 stsp }
2440 d01904d4 2019-06-25 stsp *dead_view = view;
2441 d01904d4 2019-06-25 stsp *new_view = lv;
2442 d01904d4 2019-06-25 stsp break;
2443 1e37a5c2 2019-05-12 jcs default:
2444 1e37a5c2 2019-05-12 jcs break;
2445 899d86c2 2018-05-10 stsp }
2446 e5a0f69f 2018-08-18 stsp
2447 80ddbec8 2018-04-29 stsp return err;
2448 80ddbec8 2018-04-29 stsp }
2449 80ddbec8 2018-04-29 stsp
2450 4ed7e80c 2018-05-20 stsp static const struct got_error *
2451 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2452 c2db6724 2019-01-04 stsp {
2453 c2db6724 2019-01-04 stsp const struct got_error *error;
2454 c2db6724 2019-01-04 stsp
2455 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2456 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2457 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2458 37c06ea4 2019-07-15 stsp #endif
2459 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2460 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2461 c2db6724 2019-01-04 stsp
2462 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2463 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2464 c2db6724 2019-01-04 stsp
2465 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2466 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2467 c2db6724 2019-01-04 stsp
2468 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2469 c2db6724 2019-01-04 stsp if (error != NULL)
2470 c2db6724 2019-01-04 stsp return error;
2471 c2db6724 2019-01-04 stsp
2472 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2473 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2474 c2db6724 2019-01-04 stsp
2475 c2db6724 2019-01-04 stsp return NULL;
2476 c2db6724 2019-01-04 stsp }
2477 c2db6724 2019-01-04 stsp
2478 a915003a 2019-02-05 stsp static void
2479 a915003a 2019-02-05 stsp init_curses(void)
2480 a915003a 2019-02-05 stsp {
2481 a915003a 2019-02-05 stsp initscr();
2482 a915003a 2019-02-05 stsp cbreak();
2483 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2484 a915003a 2019-02-05 stsp noecho();
2485 a915003a 2019-02-05 stsp nonl();
2486 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2487 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2488 a915003a 2019-02-05 stsp curs_set(0);
2489 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2490 6d17833f 2019-11-08 stsp start_color();
2491 6d17833f 2019-11-08 stsp use_default_colors();
2492 6d17833f 2019-11-08 stsp }
2493 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2494 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2495 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2496 a915003a 2019-02-05 stsp }
2497 a915003a 2019-02-05 stsp
2498 c2db6724 2019-01-04 stsp static const struct got_error *
2499 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2500 9f7d7167 2018-04-29 stsp {
2501 80ddbec8 2018-04-29 stsp const struct got_error *error;
2502 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2503 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2504 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2505 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2506 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2507 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2508 80ddbec8 2018-04-29 stsp int ch;
2509 04cc582a 2018-08-01 stsp struct tog_view *view;
2510 a55555f2 2019-03-28 stsp
2511 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2512 80ddbec8 2018-04-29 stsp
2513 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2514 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2515 c2db6724 2019-01-04 stsp NULL) == -1)
2516 80ddbec8 2018-04-29 stsp err(1, "pledge");
2517 80ddbec8 2018-04-29 stsp #endif
2518 80ddbec8 2018-04-29 stsp
2519 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2520 80ddbec8 2018-04-29 stsp switch (ch) {
2521 80ddbec8 2018-04-29 stsp case 'c':
2522 80ddbec8 2018-04-29 stsp start_commit = optarg;
2523 80ddbec8 2018-04-29 stsp break;
2524 ecb28ae0 2018-07-16 stsp case 'r':
2525 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2526 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2527 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2528 9ba1d308 2019-10-21 stsp optarg);
2529 ecb28ae0 2018-07-16 stsp break;
2530 80ddbec8 2018-04-29 stsp default:
2531 17020d27 2019-03-07 stsp usage_log();
2532 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2533 80ddbec8 2018-04-29 stsp }
2534 80ddbec8 2018-04-29 stsp }
2535 80ddbec8 2018-04-29 stsp
2536 80ddbec8 2018-04-29 stsp argc -= optind;
2537 80ddbec8 2018-04-29 stsp argv += optind;
2538 80ddbec8 2018-04-29 stsp
2539 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2540 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2541 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2542 ecb28ae0 2018-07-16 stsp goto done;
2543 ecb28ae0 2018-07-16 stsp }
2544 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2545 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2546 963f97a1 2019-03-18 stsp goto done;
2547 963f97a1 2019-03-18 stsp error = NULL;
2548 963f97a1 2019-03-18 stsp
2549 963f97a1 2019-03-18 stsp if (argc == 0) {
2550 963f97a1 2019-03-18 stsp path = strdup("");
2551 963f97a1 2019-03-18 stsp if (path == NULL) {
2552 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2553 ecb28ae0 2018-07-16 stsp goto done;
2554 ecb28ae0 2018-07-16 stsp }
2555 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2556 963f97a1 2019-03-18 stsp if (worktree) {
2557 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2558 963f97a1 2019-03-18 stsp argv[0]);
2559 963f97a1 2019-03-18 stsp if (error)
2560 963f97a1 2019-03-18 stsp goto done;
2561 963f97a1 2019-03-18 stsp } else {
2562 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2563 963f97a1 2019-03-18 stsp if (path == NULL) {
2564 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2565 963f97a1 2019-03-18 stsp goto done;
2566 963f97a1 2019-03-18 stsp }
2567 963f97a1 2019-03-18 stsp }
2568 963f97a1 2019-03-18 stsp } else
2569 963f97a1 2019-03-18 stsp usage_log();
2570 963f97a1 2019-03-18 stsp
2571 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2572 a1fbf39a 2019-08-11 stsp if (worktree)
2573 b9d7675a 2019-08-11 stsp repo_path = strdup(
2574 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2575 a1fbf39a 2019-08-11 stsp else
2576 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2577 a1fbf39a 2019-08-11 stsp }
2578 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2579 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2580 963f97a1 2019-03-18 stsp goto done;
2581 ecb28ae0 2018-07-16 stsp }
2582 c2db6724 2019-01-04 stsp
2583 a915003a 2019-02-05 stsp init_curses();
2584 ecb28ae0 2018-07-16 stsp
2585 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2586 80ddbec8 2018-04-29 stsp if (error != NULL)
2587 ecb28ae0 2018-07-16 stsp goto done;
2588 80ddbec8 2018-04-29 stsp
2589 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2590 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2591 c02c541e 2019-03-29 stsp if (error)
2592 c02c541e 2019-03-29 stsp goto done;
2593 c02c541e 2019-03-29 stsp
2594 15a94983 2018-12-23 stsp if (start_commit == NULL)
2595 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2596 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2597 19e70ad6 2019-05-14 stsp repo);
2598 f2b6a97d 2019-07-15 stsp else {
2599 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2600 f2b6a97d 2019-07-15 stsp if (error) {
2601 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2602 f2b6a97d 2019-07-15 stsp goto done;
2603 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2604 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2605 f2b6a97d 2019-07-15 stsp }
2606 f2b6a97d 2019-07-15 stsp }
2607 80ddbec8 2018-04-29 stsp if (error != NULL)
2608 8b473291 2019-02-21 stsp goto done;
2609 8b473291 2019-02-21 stsp
2610 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2611 8b473291 2019-02-21 stsp if (error)
2612 ecb28ae0 2018-07-16 stsp goto done;
2613 ecb28ae0 2018-07-16 stsp
2614 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2615 04cc582a 2018-08-01 stsp if (view == NULL) {
2616 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2617 04cc582a 2018-08-01 stsp goto done;
2618 04cc582a 2018-08-01 stsp }
2619 2fc00ff4 2019-08-31 stsp if (worktree) {
2620 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2621 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2622 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2623 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2624 2fc00ff4 2019-08-31 stsp goto done;
2625 2fc00ff4 2019-08-31 stsp }
2626 2fc00ff4 2019-08-31 stsp }
2627 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2628 2fc00ff4 2019-08-31 stsp path, 1);
2629 ba4f502b 2018-08-04 stsp if (error)
2630 ba4f502b 2018-08-04 stsp goto done;
2631 2fc00ff4 2019-08-31 stsp if (worktree) {
2632 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2633 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2634 2fc00ff4 2019-08-31 stsp worktree = NULL;
2635 2fc00ff4 2019-08-31 stsp }
2636 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2637 ecb28ae0 2018-07-16 stsp done:
2638 ecb28ae0 2018-07-16 stsp free(repo_path);
2639 ecb28ae0 2018-07-16 stsp free(cwd);
2640 ecb28ae0 2018-07-16 stsp free(path);
2641 899d86c2 2018-05-10 stsp free(start_id);
2642 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2643 ecb28ae0 2018-07-16 stsp if (repo)
2644 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2645 ec142235 2019-03-07 stsp if (worktree)
2646 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2647 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2648 80ddbec8 2018-04-29 stsp return error;
2649 9f7d7167 2018-04-29 stsp }
2650 9f7d7167 2018-04-29 stsp
2651 4ed7e80c 2018-05-20 stsp __dead static void
2652 9f7d7167 2018-04-29 stsp usage_diff(void)
2653 9f7d7167 2018-04-29 stsp {
2654 80ddbec8 2018-04-29 stsp endwin();
2655 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2656 9f7d7167 2018-04-29 stsp getprogname());
2657 9f7d7167 2018-04-29 stsp exit(1);
2658 b304db33 2018-05-20 stsp }
2659 b304db33 2018-05-20 stsp
2660 b304db33 2018-05-20 stsp static char *
2661 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2662 b304db33 2018-05-20 stsp {
2663 b304db33 2018-05-20 stsp char *line;
2664 b304db33 2018-05-20 stsp size_t linelen;
2665 b304db33 2018-05-20 stsp size_t lineno;
2666 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2667 b304db33 2018-05-20 stsp
2668 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2669 b304db33 2018-05-20 stsp if (len)
2670 b304db33 2018-05-20 stsp *len = linelen;
2671 b304db33 2018-05-20 stsp return line;
2672 26ed57b2 2018-05-19 stsp }
2673 26ed57b2 2018-05-19 stsp
2674 6d17833f 2019-11-08 stsp static int
2675 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2676 6d17833f 2019-11-08 stsp {
2677 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2678 6d17833f 2019-11-08 stsp
2679 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, &regmatch, 0) == 0;
2680 6d17833f 2019-11-08 stsp }
2681 6d17833f 2019-11-08 stsp
2682 f26dddb7 2019-11-08 stsp struct tog_color *
2683 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2684 6d17833f 2019-11-08 stsp {
2685 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2686 6d17833f 2019-11-08 stsp
2687 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2688 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2689 6d17833f 2019-11-08 stsp return tc;
2690 6d17833f 2019-11-08 stsp }
2691 6d17833f 2019-11-08 stsp
2692 6d17833f 2019-11-08 stsp return NULL;
2693 6d17833f 2019-11-08 stsp }
2694 6d17833f 2019-11-08 stsp
2695 4ed7e80c 2018-05-20 stsp static const struct got_error *
2696 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2697 6d17833f 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines, char *header,
2698 f26dddb7 2019-11-08 stsp struct tog_colors *colors)
2699 26ed57b2 2018-05-19 stsp {
2700 61e69b96 2018-05-20 stsp const struct got_error *err;
2701 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2702 b304db33 2018-05-20 stsp char *line;
2703 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2704 b304db33 2018-05-20 stsp size_t len;
2705 61e69b96 2018-05-20 stsp wchar_t *wline;
2706 e0b650dd 2018-05-20 stsp int width;
2707 26ed57b2 2018-05-19 stsp
2708 26ed57b2 2018-05-19 stsp rewind(f);
2709 f7d12f7e 2018-08-01 stsp werase(view->window);
2710 a3404814 2018-09-02 stsp
2711 a3404814 2018-09-02 stsp if (header) {
2712 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2713 a3404814 2018-09-02 stsp if (err) {
2714 a3404814 2018-09-02 stsp return err;
2715 a3404814 2018-09-02 stsp }
2716 a3404814 2018-09-02 stsp
2717 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2718 a3404814 2018-09-02 stsp wstandout(view->window);
2719 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2720 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2721 a3404814 2018-09-02 stsp wstandend(view->window);
2722 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2723 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2724 26ed57b2 2018-05-19 stsp
2725 a3404814 2018-09-02 stsp if (max_lines <= 1)
2726 a3404814 2018-09-02 stsp return NULL;
2727 a3404814 2018-09-02 stsp max_lines--;
2728 a3404814 2018-09-02 stsp }
2729 a3404814 2018-09-02 stsp
2730 26ed57b2 2018-05-19 stsp *eof = 0;
2731 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2732 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2733 26ed57b2 2018-05-19 stsp if (line == NULL) {
2734 26ed57b2 2018-05-19 stsp *eof = 1;
2735 26ed57b2 2018-05-19 stsp break;
2736 26ed57b2 2018-05-19 stsp }
2737 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2738 26ed57b2 2018-05-19 stsp free(line);
2739 26ed57b2 2018-05-19 stsp continue;
2740 26ed57b2 2018-05-19 stsp }
2741 26ed57b2 2018-05-19 stsp
2742 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2743 61e69b96 2018-05-20 stsp if (err) {
2744 61e69b96 2018-05-20 stsp free(line);
2745 61e69b96 2018-05-20 stsp return err;
2746 61e69b96 2018-05-20 stsp }
2747 6d17833f 2019-11-08 stsp
2748 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2749 f26dddb7 2019-11-08 stsp if (tc)
2750 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2751 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2752 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2753 f26dddb7 2019-11-08 stsp if (tc)
2754 6d17833f 2019-11-08 stsp wattr_off(view->window,
2755 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2756 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2757 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2758 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2759 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2760 26ed57b2 2018-05-19 stsp free(line);
2761 2550e4c3 2018-07-13 stsp free(wline);
2762 2550e4c3 2018-07-13 stsp wline = NULL;
2763 26ed57b2 2018-05-19 stsp }
2764 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2765 26ed57b2 2018-05-19 stsp
2766 1a57306a 2018-09-02 stsp view_vborder(view);
2767 c3e9aa98 2019-05-13 jcs
2768 c3e9aa98 2019-05-13 jcs if (*eof) {
2769 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2770 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2771 c3e9aa98 2019-05-13 jcs nprinted++;
2772 c3e9aa98 2019-05-13 jcs }
2773 c3e9aa98 2019-05-13 jcs
2774 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2775 c3e9aa98 2019-05-13 jcs if (err) {
2776 c3e9aa98 2019-05-13 jcs return err;
2777 c3e9aa98 2019-05-13 jcs }
2778 26ed57b2 2018-05-19 stsp
2779 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2780 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2781 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2782 c3e9aa98 2019-05-13 jcs }
2783 c3e9aa98 2019-05-13 jcs
2784 26ed57b2 2018-05-19 stsp return NULL;
2785 abd2672a 2018-12-23 stsp }
2786 abd2672a 2018-12-23 stsp
2787 abd2672a 2018-12-23 stsp static char *
2788 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2789 abd2672a 2018-12-23 stsp {
2790 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2791 09867e48 2019-08-13 stsp char *p, *s;
2792 09867e48 2019-08-13 stsp
2793 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2794 09867e48 2019-08-13 stsp if (tm == NULL)
2795 09867e48 2019-08-13 stsp return NULL;
2796 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2797 09867e48 2019-08-13 stsp if (s == NULL)
2798 09867e48 2019-08-13 stsp return NULL;
2799 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2800 abd2672a 2018-12-23 stsp if (p)
2801 abd2672a 2018-12-23 stsp *p = '\0';
2802 abd2672a 2018-12-23 stsp return s;
2803 9f7d7167 2018-04-29 stsp }
2804 9f7d7167 2018-04-29 stsp
2805 4ed7e80c 2018-05-20 stsp static const struct got_error *
2806 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2807 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2808 abd2672a 2018-12-23 stsp {
2809 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2810 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2811 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2812 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2813 45d799e2 2018-12-23 stsp time_t committer_time;
2814 45d799e2 2018-12-23 stsp const char *author, *committer;
2815 8b473291 2019-02-21 stsp char *refs_str = NULL;
2816 abd2672a 2018-12-23 stsp
2817 8b473291 2019-02-21 stsp if (refs) {
2818 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2819 8b473291 2019-02-21 stsp if (err)
2820 8b473291 2019-02-21 stsp return err;
2821 8b473291 2019-02-21 stsp }
2822 8b473291 2019-02-21 stsp
2823 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2824 abd2672a 2018-12-23 stsp if (err)
2825 abd2672a 2018-12-23 stsp return err;
2826 abd2672a 2018-12-23 stsp
2827 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2828 15a94983 2018-12-23 stsp if (err) {
2829 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2830 15a94983 2018-12-23 stsp goto done;
2831 15a94983 2018-12-23 stsp }
2832 abd2672a 2018-12-23 stsp
2833 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2834 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2835 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2836 abd2672a 2018-12-23 stsp goto done;
2837 abd2672a 2018-12-23 stsp }
2838 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2839 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2840 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2841 abd2672a 2018-12-23 stsp goto done;
2842 abd2672a 2018-12-23 stsp }
2843 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2844 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2845 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2846 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2847 abd2672a 2018-12-23 stsp goto done;
2848 abd2672a 2018-12-23 stsp }
2849 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2850 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2851 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2852 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2853 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2854 abd2672a 2018-12-23 stsp goto done;
2855 abd2672a 2018-12-23 stsp }
2856 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2857 5943eee2 2019-08-13 stsp if (err)
2858 5943eee2 2019-08-13 stsp goto done;
2859 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2860 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2861 abd2672a 2018-12-23 stsp goto done;
2862 abd2672a 2018-12-23 stsp }
2863 abd2672a 2018-12-23 stsp done:
2864 abd2672a 2018-12-23 stsp free(id_str);
2865 5943eee2 2019-08-13 stsp free(logmsg);
2866 8b473291 2019-02-21 stsp free(refs_str);
2867 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2868 abd2672a 2018-12-23 stsp return err;
2869 abd2672a 2018-12-23 stsp }
2870 abd2672a 2018-12-23 stsp
2871 abd2672a 2018-12-23 stsp static const struct got_error *
2872 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2873 26ed57b2 2018-05-19 stsp {
2874 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2875 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2876 15a94983 2018-12-23 stsp int obj_type;
2877 26ed57b2 2018-05-19 stsp
2878 511a516b 2018-05-19 stsp f = got_opentemp();
2879 48ae06ee 2018-10-18 stsp if (f == NULL) {
2880 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2881 48ae06ee 2018-10-18 stsp goto done;
2882 48ae06ee 2018-10-18 stsp }
2883 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2884 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2885 fb43ecf1 2019-02-11 stsp goto done;
2886 fb43ecf1 2019-02-11 stsp }
2887 48ae06ee 2018-10-18 stsp s->f = f;
2888 26ed57b2 2018-05-19 stsp
2889 15a94983 2018-12-23 stsp if (s->id1)
2890 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2891 15a94983 2018-12-23 stsp else
2892 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2893 15a94983 2018-12-23 stsp if (err)
2894 15a94983 2018-12-23 stsp goto done;
2895 15a94983 2018-12-23 stsp
2896 15a94983 2018-12-23 stsp switch (obj_type) {
2897 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2898 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2899 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2900 26ed57b2 2018-05-19 stsp break;
2901 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2902 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2903 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2904 26ed57b2 2018-05-19 stsp break;
2905 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2906 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2907 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2908 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2909 abd2672a 2018-12-23 stsp
2910 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2911 abd2672a 2018-12-23 stsp if (err)
2912 abd2672a 2018-12-23 stsp break;
2913 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2914 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2915 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2916 15a087fe 2019-02-21 stsp else {
2917 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2918 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2919 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2920 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2921 8b473291 2019-02-21 stsp s->repo, f);
2922 15a087fe 2019-02-21 stsp break;
2923 15a087fe 2019-02-21 stsp }
2924 abd2672a 2018-12-23 stsp }
2925 abd2672a 2018-12-23 stsp }
2926 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2927 abd2672a 2018-12-23 stsp
2928 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2929 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2930 26ed57b2 2018-05-19 stsp break;
2931 abd2672a 2018-12-23 stsp }
2932 26ed57b2 2018-05-19 stsp default:
2933 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2934 48ae06ee 2018-10-18 stsp break;
2935 26ed57b2 2018-05-19 stsp }
2936 48ae06ee 2018-10-18 stsp done:
2937 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2938 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2939 48ae06ee 2018-10-18 stsp return err;
2940 48ae06ee 2018-10-18 stsp }
2941 26ed57b2 2018-05-19 stsp
2942 f5215bb9 2019-02-22 stsp static void
2943 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2944 f5215bb9 2019-02-22 stsp {
2945 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2946 f5215bb9 2019-02-22 stsp update_panels();
2947 f5215bb9 2019-02-22 stsp doupdate();
2948 f5215bb9 2019-02-22 stsp }
2949 f5215bb9 2019-02-22 stsp
2950 48ae06ee 2018-10-18 stsp static const struct got_error *
2951 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2952 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2953 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2954 48ae06ee 2018-10-18 stsp {
2955 48ae06ee 2018-10-18 stsp const struct got_error *err;
2956 5dc9f4bc 2018-08-04 stsp
2957 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2958 15a94983 2018-12-23 stsp int type1, type2;
2959 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2960 15a94983 2018-12-23 stsp if (err)
2961 15a94983 2018-12-23 stsp return err;
2962 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2963 15a94983 2018-12-23 stsp if (err)
2964 15a94983 2018-12-23 stsp return err;
2965 15a94983 2018-12-23 stsp
2966 15a94983 2018-12-23 stsp if (type1 != type2)
2967 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2968 15a94983 2018-12-23 stsp }
2969 48ae06ee 2018-10-18 stsp
2970 15a94983 2018-12-23 stsp if (id1) {
2971 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2972 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2973 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2974 48ae06ee 2018-10-18 stsp } else
2975 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2976 48ae06ee 2018-10-18 stsp
2977 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2978 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2979 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2980 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2981 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2982 48ae06ee 2018-10-18 stsp }
2983 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2984 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2985 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2986 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2987 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2988 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2989 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2990 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&view->state.diff.colors);
2991 6d17833f 2019-11-08 stsp
2992 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2993 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2994 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
2995 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
2996 6d17833f 2019-11-08 stsp if (err)
2997 6d17833f 2019-11-08 stsp return err;
2998 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors, "^\\+",
2999 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3000 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3001 6d17833f 2019-11-08 stsp if (err) {
3002 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3003 6d17833f 2019-11-08 stsp return err;
3004 6d17833f 2019-11-08 stsp }
3005 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3006 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3007 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3008 6d17833f 2019-11-08 stsp if (err) {
3009 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3010 6d17833f 2019-11-08 stsp return err;
3011 6d17833f 2019-11-08 stsp }
3012 6d17833f 2019-11-08 stsp
3013 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3014 11b20872 2019-11-08 stsp "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3015 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3016 6d17833f 2019-11-08 stsp if (err) {
3017 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3018 6d17833f 2019-11-08 stsp return err;
3019 6d17833f 2019-11-08 stsp }
3020 11b20872 2019-11-08 stsp
3021 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3022 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3023 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3024 11b20872 2019-11-08 stsp if (err) {
3025 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
3026 11b20872 2019-11-08 stsp return err;
3027 11b20872 2019-11-08 stsp }
3028 11b20872 2019-11-08 stsp
3029 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3030 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3031 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3032 11b20872 2019-11-08 stsp if (err) {
3033 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
3034 11b20872 2019-11-08 stsp return err;
3035 11b20872 2019-11-08 stsp }
3036 6d17833f 2019-11-08 stsp }
3037 5dc9f4bc 2018-08-04 stsp
3038 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3039 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3040 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3041 f5215bb9 2019-02-22 stsp
3042 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
3043 48ae06ee 2018-10-18 stsp if (err) {
3044 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3045 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3046 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3047 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3048 48ae06ee 2018-10-18 stsp return err;
3049 48ae06ee 2018-10-18 stsp }
3050 48ae06ee 2018-10-18 stsp
3051 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3052 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3053 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3054 e5a0f69f 2018-08-18 stsp
3055 5dc9f4bc 2018-08-04 stsp return NULL;
3056 5dc9f4bc 2018-08-04 stsp }
3057 5dc9f4bc 2018-08-04 stsp
3058 e5a0f69f 2018-08-18 stsp static const struct got_error *
3059 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3060 5dc9f4bc 2018-08-04 stsp {
3061 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3062 e5a0f69f 2018-08-18 stsp
3063 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3064 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3065 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3066 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3067 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3068 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3069 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3070 e5a0f69f 2018-08-18 stsp return err;
3071 5dc9f4bc 2018-08-04 stsp }
3072 5dc9f4bc 2018-08-04 stsp
3073 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3074 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3075 5dc9f4bc 2018-08-04 stsp {
3076 a3404814 2018-09-02 stsp const struct got_error *err;
3077 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3078 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3079 a3404814 2018-09-02 stsp
3080 a3404814 2018-09-02 stsp if (s->id1) {
3081 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3082 a3404814 2018-09-02 stsp if (err)
3083 a3404814 2018-09-02 stsp return err;
3084 a3404814 2018-09-02 stsp }
3085 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3086 a3404814 2018-09-02 stsp if (err)
3087 a3404814 2018-09-02 stsp return err;
3088 26ed57b2 2018-05-19 stsp
3089 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3090 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3091 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3092 a3404814 2018-09-02 stsp free(id_str1);
3093 a3404814 2018-09-02 stsp free(id_str2);
3094 a3404814 2018-09-02 stsp return err;
3095 a3404814 2018-09-02 stsp }
3096 a3404814 2018-09-02 stsp free(id_str1);
3097 a3404814 2018-09-02 stsp free(id_str2);
3098 a3404814 2018-09-02 stsp
3099 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
3100 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
3101 bddb1296 2019-11-08 stsp header, &s->colors);
3102 15a087fe 2019-02-21 stsp }
3103 15a087fe 2019-02-21 stsp
3104 15a087fe 2019-02-21 stsp static const struct got_error *
3105 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3106 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3107 15a087fe 2019-02-21 stsp {
3108 d7a04538 2019-02-21 stsp const struct got_error *err;
3109 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3110 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3111 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3112 15a087fe 2019-02-21 stsp
3113 15a087fe 2019-02-21 stsp free(s->id2);
3114 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3115 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3116 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3117 15a087fe 2019-02-21 stsp
3118 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3119 d7a04538 2019-02-21 stsp if (err)
3120 d7a04538 2019-02-21 stsp return err;
3121 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3122 15a087fe 2019-02-21 stsp free(s->id1);
3123 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3124 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3125 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3126 15a087fe 2019-02-21 stsp return NULL;
3127 0cf4efb1 2018-09-29 stsp }
3128 0cf4efb1 2018-09-29 stsp
3129 0cf4efb1 2018-09-29 stsp static const struct got_error *
3130 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3131 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3132 e5a0f69f 2018-08-18 stsp {
3133 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3134 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3135 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3136 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3137 e5a0f69f 2018-08-18 stsp int i;
3138 e5a0f69f 2018-08-18 stsp
3139 e5a0f69f 2018-08-18 stsp switch (ch) {
3140 1e37a5c2 2019-05-12 jcs case 'k':
3141 1e37a5c2 2019-05-12 jcs case KEY_UP:
3142 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3143 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3144 1e37a5c2 2019-05-12 jcs break;
3145 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3146 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3147 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3148 26ed57b2 2018-05-19 stsp break;
3149 1e37a5c2 2019-05-12 jcs i = 0;
3150 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3151 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3152 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3153 1e37a5c2 2019-05-12 jcs break;
3154 1e37a5c2 2019-05-12 jcs case 'j':
3155 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3156 1e37a5c2 2019-05-12 jcs if (!s->eof)
3157 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3158 1e37a5c2 2019-05-12 jcs break;
3159 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3160 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3161 1e37a5c2 2019-05-12 jcs case ' ':
3162 00ba99a7 2019-05-12 jcs if (s->eof)
3163 1e37a5c2 2019-05-12 jcs break;
3164 1e37a5c2 2019-05-12 jcs i = 0;
3165 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3166 1e37a5c2 2019-05-12 jcs char *line;
3167 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3168 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3169 1e37a5c2 2019-05-12 jcs if (line == NULL)
3170 34bc9ec9 2019-02-22 stsp break;
3171 1e37a5c2 2019-05-12 jcs }
3172 1e37a5c2 2019-05-12 jcs break;
3173 1e37a5c2 2019-05-12 jcs case '[':
3174 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3175 1e37a5c2 2019-05-12 jcs s->diff_context--;
3176 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3177 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3178 1e37a5c2 2019-05-12 jcs }
3179 1e37a5c2 2019-05-12 jcs break;
3180 1e37a5c2 2019-05-12 jcs case ']':
3181 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3182 1e37a5c2 2019-05-12 jcs s->diff_context++;
3183 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3184 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3185 1e37a5c2 2019-05-12 jcs }
3186 1e37a5c2 2019-05-12 jcs break;
3187 1e37a5c2 2019-05-12 jcs case '<':
3188 1e37a5c2 2019-05-12 jcs case ',':
3189 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3190 48ae06ee 2018-10-18 stsp break;
3191 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3192 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3193 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3194 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3195 48ae06ee 2018-10-18 stsp break;
3196 6524637e 2019-02-21 stsp
3197 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3198 1e37a5c2 2019-05-12 jcs KEY_UP);
3199 1e37a5c2 2019-05-12 jcs if (err)
3200 1e37a5c2 2019-05-12 jcs break;
3201 15a087fe 2019-02-21 stsp
3202 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3203 1e37a5c2 2019-05-12 jcs if (err)
3204 1e37a5c2 2019-05-12 jcs break;
3205 15a087fe 2019-02-21 stsp
3206 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3207 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3208 15a087fe 2019-02-21 stsp
3209 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3210 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3211 1e37a5c2 2019-05-12 jcs break;
3212 1e37a5c2 2019-05-12 jcs case '>':
3213 1e37a5c2 2019-05-12 jcs case '.':
3214 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3215 15a087fe 2019-02-21 stsp break;
3216 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3217 5e224a3e 2019-02-22 stsp
3218 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3219 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3220 5e224a3e 2019-02-22 stsp
3221 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
3222 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
3223 1e37a5c2 2019-05-12 jcs update_panels();
3224 1e37a5c2 2019-05-12 jcs doupdate();
3225 6e73b0d6 2019-02-22 stsp
3226 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
3227 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3228 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3229 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
3230 fb872ab2 2019-02-21 stsp if (err)
3231 fb872ab2 2019-02-21 stsp break;
3232 1e37a5c2 2019-05-12 jcs }
3233 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3234 1e37a5c2 2019-05-12 jcs KEY_DOWN);
3235 1e37a5c2 2019-05-12 jcs if (err)
3236 1e37a5c2 2019-05-12 jcs break;
3237 15a087fe 2019-02-21 stsp
3238 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3239 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3240 1e37a5c2 2019-05-12 jcs break;
3241 15a087fe 2019-02-21 stsp
3242 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3243 1e37a5c2 2019-05-12 jcs if (err)
3244 1e37a5c2 2019-05-12 jcs break;
3245 15a087fe 2019-02-21 stsp
3246 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3247 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3248 1e37a5c2 2019-05-12 jcs
3249 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3250 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3251 1e37a5c2 2019-05-12 jcs break;
3252 1e37a5c2 2019-05-12 jcs default:
3253 1e37a5c2 2019-05-12 jcs break;
3254 26ed57b2 2018-05-19 stsp }
3255 e5a0f69f 2018-08-18 stsp
3256 bcbd79e2 2018-08-19 stsp return err;
3257 26ed57b2 2018-05-19 stsp }
3258 26ed57b2 2018-05-19 stsp
3259 4ed7e80c 2018-05-20 stsp static const struct got_error *
3260 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3261 9f7d7167 2018-04-29 stsp {
3262 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3263 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3264 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3265 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3266 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
3267 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3268 26ed57b2 2018-05-19 stsp int ch;
3269 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3270 70ac5f84 2019-03-28 stsp
3271 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3272 26ed57b2 2018-05-19 stsp
3273 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3274 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3275 eb6600df 2019-01-04 stsp NULL) == -1)
3276 26ed57b2 2018-05-19 stsp err(1, "pledge");
3277 26ed57b2 2018-05-19 stsp #endif
3278 26ed57b2 2018-05-19 stsp
3279 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3280 26ed57b2 2018-05-19 stsp switch (ch) {
3281 26ed57b2 2018-05-19 stsp default:
3282 17020d27 2019-03-07 stsp usage_diff();
3283 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3284 26ed57b2 2018-05-19 stsp }
3285 26ed57b2 2018-05-19 stsp }
3286 26ed57b2 2018-05-19 stsp
3287 26ed57b2 2018-05-19 stsp argc -= optind;
3288 26ed57b2 2018-05-19 stsp argv += optind;
3289 26ed57b2 2018-05-19 stsp
3290 26ed57b2 2018-05-19 stsp if (argc == 0) {
3291 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3292 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3293 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
3294 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3295 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3296 15a94983 2018-12-23 stsp id_str1 = argv[0];
3297 15a94983 2018-12-23 stsp id_str2 = argv[1];
3298 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
3299 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
3300 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3301 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
3302 15a94983 2018-12-23 stsp id_str1 = argv[1];
3303 15a94983 2018-12-23 stsp id_str2 = argv[2];
3304 26ed57b2 2018-05-19 stsp } else
3305 26ed57b2 2018-05-19 stsp usage_diff();
3306 a915003a 2019-02-05 stsp
3307 a915003a 2019-02-05 stsp init_curses();
3308 eb6600df 2019-01-04 stsp
3309 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3310 eb6600df 2019-01-04 stsp if (error)
3311 eb6600df 2019-01-04 stsp goto done;
3312 26ed57b2 2018-05-19 stsp
3313 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3314 26ed57b2 2018-05-19 stsp if (error)
3315 26ed57b2 2018-05-19 stsp goto done;
3316 26ed57b2 2018-05-19 stsp
3317 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3318 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3319 26ed57b2 2018-05-19 stsp if (error)
3320 26ed57b2 2018-05-19 stsp goto done;
3321 26ed57b2 2018-05-19 stsp
3322 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3323 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3324 26ed57b2 2018-05-19 stsp if (error)
3325 26ed57b2 2018-05-19 stsp goto done;
3326 26ed57b2 2018-05-19 stsp
3327 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3328 8b473291 2019-02-21 stsp if (error)
3329 8b473291 2019-02-21 stsp goto done;
3330 8b473291 2019-02-21 stsp
3331 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3332 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3333 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3334 ea5e7bb5 2018-08-01 stsp goto done;
3335 ea5e7bb5 2018-08-01 stsp }
3336 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3337 5dc9f4bc 2018-08-04 stsp if (error)
3338 5dc9f4bc 2018-08-04 stsp goto done;
3339 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3340 26ed57b2 2018-05-19 stsp done:
3341 c02c541e 2019-03-29 stsp free(repo_path);
3342 921be706 2019-06-28 stsp if (repo)
3343 921be706 2019-06-28 stsp got_repo_close(repo);
3344 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3345 26ed57b2 2018-05-19 stsp return error;
3346 9f7d7167 2018-04-29 stsp }
3347 9f7d7167 2018-04-29 stsp
3348 4ed7e80c 2018-05-20 stsp __dead static void
3349 9f7d7167 2018-04-29 stsp usage_blame(void)
3350 9f7d7167 2018-04-29 stsp {
3351 80ddbec8 2018-04-29 stsp endwin();
3352 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3353 9f7d7167 2018-04-29 stsp getprogname());
3354 9f7d7167 2018-04-29 stsp exit(1);
3355 9f7d7167 2018-04-29 stsp }
3356 84451b3e 2018-07-10 stsp
3357 84451b3e 2018-07-10 stsp struct tog_blame_line {
3358 84451b3e 2018-07-10 stsp int annotated;
3359 84451b3e 2018-07-10 stsp struct got_object_id *id;
3360 84451b3e 2018-07-10 stsp };
3361 9f7d7167 2018-04-29 stsp
3362 4ed7e80c 2018-05-20 stsp static const struct got_error *
3363 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3364 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3365 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3366 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3367 11b20872 2019-11-08 stsp struct tog_colors *colors)
3368 84451b3e 2018-07-10 stsp {
3369 84451b3e 2018-07-10 stsp const struct got_error *err;
3370 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3371 84451b3e 2018-07-10 stsp char *line;
3372 84451b3e 2018-07-10 stsp size_t len;
3373 84451b3e 2018-07-10 stsp wchar_t *wline;
3374 27a741e5 2019-09-11 stsp int width;
3375 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3376 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3377 ab089a2a 2018-07-12 stsp char *id_str;
3378 11b20872 2019-11-08 stsp struct tog_color *tc;
3379 ab089a2a 2018-07-12 stsp
3380 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3381 ab089a2a 2018-07-12 stsp if (err)
3382 ab089a2a 2018-07-12 stsp return err;
3383 84451b3e 2018-07-10 stsp
3384 84451b3e 2018-07-10 stsp rewind(f);
3385 f7d12f7e 2018-08-01 stsp werase(view->window);
3386 84451b3e 2018-07-10 stsp
3387 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3388 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3389 ab089a2a 2018-07-12 stsp free(id_str);
3390 ab089a2a 2018-07-12 stsp return err;
3391 ab089a2a 2018-07-12 stsp }
3392 ab089a2a 2018-07-12 stsp
3393 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3394 ab089a2a 2018-07-12 stsp free(line);
3395 2550e4c3 2018-07-13 stsp line = NULL;
3396 1cae65b4 2019-09-22 stsp if (err)
3397 1cae65b4 2019-09-22 stsp return err;
3398 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3399 a3404814 2018-09-02 stsp wstandout(view->window);
3400 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3401 11b20872 2019-11-08 stsp if (tc)
3402 11b20872 2019-11-08 stsp wattr_on(view->window,
3403 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3404 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3405 11b20872 2019-11-08 stsp if (tc)
3406 11b20872 2019-11-08 stsp wattr_off(view->window,
3407 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3408 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3409 a3404814 2018-09-02 stsp wstandend(view->window);
3410 2550e4c3 2018-07-13 stsp free(wline);
3411 2550e4c3 2018-07-13 stsp wline = NULL;
3412 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3413 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3414 ab089a2a 2018-07-12 stsp
3415 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3416 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3417 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3418 ab089a2a 2018-07-12 stsp free(id_str);
3419 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3420 ab089a2a 2018-07-12 stsp }
3421 ab089a2a 2018-07-12 stsp free(id_str);
3422 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3423 3f60a8ef 2018-07-10 stsp free(line);
3424 2550e4c3 2018-07-13 stsp line = NULL;
3425 3f60a8ef 2018-07-10 stsp if (err)
3426 3f60a8ef 2018-07-10 stsp return err;
3427 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3428 2550e4c3 2018-07-13 stsp free(wline);
3429 2550e4c3 2018-07-13 stsp wline = NULL;
3430 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3431 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3432 3f60a8ef 2018-07-10 stsp
3433 84451b3e 2018-07-10 stsp *eof = 0;
3434 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3435 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3436 84451b3e 2018-07-10 stsp if (line == NULL) {
3437 84451b3e 2018-07-10 stsp *eof = 1;
3438 84451b3e 2018-07-10 stsp break;
3439 84451b3e 2018-07-10 stsp }
3440 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3441 84451b3e 2018-07-10 stsp free(line);
3442 84451b3e 2018-07-10 stsp continue;
3443 84451b3e 2018-07-10 stsp }
3444 84451b3e 2018-07-10 stsp
3445 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3446 27a741e5 2019-09-11 stsp width = 9;
3447 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3448 27a741e5 2019-09-11 stsp if (wline == NULL)
3449 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3450 27a741e5 2019-09-11 stsp } else {
3451 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3452 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3453 27a741e5 2019-09-11 stsp width += 9;
3454 27a741e5 2019-09-11 stsp }
3455 84451b3e 2018-07-10 stsp if (err) {
3456 84451b3e 2018-07-10 stsp free(line);
3457 84451b3e 2018-07-10 stsp return err;
3458 84451b3e 2018-07-10 stsp }
3459 84451b3e 2018-07-10 stsp
3460 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3461 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3462 b700b5d6 2018-07-10 stsp
3463 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3464 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3465 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3466 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3467 27a741e5 2019-09-11 stsp !(view->focussed &&
3468 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3469 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3470 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3471 8d0fe45a 2019-08-12 stsp char *id_str;
3472 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3473 8d0fe45a 2019-08-12 stsp if (err) {
3474 8d0fe45a 2019-08-12 stsp free(line);
3475 8d0fe45a 2019-08-12 stsp free(wline);
3476 8d0fe45a 2019-08-12 stsp return err;
3477 8d0fe45a 2019-08-12 stsp }
3478 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3479 11b20872 2019-11-08 stsp if (tc)
3480 11b20872 2019-11-08 stsp wattr_on(view->window,
3481 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3482 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3483 11b20872 2019-11-08 stsp if (tc)
3484 11b20872 2019-11-08 stsp wattr_off(view->window,
3485 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3486 8d0fe45a 2019-08-12 stsp free(id_str);
3487 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3488 8d0fe45a 2019-08-12 stsp } else {
3489 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3490 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3491 84451b3e 2018-07-10 stsp }
3492 ee41ec32 2018-07-10 stsp } else {
3493 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3494 ee41ec32 2018-07-10 stsp prev_id = NULL;
3495 ee41ec32 2018-07-10 stsp }
3496 84451b3e 2018-07-10 stsp
3497 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3498 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3499 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3500 27a741e5 2019-09-11 stsp
3501 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3502 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3503 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3504 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3505 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3506 84451b3e 2018-07-10 stsp free(line);
3507 2550e4c3 2018-07-13 stsp free(wline);
3508 2550e4c3 2018-07-13 stsp wline = NULL;
3509 84451b3e 2018-07-10 stsp }
3510 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3511 84451b3e 2018-07-10 stsp
3512 1a57306a 2018-09-02 stsp view_vborder(view);
3513 84451b3e 2018-07-10 stsp
3514 84451b3e 2018-07-10 stsp return NULL;
3515 84451b3e 2018-07-10 stsp }
3516 84451b3e 2018-07-10 stsp
3517 84451b3e 2018-07-10 stsp static const struct got_error *
3518 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3519 84451b3e 2018-07-10 stsp {
3520 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3521 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3522 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3523 1a76625f 2018-10-22 stsp int errcode;
3524 84451b3e 2018-07-10 stsp
3525 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3526 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3527 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3528 84451b3e 2018-07-10 stsp
3529 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3530 1a76625f 2018-10-22 stsp if (errcode)
3531 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3532 84451b3e 2018-07-10 stsp
3533 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3534 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3535 d68a0a7d 2018-07-10 stsp goto done;
3536 d68a0a7d 2018-07-10 stsp }
3537 d68a0a7d 2018-07-10 stsp
3538 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3539 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3540 d68a0a7d 2018-07-10 stsp
3541 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3542 d68a0a7d 2018-07-10 stsp if (line->annotated)
3543 d68a0a7d 2018-07-10 stsp goto done;
3544 d68a0a7d 2018-07-10 stsp
3545 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3546 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3547 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3548 84451b3e 2018-07-10 stsp goto done;
3549 84451b3e 2018-07-10 stsp }
3550 84451b3e 2018-07-10 stsp line->annotated = 1;
3551 84451b3e 2018-07-10 stsp done:
3552 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3553 1a76625f 2018-10-22 stsp if (errcode)
3554 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3555 84451b3e 2018-07-10 stsp return err;
3556 84451b3e 2018-07-10 stsp }
3557 84451b3e 2018-07-10 stsp
3558 84451b3e 2018-07-10 stsp static void *
3559 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3560 84451b3e 2018-07-10 stsp {
3561 18430de3 2018-07-10 stsp const struct got_error *err;
3562 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3563 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3564 1a76625f 2018-10-22 stsp int errcode;
3565 18430de3 2018-07-10 stsp
3566 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
3567 61266923 2020-01-14 stsp if (err)
3568 61266923 2020-01-14 stsp return (void *)err;
3569 61266923 2020-01-14 stsp
3570 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3571 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3572 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3573 fc06ba56 2019-08-22 stsp err = NULL;
3574 18430de3 2018-07-10 stsp
3575 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3576 1a76625f 2018-10-22 stsp if (errcode)
3577 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3578 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3579 18430de3 2018-07-10 stsp
3580 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3581 c9beca56 2018-07-22 stsp ta->repo = NULL;
3582 c9beca56 2018-07-22 stsp *ta->complete = 1;
3583 18430de3 2018-07-10 stsp
3584 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3585 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3586 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3587 18430de3 2018-07-10 stsp
3588 18430de3 2018-07-10 stsp return (void *)err;
3589 84451b3e 2018-07-10 stsp }
3590 84451b3e 2018-07-10 stsp
3591 245d91c1 2018-07-12 stsp static struct got_object_id *
3592 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3593 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3594 245d91c1 2018-07-12 stsp {
3595 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3596 8d0fe45a 2019-08-12 stsp
3597 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3598 8d0fe45a 2019-08-12 stsp return NULL;
3599 b880a918 2018-07-10 stsp
3600 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3601 245d91c1 2018-07-12 stsp if (!line->annotated)
3602 245d91c1 2018-07-12 stsp return NULL;
3603 245d91c1 2018-07-12 stsp
3604 245d91c1 2018-07-12 stsp return line->id;
3605 b880a918 2018-07-10 stsp }
3606 245d91c1 2018-07-12 stsp
3607 b880a918 2018-07-10 stsp static const struct got_error *
3608 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3609 a70480e0 2018-06-23 stsp {
3610 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3611 245d91c1 2018-07-12 stsp int i;
3612 245d91c1 2018-07-12 stsp
3613 245d91c1 2018-07-12 stsp if (blame->thread) {
3614 1a76625f 2018-10-22 stsp int errcode;
3615 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3616 1a76625f 2018-10-22 stsp if (errcode)
3617 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3618 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3619 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3620 1a76625f 2018-10-22 stsp if (errcode)
3621 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3622 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3623 1a76625f 2018-10-22 stsp if (errcode)
3624 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3625 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3626 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3627 245d91c1 2018-07-12 stsp err = NULL;
3628 245d91c1 2018-07-12 stsp blame->thread = NULL;
3629 245d91c1 2018-07-12 stsp }
3630 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3631 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3632 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3633 245d91c1 2018-07-12 stsp }
3634 245d91c1 2018-07-12 stsp if (blame->f) {
3635 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3636 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3637 245d91c1 2018-07-12 stsp blame->f = NULL;
3638 245d91c1 2018-07-12 stsp }
3639 57670559 2018-12-23 stsp if (blame->lines) {
3640 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3641 57670559 2018-12-23 stsp free(blame->lines[i].id);
3642 57670559 2018-12-23 stsp free(blame->lines);
3643 57670559 2018-12-23 stsp blame->lines = NULL;
3644 57670559 2018-12-23 stsp }
3645 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3646 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3647 245d91c1 2018-07-12 stsp
3648 245d91c1 2018-07-12 stsp return err;
3649 245d91c1 2018-07-12 stsp }
3650 245d91c1 2018-07-12 stsp
3651 245d91c1 2018-07-12 stsp static const struct got_error *
3652 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3653 fc06ba56 2019-08-22 stsp {
3654 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3655 fc06ba56 2019-08-22 stsp int *done = arg;
3656 fc06ba56 2019-08-22 stsp int errcode;
3657 fc06ba56 2019-08-22 stsp
3658 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3659 fc06ba56 2019-08-22 stsp if (errcode)
3660 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3661 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3662 fc06ba56 2019-08-22 stsp
3663 fc06ba56 2019-08-22 stsp if (*done)
3664 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3665 fc06ba56 2019-08-22 stsp
3666 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3667 fc06ba56 2019-08-22 stsp if (errcode)
3668 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3669 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3670 fc06ba56 2019-08-22 stsp
3671 fc06ba56 2019-08-22 stsp return err;
3672 fc06ba56 2019-08-22 stsp }
3673 fc06ba56 2019-08-22 stsp
3674 fc06ba56 2019-08-22 stsp static const struct got_error *
3675 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3676 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3677 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3678 245d91c1 2018-07-12 stsp struct got_repository *repo)
3679 245d91c1 2018-07-12 stsp {
3680 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3681 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3682 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3683 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3684 15a94983 2018-12-23 stsp int obj_type;
3685 a70480e0 2018-06-23 stsp
3686 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3687 27d434c2 2018-09-15 stsp if (err)
3688 15a94983 2018-12-23 stsp return err;
3689 15a94983 2018-12-23 stsp if (obj_id == NULL)
3690 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3691 27d434c2 2018-09-15 stsp
3692 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3693 84451b3e 2018-07-10 stsp if (err)
3694 84451b3e 2018-07-10 stsp goto done;
3695 27d434c2 2018-09-15 stsp
3696 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3697 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3698 84451b3e 2018-07-10 stsp goto done;
3699 84451b3e 2018-07-10 stsp }
3700 a70480e0 2018-06-23 stsp
3701 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3702 a70480e0 2018-06-23 stsp if (err)
3703 a70480e0 2018-06-23 stsp goto done;
3704 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3705 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3706 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3707 84451b3e 2018-07-10 stsp goto done;
3708 84451b3e 2018-07-10 stsp }
3709 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3710 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3711 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3712 84451b3e 2018-07-10 stsp goto done;
3713 b02560ec 2019-08-19 stsp
3714 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3715 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3716 b02560ec 2019-08-19 stsp blame->nlines--;
3717 a70480e0 2018-06-23 stsp
3718 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3719 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3720 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3721 84451b3e 2018-07-10 stsp goto done;
3722 84451b3e 2018-07-10 stsp }
3723 a70480e0 2018-06-23 stsp
3724 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3725 bd24772e 2018-07-11 stsp if (err)
3726 bd24772e 2018-07-11 stsp goto done;
3727 bd24772e 2018-07-11 stsp
3728 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3729 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3730 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3731 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3732 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3733 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3734 245d91c1 2018-07-12 stsp goto done;
3735 245d91c1 2018-07-12 stsp }
3736 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3737 245d91c1 2018-07-12 stsp
3738 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3739 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3740 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3741 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3742 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3743 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3744 245d91c1 2018-07-12 stsp *blame_complete = 0;
3745 245d91c1 2018-07-12 stsp
3746 245d91c1 2018-07-12 stsp done:
3747 245d91c1 2018-07-12 stsp if (blob)
3748 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3749 27d434c2 2018-09-15 stsp free(obj_id);
3750 245d91c1 2018-07-12 stsp if (err)
3751 245d91c1 2018-07-12 stsp stop_blame(blame);
3752 245d91c1 2018-07-12 stsp return err;
3753 245d91c1 2018-07-12 stsp }
3754 245d91c1 2018-07-12 stsp
3755 245d91c1 2018-07-12 stsp static const struct got_error *
3756 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3757 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3758 8b473291 2019-02-21 stsp struct got_repository *repo)
3759 245d91c1 2018-07-12 stsp {
3760 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3761 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3762 dbc6a6b6 2018-07-12 stsp
3763 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3764 245d91c1 2018-07-12 stsp
3765 c4843652 2019-08-12 stsp s->path = strdup(path);
3766 c4843652 2019-08-12 stsp if (s->path == NULL)
3767 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3768 c4843652 2019-08-12 stsp
3769 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3770 c4843652 2019-08-12 stsp if (err) {
3771 c4843652 2019-08-12 stsp free(s->path);
3772 7cbe629d 2018-08-04 stsp return err;
3773 c4843652 2019-08-12 stsp }
3774 245d91c1 2018-07-12 stsp
3775 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3776 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3777 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3778 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3779 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3780 fb2756b9 2018-08-04 stsp s->repo = repo;
3781 8b473291 2019-02-21 stsp s->refs = refs;
3782 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3783 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3784 7cbe629d 2018-08-04 stsp
3785 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
3786 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3787 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3788 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3789 11b20872 2019-11-08 stsp if (err)
3790 11b20872 2019-11-08 stsp return err;
3791 11b20872 2019-11-08 stsp }
3792 11b20872 2019-11-08 stsp
3793 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3794 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3795 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3796 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3797 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3798 e5a0f69f 2018-08-18 stsp
3799 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3800 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3801 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3802 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3803 7cbe629d 2018-08-04 stsp }
3804 7cbe629d 2018-08-04 stsp
3805 e5a0f69f 2018-08-18 stsp static const struct got_error *
3806 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3807 7cbe629d 2018-08-04 stsp {
3808 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3809 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3810 7cbe629d 2018-08-04 stsp
3811 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3812 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3813 e5a0f69f 2018-08-18 stsp
3814 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3815 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3816 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3817 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3818 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3819 7cbe629d 2018-08-04 stsp }
3820 e5a0f69f 2018-08-18 stsp
3821 e5a0f69f 2018-08-18 stsp free(s->path);
3822 11b20872 2019-11-08 stsp free_colors(&s->colors);
3823 e5a0f69f 2018-08-18 stsp
3824 e5a0f69f 2018-08-18 stsp return err;
3825 7cbe629d 2018-08-04 stsp }
3826 7cbe629d 2018-08-04 stsp
3827 7cbe629d 2018-08-04 stsp static const struct got_error *
3828 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3829 6c4c42e0 2019-06-24 stsp {
3830 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3831 6c4c42e0 2019-06-24 stsp
3832 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3833 6c4c42e0 2019-06-24 stsp return NULL;
3834 6c4c42e0 2019-06-24 stsp }
3835 6c4c42e0 2019-06-24 stsp
3836 6c4c42e0 2019-06-24 stsp static const struct got_error *
3837 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3838 6c4c42e0 2019-06-24 stsp {
3839 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3840 6c4c42e0 2019-06-24 stsp int lineno;
3841 6c4c42e0 2019-06-24 stsp
3842 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3843 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3844 6c4c42e0 2019-06-24 stsp return NULL;
3845 6c4c42e0 2019-06-24 stsp }
3846 6c4c42e0 2019-06-24 stsp
3847 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3848 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3849 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3850 6c4c42e0 2019-06-24 stsp else
3851 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3852 6c4c42e0 2019-06-24 stsp } else {
3853 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3854 6c4c42e0 2019-06-24 stsp lineno = 1;
3855 6c4c42e0 2019-06-24 stsp else
3856 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3857 6c4c42e0 2019-06-24 stsp }
3858 6c4c42e0 2019-06-24 stsp
3859 6c4c42e0 2019-06-24 stsp while (1) {
3860 6c4c42e0 2019-06-24 stsp char *line = NULL;
3861 6c4c42e0 2019-06-24 stsp off_t offset;
3862 6c4c42e0 2019-06-24 stsp size_t len;
3863 6c4c42e0 2019-06-24 stsp
3864 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3865 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3866 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3867 6c4c42e0 2019-06-24 stsp free(line);
3868 6c4c42e0 2019-06-24 stsp break;
3869 6c4c42e0 2019-06-24 stsp }
3870 2246482e 2019-06-25 stsp
3871 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3872 6c4c42e0 2019-06-24 stsp lineno = 1;
3873 6c4c42e0 2019-06-24 stsp else
3874 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3875 6c4c42e0 2019-06-24 stsp }
3876 6c4c42e0 2019-06-24 stsp
3877 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3878 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3879 6c4c42e0 2019-06-24 stsp free(line);
3880 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3881 6c4c42e0 2019-06-24 stsp }
3882 6c4c42e0 2019-06-24 stsp free(line);
3883 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3884 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3885 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3886 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3887 6c4c42e0 2019-06-24 stsp free(line);
3888 6c4c42e0 2019-06-24 stsp break;
3889 6c4c42e0 2019-06-24 stsp }
3890 6c4c42e0 2019-06-24 stsp free(line);
3891 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3892 6c4c42e0 2019-06-24 stsp lineno++;
3893 6c4c42e0 2019-06-24 stsp else
3894 6c4c42e0 2019-06-24 stsp lineno--;
3895 6c4c42e0 2019-06-24 stsp }
3896 6c4c42e0 2019-06-24 stsp
3897 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3898 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3899 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3900 6c4c42e0 2019-06-24 stsp }
3901 6c4c42e0 2019-06-24 stsp
3902 6c4c42e0 2019-06-24 stsp return NULL;
3903 6c4c42e0 2019-06-24 stsp }
3904 6c4c42e0 2019-06-24 stsp
3905 6c4c42e0 2019-06-24 stsp static const struct got_error *
3906 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3907 7cbe629d 2018-08-04 stsp {
3908 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3909 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3910 2b380cc8 2018-10-24 stsp int errcode;
3911 2b380cc8 2018-10-24 stsp
3912 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3913 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3914 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3915 2b380cc8 2018-10-24 stsp if (errcode)
3916 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3917 51fe7530 2019-08-19 stsp
3918 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3919 2b380cc8 2018-10-24 stsp }
3920 e5a0f69f 2018-08-18 stsp
3921 51fe7530 2019-08-19 stsp if (s->blame_complete)
3922 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3923 51fe7530 2019-08-19 stsp
3924 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3925 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3926 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3927 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3928 e5a0f69f 2018-08-18 stsp
3929 669b5ffa 2018-10-07 stsp view_vborder(view);
3930 e5a0f69f 2018-08-18 stsp return err;
3931 e5a0f69f 2018-08-18 stsp }
3932 e5a0f69f 2018-08-18 stsp
3933 e5a0f69f 2018-08-18 stsp static const struct got_error *
3934 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3935 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3936 e5a0f69f 2018-08-18 stsp {
3937 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3938 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3939 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3940 669b5ffa 2018-10-07 stsp int begin_x = 0;
3941 7cbe629d 2018-08-04 stsp
3942 e5a0f69f 2018-08-18 stsp switch (ch) {
3943 1e37a5c2 2019-05-12 jcs case 'q':
3944 1e37a5c2 2019-05-12 jcs s->done = 1;
3945 1e37a5c2 2019-05-12 jcs break;
3946 1e37a5c2 2019-05-12 jcs case 'k':
3947 1e37a5c2 2019-05-12 jcs case KEY_UP:
3948 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3949 1e37a5c2 2019-05-12 jcs s->selected_line--;
3950 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3951 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3952 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3953 1e37a5c2 2019-05-12 jcs break;
3954 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3955 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3956 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3957 e5a0f69f 2018-08-18 stsp break;
3958 1e37a5c2 2019-05-12 jcs }
3959 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3960 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3961 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3962 1e37a5c2 2019-05-12 jcs else
3963 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3964 1e37a5c2 2019-05-12 jcs break;
3965 1e37a5c2 2019-05-12 jcs case 'j':
3966 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3967 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3968 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3969 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3970 1e37a5c2 2019-05-12 jcs s->selected_line++;
3971 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3972 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3973 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3974 1e37a5c2 2019-05-12 jcs break;
3975 1e37a5c2 2019-05-12 jcs case 'b':
3976 1e37a5c2 2019-05-12 jcs case 'p': {
3977 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3978 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3979 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3980 1e37a5c2 2019-05-12 jcs if (id == NULL)
3981 e5a0f69f 2018-08-18 stsp break;
3982 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3983 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3984 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3985 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3986 1e37a5c2 2019-05-12 jcs int obj_type;
3987 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3988 1e37a5c2 2019-05-12 jcs s->repo, id);
3989 e5a0f69f 2018-08-18 stsp if (err)
3990 e5a0f69f 2018-08-18 stsp break;
3991 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3992 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3993 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3994 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3995 e5a0f69f 2018-08-18 stsp break;
3996 e5a0f69f 2018-08-18 stsp }
3997 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3998 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3999 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4000 e5a0f69f 2018-08-18 stsp if (err) {
4001 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4002 1e37a5c2 2019-05-12 jcs err = NULL;
4003 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4004 e5a0f69f 2018-08-18 stsp break;
4005 e5a0f69f 2018-08-18 stsp }
4006 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4007 1e37a5c2 2019-05-12 jcs blob_id);
4008 1e37a5c2 2019-05-12 jcs free(blob_id);
4009 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4010 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4011 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4012 e5a0f69f 2018-08-18 stsp break;
4013 1e37a5c2 2019-05-12 jcs }
4014 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4015 1e37a5c2 2019-05-12 jcs pid->id);
4016 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4017 1e37a5c2 2019-05-12 jcs } else {
4018 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4019 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4020 1e37a5c2 2019-05-12 jcs break;
4021 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4022 1e37a5c2 2019-05-12 jcs id);
4023 1e37a5c2 2019-05-12 jcs }
4024 1e37a5c2 2019-05-12 jcs if (err)
4025 e5a0f69f 2018-08-18 stsp break;
4026 1e37a5c2 2019-05-12 jcs s->done = 1;
4027 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4028 1e37a5c2 2019-05-12 jcs s->done = 0;
4029 1e37a5c2 2019-05-12 jcs if (thread_err)
4030 1e37a5c2 2019-05-12 jcs break;
4031 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4032 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4033 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4034 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4035 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4036 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4037 1e37a5c2 2019-05-12 jcs if (err)
4038 1e37a5c2 2019-05-12 jcs break;
4039 1e37a5c2 2019-05-12 jcs break;
4040 1e37a5c2 2019-05-12 jcs }
4041 1e37a5c2 2019-05-12 jcs case 'B': {
4042 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4043 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4044 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4045 1e37a5c2 2019-05-12 jcs break;
4046 1e37a5c2 2019-05-12 jcs s->done = 1;
4047 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4048 1e37a5c2 2019-05-12 jcs s->done = 0;
4049 1e37a5c2 2019-05-12 jcs if (thread_err)
4050 1e37a5c2 2019-05-12 jcs break;
4051 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4052 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4053 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4054 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4055 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4056 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4057 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4058 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4059 1e37a5c2 2019-05-12 jcs if (err)
4060 1e37a5c2 2019-05-12 jcs break;
4061 1e37a5c2 2019-05-12 jcs break;
4062 1e37a5c2 2019-05-12 jcs }
4063 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4064 1e37a5c2 2019-05-12 jcs case '\r': {
4065 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4066 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4067 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4068 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4069 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4070 1e37a5c2 2019-05-12 jcs if (id == NULL)
4071 1e37a5c2 2019-05-12 jcs break;
4072 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4073 1e37a5c2 2019-05-12 jcs if (err)
4074 1e37a5c2 2019-05-12 jcs break;
4075 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4076 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4077 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4078 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4079 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4080 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4081 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4082 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4083 1e37a5c2 2019-05-12 jcs break;
4084 15a94983 2018-12-23 stsp }
4085 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4086 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4087 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4088 1e37a5c2 2019-05-12 jcs if (err) {
4089 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4090 1e37a5c2 2019-05-12 jcs break;
4091 1e37a5c2 2019-05-12 jcs }
4092 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4093 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4094 1e37a5c2 2019-05-12 jcs if (err)
4095 34bc9ec9 2019-02-22 stsp break;
4096 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4097 1e37a5c2 2019-05-12 jcs if (err) {
4098 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4099 e5a0f69f 2018-08-18 stsp break;
4100 e5a0f69f 2018-08-18 stsp }
4101 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4102 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4103 1e37a5c2 2019-05-12 jcs } else
4104 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4105 1e37a5c2 2019-05-12 jcs if (err)
4106 e5a0f69f 2018-08-18 stsp break;
4107 1e37a5c2 2019-05-12 jcs break;
4108 1e37a5c2 2019-05-12 jcs }
4109 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4110 1e37a5c2 2019-05-12 jcs case ' ':
4111 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4112 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4113 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4114 e5a0f69f 2018-08-18 stsp break;
4115 1e37a5c2 2019-05-12 jcs }
4116 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4117 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4118 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4119 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4120 e5a0f69f 2018-08-18 stsp break;
4121 1e37a5c2 2019-05-12 jcs }
4122 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4123 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4124 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4125 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4126 1e37a5c2 2019-05-12 jcs else
4127 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4128 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4129 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4130 1e37a5c2 2019-05-12 jcs break;
4131 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4132 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4133 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4134 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4135 1e37a5c2 2019-05-12 jcs }
4136 1e37a5c2 2019-05-12 jcs break;
4137 1e37a5c2 2019-05-12 jcs default:
4138 1e37a5c2 2019-05-12 jcs break;
4139 a70480e0 2018-06-23 stsp }
4140 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4141 a70480e0 2018-06-23 stsp }
4142 a70480e0 2018-06-23 stsp
4143 a70480e0 2018-06-23 stsp static const struct got_error *
4144 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4145 9f7d7167 2018-04-29 stsp {
4146 a70480e0 2018-06-23 stsp const struct got_error *error;
4147 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4148 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4149 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4150 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4151 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4152 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4153 a70480e0 2018-06-23 stsp int ch;
4154 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4155 a70480e0 2018-06-23 stsp
4156 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4157 70ac5f84 2019-03-28 stsp
4158 a70480e0 2018-06-23 stsp #ifndef PROFILE
4159 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4160 8e94dd5b 2019-01-04 stsp NULL) == -1)
4161 a70480e0 2018-06-23 stsp err(1, "pledge");
4162 a70480e0 2018-06-23 stsp #endif
4163 a70480e0 2018-06-23 stsp
4164 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4165 a70480e0 2018-06-23 stsp switch (ch) {
4166 a70480e0 2018-06-23 stsp case 'c':
4167 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4168 a70480e0 2018-06-23 stsp break;
4169 69069811 2018-08-02 stsp case 'r':
4170 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4171 69069811 2018-08-02 stsp if (repo_path == NULL)
4172 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4173 9ba1d308 2019-10-21 stsp optarg);
4174 69069811 2018-08-02 stsp break;
4175 a70480e0 2018-06-23 stsp default:
4176 17020d27 2019-03-07 stsp usage_blame();
4177 a70480e0 2018-06-23 stsp /* NOTREACHED */
4178 a70480e0 2018-06-23 stsp }
4179 a70480e0 2018-06-23 stsp }
4180 a70480e0 2018-06-23 stsp
4181 a70480e0 2018-06-23 stsp argc -= optind;
4182 a70480e0 2018-06-23 stsp argv += optind;
4183 a70480e0 2018-06-23 stsp
4184 69069811 2018-08-02 stsp if (argc == 1)
4185 69069811 2018-08-02 stsp path = argv[0];
4186 69069811 2018-08-02 stsp else
4187 a70480e0 2018-06-23 stsp usage_blame();
4188 69069811 2018-08-02 stsp
4189 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4190 69069811 2018-08-02 stsp if (cwd == NULL) {
4191 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4192 69069811 2018-08-02 stsp goto done;
4193 69069811 2018-08-02 stsp }
4194 69069811 2018-08-02 stsp if (repo_path == NULL) {
4195 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4196 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4197 69069811 2018-08-02 stsp goto done;
4198 eb41ed75 2019-02-05 stsp else
4199 eb41ed75 2019-02-05 stsp error = NULL;
4200 eb41ed75 2019-02-05 stsp if (worktree) {
4201 eb41ed75 2019-02-05 stsp repo_path =
4202 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4203 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
4204 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4205 eb41ed75 2019-02-05 stsp if (error)
4206 eb41ed75 2019-02-05 stsp goto done;
4207 eb41ed75 2019-02-05 stsp } else {
4208 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4209 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
4210 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4211 eb41ed75 2019-02-05 stsp goto done;
4212 eb41ed75 2019-02-05 stsp }
4213 69069811 2018-08-02 stsp }
4214 69069811 2018-08-02 stsp }
4215 a915003a 2019-02-05 stsp
4216 a915003a 2019-02-05 stsp init_curses();
4217 69069811 2018-08-02 stsp
4218 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4219 c02c541e 2019-03-29 stsp if (error != NULL)
4220 8e94dd5b 2019-01-04 stsp goto done;
4221 69069811 2018-08-02 stsp
4222 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4223 c02c541e 2019-03-29 stsp if (error)
4224 92205607 2019-01-04 stsp goto done;
4225 69069811 2018-08-02 stsp
4226 eb41ed75 2019-02-05 stsp if (worktree) {
4227 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4228 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4229 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4230 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4231 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4232 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4233 eb41ed75 2019-02-05 stsp path) == -1) {
4234 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4235 eb41ed75 2019-02-05 stsp goto done;
4236 eb41ed75 2019-02-05 stsp }
4237 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4238 eb41ed75 2019-02-05 stsp free(p);
4239 eb41ed75 2019-02-05 stsp } else {
4240 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4241 eb41ed75 2019-02-05 stsp }
4242 eb41ed75 2019-02-05 stsp if (error)
4243 69069811 2018-08-02 stsp goto done;
4244 a70480e0 2018-06-23 stsp
4245 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4246 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4247 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4248 a70480e0 2018-06-23 stsp if (error != NULL)
4249 66b4983c 2018-06-23 stsp goto done;
4250 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4251 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4252 a70480e0 2018-06-23 stsp } else {
4253 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
4254 f2b6a97d 2019-07-15 stsp if (error) {
4255 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4256 f2b6a97d 2019-07-15 stsp goto done;
4257 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4258 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4259 f2b6a97d 2019-07-15 stsp }
4260 a70480e0 2018-06-23 stsp }
4261 a19e88aa 2018-06-23 stsp if (error != NULL)
4262 8b473291 2019-02-21 stsp goto done;
4263 8b473291 2019-02-21 stsp
4264 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4265 8b473291 2019-02-21 stsp if (error)
4266 a19e88aa 2018-06-23 stsp goto done;
4267 a70480e0 2018-06-23 stsp
4268 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4269 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4270 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4271 e1cd8fed 2018-08-01 stsp goto done;
4272 e1cd8fed 2018-08-01 stsp }
4273 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4274 7cbe629d 2018-08-04 stsp if (error)
4275 7cbe629d 2018-08-04 stsp goto done;
4276 12314ad4 2019-08-31 stsp if (worktree) {
4277 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4278 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4279 12314ad4 2019-08-31 stsp worktree = NULL;
4280 12314ad4 2019-08-31 stsp }
4281 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4282 a70480e0 2018-06-23 stsp done:
4283 69069811 2018-08-02 stsp free(repo_path);
4284 69069811 2018-08-02 stsp free(cwd);
4285 a70480e0 2018-06-23 stsp free(commit_id);
4286 eb41ed75 2019-02-05 stsp if (worktree)
4287 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4288 a70480e0 2018-06-23 stsp if (repo)
4289 a70480e0 2018-06-23 stsp got_repo_close(repo);
4290 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4291 a70480e0 2018-06-23 stsp return error;
4292 ffd1d5e5 2018-06-23 stsp }
4293 ffd1d5e5 2018-06-23 stsp
4294 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4295 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4296 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4297 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4298 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4299 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4300 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4301 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4302 ffd1d5e5 2018-06-23 stsp {
4303 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4304 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4305 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4306 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4307 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4308 ffd1d5e5 2018-06-23 stsp
4309 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4310 ffd1d5e5 2018-06-23 stsp
4311 f7d12f7e 2018-08-01 stsp werase(view->window);
4312 ffd1d5e5 2018-06-23 stsp
4313 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4314 ffd1d5e5 2018-06-23 stsp return NULL;
4315 ffd1d5e5 2018-06-23 stsp
4316 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4317 ffd1d5e5 2018-06-23 stsp if (err)
4318 ffd1d5e5 2018-06-23 stsp return err;
4319 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4320 a3404814 2018-09-02 stsp wstandout(view->window);
4321 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4322 11b20872 2019-11-08 stsp if (tc)
4323 11b20872 2019-11-08 stsp wattr_on(view->window,
4324 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4325 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4326 11b20872 2019-11-08 stsp if (tc)
4327 11b20872 2019-11-08 stsp wattr_off(view->window,
4328 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4329 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4330 a3404814 2018-09-02 stsp wstandend(view->window);
4331 2550e4c3 2018-07-13 stsp free(wline);
4332 2550e4c3 2018-07-13 stsp wline = NULL;
4333 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4334 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4335 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4336 ffd1d5e5 2018-06-23 stsp return NULL;
4337 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4338 ce52c690 2018-06-23 stsp if (err)
4339 ce52c690 2018-06-23 stsp return err;
4340 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4341 2550e4c3 2018-07-13 stsp free(wline);
4342 2550e4c3 2018-07-13 stsp wline = NULL;
4343 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4344 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4345 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4346 ffd1d5e5 2018-06-23 stsp return NULL;
4347 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4348 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4349 a1eca9bb 2018-06-23 stsp return NULL;
4350 ffd1d5e5 2018-06-23 stsp
4351 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4352 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4353 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4354 0cf4efb1 2018-09-29 stsp if (view->focussed)
4355 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4356 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4357 ffd1d5e5 2018-06-23 stsp }
4358 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4359 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4360 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4361 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4362 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4363 ffd1d5e5 2018-06-23 stsp return NULL;
4364 ffd1d5e5 2018-06-23 stsp n = 1;
4365 ffd1d5e5 2018-06-23 stsp } else {
4366 ffd1d5e5 2018-06-23 stsp n = 0;
4367 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4368 ffd1d5e5 2018-06-23 stsp }
4369 ffd1d5e5 2018-06-23 stsp
4370 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4371 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4372 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4373 848d6979 2019-08-12 stsp const char *modestr = "";
4374 56e0773d 2019-11-28 stsp mode_t mode;
4375 1d13200f 2018-07-12 stsp
4376 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4377 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4378 56e0773d 2019-11-28 stsp
4379 1d13200f 2018-07-12 stsp if (show_ids) {
4380 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4381 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4382 1d13200f 2018-07-12 stsp if (err)
4383 638f9024 2019-05-13 stsp return got_error_from_errno(
4384 230a42bd 2019-05-11 jcs "got_object_id_str");
4385 1d13200f 2018-07-12 stsp }
4386 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4387 63c5ca5d 2019-08-24 stsp modestr = "$";
4388 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4389 848d6979 2019-08-12 stsp modestr = "@";
4390 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4391 848d6979 2019-08-12 stsp modestr = "/";
4392 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4393 848d6979 2019-08-12 stsp modestr = "*";
4394 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4395 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4396 1d13200f 2018-07-12 stsp free(id_str);
4397 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4398 1d13200f 2018-07-12 stsp }
4399 1d13200f 2018-07-12 stsp free(id_str);
4400 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4401 ffd1d5e5 2018-06-23 stsp if (err) {
4402 ffd1d5e5 2018-06-23 stsp free(line);
4403 ffd1d5e5 2018-06-23 stsp break;
4404 ffd1d5e5 2018-06-23 stsp }
4405 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4406 0cf4efb1 2018-09-29 stsp if (view->focussed)
4407 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4408 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4409 ffd1d5e5 2018-06-23 stsp }
4410 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4411 f26dddb7 2019-11-08 stsp if (tc)
4412 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4413 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4414 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4415 f26dddb7 2019-11-08 stsp if (tc)
4416 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4417 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4418 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4419 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4420 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4421 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4422 ffd1d5e5 2018-06-23 stsp free(line);
4423 2550e4c3 2018-07-13 stsp free(wline);
4424 2550e4c3 2018-07-13 stsp wline = NULL;
4425 ffd1d5e5 2018-06-23 stsp n++;
4426 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4427 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4428 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4429 ffd1d5e5 2018-06-23 stsp break;
4430 ffd1d5e5 2018-06-23 stsp }
4431 ffd1d5e5 2018-06-23 stsp
4432 ffd1d5e5 2018-06-23 stsp return err;
4433 ffd1d5e5 2018-06-23 stsp }
4434 ffd1d5e5 2018-06-23 stsp
4435 ffd1d5e5 2018-06-23 stsp static void
4436 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4437 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4438 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4439 ffd1d5e5 2018-06-23 stsp {
4440 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4441 ffd1d5e5 2018-06-23 stsp int i;
4442 ffd1d5e5 2018-06-23 stsp
4443 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4444 ffd1d5e5 2018-06-23 stsp return;
4445 ffd1d5e5 2018-06-23 stsp
4446 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4447 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4448 ffd1d5e5 2018-06-23 stsp if (!isroot)
4449 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4450 ffd1d5e5 2018-06-23 stsp return;
4451 ffd1d5e5 2018-06-23 stsp }
4452 ffd1d5e5 2018-06-23 stsp
4453 56e0773d 2019-11-28 stsp i = 0;
4454 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4455 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4456 56e0773d 2019-11-28 stsp *first_displayed_entry);
4457 56e0773d 2019-11-28 stsp i++;
4458 ffd1d5e5 2018-06-23 stsp }
4459 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4460 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4461 ffd1d5e5 2018-06-23 stsp }
4462 ffd1d5e5 2018-06-23 stsp
4463 768394f3 2019-01-24 stsp static int
4464 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4465 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4466 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4467 ffd1d5e5 2018-06-23 stsp {
4468 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4469 ffd1d5e5 2018-06-23 stsp int n = 0;
4470 ffd1d5e5 2018-06-23 stsp
4471 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4472 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4473 ffd1d5e5 2018-06-23 stsp else
4474 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4475 56e0773d 2019-11-28 stsp
4476 768394f3 2019-01-24 stsp last = last_displayed_entry;
4477 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4478 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4479 768394f3 2019-01-24 stsp if (last) {
4480 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4481 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4482 768394f3 2019-01-24 stsp }
4483 ffd1d5e5 2018-06-23 stsp }
4484 768394f3 2019-01-24 stsp return n;
4485 ffd1d5e5 2018-06-23 stsp }
4486 ffd1d5e5 2018-06-23 stsp
4487 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4488 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4489 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4490 ffd1d5e5 2018-06-23 stsp {
4491 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4492 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4493 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4494 ffd1d5e5 2018-06-23 stsp
4495 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4496 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4497 56e0773d 2019-11-28 stsp + 1 /* slash */;
4498 ce52c690 2018-06-23 stsp if (te)
4499 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4500 ce52c690 2018-06-23 stsp
4501 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4502 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4503 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4504 ffd1d5e5 2018-06-23 stsp
4505 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4506 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4507 d9765a41 2018-06-23 stsp while (pt) {
4508 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4509 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4510 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4511 cb2ebc8a 2018-06-23 stsp goto done;
4512 cb2ebc8a 2018-06-23 stsp }
4513 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4514 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4515 cb2ebc8a 2018-06-23 stsp goto done;
4516 cb2ebc8a 2018-06-23 stsp }
4517 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4518 ffd1d5e5 2018-06-23 stsp }
4519 ce52c690 2018-06-23 stsp if (te) {
4520 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4521 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4522 ce52c690 2018-06-23 stsp goto done;
4523 ce52c690 2018-06-23 stsp }
4524 cb2ebc8a 2018-06-23 stsp }
4525 ce52c690 2018-06-23 stsp done:
4526 ce52c690 2018-06-23 stsp if (err) {
4527 ce52c690 2018-06-23 stsp free(*path);
4528 ce52c690 2018-06-23 stsp *path = NULL;
4529 ce52c690 2018-06-23 stsp }
4530 ce52c690 2018-06-23 stsp return err;
4531 ce52c690 2018-06-23 stsp }
4532 ce52c690 2018-06-23 stsp
4533 ce52c690 2018-06-23 stsp static const struct got_error *
4534 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4535 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4536 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4537 8b473291 2019-02-21 stsp struct got_repository *repo)
4538 ce52c690 2018-06-23 stsp {
4539 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4540 ce52c690 2018-06-23 stsp char *path;
4541 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4542 a0de39f3 2019-08-09 stsp
4543 a0de39f3 2019-08-09 stsp *new_view = NULL;
4544 69efd4c4 2018-07-18 stsp
4545 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4546 ce52c690 2018-06-23 stsp if (err)
4547 ce52c690 2018-06-23 stsp return err;
4548 ffd1d5e5 2018-06-23 stsp
4549 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4550 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4551 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4552 83ce39e3 2019-08-12 stsp goto done;
4553 83ce39e3 2019-08-12 stsp }
4554 cdf1ee82 2018-08-01 stsp
4555 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4556 e5a0f69f 2018-08-18 stsp if (err) {
4557 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4558 fc06ba56 2019-08-22 stsp err = NULL;
4559 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4560 e5a0f69f 2018-08-18 stsp } else
4561 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4562 83ce39e3 2019-08-12 stsp done:
4563 83ce39e3 2019-08-12 stsp free(path);
4564 69efd4c4 2018-07-18 stsp return err;
4565 69efd4c4 2018-07-18 stsp }
4566 69efd4c4 2018-07-18 stsp
4567 69efd4c4 2018-07-18 stsp static const struct got_error *
4568 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4569 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4570 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4571 8b473291 2019-02-21 stsp struct got_repository *repo)
4572 69efd4c4 2018-07-18 stsp {
4573 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4574 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4575 69efd4c4 2018-07-18 stsp char *path;
4576 a0de39f3 2019-08-09 stsp
4577 a0de39f3 2019-08-09 stsp *new_view = NULL;
4578 69efd4c4 2018-07-18 stsp
4579 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4580 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4581 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4582 e5a0f69f 2018-08-18 stsp
4583 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4584 69efd4c4 2018-07-18 stsp if (err)
4585 69efd4c4 2018-07-18 stsp return err;
4586 69efd4c4 2018-07-18 stsp
4587 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4588 ba4f502b 2018-08-04 stsp if (err)
4589 e5a0f69f 2018-08-18 stsp view_close(log_view);
4590 e5a0f69f 2018-08-18 stsp else
4591 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4592 cb2ebc8a 2018-06-23 stsp free(path);
4593 cb2ebc8a 2018-06-23 stsp return err;
4594 ffd1d5e5 2018-06-23 stsp }
4595 ffd1d5e5 2018-06-23 stsp
4596 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4597 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4598 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4599 8b473291 2019-02-21 stsp struct got_repository *repo)
4600 ffd1d5e5 2018-06-23 stsp {
4601 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4602 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4603 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4604 ffd1d5e5 2018-06-23 stsp
4605 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4606 ffd1d5e5 2018-06-23 stsp
4607 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4608 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4609 ffd1d5e5 2018-06-23 stsp goto done;
4610 ffd1d5e5 2018-06-23 stsp
4611 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4612 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4613 ffd1d5e5 2018-06-23 stsp goto done;
4614 ffd1d5e5 2018-06-23 stsp }
4615 ffd1d5e5 2018-06-23 stsp
4616 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4617 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4618 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4619 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4620 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4621 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4622 6484ec90 2018-09-29 stsp goto done;
4623 6484ec90 2018-09-29 stsp }
4624 8b473291 2019-02-21 stsp s->refs = refs;
4625 fb2756b9 2018-08-04 stsp s->repo = repo;
4626 c0b01bdb 2019-11-08 stsp
4627 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4628 c0b01bdb 2019-11-08 stsp
4629 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4630 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4631 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4632 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4633 c0b01bdb 2019-11-08 stsp if (err)
4634 c0b01bdb 2019-11-08 stsp goto done;
4635 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4636 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4637 c0b01bdb 2019-11-08 stsp if (err) {
4638 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4639 c0b01bdb 2019-11-08 stsp goto done;
4640 c0b01bdb 2019-11-08 stsp }
4641 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4642 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4643 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4644 c0b01bdb 2019-11-08 stsp if (err) {
4645 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4646 c0b01bdb 2019-11-08 stsp goto done;
4647 c0b01bdb 2019-11-08 stsp }
4648 e5a0f69f 2018-08-18 stsp
4649 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4650 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4651 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4652 11b20872 2019-11-08 stsp if (err) {
4653 11b20872 2019-11-08 stsp free_colors(&s->colors);
4654 11b20872 2019-11-08 stsp goto done;
4655 11b20872 2019-11-08 stsp }
4656 11b20872 2019-11-08 stsp
4657 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4658 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4659 c0b01bdb 2019-11-08 stsp if (err) {
4660 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4661 c0b01bdb 2019-11-08 stsp goto done;
4662 c0b01bdb 2019-11-08 stsp }
4663 c0b01bdb 2019-11-08 stsp }
4664 c0b01bdb 2019-11-08 stsp
4665 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4666 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4667 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4668 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4669 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4670 ad80ab7b 2018-08-04 stsp done:
4671 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4672 6484ec90 2018-09-29 stsp if (err) {
4673 fb2756b9 2018-08-04 stsp free(s->tree_label);
4674 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4675 6484ec90 2018-09-29 stsp }
4676 ad80ab7b 2018-08-04 stsp return err;
4677 ad80ab7b 2018-08-04 stsp }
4678 ad80ab7b 2018-08-04 stsp
4679 e5a0f69f 2018-08-18 stsp static const struct got_error *
4680 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4681 ad80ab7b 2018-08-04 stsp {
4682 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4683 ad80ab7b 2018-08-04 stsp
4684 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4685 fb2756b9 2018-08-04 stsp free(s->tree_label);
4686 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4687 6484ec90 2018-09-29 stsp free(s->commit_id);
4688 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4689 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4690 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4691 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4692 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4693 ad80ab7b 2018-08-04 stsp free(parent);
4694 ad80ab7b 2018-08-04 stsp
4695 ad80ab7b 2018-08-04 stsp }
4696 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4697 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4698 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4699 7c32bd05 2019-06-22 stsp
4700 7c32bd05 2019-06-22 stsp return NULL;
4701 7c32bd05 2019-06-22 stsp }
4702 7c32bd05 2019-06-22 stsp
4703 7c32bd05 2019-06-22 stsp static const struct got_error *
4704 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4705 7c32bd05 2019-06-22 stsp {
4706 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4707 7c32bd05 2019-06-22 stsp
4708 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4709 7c32bd05 2019-06-22 stsp return NULL;
4710 7c32bd05 2019-06-22 stsp }
4711 7c32bd05 2019-06-22 stsp
4712 7c32bd05 2019-06-22 stsp static int
4713 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4714 7c32bd05 2019-06-22 stsp {
4715 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4716 7c32bd05 2019-06-22 stsp
4717 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4718 56e0773d 2019-11-28 stsp 0) == 0;
4719 7c32bd05 2019-06-22 stsp }
4720 7c32bd05 2019-06-22 stsp
4721 7c32bd05 2019-06-22 stsp static const struct got_error *
4722 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4723 7c32bd05 2019-06-22 stsp {
4724 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4725 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
4726 7c32bd05 2019-06-22 stsp
4727 7c32bd05 2019-06-22 stsp if (!view->searching) {
4728 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4729 7c32bd05 2019-06-22 stsp return NULL;
4730 7c32bd05 2019-06-22 stsp }
4731 7c32bd05 2019-06-22 stsp
4732 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4733 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4734 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4735 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
4736 56e0773d 2019-11-28 stsp s->selected_entry);
4737 7c32bd05 2019-06-22 stsp else
4738 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4739 56e0773d 2019-11-28 stsp } else {
4740 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
4741 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4742 56e0773d 2019-11-28 stsp else
4743 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
4744 56e0773d 2019-11-28 stsp s->selected_entry);
4745 7c32bd05 2019-06-22 stsp }
4746 7c32bd05 2019-06-22 stsp } else {
4747 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4748 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4749 56e0773d 2019-11-28 stsp else
4750 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4751 7c32bd05 2019-06-22 stsp }
4752 7c32bd05 2019-06-22 stsp
4753 7c32bd05 2019-06-22 stsp while (1) {
4754 56e0773d 2019-11-28 stsp if (te == NULL) {
4755 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4756 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4757 ac66afb8 2019-06-24 stsp return NULL;
4758 ac66afb8 2019-06-24 stsp }
4759 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4760 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4761 56e0773d 2019-11-28 stsp else
4762 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4763 7c32bd05 2019-06-22 stsp }
4764 7c32bd05 2019-06-22 stsp
4765 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
4766 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4767 56e0773d 2019-11-28 stsp s->matched_entry = te;
4768 7c32bd05 2019-06-22 stsp break;
4769 7c32bd05 2019-06-22 stsp }
4770 7c32bd05 2019-06-22 stsp
4771 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4772 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4773 56e0773d 2019-11-28 stsp else
4774 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
4775 7c32bd05 2019-06-22 stsp }
4776 e5a0f69f 2018-08-18 stsp
4777 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4778 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4779 7c32bd05 2019-06-22 stsp s->selected = 0;
4780 7c32bd05 2019-06-22 stsp }
4781 7c32bd05 2019-06-22 stsp
4782 e5a0f69f 2018-08-18 stsp return NULL;
4783 ad80ab7b 2018-08-04 stsp }
4784 ad80ab7b 2018-08-04 stsp
4785 ad80ab7b 2018-08-04 stsp static const struct got_error *
4786 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4787 ad80ab7b 2018-08-04 stsp {
4788 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4789 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4790 e5a0f69f 2018-08-18 stsp char *parent_path;
4791 ad80ab7b 2018-08-04 stsp
4792 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4793 e5a0f69f 2018-08-18 stsp if (err)
4794 e5a0f69f 2018-08-18 stsp return err;
4795 ffd1d5e5 2018-06-23 stsp
4796 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4797 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4798 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4799 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
4800 bddb1296 2019-11-08 stsp &s->colors);
4801 e5a0f69f 2018-08-18 stsp free(parent_path);
4802 669b5ffa 2018-10-07 stsp
4803 669b5ffa 2018-10-07 stsp view_vborder(view);
4804 e5a0f69f 2018-08-18 stsp return err;
4805 e5a0f69f 2018-08-18 stsp }
4806 ce52c690 2018-06-23 stsp
4807 e5a0f69f 2018-08-18 stsp static const struct got_error *
4808 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4809 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4810 e5a0f69f 2018-08-18 stsp {
4811 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4812 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4813 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4814 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4815 ffd1d5e5 2018-06-23 stsp
4816 e5a0f69f 2018-08-18 stsp switch (ch) {
4817 1e37a5c2 2019-05-12 jcs case 'i':
4818 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4819 1e37a5c2 2019-05-12 jcs break;
4820 1e37a5c2 2019-05-12 jcs case 'l':
4821 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4822 ffd1d5e5 2018-06-23 stsp break;
4823 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4824 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4825 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4826 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4827 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4828 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4829 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4830 1e37a5c2 2019-05-12 jcs if (err)
4831 1e37a5c2 2019-05-12 jcs return err;
4832 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4833 1e37a5c2 2019-05-12 jcs if (err) {
4834 1e37a5c2 2019-05-12 jcs view_close(log_view);
4835 669b5ffa 2018-10-07 stsp break;
4836 1e37a5c2 2019-05-12 jcs }
4837 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4838 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4839 1e37a5c2 2019-05-12 jcs } else
4840 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4841 1e37a5c2 2019-05-12 jcs break;
4842 1e37a5c2 2019-05-12 jcs case 'k':
4843 1e37a5c2 2019-05-12 jcs case KEY_UP:
4844 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4845 1e37a5c2 2019-05-12 jcs s->selected--;
4846 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4847 1e37a5c2 2019-05-12 jcs break;
4848 1e37a5c2 2019-05-12 jcs }
4849 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4850 1e37a5c2 2019-05-12 jcs break;
4851 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4852 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
4853 1e37a5c2 2019-05-12 jcs break;
4854 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4855 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4856 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
4857 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4858 1e37a5c2 2019-05-12 jcs s->selected = 0;
4859 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
4860 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4861 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4862 1e37a5c2 2019-05-12 jcs break;
4863 1e37a5c2 2019-05-12 jcs case 'j':
4864 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4865 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4866 1e37a5c2 2019-05-12 jcs s->selected++;
4867 1e37a5c2 2019-05-12 jcs break;
4868 1e37a5c2 2019-05-12 jcs }
4869 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4870 56e0773d 2019-11-28 stsp == NULL)
4871 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4872 1e37a5c2 2019-05-12 jcs break;
4873 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4874 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
4875 1e37a5c2 2019-05-12 jcs break;
4876 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4877 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4878 1e37a5c2 2019-05-12 jcs == NULL) {
4879 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4880 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4881 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4882 1e37a5c2 2019-05-12 jcs break;
4883 1e37a5c2 2019-05-12 jcs }
4884 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4885 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
4886 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4887 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4888 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4889 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4890 1e37a5c2 2019-05-12 jcs do {
4891 1e37a5c2 2019-05-12 jcs ndisplayed++;
4892 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4893 1e37a5c2 2019-05-12 jcs } while (te);
4894 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4895 1e37a5c2 2019-05-12 jcs }
4896 1e37a5c2 2019-05-12 jcs break;
4897 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4898 1e37a5c2 2019-05-12 jcs case '\r':
4899 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4900 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4901 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4902 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4903 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4904 1e37a5c2 2019-05-12 jcs break;
4905 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4906 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4907 1e37a5c2 2019-05-12 jcs entry);
4908 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4909 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4910 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4911 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4912 1e37a5c2 2019-05-12 jcs s->selected_entry =
4913 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4914 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4915 1e37a5c2 2019-05-12 jcs free(parent);
4916 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
4917 56e0773d 2019-11-28 stsp s->selected_entry))) {
4918 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4919 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
4920 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
4921 1e37a5c2 2019-05-12 jcs if (err)
4922 1e37a5c2 2019-05-12 jcs break;
4923 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4924 941e9f74 2019-05-21 stsp if (err) {
4925 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4926 1e37a5c2 2019-05-12 jcs break;
4927 1e37a5c2 2019-05-12 jcs }
4928 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
4929 56e0773d 2019-11-28 stsp s->selected_entry))) {
4930 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4931 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4932 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4933 1e37a5c2 2019-05-12 jcs
4934 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4935 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4936 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4937 1e37a5c2 2019-05-12 jcs if (err)
4938 1e37a5c2 2019-05-12 jcs break;
4939 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4940 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4941 669b5ffa 2018-10-07 stsp if (err)
4942 669b5ffa 2018-10-07 stsp return err;
4943 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4944 669b5ffa 2018-10-07 stsp if (err) {
4945 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4946 669b5ffa 2018-10-07 stsp break;
4947 669b5ffa 2018-10-07 stsp }
4948 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4949 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4950 669b5ffa 2018-10-07 stsp } else
4951 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4952 1e37a5c2 2019-05-12 jcs }
4953 1e37a5c2 2019-05-12 jcs break;
4954 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4955 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4956 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4957 1e37a5c2 2019-05-12 jcs break;
4958 1e37a5c2 2019-05-12 jcs default:
4959 1e37a5c2 2019-05-12 jcs break;
4960 ffd1d5e5 2018-06-23 stsp }
4961 e5a0f69f 2018-08-18 stsp
4962 ffd1d5e5 2018-06-23 stsp return err;
4963 9f7d7167 2018-04-29 stsp }
4964 9f7d7167 2018-04-29 stsp
4965 ffd1d5e5 2018-06-23 stsp __dead static void
4966 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4967 ffd1d5e5 2018-06-23 stsp {
4968 ffd1d5e5 2018-06-23 stsp endwin();
4969 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4970 ffd1d5e5 2018-06-23 stsp getprogname());
4971 ffd1d5e5 2018-06-23 stsp exit(1);
4972 ffd1d5e5 2018-06-23 stsp }
4973 ffd1d5e5 2018-06-23 stsp
4974 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4975 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4976 ffd1d5e5 2018-06-23 stsp {
4977 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4978 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4979 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4980 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4981 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4982 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4983 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4984 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4985 ffd1d5e5 2018-06-23 stsp int ch;
4986 5221c383 2018-08-01 stsp struct tog_view *view;
4987 70ac5f84 2019-03-28 stsp
4988 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4989 ffd1d5e5 2018-06-23 stsp
4990 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4991 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4992 d188b9a6 2019-01-04 stsp NULL) == -1)
4993 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4994 ffd1d5e5 2018-06-23 stsp #endif
4995 ffd1d5e5 2018-06-23 stsp
4996 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4997 ffd1d5e5 2018-06-23 stsp switch (ch) {
4998 ffd1d5e5 2018-06-23 stsp case 'c':
4999 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5000 ffd1d5e5 2018-06-23 stsp break;
5001 ffd1d5e5 2018-06-23 stsp default:
5002 17020d27 2019-03-07 stsp usage_tree();
5003 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5004 ffd1d5e5 2018-06-23 stsp }
5005 ffd1d5e5 2018-06-23 stsp }
5006 ffd1d5e5 2018-06-23 stsp
5007 ffd1d5e5 2018-06-23 stsp argc -= optind;
5008 ffd1d5e5 2018-06-23 stsp argv += optind;
5009 ffd1d5e5 2018-06-23 stsp
5010 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
5011 52185f70 2019-02-05 stsp struct got_worktree *worktree;
5012 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
5013 52185f70 2019-02-05 stsp if (cwd == NULL)
5014 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
5015 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5016 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5017 52185f70 2019-02-05 stsp goto done;
5018 52185f70 2019-02-05 stsp if (worktree) {
5019 52185f70 2019-02-05 stsp free(cwd);
5020 52185f70 2019-02-05 stsp repo_path =
5021 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5022 52185f70 2019-02-05 stsp got_worktree_close(worktree);
5023 52185f70 2019-02-05 stsp } else
5024 52185f70 2019-02-05 stsp repo_path = cwd;
5025 52185f70 2019-02-05 stsp if (repo_path == NULL) {
5026 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5027 52185f70 2019-02-05 stsp goto done;
5028 52185f70 2019-02-05 stsp }
5029 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
5030 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
5031 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
5032 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
5033 ffd1d5e5 2018-06-23 stsp } else
5034 f13374f4 2020-01-14 stsp usage_tree();
5035 a915003a 2019-02-05 stsp
5036 a915003a 2019-02-05 stsp init_curses();
5037 ffd1d5e5 2018-06-23 stsp
5038 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5039 c02c541e 2019-03-29 stsp if (error != NULL)
5040 52185f70 2019-02-05 stsp goto done;
5041 d188b9a6 2019-01-04 stsp
5042 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5043 c02c541e 2019-03-29 stsp if (error)
5044 52185f70 2019-02-05 stsp goto done;
5045 ffd1d5e5 2018-06-23 stsp
5046 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
5047 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5048 f2b6a97d 2019-07-15 stsp else {
5049 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5050 f2b6a97d 2019-07-15 stsp if (error) {
5051 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
5052 f2b6a97d 2019-07-15 stsp goto done;
5053 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
5054 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5055 f2b6a97d 2019-07-15 stsp }
5056 f2b6a97d 2019-07-15 stsp }
5057 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5058 ffd1d5e5 2018-06-23 stsp goto done;
5059 ffd1d5e5 2018-06-23 stsp
5060 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5061 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5062 ffd1d5e5 2018-06-23 stsp goto done;
5063 ffd1d5e5 2018-06-23 stsp
5064 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5065 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5066 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5067 8b473291 2019-02-21 stsp goto done;
5068 8b473291 2019-02-21 stsp
5069 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5070 8b473291 2019-02-21 stsp if (error)
5071 ffd1d5e5 2018-06-23 stsp goto done;
5072 ffd1d5e5 2018-06-23 stsp
5073 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5074 5221c383 2018-08-01 stsp if (view == NULL) {
5075 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5076 5221c383 2018-08-01 stsp goto done;
5077 5221c383 2018-08-01 stsp }
5078 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5079 ad80ab7b 2018-08-04 stsp if (error)
5080 ad80ab7b 2018-08-04 stsp goto done;
5081 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5082 ffd1d5e5 2018-06-23 stsp done:
5083 52185f70 2019-02-05 stsp free(repo_path);
5084 ffd1d5e5 2018-06-23 stsp free(commit_id);
5085 ffd1d5e5 2018-06-23 stsp if (commit)
5086 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5087 ffd1d5e5 2018-06-23 stsp if (tree)
5088 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5089 ffd1d5e5 2018-06-23 stsp if (repo)
5090 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5091 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5092 ffd1d5e5 2018-06-23 stsp return error;
5093 9f7d7167 2018-04-29 stsp }
5094 ce5b7c56 2019-07-09 stsp
5095 ce5b7c56 2019-07-09 stsp static void
5096 ce5b7c56 2019-07-09 stsp list_commands(void)
5097 ce5b7c56 2019-07-09 stsp {
5098 ce5b7c56 2019-07-09 stsp int i;
5099 9f7d7167 2018-04-29 stsp
5100 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5101 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5102 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5103 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5104 ce5b7c56 2019-07-09 stsp }
5105 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5106 ce5b7c56 2019-07-09 stsp }
5107 ce5b7c56 2019-07-09 stsp
5108 4ed7e80c 2018-05-20 stsp __dead static void
5109 ce5b7c56 2019-07-09 stsp usage(int hflag)
5110 9f7d7167 2018-04-29 stsp {
5111 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5112 53ccebc2 2019-07-30 stsp getprogname());
5113 ce5b7c56 2019-07-09 stsp if (hflag)
5114 ce5b7c56 2019-07-09 stsp list_commands();
5115 9f7d7167 2018-04-29 stsp exit(1);
5116 9f7d7167 2018-04-29 stsp }
5117 9f7d7167 2018-04-29 stsp
5118 c2301be8 2018-04-30 stsp static char **
5119 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
5120 c2301be8 2018-04-30 stsp {
5121 c2301be8 2018-04-30 stsp char **argv;
5122 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
5123 c2301be8 2018-04-30 stsp
5124 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5125 c2301be8 2018-04-30 stsp if (argv == NULL)
5126 c2301be8 2018-04-30 stsp err(1, "calloc");
5127 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
5128 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
5129 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5130 c2301be8 2018-04-30 stsp if (arg1) {
5131 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
5132 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
5133 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5134 c2301be8 2018-04-30 stsp }
5135 c2301be8 2018-04-30 stsp
5136 c2301be8 2018-04-30 stsp return argv;
5137 c2301be8 2018-04-30 stsp }
5138 c2301be8 2018-04-30 stsp
5139 9f7d7167 2018-04-29 stsp int
5140 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5141 9f7d7167 2018-04-29 stsp {
5142 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5143 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5144 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5145 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5146 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5147 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
5148 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
5149 83cd27f8 2020-01-13 stsp };
5150 9f7d7167 2018-04-29 stsp
5151 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5152 9f7d7167 2018-04-29 stsp
5153 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5154 9f7d7167 2018-04-29 stsp switch (ch) {
5155 9f7d7167 2018-04-29 stsp case 'h':
5156 9f7d7167 2018-04-29 stsp hflag = 1;
5157 9f7d7167 2018-04-29 stsp break;
5158 53ccebc2 2019-07-30 stsp case 'V':
5159 53ccebc2 2019-07-30 stsp Vflag = 1;
5160 53ccebc2 2019-07-30 stsp break;
5161 9f7d7167 2018-04-29 stsp default:
5162 ce5b7c56 2019-07-09 stsp usage(hflag);
5163 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5164 9f7d7167 2018-04-29 stsp }
5165 9f7d7167 2018-04-29 stsp }
5166 9f7d7167 2018-04-29 stsp
5167 9f7d7167 2018-04-29 stsp argc -= optind;
5168 9f7d7167 2018-04-29 stsp argv += optind;
5169 9f7d7167 2018-04-29 stsp optind = 0;
5170 c2301be8 2018-04-30 stsp optreset = 1;
5171 9f7d7167 2018-04-29 stsp
5172 53ccebc2 2019-07-30 stsp if (Vflag) {
5173 53ccebc2 2019-07-30 stsp got_version_print_str();
5174 53ccebc2 2019-07-30 stsp return 1;
5175 53ccebc2 2019-07-30 stsp }
5176 53ccebc2 2019-07-30 stsp
5177 c2301be8 2018-04-30 stsp if (argc == 0) {
5178 f29d3e89 2018-06-23 stsp if (hflag)
5179 ce5b7c56 2019-07-09 stsp usage(hflag);
5180 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5181 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5182 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
5183 c2301be8 2018-04-30 stsp argc = 1;
5184 c2301be8 2018-04-30 stsp } else {
5185 9f7d7167 2018-04-29 stsp int i;
5186 9f7d7167 2018-04-29 stsp
5187 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
5188 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5189 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5190 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5191 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5192 9f7d7167 2018-04-29 stsp break;
5193 9f7d7167 2018-04-29 stsp }
5194 9f7d7167 2018-04-29 stsp }
5195 3642c4c6 2019-07-09 stsp
5196 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
5197 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
5198 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
5199 ce5b7c56 2019-07-09 stsp list_commands();
5200 3642c4c6 2019-07-09 stsp return 1;
5201 9f7d7167 2018-04-29 stsp }
5202 9f7d7167 2018-04-29 stsp }
5203 9f7d7167 2018-04-29 stsp
5204 3642c4c6 2019-07-09 stsp if (hflag)
5205 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
5206 3642c4c6 2019-07-09 stsp else
5207 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5208 3642c4c6 2019-07-09 stsp
5209 9f7d7167 2018-04-29 stsp endwin();
5210 c2301be8 2018-04-30 stsp free(cmd_argv);
5211 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5212 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5213 9f7d7167 2018-04-29 stsp return 0;
5214 9f7d7167 2018-04-29 stsp }