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