Blame


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