Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <util.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 9f7d7167 2018-04-29 stsp
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 9f7d7167 2018-04-29 stsp
59 881b2d3e 2018-04-30 stsp #ifndef MIN
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 881b2d3e 2018-04-30 stsp #endif
62 881b2d3e 2018-04-30 stsp
63 2bd27830 2018-10-22 stsp #ifndef MAX
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #endif
66 2bd27830 2018-10-22 stsp
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 2bd27830 2018-10-22 stsp
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp #endif
72 9f7d7167 2018-04-29 stsp
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 9f7d7167 2018-04-29 stsp
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 9f7d7167 2018-04-29 stsp
92 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
98 9f7d7167 2018-04-29 stsp };
99 9f7d7167 2018-04-29 stsp
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
106 d6b05b5b 2018-08-04 stsp };
107 c3e9aa98 2019-05-13 jcs
108 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
109 d6b05b5b 2018-08-04 stsp
110 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
111 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
112 ba4f502b 2018-08-04 stsp struct got_object_id *id;
113 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
114 1a76625f 2018-10-22 stsp int idx;
115 ba4f502b 2018-08-04 stsp };
116 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 ba4f502b 2018-08-04 stsp struct commit_queue {
118 ba4f502b 2018-08-04 stsp int ncommits;
119 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
120 6d17833f 2019-11-08 stsp };
121 6d17833f 2019-11-08 stsp
122 f26dddb7 2019-11-08 stsp struct tog_color {
123 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
124 6d17833f 2019-11-08 stsp regex_t regex;
125 6d17833f 2019-11-08 stsp short colorpair;
126 15a087fe 2019-02-21 stsp };
127 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
128 11b20872 2019-11-08 stsp
129 11b20872 2019-11-08 stsp static const struct got_error *
130 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
131 11b20872 2019-11-08 stsp int idx, short color)
132 11b20872 2019-11-08 stsp {
133 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
134 11b20872 2019-11-08 stsp struct tog_color *tc;
135 11b20872 2019-11-08 stsp int regerr = 0;
136 11b20872 2019-11-08 stsp
137 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
138 11b20872 2019-11-08 stsp return NULL;
139 11b20872 2019-11-08 stsp
140 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
141 11b20872 2019-11-08 stsp
142 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
143 11b20872 2019-11-08 stsp if (tc == NULL)
144 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
145 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
146 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
147 11b20872 2019-11-08 stsp if (regerr) {
148 11b20872 2019-11-08 stsp static char regerr_msg[512];
149 11b20872 2019-11-08 stsp static char err_msg[512];
150 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
151 11b20872 2019-11-08 stsp sizeof(regerr_msg));
152 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
153 11b20872 2019-11-08 stsp regerr_msg);
154 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
155 11b20872 2019-11-08 stsp free(tc);
156 11b20872 2019-11-08 stsp return err;
157 11b20872 2019-11-08 stsp }
158 11b20872 2019-11-08 stsp tc->colorpair = idx;
159 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 11b20872 2019-11-08 stsp return NULL;
161 11b20872 2019-11-08 stsp }
162 11b20872 2019-11-08 stsp
163 11b20872 2019-11-08 stsp static void
164 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
165 11b20872 2019-11-08 stsp {
166 11b20872 2019-11-08 stsp struct tog_color *tc;
167 11b20872 2019-11-08 stsp
168 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
169 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
170 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
171 11b20872 2019-11-08 stsp regfree(&tc->regex);
172 11b20872 2019-11-08 stsp free(tc);
173 11b20872 2019-11-08 stsp }
174 11b20872 2019-11-08 stsp }
175 11b20872 2019-11-08 stsp
176 11b20872 2019-11-08 stsp struct tog_color *
177 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
178 11b20872 2019-11-08 stsp {
179 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
180 11b20872 2019-11-08 stsp
181 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
182 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
183 11b20872 2019-11-08 stsp return tc;
184 11b20872 2019-11-08 stsp }
185 11b20872 2019-11-08 stsp
186 11b20872 2019-11-08 stsp return NULL;
187 11b20872 2019-11-08 stsp }
188 11b20872 2019-11-08 stsp
189 11b20872 2019-11-08 stsp static int
190 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
191 11b20872 2019-11-08 stsp {
192 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
194 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
195 11b20872 2019-11-08 stsp return COLOR_CYAN;
196 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
197 11b20872 2019-11-08 stsp return COLOR_YELLOW;
198 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
199 11b20872 2019-11-08 stsp return COLOR_GREEN;
200 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
202 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
204 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
205 91b8c405 2020-01-25 stsp return COLOR_CYAN;
206 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
207 11b20872 2019-11-08 stsp return COLOR_GREEN;
208 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
209 11b20872 2019-11-08 stsp return COLOR_GREEN;
210 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
211 11b20872 2019-11-08 stsp return COLOR_CYAN;
212 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 11b20872 2019-11-08 stsp return COLOR_YELLOW;
214 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
215 6458efa5 2020-11-24 stsp return COLOR_GREEN;
216 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
218 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
219 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
220 11b20872 2019-11-08 stsp
221 11b20872 2019-11-08 stsp return -1;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp
224 11b20872 2019-11-08 stsp static int
225 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
226 11b20872 2019-11-08 stsp {
227 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
228 11b20872 2019-11-08 stsp
229 11b20872 2019-11-08 stsp if (val == NULL)
230 11b20872 2019-11-08 stsp return default_color_value(envvar);
231 15a087fe 2019-02-21 stsp
232 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
233 11b20872 2019-11-08 stsp return COLOR_BLACK;
234 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
235 11b20872 2019-11-08 stsp return COLOR_RED;
236 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
237 11b20872 2019-11-08 stsp return COLOR_GREEN;
238 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
239 11b20872 2019-11-08 stsp return COLOR_YELLOW;
240 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
241 11b20872 2019-11-08 stsp return COLOR_BLUE;
242 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
243 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
244 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
245 11b20872 2019-11-08 stsp return COLOR_CYAN;
246 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
247 11b20872 2019-11-08 stsp return COLOR_WHITE;
248 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
249 11b20872 2019-11-08 stsp return -1;
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return default_color_value(envvar);
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp
255 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
256 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
257 3dbaef42 2020-11-24 stsp const char *label1, *label2;
258 15a087fe 2019-02-21 stsp FILE *f;
259 15a087fe 2019-02-21 stsp int first_displayed_line;
260 15a087fe 2019-02-21 stsp int last_displayed_line;
261 15a087fe 2019-02-21 stsp int eof;
262 15a087fe 2019-02-21 stsp int diff_context;
263 3dbaef42 2020-11-24 stsp int ignore_whitespace;
264 64453f7e 2020-11-21 stsp int force_text_diff;
265 15a087fe 2019-02-21 stsp struct got_repository *repo;
266 78756c87 2020-11-24 stsp struct got_reflist_head refs;
267 bddb1296 2019-11-08 stsp struct tog_colors colors;
268 fe621944 2020-11-10 stsp size_t nlines;
269 f44b1f58 2020-02-02 tracey off_t *line_offsets;
270 f44b1f58 2020-02-02 tracey int matched_line;
271 f44b1f58 2020-02-02 tracey int selected_line;
272 15a087fe 2019-02-21 stsp
273 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
274 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
275 b01e7d3b 2018-08-04 stsp };
276 b01e7d3b 2018-08-04 stsp
277 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
278 1a76625f 2018-10-22 stsp
279 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
280 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
281 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
282 1a76625f 2018-10-22 stsp int commits_needed;
283 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
284 1a76625f 2018-10-22 stsp struct commit_queue *commits;
285 1a76625f 2018-10-22 stsp const char *in_repo_path;
286 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
287 1a76625f 2018-10-22 stsp struct got_repository *repo;
288 1a76625f 2018-10-22 stsp int log_complete;
289 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
290 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
291 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
292 13add988 2019-10-15 stsp int *searching;
293 13add988 2019-10-15 stsp int *search_next_done;
294 13add988 2019-10-15 stsp regex_t *regex;
295 1a76625f 2018-10-22 stsp };
296 1a76625f 2018-10-22 stsp
297 1a76625f 2018-10-22 stsp struct tog_log_view_state {
298 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
299 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
300 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
301 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
302 b01e7d3b 2018-08-04 stsp int selected;
303 b01e7d3b 2018-08-04 stsp char *in_repo_path;
304 d01904d4 2019-06-25 stsp const char *head_ref_name;
305 b672a97a 2020-01-27 stsp int log_branches;
306 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
307 78756c87 2020-11-24 stsp struct got_reflist_head refs;
308 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
309 1a76625f 2018-10-22 stsp sig_atomic_t quit;
310 1a76625f 2018-10-22 stsp pthread_t thread;
311 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
312 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
313 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
314 11b20872 2019-11-08 stsp struct tog_colors colors;
315 ba4f502b 2018-08-04 stsp };
316 11b20872 2019-11-08 stsp
317 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
318 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
319 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
321 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
322 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
323 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
324 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
325 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
326 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
327 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
328 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
329 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
330 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
331 ba4f502b 2018-08-04 stsp
332 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
333 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
334 e9424729 2018-08-04 stsp int nlines;
335 e9424729 2018-08-04 stsp
336 e9424729 2018-08-04 stsp struct tog_view *view;
337 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
338 e9424729 2018-08-04 stsp int *quit;
339 e9424729 2018-08-04 stsp };
340 e9424729 2018-08-04 stsp
341 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
342 e9424729 2018-08-04 stsp const char *path;
343 e9424729 2018-08-04 stsp struct got_repository *repo;
344 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
345 e9424729 2018-08-04 stsp int *complete;
346 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
347 fc06ba56 2019-08-22 stsp void *cancel_arg;
348 e9424729 2018-08-04 stsp };
349 e9424729 2018-08-04 stsp
350 e9424729 2018-08-04 stsp struct tog_blame {
351 e9424729 2018-08-04 stsp FILE *f;
352 be659d10 2020-11-18 stsp off_t filesize;
353 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
354 6fcac457 2018-11-19 stsp int nlines;
355 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
356 e9424729 2018-08-04 stsp pthread_t thread;
357 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
358 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
359 e9424729 2018-08-04 stsp const char *path;
360 e9424729 2018-08-04 stsp };
361 e9424729 2018-08-04 stsp
362 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
363 7cbe629d 2018-08-04 stsp int first_displayed_line;
364 7cbe629d 2018-08-04 stsp int last_displayed_line;
365 7cbe629d 2018-08-04 stsp int selected_line;
366 7cbe629d 2018-08-04 stsp int blame_complete;
367 e5a0f69f 2018-08-18 stsp int eof;
368 e5a0f69f 2018-08-18 stsp int done;
369 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
370 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
371 e5a0f69f 2018-08-18 stsp char *path;
372 7cbe629d 2018-08-04 stsp struct got_repository *repo;
373 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
374 e9424729 2018-08-04 stsp struct tog_blame blame;
375 6c4c42e0 2019-06-24 stsp int matched_line;
376 11b20872 2019-11-08 stsp struct tog_colors colors;
377 ad80ab7b 2018-08-04 stsp };
378 ad80ab7b 2018-08-04 stsp
379 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
380 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
381 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
382 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
383 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
384 ad80ab7b 2018-08-04 stsp int selected;
385 ad80ab7b 2018-08-04 stsp };
386 ad80ab7b 2018-08-04 stsp
387 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
388 ad80ab7b 2018-08-04 stsp
389 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
390 ad80ab7b 2018-08-04 stsp char *tree_label;
391 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
392 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
393 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
394 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
395 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
396 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
397 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
398 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
399 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
400 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
401 6458efa5 2020-11-24 stsp struct tog_colors colors;
402 6458efa5 2020-11-24 stsp };
403 6458efa5 2020-11-24 stsp
404 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
405 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
406 6458efa5 2020-11-24 stsp struct got_reference *ref;
407 6458efa5 2020-11-24 stsp int idx;
408 6458efa5 2020-11-24 stsp };
409 6458efa5 2020-11-24 stsp
410 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
411 6458efa5 2020-11-24 stsp
412 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
413 6458efa5 2020-11-24 stsp struct got_reflist_head simplerefs; /* SIMPLEQ */
414 6458efa5 2020-11-24 stsp struct tog_reflist_head refs; /* TAILQ */
415 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
416 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
417 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
418 6458efa5 2020-11-24 stsp int nrefs, ndisplayed, selected, show_ids;
419 6458efa5 2020-11-24 stsp struct got_repository *repo;
420 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
421 bddb1296 2019-11-08 stsp struct tog_colors colors;
422 7cbe629d 2018-08-04 stsp };
423 7cbe629d 2018-08-04 stsp
424 669b5ffa 2018-10-07 stsp /*
425 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
426 669b5ffa 2018-10-07 stsp *
427 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
428 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
429 669b5ffa 2018-10-07 stsp * there is enough screen estate.
430 669b5ffa 2018-10-07 stsp *
431 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
432 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
433 669b5ffa 2018-10-07 stsp *
434 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
435 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
436 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
437 669b5ffa 2018-10-07 stsp *
438 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
439 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
440 669b5ffa 2018-10-07 stsp */
441 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
442 669b5ffa 2018-10-07 stsp
443 cc3c9aac 2018-08-01 stsp struct tog_view {
444 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
445 26ed57b2 2018-05-19 stsp WINDOW *window;
446 26ed57b2 2018-05-19 stsp PANEL *panel;
447 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
448 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
449 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
450 9970f7fc 2020-12-03 stsp int dying;
451 669b5ffa 2018-10-07 stsp struct tog_view *parent;
452 669b5ffa 2018-10-07 stsp struct tog_view *child;
453 5dc9f4bc 2018-08-04 stsp
454 e78dc838 2020-12-04 stsp /*
455 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
456 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
457 e78dc838 2020-12-04 stsp * between parent and child.
458 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
459 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
460 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
461 e78dc838 2020-12-04 stsp * situations.
462 e78dc838 2020-12-04 stsp */
463 e78dc838 2020-12-04 stsp int focus_child;
464 e78dc838 2020-12-04 stsp
465 5dc9f4bc 2018-08-04 stsp /* type-specific state */
466 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
467 5dc9f4bc 2018-08-04 stsp union {
468 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
469 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
470 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
471 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
472 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
473 5dc9f4bc 2018-08-04 stsp } state;
474 e5a0f69f 2018-08-18 stsp
475 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
476 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
477 e78dc838 2020-12-04 stsp struct tog_view *, int);
478 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
479 60493ae3 2019-06-20 stsp
480 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
481 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
482 60493ae3 2019-06-20 stsp int searching;
483 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
484 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
485 60493ae3 2019-06-20 stsp int search_next_done;
486 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
487 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
488 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
489 1803e47f 2019-06-22 stsp regex_t regex;
490 41605754 2020-11-12 stsp regmatch_t regmatch;
491 cc3c9aac 2018-08-01 stsp };
492 cd0acaa7 2018-05-20 stsp
493 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
494 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
495 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
496 78756c87 2020-11-24 stsp struct got_repository *);
497 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
498 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
499 e78dc838 2020-12-04 stsp struct tog_view *, int);
500 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
501 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
502 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
503 e5a0f69f 2018-08-18 stsp
504 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
505 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
506 78756c87 2020-11-24 stsp const char *, const char *, int);
507 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
508 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
509 e78dc838 2020-12-04 stsp struct tog_view *, int);
510 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
511 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
512 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
513 e5a0f69f 2018-08-18 stsp
514 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
515 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
516 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
517 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
518 e78dc838 2020-12-04 stsp struct tog_view *, int);
519 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
520 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
521 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
522 e5a0f69f 2018-08-18 stsp
523 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
524 78756c87 2020-11-24 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
525 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
526 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
527 e78dc838 2020-12-04 stsp struct tog_view *, int);
528 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
529 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
530 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
531 6458efa5 2020-11-24 stsp
532 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
533 6458efa5 2020-11-24 stsp struct got_repository *);
534 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
535 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
536 e78dc838 2020-12-04 stsp struct tog_view *, int);
537 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
538 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
539 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
540 25791caa 2018-10-24 stsp
541 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
542 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
543 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
544 25791caa 2018-10-24 stsp
545 25791caa 2018-10-24 stsp static void
546 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
547 25791caa 2018-10-24 stsp {
548 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
549 83baff54 2019-08-12 stsp }
550 83baff54 2019-08-12 stsp
551 83baff54 2019-08-12 stsp static void
552 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
553 83baff54 2019-08-12 stsp {
554 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
555 25791caa 2018-10-24 stsp }
556 26ed57b2 2018-05-19 stsp
557 61266923 2020-01-14 stsp static void
558 61266923 2020-01-14 stsp tog_sigcont(int signo)
559 61266923 2020-01-14 stsp {
560 61266923 2020-01-14 stsp tog_sigcont_received = 1;
561 61266923 2020-01-14 stsp }
562 61266923 2020-01-14 stsp
563 e5a0f69f 2018-08-18 stsp static const struct got_error *
564 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
565 ea5e7bb5 2018-08-01 stsp {
566 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
567 e5a0f69f 2018-08-18 stsp
568 669b5ffa 2018-10-07 stsp if (view->child) {
569 669b5ffa 2018-10-07 stsp view_close(view->child);
570 669b5ffa 2018-10-07 stsp view->child = NULL;
571 669b5ffa 2018-10-07 stsp }
572 e5a0f69f 2018-08-18 stsp if (view->close)
573 e5a0f69f 2018-08-18 stsp err = view->close(view);
574 ea5e7bb5 2018-08-01 stsp if (view->panel)
575 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
576 ea5e7bb5 2018-08-01 stsp if (view->window)
577 ea5e7bb5 2018-08-01 stsp delwin(view->window);
578 ea5e7bb5 2018-08-01 stsp free(view);
579 e5a0f69f 2018-08-18 stsp return err;
580 ea5e7bb5 2018-08-01 stsp }
581 ea5e7bb5 2018-08-01 stsp
582 ea5e7bb5 2018-08-01 stsp static struct tog_view *
583 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
584 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
585 ea5e7bb5 2018-08-01 stsp {
586 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
587 ea5e7bb5 2018-08-01 stsp
588 ea5e7bb5 2018-08-01 stsp if (view == NULL)
589 ea5e7bb5 2018-08-01 stsp return NULL;
590 ea5e7bb5 2018-08-01 stsp
591 d6b05b5b 2018-08-04 stsp view->type = type;
592 f7d12f7e 2018-08-01 stsp view->lines = LINES;
593 f7d12f7e 2018-08-01 stsp view->cols = COLS;
594 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
595 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
596 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
597 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
598 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
599 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
600 96a765a8 2018-08-04 stsp view_close(view);
601 ea5e7bb5 2018-08-01 stsp return NULL;
602 ea5e7bb5 2018-08-01 stsp }
603 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
604 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
605 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
606 96a765a8 2018-08-04 stsp view_close(view);
607 ea5e7bb5 2018-08-01 stsp return NULL;
608 ea5e7bb5 2018-08-01 stsp }
609 ea5e7bb5 2018-08-01 stsp
610 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
611 ea5e7bb5 2018-08-01 stsp return view;
612 cdf1ee82 2018-08-01 stsp }
613 cdf1ee82 2018-08-01 stsp
614 0cf4efb1 2018-09-29 stsp static int
615 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
616 0cf4efb1 2018-09-29 stsp {
617 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
618 0cf4efb1 2018-09-29 stsp return 0;
619 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
620 5c60c32a 2018-10-18 stsp }
621 5c60c32a 2018-10-18 stsp
622 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
623 5c60c32a 2018-10-18 stsp
624 5c60c32a 2018-10-18 stsp static const struct got_error *
625 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
626 5c60c32a 2018-10-18 stsp {
627 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
628 5c60c32a 2018-10-18 stsp
629 5c60c32a 2018-10-18 stsp view->begin_y = 0;
630 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
631 5c60c32a 2018-10-18 stsp view->nlines = LINES;
632 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
633 5c60c32a 2018-10-18 stsp view->lines = LINES;
634 5c60c32a 2018-10-18 stsp view->cols = COLS;
635 5c60c32a 2018-10-18 stsp err = view_resize(view);
636 5c60c32a 2018-10-18 stsp if (err)
637 5c60c32a 2018-10-18 stsp return err;
638 5c60c32a 2018-10-18 stsp
639 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
640 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
641 5c60c32a 2018-10-18 stsp
642 5c60c32a 2018-10-18 stsp return NULL;
643 5c60c32a 2018-10-18 stsp }
644 5c60c32a 2018-10-18 stsp
645 5c60c32a 2018-10-18 stsp static const struct got_error *
646 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
647 5c60c32a 2018-10-18 stsp {
648 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
649 5c60c32a 2018-10-18 stsp
650 5c60c32a 2018-10-18 stsp view->begin_x = 0;
651 5c60c32a 2018-10-18 stsp view->begin_y = 0;
652 5c60c32a 2018-10-18 stsp view->nlines = LINES;
653 5c60c32a 2018-10-18 stsp view->ncols = COLS;
654 5c60c32a 2018-10-18 stsp view->lines = LINES;
655 5c60c32a 2018-10-18 stsp view->cols = COLS;
656 5c60c32a 2018-10-18 stsp err = view_resize(view);
657 5c60c32a 2018-10-18 stsp if (err)
658 5c60c32a 2018-10-18 stsp return err;
659 5c60c32a 2018-10-18 stsp
660 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
661 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
662 5c60c32a 2018-10-18 stsp
663 5c60c32a 2018-10-18 stsp return NULL;
664 0cf4efb1 2018-09-29 stsp }
665 0cf4efb1 2018-09-29 stsp
666 5c60c32a 2018-10-18 stsp static int
667 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
668 5c60c32a 2018-10-18 stsp {
669 5c60c32a 2018-10-18 stsp return view->parent == NULL;
670 5c60c32a 2018-10-18 stsp }
671 5c60c32a 2018-10-18 stsp
672 4d8c2215 2018-08-19 stsp static const struct got_error *
673 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
674 f7d12f7e 2018-08-01 stsp {
675 f7d12f7e 2018-08-01 stsp int nlines, ncols;
676 f7d12f7e 2018-08-01 stsp
677 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
678 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
679 0cf4efb1 2018-09-29 stsp else
680 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
681 f7d12f7e 2018-08-01 stsp
682 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
683 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
684 0cf4efb1 2018-09-29 stsp else
685 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
686 f7d12f7e 2018-08-01 stsp
687 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
688 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
689 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
690 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
691 25791caa 2018-10-24 stsp wclear(view->window);
692 f7d12f7e 2018-08-01 stsp
693 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
694 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
695 0cf4efb1 2018-09-29 stsp view->lines = LINES;
696 0cf4efb1 2018-09-29 stsp view->cols = COLS;
697 6d0fee91 2018-08-01 stsp
698 6e3e5d9c 2018-10-18 stsp if (view->child) {
699 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
700 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
701 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
702 5c60c32a 2018-10-18 stsp if (view->child->focussed)
703 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
704 5c60c32a 2018-10-18 stsp else
705 5c60c32a 2018-10-18 stsp show_panel(view->panel);
706 5c60c32a 2018-10-18 stsp } else {
707 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
708 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
709 5c60c32a 2018-10-18 stsp }
710 5c60c32a 2018-10-18 stsp }
711 669b5ffa 2018-10-07 stsp
712 5c60c32a 2018-10-18 stsp return NULL;
713 669b5ffa 2018-10-07 stsp }
714 669b5ffa 2018-10-07 stsp
715 669b5ffa 2018-10-07 stsp static const struct got_error *
716 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
717 669b5ffa 2018-10-07 stsp {
718 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
719 669b5ffa 2018-10-07 stsp
720 669b5ffa 2018-10-07 stsp if (view->child == NULL)
721 669b5ffa 2018-10-07 stsp return NULL;
722 669b5ffa 2018-10-07 stsp
723 669b5ffa 2018-10-07 stsp err = view_close(view->child);
724 669b5ffa 2018-10-07 stsp view->child = NULL;
725 669b5ffa 2018-10-07 stsp return err;
726 669b5ffa 2018-10-07 stsp }
727 669b5ffa 2018-10-07 stsp
728 72a9cb46 2020-12-03 stsp static void
729 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
730 669b5ffa 2018-10-07 stsp {
731 669b5ffa 2018-10-07 stsp view->child = child;
732 669b5ffa 2018-10-07 stsp child->parent = view;
733 bfddd0d9 2018-09-29 stsp }
734 bfddd0d9 2018-09-29 stsp
735 bfddd0d9 2018-09-29 stsp static int
736 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
737 bfddd0d9 2018-09-29 stsp {
738 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
739 34bc9ec9 2019-02-22 stsp }
740 34bc9ec9 2019-02-22 stsp
741 34bc9ec9 2019-02-22 stsp static void
742 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
743 25791caa 2018-10-24 stsp {
744 25791caa 2018-10-24 stsp int cols, lines;
745 25791caa 2018-10-24 stsp struct winsize size;
746 25791caa 2018-10-24 stsp
747 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
748 25791caa 2018-10-24 stsp cols = 80; /* Default */
749 25791caa 2018-10-24 stsp lines = 24;
750 25791caa 2018-10-24 stsp } else {
751 25791caa 2018-10-24 stsp cols = size.ws_col;
752 25791caa 2018-10-24 stsp lines = size.ws_row;
753 25791caa 2018-10-24 stsp }
754 25791caa 2018-10-24 stsp resize_term(lines, cols);
755 2b49a8ae 2019-06-22 stsp }
756 2b49a8ae 2019-06-22 stsp
757 2b49a8ae 2019-06-22 stsp static const struct got_error *
758 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
759 2b49a8ae 2019-06-22 stsp {
760 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
761 2b49a8ae 2019-06-22 stsp char pattern[1024];
762 2b49a8ae 2019-06-22 stsp int ret;
763 2b49a8ae 2019-06-22 stsp
764 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
765 2b49a8ae 2019-06-22 stsp return NULL;
766 2b49a8ae 2019-06-22 stsp
767 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
768 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
769 2b49a8ae 2019-06-22 stsp
770 2b49a8ae 2019-06-22 stsp nocbreak();
771 2b49a8ae 2019-06-22 stsp echo();
772 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
773 2b49a8ae 2019-06-22 stsp cbreak();
774 2b49a8ae 2019-06-22 stsp noecho();
775 2b49a8ae 2019-06-22 stsp if (ret == ERR)
776 2b49a8ae 2019-06-22 stsp return NULL;
777 2b49a8ae 2019-06-22 stsp
778 2b49a8ae 2019-06-22 stsp if (view->searching) {
779 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
780 2b49a8ae 2019-06-22 stsp view->searching = 0;
781 2b49a8ae 2019-06-22 stsp }
782 2b49a8ae 2019-06-22 stsp
783 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
784 7c32bd05 2019-06-22 stsp err = view->search_start(view);
785 7c32bd05 2019-06-22 stsp if (err) {
786 7c32bd05 2019-06-22 stsp regfree(&view->regex);
787 7c32bd05 2019-06-22 stsp return err;
788 7c32bd05 2019-06-22 stsp }
789 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
790 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
791 2b49a8ae 2019-06-22 stsp view->search_next(view);
792 2b49a8ae 2019-06-22 stsp }
793 2b49a8ae 2019-06-22 stsp
794 2b49a8ae 2019-06-22 stsp return NULL;
795 0cf4efb1 2018-09-29 stsp }
796 6d0fee91 2018-08-01 stsp
797 0cf4efb1 2018-09-29 stsp static const struct got_error *
798 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
799 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
800 e5a0f69f 2018-08-18 stsp {
801 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
802 669b5ffa 2018-10-07 stsp struct tog_view *v;
803 1a76625f 2018-10-22 stsp int ch, errcode;
804 e5a0f69f 2018-08-18 stsp
805 e5a0f69f 2018-08-18 stsp *new = NULL;
806 8f4ed634 2020-03-26 stsp
807 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
808 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
809 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
810 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
811 e5a0f69f 2018-08-18 stsp
812 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
813 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
814 82954512 2020-02-03 stsp if (errcode)
815 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
816 82954512 2020-02-03 stsp "pthread_mutex_unlock");
817 82954512 2020-02-03 stsp pthread_yield();
818 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
819 82954512 2020-02-03 stsp if (errcode)
820 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
821 82954512 2020-02-03 stsp "pthread_mutex_lock");
822 60493ae3 2019-06-20 stsp view->search_next(view);
823 60493ae3 2019-06-20 stsp return NULL;
824 60493ae3 2019-06-20 stsp }
825 60493ae3 2019-06-20 stsp
826 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
827 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
828 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
829 1a76625f 2018-10-22 stsp if (errcode)
830 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
831 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
832 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
833 1a76625f 2018-10-22 stsp if (errcode)
834 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
835 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
836 25791caa 2018-10-24 stsp
837 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
838 25791caa 2018-10-24 stsp tog_resizeterm();
839 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
840 61266923 2020-01-14 stsp tog_sigcont_received = 0;
841 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
842 25791caa 2018-10-24 stsp err = view_resize(v);
843 25791caa 2018-10-24 stsp if (err)
844 25791caa 2018-10-24 stsp return err;
845 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
846 25791caa 2018-10-24 stsp if (err)
847 25791caa 2018-10-24 stsp return err;
848 25791caa 2018-10-24 stsp }
849 25791caa 2018-10-24 stsp }
850 25791caa 2018-10-24 stsp
851 e5a0f69f 2018-08-18 stsp switch (ch) {
852 1e37a5c2 2019-05-12 jcs case ERR:
853 1e37a5c2 2019-05-12 jcs break;
854 1e37a5c2 2019-05-12 jcs case '\t':
855 1e37a5c2 2019-05-12 jcs if (view->child) {
856 e78dc838 2020-12-04 stsp view->focussed = 0;
857 e78dc838 2020-12-04 stsp view->child->focussed = 1;
858 e78dc838 2020-12-04 stsp view->focus_child = 1;
859 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
860 e78dc838 2020-12-04 stsp view->focussed = 0;
861 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
862 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
863 1e37a5c2 2019-05-12 jcs }
864 1e37a5c2 2019-05-12 jcs break;
865 1e37a5c2 2019-05-12 jcs case 'q':
866 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
867 9970f7fc 2020-12-03 stsp view->dying = 1;
868 1e37a5c2 2019-05-12 jcs break;
869 1e37a5c2 2019-05-12 jcs case 'Q':
870 1e37a5c2 2019-05-12 jcs *done = 1;
871 1e37a5c2 2019-05-12 jcs break;
872 1e37a5c2 2019-05-12 jcs case 'f':
873 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
874 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
875 1e37a5c2 2019-05-12 jcs break;
876 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
877 e78dc838 2020-12-04 stsp view->focussed = 0;
878 e78dc838 2020-12-04 stsp view->child->focussed = 1;
879 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
880 1e37a5c2 2019-05-12 jcs } else
881 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
882 1e37a5c2 2019-05-12 jcs if (err)
883 1e37a5c2 2019-05-12 jcs break;
884 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
885 9970f7fc 2020-12-03 stsp KEY_RESIZE);
886 1e37a5c2 2019-05-12 jcs } else {
887 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
888 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
889 e78dc838 2020-12-04 stsp view->focussed = 1;
890 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
891 1e37a5c2 2019-05-12 jcs } else {
892 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
893 669b5ffa 2018-10-07 stsp }
894 1e37a5c2 2019-05-12 jcs if (err)
895 1e37a5c2 2019-05-12 jcs break;
896 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
897 1e37a5c2 2019-05-12 jcs }
898 1e37a5c2 2019-05-12 jcs break;
899 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
900 60493ae3 2019-06-20 stsp break;
901 60493ae3 2019-06-20 stsp case '/':
902 60493ae3 2019-06-20 stsp if (view->search_start)
903 2b49a8ae 2019-06-22 stsp view_search_start(view);
904 60493ae3 2019-06-20 stsp else
905 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
906 1e37a5c2 2019-05-12 jcs break;
907 b1bf1435 2019-06-21 stsp case 'N':
908 60493ae3 2019-06-20 stsp case 'n':
909 8f4ed634 2020-03-26 stsp if (view->search_next) {
910 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
911 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
912 60493ae3 2019-06-20 stsp view->search_next_done = 0;
913 60493ae3 2019-06-20 stsp view->search_next(view);
914 60493ae3 2019-06-20 stsp } else
915 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
916 60493ae3 2019-06-20 stsp break;
917 1e37a5c2 2019-05-12 jcs default:
918 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
919 1e37a5c2 2019-05-12 jcs break;
920 e5a0f69f 2018-08-18 stsp }
921 e5a0f69f 2018-08-18 stsp
922 e5a0f69f 2018-08-18 stsp return err;
923 e5a0f69f 2018-08-18 stsp }
924 e5a0f69f 2018-08-18 stsp
925 1a57306a 2018-09-02 stsp void
926 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
927 1a57306a 2018-09-02 stsp {
928 0cf4efb1 2018-09-29 stsp PANEL *panel;
929 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
930 0cf4efb1 2018-09-29 stsp
931 669b5ffa 2018-10-07 stsp if (view->parent)
932 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
933 669b5ffa 2018-10-07 stsp
934 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
935 0cf4efb1 2018-09-29 stsp if (panel == NULL)
936 1a57306a 2018-09-02 stsp return;
937 1a57306a 2018-09-02 stsp
938 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
939 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
940 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
941 bcbd79e2 2018-08-19 stsp }
942 bcbd79e2 2018-08-19 stsp
943 a3404814 2018-09-02 stsp int
944 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
945 a3404814 2018-09-02 stsp {
946 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
947 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
948 669b5ffa 2018-10-07 stsp return 0;
949 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
950 669b5ffa 2018-10-07 stsp return 0;
951 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
952 a3404814 2018-09-02 stsp return 0;
953 a3404814 2018-09-02 stsp
954 669b5ffa 2018-10-07 stsp return view->focussed;
955 a3404814 2018-09-02 stsp }
956 a3404814 2018-09-02 stsp
957 bcbd79e2 2018-08-19 stsp static const struct got_error *
958 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
959 e5a0f69f 2018-08-18 stsp {
960 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
961 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
962 e78dc838 2020-12-04 stsp struct tog_view *new_view, *main_view;
963 fd823528 2018-10-22 stsp int fast_refresh = 10;
964 1a76625f 2018-10-22 stsp int done = 0, errcode;
965 e5a0f69f 2018-08-18 stsp
966 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
967 1a76625f 2018-10-22 stsp if (errcode)
968 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
969 1a76625f 2018-10-22 stsp
970 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
971 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
972 e5a0f69f 2018-08-18 stsp
973 a81bf10d 2018-09-29 stsp main_view = view;
974 1004088d 2018-09-29 stsp view->focussed = 1;
975 878940b7 2018-09-29 stsp err = view->show(view);
976 0cf4efb1 2018-09-29 stsp if (err)
977 0cf4efb1 2018-09-29 stsp return err;
978 0cf4efb1 2018-09-29 stsp update_panels();
979 0cf4efb1 2018-09-29 stsp doupdate();
980 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
981 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
982 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
983 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
984 fd823528 2018-10-22 stsp
985 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
986 e5a0f69f 2018-08-18 stsp if (err)
987 e5a0f69f 2018-08-18 stsp break;
988 9970f7fc 2020-12-03 stsp if (view->dying) {
989 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
990 669b5ffa 2018-10-07 stsp
991 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
992 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
993 9970f7fc 2020-12-03 stsp entry);
994 e78dc838 2020-12-04 stsp else if (view->parent)
995 669b5ffa 2018-10-07 stsp prev = view->parent;
996 669b5ffa 2018-10-07 stsp
997 e78dc838 2020-12-04 stsp if (view->parent) {
998 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
999 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1000 e78dc838 2020-12-04 stsp } else
1001 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1002 669b5ffa 2018-10-07 stsp
1003 9970f7fc 2020-12-03 stsp err = view_close(view);
1004 9970f7fc 2020-12-03 stsp if (err || (view == main_view && new_view == NULL))
1005 e5a0f69f 2018-08-18 stsp goto done;
1006 669b5ffa 2018-10-07 stsp
1007 e78dc838 2020-12-04 stsp view = NULL;
1008 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1009 e78dc838 2020-12-04 stsp if (v->focussed)
1010 e78dc838 2020-12-04 stsp break;
1011 0cf4efb1 2018-09-29 stsp }
1012 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1013 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1014 e78dc838 2020-12-04 stsp if (prev)
1015 e78dc838 2020-12-04 stsp view = prev;
1016 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1017 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1018 e78dc838 2020-12-04 stsp tog_view_list_head);
1019 e78dc838 2020-12-04 stsp }
1020 e78dc838 2020-12-04 stsp if (view) {
1021 e78dc838 2020-12-04 stsp if (view->focus_child) {
1022 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1023 e78dc838 2020-12-04 stsp view = view->child;
1024 e78dc838 2020-12-04 stsp } else
1025 e78dc838 2020-12-04 stsp view->focussed = 1;
1026 e78dc838 2020-12-04 stsp }
1027 e78dc838 2020-12-04 stsp }
1028 e5a0f69f 2018-08-18 stsp }
1029 bcbd79e2 2018-08-19 stsp if (new_view) {
1030 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1031 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1032 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1033 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1034 86c66b02 2018-10-18 stsp continue;
1035 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1036 86c66b02 2018-10-18 stsp err = view_close(v);
1037 86c66b02 2018-10-18 stsp if (err)
1038 86c66b02 2018-10-18 stsp goto done;
1039 86c66b02 2018-10-18 stsp break;
1040 86c66b02 2018-10-18 stsp }
1041 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1042 fed7eaa8 2018-10-24 stsp view = new_view;
1043 e78dc838 2020-12-04 stsp }
1044 669b5ffa 2018-10-07 stsp if (view) {
1045 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1046 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1047 e78dc838 2020-12-04 stsp view = view->child;
1048 e78dc838 2020-12-04 stsp } else {
1049 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1050 e78dc838 2020-12-04 stsp view = view->parent;
1051 1a76625f 2018-10-22 stsp }
1052 e78dc838 2020-12-04 stsp show_panel(view->panel);
1053 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1054 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1055 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1056 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1057 669b5ffa 2018-10-07 stsp if (err)
1058 1a76625f 2018-10-22 stsp goto done;
1059 669b5ffa 2018-10-07 stsp }
1060 669b5ffa 2018-10-07 stsp err = view->show(view);
1061 0cf4efb1 2018-09-29 stsp if (err)
1062 1a76625f 2018-10-22 stsp goto done;
1063 669b5ffa 2018-10-07 stsp if (view->child) {
1064 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1065 669b5ffa 2018-10-07 stsp if (err)
1066 1a76625f 2018-10-22 stsp goto done;
1067 669b5ffa 2018-10-07 stsp }
1068 1a76625f 2018-10-22 stsp update_panels();
1069 1a76625f 2018-10-22 stsp doupdate();
1070 0cf4efb1 2018-09-29 stsp }
1071 e5a0f69f 2018-08-18 stsp }
1072 e5a0f69f 2018-08-18 stsp done:
1073 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1074 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1075 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1076 e5a0f69f 2018-08-18 stsp view_close(view);
1077 e5a0f69f 2018-08-18 stsp }
1078 1a76625f 2018-10-22 stsp
1079 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1080 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1081 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1082 1a76625f 2018-10-22 stsp
1083 e5a0f69f 2018-08-18 stsp return err;
1084 ea5e7bb5 2018-08-01 stsp }
1085 ea5e7bb5 2018-08-01 stsp
1086 4ed7e80c 2018-05-20 stsp __dead static void
1087 9f7d7167 2018-04-29 stsp usage_log(void)
1088 9f7d7167 2018-04-29 stsp {
1089 80ddbec8 2018-04-29 stsp endwin();
1090 c70c5802 2018-08-01 stsp fprintf(stderr,
1091 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1092 9f7d7167 2018-04-29 stsp getprogname());
1093 9f7d7167 2018-04-29 stsp exit(1);
1094 80ddbec8 2018-04-29 stsp }
1095 80ddbec8 2018-04-29 stsp
1096 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1097 80ddbec8 2018-04-29 stsp static const struct got_error *
1098 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1099 963b370f 2018-05-20 stsp {
1100 00dfcb92 2018-06-11 stsp char *vis = NULL;
1101 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1102 963b370f 2018-05-20 stsp
1103 963b370f 2018-05-20 stsp *ws = NULL;
1104 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1105 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1106 00dfcb92 2018-06-11 stsp int vislen;
1107 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1108 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1109 00dfcb92 2018-06-11 stsp
1110 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1111 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1112 00dfcb92 2018-06-11 stsp if (err)
1113 00dfcb92 2018-06-11 stsp return err;
1114 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1115 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1116 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1117 a7f50699 2018-06-11 stsp goto done;
1118 a7f50699 2018-06-11 stsp }
1119 00dfcb92 2018-06-11 stsp }
1120 963b370f 2018-05-20 stsp
1121 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1122 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1123 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1124 a7f50699 2018-06-11 stsp goto done;
1125 a7f50699 2018-06-11 stsp }
1126 963b370f 2018-05-20 stsp
1127 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1129 a7f50699 2018-06-11 stsp done:
1130 00dfcb92 2018-06-11 stsp free(vis);
1131 963b370f 2018-05-20 stsp if (err) {
1132 963b370f 2018-05-20 stsp free(*ws);
1133 963b370f 2018-05-20 stsp *ws = NULL;
1134 963b370f 2018-05-20 stsp *wlen = 0;
1135 963b370f 2018-05-20 stsp }
1136 963b370f 2018-05-20 stsp return err;
1137 963b370f 2018-05-20 stsp }
1138 963b370f 2018-05-20 stsp
1139 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 963b370f 2018-05-20 stsp static const struct got_error *
1141 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1142 27a741e5 2019-09-11 stsp int col_tab_align)
1143 963b370f 2018-05-20 stsp {
1144 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1145 963b370f 2018-05-20 stsp int cols = 0;
1146 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1147 963b370f 2018-05-20 stsp size_t wlen;
1148 963b370f 2018-05-20 stsp int i;
1149 963b370f 2018-05-20 stsp
1150 963b370f 2018-05-20 stsp *wlinep = NULL;
1151 b700b5d6 2018-07-10 stsp *widthp = 0;
1152 963b370f 2018-05-20 stsp
1153 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1154 963b370f 2018-05-20 stsp if (err)
1155 963b370f 2018-05-20 stsp return err;
1156 963b370f 2018-05-20 stsp
1157 963b370f 2018-05-20 stsp i = 0;
1158 27a741e5 2019-09-11 stsp while (i < wlen) {
1159 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1160 27a741e5 2019-09-11 stsp
1161 27a741e5 2019-09-11 stsp if (width == 0) {
1162 b700b5d6 2018-07-10 stsp i++;
1163 27a741e5 2019-09-11 stsp continue;
1164 27a741e5 2019-09-11 stsp }
1165 27a741e5 2019-09-11 stsp
1166 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1167 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1168 27a741e5 2019-09-11 stsp break;
1169 27a741e5 2019-09-11 stsp cols += width;
1170 3c1f04f1 2018-09-13 stsp i++;
1171 27a741e5 2019-09-11 stsp } else if (width == -1) {
1172 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1173 27a741e5 2019-09-11 stsp width = TABSIZE -
1174 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1175 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1176 27a741e5 2019-09-11 stsp break;
1177 27a741e5 2019-09-11 stsp cols += width;
1178 27a741e5 2019-09-11 stsp }
1179 b700b5d6 2018-07-10 stsp i++;
1180 27a741e5 2019-09-11 stsp } else {
1181 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1182 963b370f 2018-05-20 stsp goto done;
1183 963b370f 2018-05-20 stsp }
1184 963b370f 2018-05-20 stsp }
1185 963b370f 2018-05-20 stsp wline[i] = L'\0';
1186 b700b5d6 2018-07-10 stsp if (widthp)
1187 b700b5d6 2018-07-10 stsp *widthp = cols;
1188 963b370f 2018-05-20 stsp done:
1189 963b370f 2018-05-20 stsp if (err)
1190 963b370f 2018-05-20 stsp free(wline);
1191 963b370f 2018-05-20 stsp else
1192 963b370f 2018-05-20 stsp *wlinep = wline;
1193 963b370f 2018-05-20 stsp return err;
1194 963b370f 2018-05-20 stsp }
1195 963b370f 2018-05-20 stsp
1196 8b473291 2019-02-21 stsp static const struct got_error*
1197 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1199 8b473291 2019-02-21 stsp {
1200 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1201 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1202 8b473291 2019-02-21 stsp char *s;
1203 8b473291 2019-02-21 stsp const char *name;
1204 8b473291 2019-02-21 stsp
1205 8b473291 2019-02-21 stsp *refs_str = NULL;
1206 8b473291 2019-02-21 stsp
1207 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1208 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1209 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1210 52b5abe1 2019-08-13 stsp int cmp;
1211 52b5abe1 2019-08-13 stsp
1212 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1213 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1214 8b473291 2019-02-21 stsp continue;
1215 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1216 8b473291 2019-02-21 stsp name += 5;
1217 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1218 7143d404 2019-03-12 stsp continue;
1219 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1220 8b473291 2019-02-21 stsp name += 6;
1221 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1222 8b473291 2019-02-21 stsp name += 8;
1223 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1224 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1225 79cc719f 2020-04-24 stsp continue;
1226 79cc719f 2020-04-24 stsp }
1227 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1228 48cae60d 2020-09-22 stsp if (err)
1229 48cae60d 2020-09-22 stsp break;
1230 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1231 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1232 5d844a1e 2019-08-13 stsp if (err) {
1233 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1234 48cae60d 2020-09-22 stsp free(ref_id);
1235 5d844a1e 2019-08-13 stsp break;
1236 48cae60d 2020-09-22 stsp }
1237 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1238 5d844a1e 2019-08-13 stsp err = NULL;
1239 5d844a1e 2019-08-13 stsp tag = NULL;
1240 5d844a1e 2019-08-13 stsp }
1241 52b5abe1 2019-08-13 stsp }
1242 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1243 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1244 48cae60d 2020-09-22 stsp free(ref_id);
1245 52b5abe1 2019-08-13 stsp if (tag)
1246 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1247 52b5abe1 2019-08-13 stsp if (cmp != 0)
1248 52b5abe1 2019-08-13 stsp continue;
1249 8b473291 2019-02-21 stsp s = *refs_str;
1250 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1252 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1253 8b473291 2019-02-21 stsp free(s);
1254 8b473291 2019-02-21 stsp *refs_str = NULL;
1255 8b473291 2019-02-21 stsp break;
1256 8b473291 2019-02-21 stsp }
1257 8b473291 2019-02-21 stsp free(s);
1258 8b473291 2019-02-21 stsp }
1259 8b473291 2019-02-21 stsp
1260 8b473291 2019-02-21 stsp return err;
1261 8b473291 2019-02-21 stsp }
1262 8b473291 2019-02-21 stsp
1263 963b370f 2018-05-20 stsp static const struct got_error *
1264 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1265 27a741e5 2019-09-11 stsp int col_tab_align)
1266 5813d178 2019-03-09 stsp {
1267 5813d178 2019-03-09 stsp char *smallerthan, *at;
1268 5813d178 2019-03-09 stsp
1269 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1270 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1271 5813d178 2019-03-09 stsp author = smallerthan + 1;
1272 5813d178 2019-03-09 stsp at = strchr(author, '@');
1273 5813d178 2019-03-09 stsp if (at)
1274 5813d178 2019-03-09 stsp *at = '\0';
1275 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1276 5813d178 2019-03-09 stsp }
1277 5813d178 2019-03-09 stsp
1278 5813d178 2019-03-09 stsp static const struct got_error *
1279 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1281 8fdc79fe 2020-12-01 naddy int author_display_cols)
1282 80ddbec8 2018-04-29 stsp {
1283 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1284 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1285 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1287 5813d178 2019-03-09 stsp char *author = NULL;
1288 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1290 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1291 bb737323 2018-05-20 stsp int col, limit;
1292 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1293 ccb26ccd 2018-11-05 stsp struct tm tm;
1294 45d799e2 2018-12-23 stsp time_t committer_time;
1295 11b20872 2019-11-08 stsp struct tog_color *tc;
1296 80ddbec8 2018-04-29 stsp
1297 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1298 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1299 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1300 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1301 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1302 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1303 b39d25c7 2018-07-10 stsp
1304 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1305 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1306 b39d25c7 2018-07-10 stsp else
1307 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1309 11b20872 2019-11-08 stsp if (tc)
1310 11b20872 2019-11-08 stsp wattr_on(view->window,
1311 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1312 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1313 11b20872 2019-11-08 stsp if (tc)
1314 11b20872 2019-11-08 stsp wattr_off(view->window,
1315 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1316 27a741e5 2019-09-11 stsp col = limit;
1317 b39d25c7 2018-07-10 stsp if (col > avail)
1318 b39d25c7 2018-07-10 stsp goto done;
1319 6570a66d 2019-11-08 stsp
1320 6570a66d 2019-11-08 stsp if (avail >= 120) {
1321 6570a66d 2019-11-08 stsp char *id_str;
1322 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1323 6570a66d 2019-11-08 stsp if (err)
1324 6570a66d 2019-11-08 stsp goto done;
1325 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1326 11b20872 2019-11-08 stsp if (tc)
1327 11b20872 2019-11-08 stsp wattr_on(view->window,
1328 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1329 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1330 11b20872 2019-11-08 stsp if (tc)
1331 11b20872 2019-11-08 stsp wattr_off(view->window,
1332 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1333 6570a66d 2019-11-08 stsp free(id_str);
1334 6570a66d 2019-11-08 stsp col += 9;
1335 6570a66d 2019-11-08 stsp if (col > avail)
1336 6570a66d 2019-11-08 stsp goto done;
1337 6570a66d 2019-11-08 stsp }
1338 b39d25c7 2018-07-10 stsp
1339 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1340 5813d178 2019-03-09 stsp if (author == NULL) {
1341 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1342 80ddbec8 2018-04-29 stsp goto done;
1343 80ddbec8 2018-04-29 stsp }
1344 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1345 bb737323 2018-05-20 stsp if (err)
1346 bb737323 2018-05-20 stsp goto done;
1347 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1348 11b20872 2019-11-08 stsp if (tc)
1349 11b20872 2019-11-08 stsp wattr_on(view->window,
1350 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1351 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1352 11b20872 2019-11-08 stsp if (tc)
1353 11b20872 2019-11-08 stsp wattr_off(view->window,
1354 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1355 bb737323 2018-05-20 stsp col += author_width;
1356 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1357 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1358 bb737323 2018-05-20 stsp col++;
1359 bb737323 2018-05-20 stsp author_width++;
1360 bb737323 2018-05-20 stsp }
1361 9c2eaf34 2018-05-20 stsp if (col > avail)
1362 9c2eaf34 2018-05-20 stsp goto done;
1363 80ddbec8 2018-04-29 stsp
1364 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1365 5943eee2 2019-08-13 stsp if (err)
1366 6d9fbc00 2018-04-29 stsp goto done;
1367 bb737323 2018-05-20 stsp logmsg = logmsg0;
1368 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1369 bb737323 2018-05-20 stsp logmsg++;
1370 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1371 bb737323 2018-05-20 stsp if (newline)
1372 bb737323 2018-05-20 stsp *newline = '\0';
1373 bb737323 2018-05-20 stsp limit = avail - col;
1374 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1375 bb737323 2018-05-20 stsp if (err)
1376 bb737323 2018-05-20 stsp goto done;
1377 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1378 bb737323 2018-05-20 stsp col += logmsg_width;
1379 27a741e5 2019-09-11 stsp while (col < avail) {
1380 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1381 bb737323 2018-05-20 stsp col++;
1382 881b2d3e 2018-04-30 stsp }
1383 80ddbec8 2018-04-29 stsp done:
1384 80ddbec8 2018-04-29 stsp free(logmsg0);
1385 bb737323 2018-05-20 stsp free(wlogmsg);
1386 5813d178 2019-03-09 stsp free(author);
1387 bb737323 2018-05-20 stsp free(wauthor);
1388 80ddbec8 2018-04-29 stsp free(line);
1389 80ddbec8 2018-04-29 stsp return err;
1390 80ddbec8 2018-04-29 stsp }
1391 26ed57b2 2018-05-19 stsp
1392 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1393 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1394 899d86c2 2018-05-10 stsp struct got_object_id *id)
1395 80ddbec8 2018-04-29 stsp {
1396 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1397 80ddbec8 2018-04-29 stsp
1398 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1399 80ddbec8 2018-04-29 stsp if (entry == NULL)
1400 899d86c2 2018-05-10 stsp return NULL;
1401 99db9666 2018-05-07 stsp
1402 899d86c2 2018-05-10 stsp entry->id = id;
1403 99db9666 2018-05-07 stsp entry->commit = commit;
1404 899d86c2 2018-05-10 stsp return entry;
1405 99db9666 2018-05-07 stsp }
1406 80ddbec8 2018-04-29 stsp
1407 99db9666 2018-05-07 stsp static void
1408 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1409 99db9666 2018-05-07 stsp {
1410 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1411 99db9666 2018-05-07 stsp
1412 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1413 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1414 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1415 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1416 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1417 99db9666 2018-05-07 stsp free(entry);
1418 99db9666 2018-05-07 stsp }
1419 99db9666 2018-05-07 stsp
1420 99db9666 2018-05-07 stsp static void
1421 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1422 99db9666 2018-05-07 stsp {
1423 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1424 99db9666 2018-05-07 stsp pop_commit(commits);
1425 c4972b91 2018-05-07 stsp }
1426 c4972b91 2018-05-07 stsp
1427 c4972b91 2018-05-07 stsp static const struct got_error *
1428 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1429 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1430 13add988 2019-10-15 stsp {
1431 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1432 13add988 2019-10-15 stsp regmatch_t regmatch;
1433 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1434 13add988 2019-10-15 stsp
1435 13add988 2019-10-15 stsp *have_match = 0;
1436 13add988 2019-10-15 stsp
1437 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1438 13add988 2019-10-15 stsp if (err)
1439 13add988 2019-10-15 stsp return err;
1440 13add988 2019-10-15 stsp
1441 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1442 13add988 2019-10-15 stsp if (err)
1443 13add988 2019-10-15 stsp goto done;
1444 13add988 2019-10-15 stsp
1445 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1447 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1448 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1449 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1450 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1451 13add988 2019-10-15 stsp *have_match = 1;
1452 13add988 2019-10-15 stsp done:
1453 13add988 2019-10-15 stsp free(id_str);
1454 13add988 2019-10-15 stsp free(logmsg);
1455 13add988 2019-10-15 stsp return err;
1456 13add988 2019-10-15 stsp }
1457 13add988 2019-10-15 stsp
1458 13add988 2019-10-15 stsp static const struct got_error *
1459 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1461 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1462 c4972b91 2018-05-07 stsp {
1463 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1464 7c1452c1 2020-03-26 stsp int nqueued = 0;
1465 9ba79e04 2018-06-11 stsp
1466 1a76625f 2018-10-22 stsp /*
1467 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1468 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1469 1a76625f 2018-10-22 stsp * while updating the display.
1470 1a76625f 2018-10-22 stsp */
1471 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1472 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1474 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1475 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1476 1a76625f 2018-10-22 stsp int errcode;
1477 899d86c2 2018-05-10 stsp
1478 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1480 ecb28ae0 2018-07-16 stsp break;
1481 899d86c2 2018-05-10 stsp
1482 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1483 9ba79e04 2018-06-11 stsp if (err)
1484 9ba79e04 2018-06-11 stsp break;
1485 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1486 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1488 9ba79e04 2018-06-11 stsp break;
1489 9ba79e04 2018-06-11 stsp }
1490 93e45b7c 2018-09-24 stsp
1491 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1492 1a76625f 2018-10-22 stsp if (errcode) {
1493 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1494 13add988 2019-10-15 stsp "pthread_mutex_lock");
1495 1a76625f 2018-10-22 stsp break;
1496 1a76625f 2018-10-22 stsp }
1497 1a76625f 2018-10-22 stsp
1498 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1499 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1500 ecb28ae0 2018-07-16 stsp nqueued++;
1501 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1502 1a76625f 2018-10-22 stsp
1503 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1504 7c1452c1 2020-03-26 stsp int have_match;
1505 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1506 7c1452c1 2020-03-26 stsp if (err)
1507 7c1452c1 2020-03-26 stsp break;
1508 7c1452c1 2020-03-26 stsp if (have_match)
1509 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1510 13add988 2019-10-15 stsp }
1511 13add988 2019-10-15 stsp
1512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1513 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1514 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1515 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1516 7c1452c1 2020-03-26 stsp if (err)
1517 13add988 2019-10-15 stsp break;
1518 899d86c2 2018-05-10 stsp }
1519 899d86c2 2018-05-10 stsp
1520 9ba79e04 2018-06-11 stsp return err;
1521 0553a4e3 2018-04-30 stsp }
1522 0553a4e3 2018-04-30 stsp
1523 2b779855 2020-12-05 naddy static void
1524 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1525 2b779855 2020-12-05 naddy {
1526 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1527 2b779855 2020-12-05 naddy int ncommits = 0;
1528 2b779855 2020-12-05 naddy
1529 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1530 2b779855 2020-12-05 naddy while (entry) {
1531 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1532 2b779855 2020-12-05 naddy s->selected_entry = entry;
1533 2b779855 2020-12-05 naddy break;
1534 2b779855 2020-12-05 naddy }
1535 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1536 2b779855 2020-12-05 naddy ncommits++;
1537 2b779855 2020-12-05 naddy }
1538 2b779855 2020-12-05 naddy }
1539 2b779855 2020-12-05 naddy
1540 0553a4e3 2018-04-30 stsp static const struct got_error *
1541 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1542 0553a4e3 2018-04-30 stsp {
1543 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1544 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1545 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1546 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1547 60493ae3 2019-06-20 stsp int width;
1548 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1549 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1550 8b473291 2019-02-21 stsp char *refs_str = NULL;
1551 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1552 11b20872 2019-11-08 stsp struct tog_color *tc;
1553 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1554 0553a4e3 2018-04-30 stsp
1555 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1556 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1557 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1558 1a76625f 2018-10-22 stsp if (err)
1559 ecb28ae0 2018-07-16 stsp return err;
1560 8fdc79fe 2020-12-01 naddy err = build_refs_str(&refs_str, &s->refs,
1561 8fdc79fe 2020-12-01 naddy s->selected_entry->id, s->repo);
1562 8fdc79fe 2020-12-01 naddy if (err)
1563 8fdc79fe 2020-12-01 naddy goto done;
1564 867c6645 2018-07-10 stsp }
1565 359bfafd 2019-02-22 stsp
1566 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1567 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1568 1a76625f 2018-10-22 stsp
1569 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1570 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1571 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1572 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1573 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1574 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1575 8f4ed634 2020-03-26 stsp goto done;
1576 8f4ed634 2020-03-26 stsp }
1577 8f4ed634 2020-03-26 stsp } else {
1578 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1579 f9686aa5 2020-03-27 stsp
1580 f9686aa5 2020-03-27 stsp if (view->searching) {
1581 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1582 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1583 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1584 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1585 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1586 f9686aa5 2020-03-27 stsp search_str = "searching...";
1587 f9686aa5 2020-03-27 stsp }
1588 f9686aa5 2020-03-27 stsp
1589 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1590 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1591 f9686aa5 2020-03-27 stsp search_str ? search_str :
1592 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1593 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1594 8f4ed634 2020-03-26 stsp goto done;
1595 8f4ed634 2020-03-26 stsp }
1596 8b473291 2019-02-21 stsp }
1597 1a76625f 2018-10-22 stsp
1598 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1599 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1600 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1601 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1602 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1603 1a76625f 2018-10-22 stsp header = NULL;
1604 1a76625f 2018-10-22 stsp goto done;
1605 1a76625f 2018-10-22 stsp }
1606 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1607 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1608 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1609 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1610 1a76625f 2018-10-22 stsp header = NULL;
1611 1a76625f 2018-10-22 stsp goto done;
1612 ecb28ae0 2018-07-16 stsp }
1613 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1614 1a76625f 2018-10-22 stsp if (err)
1615 1a76625f 2018-10-22 stsp goto done;
1616 867c6645 2018-07-10 stsp
1617 2814baeb 2018-08-01 stsp werase(view->window);
1618 867c6645 2018-07-10 stsp
1619 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1620 a3404814 2018-09-02 stsp wstandout(view->window);
1621 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1622 11b20872 2019-11-08 stsp if (tc)
1623 11b20872 2019-11-08 stsp wattr_on(view->window,
1624 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1625 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1626 11b20872 2019-11-08 stsp if (tc)
1627 11b20872 2019-11-08 stsp wattr_off(view->window,
1628 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1629 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1630 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1631 1a76625f 2018-10-22 stsp width++;
1632 1a76625f 2018-10-22 stsp }
1633 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1634 a3404814 2018-09-02 stsp wstandend(view->window);
1635 ecb28ae0 2018-07-16 stsp free(wline);
1636 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1637 1a76625f 2018-10-22 stsp goto done;
1638 0553a4e3 2018-04-30 stsp
1639 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1640 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1641 5813d178 2019-03-09 stsp ncommits = 0;
1642 5813d178 2019-03-09 stsp while (entry) {
1643 5813d178 2019-03-09 stsp char *author;
1644 5813d178 2019-03-09 stsp wchar_t *wauthor;
1645 5813d178 2019-03-09 stsp int width;
1646 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1647 5813d178 2019-03-09 stsp break;
1648 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1649 5813d178 2019-03-09 stsp if (author == NULL) {
1650 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1651 5813d178 2019-03-09 stsp goto done;
1652 5813d178 2019-03-09 stsp }
1653 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1654 27a741e5 2019-09-11 stsp date_display_cols);
1655 5813d178 2019-03-09 stsp if (author_cols < width)
1656 5813d178 2019-03-09 stsp author_cols = width;
1657 5813d178 2019-03-09 stsp free(wauthor);
1658 5813d178 2019-03-09 stsp free(author);
1659 7ca04879 2019-10-19 stsp ncommits++;
1660 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1661 5813d178 2019-03-09 stsp }
1662 5813d178 2019-03-09 stsp
1663 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1664 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1665 867c6645 2018-07-10 stsp ncommits = 0;
1666 899d86c2 2018-05-10 stsp while (entry) {
1667 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1668 899d86c2 2018-05-10 stsp break;
1669 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1670 2814baeb 2018-08-01 stsp wstandout(view->window);
1671 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1672 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1673 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1674 2814baeb 2018-08-01 stsp wstandend(view->window);
1675 0553a4e3 2018-04-30 stsp if (err)
1676 60493ae3 2019-06-20 stsp goto done;
1677 0553a4e3 2018-04-30 stsp ncommits++;
1678 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1679 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1680 80ddbec8 2018-04-29 stsp }
1681 80ddbec8 2018-04-29 stsp
1682 1a57306a 2018-09-02 stsp view_vborder(view);
1683 1a76625f 2018-10-22 stsp done:
1684 1a76625f 2018-10-22 stsp free(id_str);
1685 8b473291 2019-02-21 stsp free(refs_str);
1686 1a76625f 2018-10-22 stsp free(ncommits_str);
1687 1a76625f 2018-10-22 stsp free(header);
1688 80ddbec8 2018-04-29 stsp return err;
1689 9f7d7167 2018-04-29 stsp }
1690 07b55e75 2018-05-10 stsp
1691 07b55e75 2018-05-10 stsp static void
1692 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1693 07b55e75 2018-05-10 stsp {
1694 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1695 07b55e75 2018-05-10 stsp int nscrolled = 0;
1696 07b55e75 2018-05-10 stsp
1697 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1698 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1699 07b55e75 2018-05-10 stsp return;
1700 9f7d7167 2018-04-29 stsp
1701 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1702 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1703 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1704 07b55e75 2018-05-10 stsp if (entry) {
1705 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1706 07b55e75 2018-05-10 stsp nscrolled++;
1707 07b55e75 2018-05-10 stsp }
1708 07b55e75 2018-05-10 stsp }
1709 aa075928 2018-05-10 stsp }
1710 aa075928 2018-05-10 stsp
1711 aa075928 2018-05-10 stsp static const struct got_error *
1712 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1713 aa075928 2018-05-10 stsp {
1714 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1715 5e224a3e 2019-02-22 stsp int errcode;
1716 8a42fca8 2019-02-22 stsp
1717 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1718 aa075928 2018-05-10 stsp
1719 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1720 ffe38506 2020-12-01 naddy if (ta->log_complete)
1721 5e224a3e 2019-02-22 stsp break;
1722 b295e71b 2019-02-22 stsp
1723 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1724 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1725 7aafa0d1 2019-02-22 stsp if (errcode)
1726 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1727 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1728 7c1452c1 2020-03-26 stsp
1729 7c1452c1 2020-03-26 stsp /*
1730 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1731 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1732 7c1452c1 2020-03-26 stsp */
1733 7c1452c1 2020-03-26 stsp if (!wait)
1734 7c1452c1 2020-03-26 stsp break;
1735 7c1452c1 2020-03-26 stsp
1736 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1737 ffe38506 2020-12-01 naddy show_log_view(view);
1738 7c1452c1 2020-03-26 stsp update_panels();
1739 7c1452c1 2020-03-26 stsp doupdate();
1740 7c1452c1 2020-03-26 stsp
1741 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1742 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1743 82954512 2020-02-03 stsp if (errcode)
1744 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1745 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1746 82954512 2020-02-03 stsp
1747 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1748 ffe38506 2020-12-01 naddy show_log_view(view);
1749 7c1452c1 2020-03-26 stsp update_panels();
1750 7c1452c1 2020-03-26 stsp doupdate();
1751 5e224a3e 2019-02-22 stsp }
1752 5e224a3e 2019-02-22 stsp
1753 5e224a3e 2019-02-22 stsp return NULL;
1754 5e224a3e 2019-02-22 stsp }
1755 5e224a3e 2019-02-22 stsp
1756 5e224a3e 2019-02-22 stsp static const struct got_error *
1757 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1758 5e224a3e 2019-02-22 stsp {
1759 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1760 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1761 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1762 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1763 5e224a3e 2019-02-22 stsp
1764 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1765 5e224a3e 2019-02-22 stsp return NULL;
1766 5e224a3e 2019-02-22 stsp
1767 ffe38506 2020-12-01 naddy ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1768 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1769 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1770 08ebd0a9 2019-02-22 stsp /*
1771 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1772 08ebd0a9 2019-02-22 stsp */
1773 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1774 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1775 5e224a3e 2019-02-22 stsp if (err)
1776 5e224a3e 2019-02-22 stsp return err;
1777 7aafa0d1 2019-02-22 stsp }
1778 b295e71b 2019-02-22 stsp
1779 7aafa0d1 2019-02-22 stsp do {
1780 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1781 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1782 88048b54 2019-02-21 stsp break;
1783 88048b54 2019-02-21 stsp
1784 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1785 aa075928 2018-05-10 stsp
1786 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1787 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1788 dd0a52c1 2018-05-20 stsp break;
1789 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1790 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1791 aa075928 2018-05-10 stsp
1792 dd0a52c1 2018-05-20 stsp return err;
1793 07b55e75 2018-05-10 stsp }
1794 4a7f7875 2018-05-10 stsp
1795 cd0acaa7 2018-05-20 stsp static const struct got_error *
1796 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1797 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1798 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1799 cd0acaa7 2018-05-20 stsp {
1800 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1801 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1802 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1803 cd0acaa7 2018-05-20 stsp
1804 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1805 15a94983 2018-12-23 stsp if (diff_view == NULL)
1806 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1807 ea5e7bb5 2018-08-01 stsp
1808 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1809 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1810 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1811 e5a0f69f 2018-08-18 stsp if (err == NULL)
1812 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1813 cd0acaa7 2018-05-20 stsp return err;
1814 4a7f7875 2018-05-10 stsp }
1815 4a7f7875 2018-05-10 stsp
1816 80ddbec8 2018-04-29 stsp static const struct got_error *
1817 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1818 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1819 9343a5fb 2018-06-23 stsp {
1820 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1821 941e9f74 2019-05-21 stsp
1822 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1823 941e9f74 2019-05-21 stsp if (parent == NULL)
1824 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1825 941e9f74 2019-05-21 stsp
1826 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1827 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1828 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1829 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1830 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1831 941e9f74 2019-05-21 stsp s->tree = subtree;
1832 941e9f74 2019-05-21 stsp s->selected = 0;
1833 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1834 941e9f74 2019-05-21 stsp return NULL;
1835 941e9f74 2019-05-21 stsp }
1836 941e9f74 2019-05-21 stsp
1837 941e9f74 2019-05-21 stsp static const struct got_error *
1838 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1839 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1840 941e9f74 2019-05-21 stsp {
1841 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1842 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1843 941e9f74 2019-05-21 stsp const char *p;
1844 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1845 9343a5fb 2018-06-23 stsp
1846 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1847 941e9f74 2019-05-21 stsp p = path;
1848 941e9f74 2019-05-21 stsp while (*p) {
1849 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1850 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1851 56e0773d 2019-11-28 stsp char *te_name;
1852 33cbf02b 2020-01-12 stsp
1853 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1854 33cbf02b 2020-01-12 stsp p++;
1855 941e9f74 2019-05-21 stsp
1856 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1857 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1858 941e9f74 2019-05-21 stsp if (slash == NULL)
1859 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1860 33cbf02b 2020-01-12 stsp else
1861 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1862 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1863 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1864 56e0773d 2019-11-28 stsp break;
1865 941e9f74 2019-05-21 stsp }
1866 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1867 56e0773d 2019-11-28 stsp if (te == NULL) {
1868 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1869 56e0773d 2019-11-28 stsp free(te_name);
1870 941e9f74 2019-05-21 stsp break;
1871 941e9f74 2019-05-21 stsp }
1872 56e0773d 2019-11-28 stsp free(te_name);
1873 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1874 941e9f74 2019-05-21 stsp
1875 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1876 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1877 b03c880f 2019-05-21 stsp
1878 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1879 941e9f74 2019-05-21 stsp if (slash)
1880 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1881 941e9f74 2019-05-21 stsp else
1882 941e9f74 2019-05-21 stsp subpath = strdup(path);
1883 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1884 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1885 941e9f74 2019-05-21 stsp break;
1886 941e9f74 2019-05-21 stsp }
1887 941e9f74 2019-05-21 stsp
1888 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1889 941e9f74 2019-05-21 stsp subpath);
1890 941e9f74 2019-05-21 stsp if (err)
1891 941e9f74 2019-05-21 stsp break;
1892 941e9f74 2019-05-21 stsp
1893 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1894 941e9f74 2019-05-21 stsp free(tree_id);
1895 941e9f74 2019-05-21 stsp if (err)
1896 941e9f74 2019-05-21 stsp break;
1897 941e9f74 2019-05-21 stsp
1898 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1899 941e9f74 2019-05-21 stsp if (err) {
1900 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1901 941e9f74 2019-05-21 stsp break;
1902 941e9f74 2019-05-21 stsp }
1903 941e9f74 2019-05-21 stsp if (slash == NULL)
1904 941e9f74 2019-05-21 stsp break;
1905 941e9f74 2019-05-21 stsp free(subpath);
1906 941e9f74 2019-05-21 stsp subpath = NULL;
1907 941e9f74 2019-05-21 stsp p = slash;
1908 941e9f74 2019-05-21 stsp }
1909 941e9f74 2019-05-21 stsp
1910 941e9f74 2019-05-21 stsp free(subpath);
1911 1a76625f 2018-10-22 stsp return err;
1912 61266923 2020-01-14 stsp }
1913 61266923 2020-01-14 stsp
1914 61266923 2020-01-14 stsp static const struct got_error *
1915 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1916 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1917 78756c87 2020-11-24 stsp struct got_repository *repo)
1918 55cccc34 2020-02-20 stsp {
1919 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1920 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1921 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1922 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1923 55cccc34 2020-02-20 stsp
1924 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1925 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1926 55cccc34 2020-02-20 stsp if (err)
1927 55cccc34 2020-02-20 stsp return err;
1928 55cccc34 2020-02-20 stsp
1929 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1930 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1931 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1932 55cccc34 2020-02-20 stsp
1933 78756c87 2020-11-24 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1934 55cccc34 2020-02-20 stsp if (err) {
1935 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1936 55cccc34 2020-02-20 stsp return err;
1937 55cccc34 2020-02-20 stsp }
1938 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1939 55cccc34 2020-02-20 stsp
1940 55cccc34 2020-02-20 stsp *new_view = tree_view;
1941 55cccc34 2020-02-20 stsp
1942 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1943 55cccc34 2020-02-20 stsp return NULL;
1944 55cccc34 2020-02-20 stsp
1945 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1946 55cccc34 2020-02-20 stsp }
1947 55cccc34 2020-02-20 stsp
1948 55cccc34 2020-02-20 stsp static const struct got_error *
1949 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1950 61266923 2020-01-14 stsp {
1951 61266923 2020-01-14 stsp sigset_t sigset;
1952 61266923 2020-01-14 stsp int errcode;
1953 61266923 2020-01-14 stsp
1954 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1955 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1956 61266923 2020-01-14 stsp
1957 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1958 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1959 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1960 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1961 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1962 61266923 2020-01-14 stsp
1963 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1964 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1965 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1966 61266923 2020-01-14 stsp
1967 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1968 61266923 2020-01-14 stsp if (errcode)
1969 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1970 61266923 2020-01-14 stsp
1971 61266923 2020-01-14 stsp return NULL;
1972 1a76625f 2018-10-22 stsp }
1973 1a76625f 2018-10-22 stsp
1974 1a76625f 2018-10-22 stsp static void *
1975 1a76625f 2018-10-22 stsp log_thread(void *arg)
1976 1a76625f 2018-10-22 stsp {
1977 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1978 1a76625f 2018-10-22 stsp int errcode = 0;
1979 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1980 1a76625f 2018-10-22 stsp int done = 0;
1981 61266923 2020-01-14 stsp
1982 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1983 61266923 2020-01-14 stsp if (err)
1984 61266923 2020-01-14 stsp return (void *)err;
1985 1a76625f 2018-10-22 stsp
1986 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1987 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1988 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1989 13add988 2019-10-15 stsp a->regex);
1990 1a76625f 2018-10-22 stsp if (err) {
1991 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1992 1a76625f 2018-10-22 stsp return (void *)err;
1993 1a76625f 2018-10-22 stsp err = NULL;
1994 1a76625f 2018-10-22 stsp done = 1;
1995 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1996 1a76625f 2018-10-22 stsp a->commits_needed--;
1997 1a76625f 2018-10-22 stsp
1998 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1999 3abe8080 2019-04-10 stsp if (errcode) {
2000 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2001 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2002 3abe8080 2019-04-10 stsp break;
2003 3abe8080 2019-04-10 stsp } else if (*a->quit)
2004 1a76625f 2018-10-22 stsp done = 1;
2005 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2006 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2007 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2008 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2009 1a76625f 2018-10-22 stsp }
2010 1a76625f 2018-10-22 stsp
2011 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2012 7c1452c1 2020-03-26 stsp if (errcode) {
2013 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2014 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2015 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2016 7c1452c1 2020-03-26 stsp break;
2017 7c1452c1 2020-03-26 stsp }
2018 7c1452c1 2020-03-26 stsp
2019 1a76625f 2018-10-22 stsp if (done)
2020 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2021 7c1452c1 2020-03-26 stsp else {
2022 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2023 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2024 7c1452c1 2020-03-26 stsp &tog_mutex);
2025 7c1452c1 2020-03-26 stsp if (errcode)
2026 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2027 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2028 7c1452c1 2020-03-26 stsp }
2029 1a76625f 2018-10-22 stsp }
2030 1a76625f 2018-10-22 stsp
2031 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2032 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2033 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2034 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2035 1a76625f 2018-10-22 stsp }
2036 3abe8080 2019-04-10 stsp a->log_complete = 1;
2037 1a76625f 2018-10-22 stsp return (void *)err;
2038 1a76625f 2018-10-22 stsp }
2039 1a76625f 2018-10-22 stsp
2040 1a76625f 2018-10-22 stsp static const struct got_error *
2041 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2042 1a76625f 2018-10-22 stsp {
2043 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2044 1a76625f 2018-10-22 stsp int errcode;
2045 1a76625f 2018-10-22 stsp
2046 1a76625f 2018-10-22 stsp if (s->thread) {
2047 1a76625f 2018-10-22 stsp s->quit = 1;
2048 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2049 1a76625f 2018-10-22 stsp if (errcode)
2050 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2051 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2052 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2053 1a76625f 2018-10-22 stsp if (errcode)
2054 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2055 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2056 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2057 1a76625f 2018-10-22 stsp if (errcode)
2058 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2059 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2060 1a76625f 2018-10-22 stsp if (errcode)
2061 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2062 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2063 1a76625f 2018-10-22 stsp s->thread = NULL;
2064 1a76625f 2018-10-22 stsp }
2065 1a76625f 2018-10-22 stsp
2066 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2067 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2068 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2069 1a76625f 2018-10-22 stsp }
2070 1a76625f 2018-10-22 stsp
2071 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2072 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2073 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2074 1a76625f 2018-10-22 stsp }
2075 1a76625f 2018-10-22 stsp
2076 9343a5fb 2018-06-23 stsp return err;
2077 9343a5fb 2018-06-23 stsp }
2078 9343a5fb 2018-06-23 stsp
2079 9343a5fb 2018-06-23 stsp static const struct got_error *
2080 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2081 1a76625f 2018-10-22 stsp {
2082 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2083 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2084 276b94a1 2020-11-13 naddy int errcode;
2085 1a76625f 2018-10-22 stsp
2086 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2087 276b94a1 2020-11-13 naddy
2088 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2089 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2090 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2091 276b94a1 2020-11-13 naddy
2092 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2093 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2094 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2095 276b94a1 2020-11-13 naddy
2096 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2097 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2098 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2099 1a76625f 2018-10-22 stsp free(s->start_id);
2100 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2101 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
2102 1a76625f 2018-10-22 stsp return err;
2103 1a76625f 2018-10-22 stsp }
2104 1a76625f 2018-10-22 stsp
2105 1a76625f 2018-10-22 stsp static const struct got_error *
2106 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2107 60493ae3 2019-06-20 stsp {
2108 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2109 60493ae3 2019-06-20 stsp
2110 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2111 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2112 60493ae3 2019-06-20 stsp return NULL;
2113 60493ae3 2019-06-20 stsp }
2114 60493ae3 2019-06-20 stsp
2115 60493ae3 2019-06-20 stsp static const struct got_error *
2116 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2117 60493ae3 2019-06-20 stsp {
2118 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2119 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2120 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2121 60493ae3 2019-06-20 stsp
2122 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2123 f9686aa5 2020-03-27 stsp show_log_view(view);
2124 f9686aa5 2020-03-27 stsp update_panels();
2125 f9686aa5 2020-03-27 stsp doupdate();
2126 f9686aa5 2020-03-27 stsp
2127 96e2b566 2019-07-08 stsp if (s->search_entry) {
2128 13add988 2019-10-15 stsp int errcode, ch;
2129 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2130 13add988 2019-10-15 stsp if (errcode)
2131 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2132 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2133 13add988 2019-10-15 stsp ch = wgetch(view->window);
2134 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2135 13add988 2019-10-15 stsp if (errcode)
2136 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2137 13add988 2019-10-15 stsp "pthread_mutex_lock");
2138 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2139 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2140 678cbce5 2019-07-28 stsp return NULL;
2141 678cbce5 2019-07-28 stsp }
2142 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2143 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2144 96e2b566 2019-07-08 stsp else
2145 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2146 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2147 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2148 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2149 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2150 b1bf1435 2019-06-21 stsp else
2151 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2152 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2153 20be8d96 2019-06-21 stsp } else {
2154 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2155 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2156 20be8d96 2019-06-21 stsp else
2157 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2158 20be8d96 2019-06-21 stsp }
2159 60493ae3 2019-06-20 stsp
2160 60493ae3 2019-06-20 stsp while (1) {
2161 13add988 2019-10-15 stsp int have_match = 0;
2162 13add988 2019-10-15 stsp
2163 60493ae3 2019-06-20 stsp if (entry == NULL) {
2164 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2165 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2166 f9967bca 2020-03-27 stsp view->search_next_done =
2167 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2168 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2169 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2170 f801134a 2019-06-25 stsp return NULL;
2171 60493ae3 2019-06-20 stsp }
2172 96e2b566 2019-07-08 stsp /*
2173 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2174 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2175 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2176 96e2b566 2019-07-08 stsp */
2177 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2178 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2179 60493ae3 2019-06-20 stsp }
2180 60493ae3 2019-06-20 stsp
2181 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2182 13add988 2019-10-15 stsp &view->regex);
2183 5943eee2 2019-08-13 stsp if (err)
2184 13add988 2019-10-15 stsp break;
2185 13add988 2019-10-15 stsp if (have_match) {
2186 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2188 60493ae3 2019-06-20 stsp break;
2189 60493ae3 2019-06-20 stsp }
2190 13add988 2019-10-15 stsp
2191 96e2b566 2019-07-08 stsp s->search_entry = entry;
2192 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2193 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2194 b1bf1435 2019-06-21 stsp else
2195 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2196 60493ae3 2019-06-20 stsp }
2197 60493ae3 2019-06-20 stsp
2198 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2199 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2200 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2201 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2202 60493ae3 2019-06-20 stsp if (err)
2203 60493ae3 2019-06-20 stsp return err;
2204 ead14cbe 2019-06-21 stsp cur++;
2205 ead14cbe 2019-06-21 stsp }
2206 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2207 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2208 60493ae3 2019-06-20 stsp if (err)
2209 60493ae3 2019-06-20 stsp return err;
2210 ead14cbe 2019-06-21 stsp cur--;
2211 60493ae3 2019-06-20 stsp }
2212 60493ae3 2019-06-20 stsp }
2213 60493ae3 2019-06-20 stsp
2214 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2215 96e2b566 2019-07-08 stsp
2216 60493ae3 2019-06-20 stsp return NULL;
2217 60493ae3 2019-06-20 stsp }
2218 60493ae3 2019-06-20 stsp
2219 60493ae3 2019-06-20 stsp static const struct got_error *
2220 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2221 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2222 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2223 80ddbec8 2018-04-29 stsp {
2224 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2225 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2226 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2227 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2228 1a76625f 2018-10-22 stsp int errcode;
2229 80ddbec8 2018-04-29 stsp
2230 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
2231 78756c87 2020-11-24 stsp
2232 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2233 f135c941 2020-02-20 stsp free(s->in_repo_path);
2234 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2235 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2236 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2237 f135c941 2020-02-20 stsp }
2238 ecb28ae0 2018-07-16 stsp
2239 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2240 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2241 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2242 9ba79e04 2018-06-11 stsp
2243 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2244 78756c87 2020-11-24 stsp if (err)
2245 78756c87 2020-11-24 stsp goto done;
2246 78756c87 2020-11-24 stsp
2247 fb2756b9 2018-08-04 stsp s->repo = repo;
2248 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2249 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2250 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2251 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2252 5036bf37 2018-09-24 stsp goto done;
2253 5036bf37 2018-09-24 stsp }
2254 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2255 e5a0f69f 2018-08-18 stsp
2256 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2257 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2258 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2259 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2260 11b20872 2019-11-08 stsp if (err)
2261 11b20872 2019-11-08 stsp goto done;
2262 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2263 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2264 11b20872 2019-11-08 stsp if (err) {
2265 11b20872 2019-11-08 stsp free_colors(&s->colors);
2266 11b20872 2019-11-08 stsp goto done;
2267 11b20872 2019-11-08 stsp }
2268 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2269 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2270 11b20872 2019-11-08 stsp if (err) {
2271 11b20872 2019-11-08 stsp free_colors(&s->colors);
2272 11b20872 2019-11-08 stsp goto done;
2273 11b20872 2019-11-08 stsp }
2274 11b20872 2019-11-08 stsp }
2275 11b20872 2019-11-08 stsp
2276 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2277 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2278 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2279 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2280 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2281 1a76625f 2018-10-22 stsp
2282 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2283 1a76625f 2018-10-22 stsp if (err)
2284 1a76625f 2018-10-22 stsp goto done;
2285 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2286 b672a97a 2020-01-27 stsp !s->log_branches);
2287 1a76625f 2018-10-22 stsp if (err)
2288 1a76625f 2018-10-22 stsp goto done;
2289 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2290 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2291 c5b78334 2020-01-12 stsp if (err)
2292 c5b78334 2020-01-12 stsp goto done;
2293 1a76625f 2018-10-22 stsp
2294 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2295 1a76625f 2018-10-22 stsp if (errcode) {
2296 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2297 1a76625f 2018-10-22 stsp goto done;
2298 1a76625f 2018-10-22 stsp }
2299 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2300 7c1452c1 2020-03-26 stsp if (errcode) {
2301 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2302 7c1452c1 2020-03-26 stsp goto done;
2303 7c1452c1 2020-03-26 stsp }
2304 1a76625f 2018-10-22 stsp
2305 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2306 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2307 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2308 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2309 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2310 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2311 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2312 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2313 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2314 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2315 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2316 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2317 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2318 ba4f502b 2018-08-04 stsp done:
2319 1a76625f 2018-10-22 stsp if (err)
2320 1a76625f 2018-10-22 stsp close_log_view(view);
2321 ba4f502b 2018-08-04 stsp return err;
2322 ba4f502b 2018-08-04 stsp }
2323 ba4f502b 2018-08-04 stsp
2324 e5a0f69f 2018-08-18 stsp static const struct got_error *
2325 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2326 ba4f502b 2018-08-04 stsp {
2327 f2f6d207 2020-11-24 stsp const struct got_error *err;
2328 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2329 ba4f502b 2018-08-04 stsp
2330 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2331 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2332 2b380cc8 2018-10-24 stsp &s->thread_args);
2333 2b380cc8 2018-10-24 stsp if (errcode)
2334 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2335 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2336 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2337 f2f6d207 2020-11-24 stsp if (err)
2338 f2f6d207 2020-11-24 stsp return err;
2339 f2f6d207 2020-11-24 stsp }
2340 2b380cc8 2018-10-24 stsp }
2341 2b380cc8 2018-10-24 stsp
2342 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2343 e5a0f69f 2018-08-18 stsp }
2344 04cc582a 2018-08-01 stsp
2345 e5a0f69f 2018-08-18 stsp static const struct got_error *
2346 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2347 e5a0f69f 2018-08-18 stsp {
2348 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2349 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2350 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2351 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2352 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2353 669b5ffa 2018-10-07 stsp int begin_x = 0;
2354 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2355 80ddbec8 2018-04-29 stsp
2356 e5a0f69f 2018-08-18 stsp switch (ch) {
2357 1e37a5c2 2019-05-12 jcs case 'q':
2358 1e37a5c2 2019-05-12 jcs s->quit = 1;
2359 1e37a5c2 2019-05-12 jcs break;
2360 1e37a5c2 2019-05-12 jcs case 'k':
2361 1e37a5c2 2019-05-12 jcs case KEY_UP:
2362 1e37a5c2 2019-05-12 jcs case '<':
2363 1e37a5c2 2019-05-12 jcs case ',':
2364 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2365 1a76625f 2018-10-22 stsp break;
2366 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2367 1e37a5c2 2019-05-12 jcs s->selected--;
2368 1144d21a 2019-06-21 stsp else
2369 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2370 2b779855 2020-12-05 naddy select_commit(s);
2371 1e37a5c2 2019-05-12 jcs break;
2372 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2373 a4292ac5 2019-05-12 jcs case CTRL('b'):
2374 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2375 e5a0f69f 2018-08-18 stsp break;
2376 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2377 1e37a5c2 2019-05-12 jcs s->selected = 0;
2378 2b779855 2020-12-05 naddy else
2379 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2380 2b779855 2020-12-05 naddy select_commit(s);
2381 1e37a5c2 2019-05-12 jcs break;
2382 1e37a5c2 2019-05-12 jcs case 'j':
2383 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2384 1e37a5c2 2019-05-12 jcs case '>':
2385 1e37a5c2 2019-05-12 jcs case '.':
2386 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2387 e5a0f69f 2018-08-18 stsp break;
2388 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2389 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2390 1e37a5c2 2019-05-12 jcs s->selected++;
2391 2b779855 2020-12-05 naddy else {
2392 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2393 2b779855 2020-12-05 naddy if (err)
2394 2b779855 2020-12-05 naddy break;
2395 80ddbec8 2018-04-29 stsp }
2396 2b779855 2020-12-05 naddy select_commit(s);
2397 1e37a5c2 2019-05-12 jcs break;
2398 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2399 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2400 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2401 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2402 1e37a5c2 2019-05-12 jcs if (first == NULL)
2403 e5a0f69f 2018-08-18 stsp break;
2404 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2405 1cae65b4 2019-09-22 stsp if (err)
2406 1cae65b4 2019-09-22 stsp break;
2407 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2408 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2409 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2410 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2411 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2412 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2413 1e37a5c2 2019-05-12 jcs }
2414 2b779855 2020-12-05 naddy select_commit(s);
2415 1e37a5c2 2019-05-12 jcs break;
2416 1e37a5c2 2019-05-12 jcs }
2417 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2418 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2419 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2420 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2421 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2422 2b779855 2020-12-05 naddy select_commit(s);
2423 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2424 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2425 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2426 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2427 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2428 0bf7f153 2020-12-02 naddy }
2429 1e37a5c2 2019-05-12 jcs break;
2430 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2431 87c7274c 2019-05-12 jcs case ' ':
2432 1e37a5c2 2019-05-12 jcs case '\r':
2433 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2434 e5a0f69f 2018-08-18 stsp break;
2435 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2436 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2437 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2438 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2439 78756c87 2020-11-24 stsp view, s->repo);
2440 1e37a5c2 2019-05-12 jcs if (err)
2441 1e37a5c2 2019-05-12 jcs break;
2442 e78dc838 2020-12-04 stsp view->focussed = 0;
2443 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2444 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2445 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2446 f7013a22 2018-10-24 stsp if (err)
2447 1e37a5c2 2019-05-12 jcs return err;
2448 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2449 e78dc838 2020-12-04 stsp view->focus_child = 1;
2450 1e37a5c2 2019-05-12 jcs } else
2451 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2452 1e37a5c2 2019-05-12 jcs break;
2453 1e37a5c2 2019-05-12 jcs case 't':
2454 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2455 5036bf37 2018-09-24 stsp break;
2456 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2457 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2458 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2459 78756c87 2020-11-24 stsp s->selected_entry, s->in_repo_path, s->repo);
2460 1e37a5c2 2019-05-12 jcs if (err)
2461 e5a0f69f 2018-08-18 stsp break;
2462 e78dc838 2020-12-04 stsp view->focussed = 0;
2463 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2464 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2465 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2466 1e37a5c2 2019-05-12 jcs if (err)
2467 1e37a5c2 2019-05-12 jcs return err;
2468 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2469 e78dc838 2020-12-04 stsp view->focus_child = 1;
2470 1e37a5c2 2019-05-12 jcs } else
2471 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2472 1e37a5c2 2019-05-12 jcs break;
2473 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2474 74cfe85e 2020-10-20 stsp if (got_path_cmp(s->in_repo_path, "/",
2475 74cfe85e 2020-10-20 stsp strlen(s->in_repo_path), 1) == 0)
2476 1e37a5c2 2019-05-12 jcs break;
2477 74cfe85e 2020-10-20 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2478 74cfe85e 2020-10-20 stsp if (err)
2479 74cfe85e 2020-10-20 stsp return err;
2480 74cfe85e 2020-10-20 stsp err = stop_log_thread(s);
2481 74cfe85e 2020-10-20 stsp if (err) {
2482 74cfe85e 2020-10-20 stsp free(parent_path);
2483 74cfe85e 2020-10-20 stsp return err;
2484 74cfe85e 2020-10-20 stsp }
2485 74cfe85e 2020-10-20 stsp lv = view_open(view->nlines, view->ncols,
2486 74cfe85e 2020-10-20 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2487 74cfe85e 2020-10-20 stsp if (lv == NULL) {
2488 74cfe85e 2020-10-20 stsp free(parent_path);
2489 74cfe85e 2020-10-20 stsp return got_error_from_errno("view_open");
2490 74cfe85e 2020-10-20 stsp }
2491 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2492 78756c87 2020-11-24 stsp parent_path, s->log_branches);
2493 74cfe85e 2020-10-20 stsp free(parent_path);
2494 74cfe85e 2020-10-20 stsp if (err)
2495 74cfe85e 2020-10-20 stsp return err;;
2496 e78dc838 2020-12-04 stsp view->focussed = 0;
2497 e78dc838 2020-12-04 stsp lv->focussed = 1;
2498 74cfe85e 2020-10-20 stsp if (view_is_parent_view(view))
2499 74cfe85e 2020-10-20 stsp *new_view = lv;
2500 e78dc838 2020-12-04 stsp else
2501 74cfe85e 2020-10-20 stsp view_set_child(view->parent, lv);
2502 1e37a5c2 2019-05-12 jcs break;
2503 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2504 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2505 d01904d4 2019-06-25 stsp if (err)
2506 d01904d4 2019-06-25 stsp return err;
2507 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2508 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2509 d01904d4 2019-06-25 stsp if (lv == NULL)
2510 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2511 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2512 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2513 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2514 ef129c5e 2019-08-03 stsp if (err) {
2515 ef129c5e 2019-08-03 stsp view_close(lv);
2516 d01904d4 2019-06-25 stsp return err;
2517 ef129c5e 2019-08-03 stsp }
2518 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2519 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2520 e6b23735 2019-10-04 stsp free(start_id);
2521 e6b23735 2019-10-04 stsp view_close(lv);
2522 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2523 e6b23735 2019-10-04 stsp }
2524 78756c87 2020-11-24 stsp err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2525 78756c87 2020-11-24 stsp in_repo_path, s->log_branches);
2526 ef129c5e 2019-08-03 stsp if (err) {
2527 ef129c5e 2019-08-03 stsp free(start_id);
2528 b672a97a 2020-01-27 stsp view_close(lv);
2529 b672a97a 2020-01-27 stsp return err;;
2530 b672a97a 2020-01-27 stsp }
2531 9970f7fc 2020-12-03 stsp view->dying = 1;
2532 b672a97a 2020-01-27 stsp *new_view = lv;
2533 b672a97a 2020-01-27 stsp break;
2534 b672a97a 2020-01-27 stsp case 'B':
2535 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2536 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2537 b672a97a 2020-01-27 stsp if (err)
2538 b672a97a 2020-01-27 stsp return err;
2539 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2540 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2541 b672a97a 2020-01-27 stsp if (lv == NULL)
2542 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2543 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo,
2544 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2545 b672a97a 2020-01-27 stsp if (err) {
2546 ef129c5e 2019-08-03 stsp view_close(lv);
2547 d01904d4 2019-06-25 stsp return err;;
2548 ef129c5e 2019-08-03 stsp }
2549 9970f7fc 2020-12-03 stsp view->dying = 1;
2550 d01904d4 2019-06-25 stsp *new_view = lv;
2551 d01904d4 2019-06-25 stsp break;
2552 6458efa5 2020-11-24 stsp case 'r':
2553 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2554 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2555 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2556 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2557 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2558 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2559 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2560 6458efa5 2020-11-24 stsp if (err) {
2561 6458efa5 2020-11-24 stsp view_close(ref_view);
2562 6458efa5 2020-11-24 stsp return err;
2563 6458efa5 2020-11-24 stsp }
2564 e78dc838 2020-12-04 stsp view->focussed = 0;
2565 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2566 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2567 6458efa5 2020-11-24 stsp err = view_close_child(view);
2568 6458efa5 2020-11-24 stsp if (err)
2569 6458efa5 2020-11-24 stsp return err;
2570 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2571 e78dc838 2020-12-04 stsp view->focus_child = 1;
2572 6458efa5 2020-11-24 stsp } else
2573 6458efa5 2020-11-24 stsp *new_view = ref_view;
2574 6458efa5 2020-11-24 stsp break;
2575 1e37a5c2 2019-05-12 jcs default:
2576 1e37a5c2 2019-05-12 jcs break;
2577 899d86c2 2018-05-10 stsp }
2578 e5a0f69f 2018-08-18 stsp
2579 80ddbec8 2018-04-29 stsp return err;
2580 80ddbec8 2018-04-29 stsp }
2581 80ddbec8 2018-04-29 stsp
2582 4ed7e80c 2018-05-20 stsp static const struct got_error *
2583 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2584 c2db6724 2019-01-04 stsp {
2585 c2db6724 2019-01-04 stsp const struct got_error *error;
2586 c2db6724 2019-01-04 stsp
2587 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2588 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2589 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2590 37c06ea4 2019-07-15 stsp #endif
2591 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2592 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2593 c2db6724 2019-01-04 stsp
2594 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2595 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2596 c2db6724 2019-01-04 stsp
2597 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2598 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2599 c2db6724 2019-01-04 stsp
2600 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2601 c2db6724 2019-01-04 stsp if (error != NULL)
2602 c2db6724 2019-01-04 stsp return error;
2603 c2db6724 2019-01-04 stsp
2604 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2605 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2606 c2db6724 2019-01-04 stsp
2607 c2db6724 2019-01-04 stsp return NULL;
2608 c2db6724 2019-01-04 stsp }
2609 c2db6724 2019-01-04 stsp
2610 a915003a 2019-02-05 stsp static void
2611 a915003a 2019-02-05 stsp init_curses(void)
2612 a915003a 2019-02-05 stsp {
2613 a915003a 2019-02-05 stsp initscr();
2614 a915003a 2019-02-05 stsp cbreak();
2615 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2616 a915003a 2019-02-05 stsp noecho();
2617 a915003a 2019-02-05 stsp nonl();
2618 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2619 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2620 a915003a 2019-02-05 stsp curs_set(0);
2621 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2622 6d17833f 2019-11-08 stsp start_color();
2623 6d17833f 2019-11-08 stsp use_default_colors();
2624 6d17833f 2019-11-08 stsp }
2625 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2626 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2627 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2628 a915003a 2019-02-05 stsp }
2629 a915003a 2019-02-05 stsp
2630 c2db6724 2019-01-04 stsp static const struct got_error *
2631 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2632 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2633 f135c941 2020-02-20 stsp {
2634 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2635 f135c941 2020-02-20 stsp
2636 f135c941 2020-02-20 stsp if (argc == 0) {
2637 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2638 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2639 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2640 f135c941 2020-02-20 stsp return NULL;
2641 f135c941 2020-02-20 stsp }
2642 f135c941 2020-02-20 stsp
2643 f135c941 2020-02-20 stsp if (worktree) {
2644 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2645 bfd61697 2020-11-14 stsp char *p;
2646 f135c941 2020-02-20 stsp
2647 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2648 f135c941 2020-02-20 stsp if (err)
2649 f135c941 2020-02-20 stsp return err;
2650 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2651 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2652 bfd61697 2020-11-14 stsp p) == -1) {
2653 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2654 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2655 f135c941 2020-02-20 stsp }
2656 f135c941 2020-02-20 stsp free(p);
2657 f135c941 2020-02-20 stsp } else
2658 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2659 f135c941 2020-02-20 stsp
2660 f135c941 2020-02-20 stsp return err;
2661 f135c941 2020-02-20 stsp }
2662 f135c941 2020-02-20 stsp
2663 f135c941 2020-02-20 stsp static const struct got_error *
2664 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2665 9f7d7167 2018-04-29 stsp {
2666 80ddbec8 2018-04-29 stsp const struct got_error *error;
2667 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2668 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2669 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2670 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2671 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2672 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2673 04cc582a 2018-08-01 stsp struct tog_view *view;
2674 80ddbec8 2018-04-29 stsp
2675 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2676 80ddbec8 2018-04-29 stsp switch (ch) {
2677 b672a97a 2020-01-27 stsp case 'b':
2678 b672a97a 2020-01-27 stsp log_branches = 1;
2679 b672a97a 2020-01-27 stsp break;
2680 80ddbec8 2018-04-29 stsp case 'c':
2681 80ddbec8 2018-04-29 stsp start_commit = optarg;
2682 80ddbec8 2018-04-29 stsp break;
2683 ecb28ae0 2018-07-16 stsp case 'r':
2684 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2685 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2686 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2687 9ba1d308 2019-10-21 stsp optarg);
2688 ecb28ae0 2018-07-16 stsp break;
2689 80ddbec8 2018-04-29 stsp default:
2690 17020d27 2019-03-07 stsp usage_log();
2691 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2692 80ddbec8 2018-04-29 stsp }
2693 80ddbec8 2018-04-29 stsp }
2694 80ddbec8 2018-04-29 stsp
2695 80ddbec8 2018-04-29 stsp argc -= optind;
2696 80ddbec8 2018-04-29 stsp argv += optind;
2697 80ddbec8 2018-04-29 stsp
2698 f135c941 2020-02-20 stsp if (argc > 1)
2699 f135c941 2020-02-20 stsp usage_log();
2700 f135c941 2020-02-20 stsp
2701 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2702 6962eb72 2020-02-20 stsp if (cwd == NULL)
2703 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2704 6962eb72 2020-02-20 stsp
2705 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2706 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2707 963f97a1 2019-03-18 stsp goto done;
2708 963f97a1 2019-03-18 stsp
2709 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2710 a1fbf39a 2019-08-11 stsp if (worktree)
2711 6962eb72 2020-02-20 stsp repo_path =
2712 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2713 a1fbf39a 2019-08-11 stsp else
2714 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2715 a1fbf39a 2019-08-11 stsp }
2716 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2717 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2718 963f97a1 2019-03-18 stsp goto done;
2719 ecb28ae0 2018-07-16 stsp }
2720 ecb28ae0 2018-07-16 stsp
2721 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2722 80ddbec8 2018-04-29 stsp if (error != NULL)
2723 ecb28ae0 2018-07-16 stsp goto done;
2724 80ddbec8 2018-04-29 stsp
2725 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2726 f135c941 2020-02-20 stsp repo, worktree);
2727 f135c941 2020-02-20 stsp if (error)
2728 f135c941 2020-02-20 stsp goto done;
2729 f135c941 2020-02-20 stsp
2730 f135c941 2020-02-20 stsp init_curses();
2731 f135c941 2020-02-20 stsp
2732 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2733 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2734 c02c541e 2019-03-29 stsp if (error)
2735 c02c541e 2019-03-29 stsp goto done;
2736 c02c541e 2019-03-29 stsp
2737 15a94983 2018-12-23 stsp if (start_commit == NULL)
2738 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2739 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2740 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2741 7a1d6b72 2020-01-15 stsp else
2742 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2743 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2744 80ddbec8 2018-04-29 stsp if (error != NULL)
2745 8b473291 2019-02-21 stsp goto done;
2746 8b473291 2019-02-21 stsp
2747 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2748 04cc582a 2018-08-01 stsp if (view == NULL) {
2749 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2750 04cc582a 2018-08-01 stsp goto done;
2751 04cc582a 2018-08-01 stsp }
2752 2fc00ff4 2019-08-31 stsp if (worktree) {
2753 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2754 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2755 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2756 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2757 2fc00ff4 2019-08-31 stsp goto done;
2758 2fc00ff4 2019-08-31 stsp }
2759 2fc00ff4 2019-08-31 stsp }
2760 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2761 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2762 ba4f502b 2018-08-04 stsp if (error)
2763 ba4f502b 2018-08-04 stsp goto done;
2764 2fc00ff4 2019-08-31 stsp if (worktree) {
2765 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2766 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2767 2fc00ff4 2019-08-31 stsp worktree = NULL;
2768 2fc00ff4 2019-08-31 stsp }
2769 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2770 ecb28ae0 2018-07-16 stsp done:
2771 f135c941 2020-02-20 stsp free(in_repo_path);
2772 ecb28ae0 2018-07-16 stsp free(repo_path);
2773 ecb28ae0 2018-07-16 stsp free(cwd);
2774 899d86c2 2018-05-10 stsp free(start_id);
2775 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2776 ecb28ae0 2018-07-16 stsp if (repo)
2777 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2778 ec142235 2019-03-07 stsp if (worktree)
2779 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2780 80ddbec8 2018-04-29 stsp return error;
2781 9f7d7167 2018-04-29 stsp }
2782 9f7d7167 2018-04-29 stsp
2783 4ed7e80c 2018-05-20 stsp __dead static void
2784 9f7d7167 2018-04-29 stsp usage_diff(void)
2785 9f7d7167 2018-04-29 stsp {
2786 80ddbec8 2018-04-29 stsp endwin();
2787 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2788 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2789 9f7d7167 2018-04-29 stsp exit(1);
2790 b304db33 2018-05-20 stsp }
2791 b304db33 2018-05-20 stsp
2792 b304db33 2018-05-20 stsp static char *
2793 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2794 b304db33 2018-05-20 stsp {
2795 b304db33 2018-05-20 stsp char *line;
2796 b304db33 2018-05-20 stsp size_t linelen;
2797 b304db33 2018-05-20 stsp size_t lineno;
2798 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2799 b304db33 2018-05-20 stsp
2800 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2801 b304db33 2018-05-20 stsp if (len)
2802 b304db33 2018-05-20 stsp *len = linelen;
2803 b304db33 2018-05-20 stsp return line;
2804 26ed57b2 2018-05-19 stsp }
2805 26ed57b2 2018-05-19 stsp
2806 6d17833f 2019-11-08 stsp static int
2807 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2808 41605754 2020-11-12 stsp regmatch_t *regmatch)
2809 6d17833f 2019-11-08 stsp {
2810 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2811 6d17833f 2019-11-08 stsp }
2812 6d17833f 2019-11-08 stsp
2813 f26dddb7 2019-11-08 stsp struct tog_color *
2814 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2815 6d17833f 2019-11-08 stsp {
2816 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2817 6d17833f 2019-11-08 stsp
2818 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2819 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2820 6d17833f 2019-11-08 stsp return tc;
2821 6d17833f 2019-11-08 stsp }
2822 6d17833f 2019-11-08 stsp
2823 6d17833f 2019-11-08 stsp return NULL;
2824 6d17833f 2019-11-08 stsp }
2825 6d17833f 2019-11-08 stsp
2826 4ed7e80c 2018-05-20 stsp static const struct got_error *
2827 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2828 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2829 41605754 2020-11-12 stsp {
2830 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2831 41605754 2020-11-12 stsp wchar_t *wline;
2832 41605754 2020-11-12 stsp int width;
2833 41605754 2020-11-12 stsp char *s;
2834 41605754 2020-11-12 stsp
2835 41605754 2020-11-12 stsp *wtotal = 0;
2836 41605754 2020-11-12 stsp
2837 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2838 41605754 2020-11-12 stsp if (s == NULL)
2839 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2840 41605754 2020-11-12 stsp
2841 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2842 41605754 2020-11-12 stsp if (err) {
2843 41605754 2020-11-12 stsp free(s);
2844 41605754 2020-11-12 stsp return err;
2845 41605754 2020-11-12 stsp }
2846 41605754 2020-11-12 stsp waddwstr(window, wline);
2847 41605754 2020-11-12 stsp free(wline);
2848 41605754 2020-11-12 stsp free(s);
2849 41605754 2020-11-12 stsp wlimit -= width;
2850 41605754 2020-11-12 stsp *wtotal += width;
2851 41605754 2020-11-12 stsp
2852 41605754 2020-11-12 stsp if (wlimit > 0) {
2853 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2854 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2855 41605754 2020-11-12 stsp if (s == NULL) {
2856 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2857 41605754 2020-11-12 stsp free(s);
2858 41605754 2020-11-12 stsp return err;
2859 41605754 2020-11-12 stsp }
2860 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2861 41605754 2020-11-12 stsp if (err) {
2862 41605754 2020-11-12 stsp free(s);
2863 41605754 2020-11-12 stsp return err;
2864 41605754 2020-11-12 stsp }
2865 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2866 41605754 2020-11-12 stsp waddwstr(window, wline);
2867 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2868 41605754 2020-11-12 stsp free(wline);
2869 41605754 2020-11-12 stsp free(s);
2870 41605754 2020-11-12 stsp wlimit -= width;
2871 41605754 2020-11-12 stsp *wtotal += width;
2872 41605754 2020-11-12 stsp }
2873 41605754 2020-11-12 stsp
2874 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2875 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2876 41605754 2020-11-12 stsp line + regmatch->rm_eo, wlimit, col_tab_align);
2877 41605754 2020-11-12 stsp if (err)
2878 41605754 2020-11-12 stsp return err;
2879 41605754 2020-11-12 stsp waddwstr(window, wline);
2880 41605754 2020-11-12 stsp free(wline);
2881 41605754 2020-11-12 stsp *wtotal += width;
2882 41605754 2020-11-12 stsp }
2883 41605754 2020-11-12 stsp
2884 41605754 2020-11-12 stsp return NULL;
2885 41605754 2020-11-12 stsp }
2886 41605754 2020-11-12 stsp
2887 41605754 2020-11-12 stsp static const struct got_error *
2888 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2889 26ed57b2 2018-05-19 stsp {
2890 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2891 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2892 61e69b96 2018-05-20 stsp const struct got_error *err;
2893 fe621944 2020-11-10 stsp int nprinted = 0;
2894 b304db33 2018-05-20 stsp char *line;
2895 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2896 b304db33 2018-05-20 stsp size_t len;
2897 61e69b96 2018-05-20 stsp wchar_t *wline;
2898 e0b650dd 2018-05-20 stsp int width;
2899 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2900 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2901 fe621944 2020-11-10 stsp off_t line_offset;
2902 26ed57b2 2018-05-19 stsp
2903 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2904 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2905 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2906 fe621944 2020-11-10 stsp
2907 f7d12f7e 2018-08-01 stsp werase(view->window);
2908 a3404814 2018-09-02 stsp
2909 a3404814 2018-09-02 stsp if (header) {
2910 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2911 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2912 135a2da0 2020-11-11 stsp header) == -1)
2913 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2914 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2915 135a2da0 2020-11-11 stsp free(line);
2916 135a2da0 2020-11-11 stsp if (err)
2917 a3404814 2018-09-02 stsp return err;
2918 a3404814 2018-09-02 stsp
2919 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2920 a3404814 2018-09-02 stsp wstandout(view->window);
2921 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2922 e54cc94a 2020-11-11 stsp free(wline);
2923 e54cc94a 2020-11-11 stsp wline = NULL;
2924 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2925 a3404814 2018-09-02 stsp wstandend(view->window);
2926 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2927 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2928 26ed57b2 2018-05-19 stsp
2929 a3404814 2018-09-02 stsp if (max_lines <= 1)
2930 a3404814 2018-09-02 stsp return NULL;
2931 a3404814 2018-09-02 stsp max_lines--;
2932 a3404814 2018-09-02 stsp }
2933 a3404814 2018-09-02 stsp
2934 89f1a395 2020-12-01 naddy s->eof = 0;
2935 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2936 89f1a395 2020-12-01 naddy line = parse_next_line(s->f, &len);
2937 26ed57b2 2018-05-19 stsp if (line == NULL) {
2938 89f1a395 2020-12-01 naddy s->eof = 1;
2939 26ed57b2 2018-05-19 stsp break;
2940 61e69b96 2018-05-20 stsp }
2941 6d17833f 2019-11-08 stsp
2942 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
2943 f26dddb7 2019-11-08 stsp if (tc)
2944 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2945 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2946 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
2947 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2948 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2949 41605754 2020-11-12 stsp view->window, regmatch);
2950 41605754 2020-11-12 stsp if (err) {
2951 41605754 2020-11-12 stsp free(line);
2952 41605754 2020-11-12 stsp return err;
2953 41605754 2020-11-12 stsp }
2954 41605754 2020-11-12 stsp } else {
2955 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2956 41605754 2020-11-12 stsp if (err) {
2957 41605754 2020-11-12 stsp free(line);
2958 41605754 2020-11-12 stsp return err;
2959 41605754 2020-11-12 stsp }
2960 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2961 41605754 2020-11-12 stsp free(wline);
2962 41605754 2020-11-12 stsp wline = NULL;
2963 41605754 2020-11-12 stsp }
2964 f26dddb7 2019-11-08 stsp if (tc)
2965 6d17833f 2019-11-08 stsp wattr_off(view->window,
2966 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2967 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2968 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2969 fe621944 2020-11-10 stsp nprinted++;
2970 26ed57b2 2018-05-19 stsp free(line);
2971 26ed57b2 2018-05-19 stsp }
2972 fe621944 2020-11-10 stsp if (nprinted >= 1)
2973 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
2974 89f1a395 2020-12-01 naddy (nprinted - 1);
2975 fe621944 2020-11-10 stsp else
2976 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
2977 26ed57b2 2018-05-19 stsp
2978 1a57306a 2018-09-02 stsp view_vborder(view);
2979 c3e9aa98 2019-05-13 jcs
2980 89f1a395 2020-12-01 naddy if (s->eof) {
2981 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2982 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2983 c3e9aa98 2019-05-13 jcs nprinted++;
2984 c3e9aa98 2019-05-13 jcs }
2985 c3e9aa98 2019-05-13 jcs
2986 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2987 c3e9aa98 2019-05-13 jcs if (err) {
2988 c3e9aa98 2019-05-13 jcs return err;
2989 c3e9aa98 2019-05-13 jcs }
2990 26ed57b2 2018-05-19 stsp
2991 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2992 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2993 e54cc94a 2020-11-11 stsp free(wline);
2994 e54cc94a 2020-11-11 stsp wline = NULL;
2995 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2996 c3e9aa98 2019-05-13 jcs }
2997 c3e9aa98 2019-05-13 jcs
2998 26ed57b2 2018-05-19 stsp return NULL;
2999 abd2672a 2018-12-23 stsp }
3000 abd2672a 2018-12-23 stsp
3001 abd2672a 2018-12-23 stsp static char *
3002 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3003 abd2672a 2018-12-23 stsp {
3004 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3005 09867e48 2019-08-13 stsp char *p, *s;
3006 09867e48 2019-08-13 stsp
3007 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3008 09867e48 2019-08-13 stsp if (tm == NULL)
3009 09867e48 2019-08-13 stsp return NULL;
3010 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3011 09867e48 2019-08-13 stsp if (s == NULL)
3012 09867e48 2019-08-13 stsp return NULL;
3013 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3014 abd2672a 2018-12-23 stsp if (p)
3015 abd2672a 2018-12-23 stsp *p = '\0';
3016 abd2672a 2018-12-23 stsp return s;
3017 9f7d7167 2018-04-29 stsp }
3018 9f7d7167 2018-04-29 stsp
3019 4ed7e80c 2018-05-20 stsp static const struct got_error *
3020 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3021 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3022 0208f208 2020-05-05 stsp {
3023 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3024 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3025 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3026 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3027 0208f208 2020-05-05 stsp
3028 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3029 0208f208 2020-05-05 stsp if (qid != NULL) {
3030 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3031 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3032 0208f208 2020-05-05 stsp qid->id);
3033 0208f208 2020-05-05 stsp if (err)
3034 0208f208 2020-05-05 stsp return err;
3035 0208f208 2020-05-05 stsp
3036 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3037 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3038 0208f208 2020-05-05 stsp
3039 0208f208 2020-05-05 stsp }
3040 0208f208 2020-05-05 stsp
3041 0208f208 2020-05-05 stsp if (tree_id1) {
3042 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3043 0208f208 2020-05-05 stsp if (err)
3044 0208f208 2020-05-05 stsp goto done;
3045 0208f208 2020-05-05 stsp }
3046 0208f208 2020-05-05 stsp
3047 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3048 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3049 0208f208 2020-05-05 stsp if (err)
3050 0208f208 2020-05-05 stsp goto done;
3051 0208f208 2020-05-05 stsp
3052 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3053 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3054 0208f208 2020-05-05 stsp done:
3055 0208f208 2020-05-05 stsp if (tree1)
3056 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3057 0208f208 2020-05-05 stsp if (tree2)
3058 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3059 0208f208 2020-05-05 stsp return err;
3060 0208f208 2020-05-05 stsp }
3061 0208f208 2020-05-05 stsp
3062 0208f208 2020-05-05 stsp static const struct got_error *
3063 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3064 abd2672a 2018-12-23 stsp {
3065 fe621944 2020-11-10 stsp off_t *p;
3066 fe621944 2020-11-10 stsp
3067 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3068 fe621944 2020-11-10 stsp if (p == NULL)
3069 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3070 fe621944 2020-11-10 stsp *line_offsets = p;
3071 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3072 fe621944 2020-11-10 stsp (*nlines)++;
3073 fe621944 2020-11-10 stsp return NULL;
3074 fe621944 2020-11-10 stsp }
3075 fe621944 2020-11-10 stsp
3076 fe621944 2020-11-10 stsp static const struct got_error *
3077 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3078 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3079 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3080 fe621944 2020-11-10 stsp {
3081 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3082 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3083 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3084 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3085 45d799e2 2018-12-23 stsp time_t committer_time;
3086 45d799e2 2018-12-23 stsp const char *author, *committer;
3087 8b473291 2019-02-21 stsp char *refs_str = NULL;
3088 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3089 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3090 fe621944 2020-11-10 stsp off_t outoff = 0;
3091 fe621944 2020-11-10 stsp int n;
3092 abd2672a 2018-12-23 stsp
3093 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3094 0208f208 2020-05-05 stsp
3095 8b473291 2019-02-21 stsp if (refs) {
3096 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3097 8b473291 2019-02-21 stsp if (err)
3098 8b473291 2019-02-21 stsp return err;
3099 8b473291 2019-02-21 stsp }
3100 8b473291 2019-02-21 stsp
3101 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3102 abd2672a 2018-12-23 stsp if (err)
3103 abd2672a 2018-12-23 stsp return err;
3104 abd2672a 2018-12-23 stsp
3105 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3106 15a94983 2018-12-23 stsp if (err) {
3107 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3108 15a94983 2018-12-23 stsp goto done;
3109 15a94983 2018-12-23 stsp }
3110 abd2672a 2018-12-23 stsp
3111 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3112 fe621944 2020-11-10 stsp if (err)
3113 fe621944 2020-11-10 stsp goto done;
3114 fe621944 2020-11-10 stsp
3115 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3116 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3117 fe621944 2020-11-10 stsp if (n < 0) {
3118 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3119 abd2672a 2018-12-23 stsp goto done;
3120 abd2672a 2018-12-23 stsp }
3121 fe621944 2020-11-10 stsp outoff += n;
3122 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3123 fe621944 2020-11-10 stsp if (err)
3124 fe621944 2020-11-10 stsp goto done;
3125 fe621944 2020-11-10 stsp
3126 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3127 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3128 fe621944 2020-11-10 stsp if (n < 0) {
3129 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3130 abd2672a 2018-12-23 stsp goto done;
3131 abd2672a 2018-12-23 stsp }
3132 fe621944 2020-11-10 stsp outoff += n;
3133 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3134 fe621944 2020-11-10 stsp if (err)
3135 fe621944 2020-11-10 stsp goto done;
3136 fe621944 2020-11-10 stsp
3137 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3138 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3139 fe621944 2020-11-10 stsp if (datestr) {
3140 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3141 fe621944 2020-11-10 stsp if (n < 0) {
3142 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3143 fe621944 2020-11-10 stsp goto done;
3144 fe621944 2020-11-10 stsp }
3145 fe621944 2020-11-10 stsp outoff += n;
3146 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3147 fe621944 2020-11-10 stsp if (err)
3148 fe621944 2020-11-10 stsp goto done;
3149 abd2672a 2018-12-23 stsp }
3150 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3151 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3152 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3153 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3154 fe621944 2020-11-10 stsp if (n < 0) {
3155 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3156 fe621944 2020-11-10 stsp goto done;
3157 fe621944 2020-11-10 stsp }
3158 fe621944 2020-11-10 stsp outoff += n;
3159 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3160 fe621944 2020-11-10 stsp if (err)
3161 fe621944 2020-11-10 stsp goto done;
3162 abd2672a 2018-12-23 stsp }
3163 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3164 5943eee2 2019-08-13 stsp if (err)
3165 5943eee2 2019-08-13 stsp goto done;
3166 fe621944 2020-11-10 stsp s = logmsg;
3167 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3168 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3169 fe621944 2020-11-10 stsp if (n < 0) {
3170 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3171 fe621944 2020-11-10 stsp goto done;
3172 fe621944 2020-11-10 stsp }
3173 fe621944 2020-11-10 stsp outoff += n;
3174 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3175 fe621944 2020-11-10 stsp if (err)
3176 fe621944 2020-11-10 stsp goto done;
3177 abd2672a 2018-12-23 stsp }
3178 fe621944 2020-11-10 stsp
3179 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3180 0208f208 2020-05-05 stsp if (err)
3181 0208f208 2020-05-05 stsp goto done;
3182 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3183 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3184 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3185 fe621944 2020-11-10 stsp if (n < 0) {
3186 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3187 fe621944 2020-11-10 stsp goto done;
3188 fe621944 2020-11-10 stsp }
3189 fe621944 2020-11-10 stsp outoff += n;
3190 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3191 fe621944 2020-11-10 stsp if (err)
3192 fe621944 2020-11-10 stsp goto done;
3193 0208f208 2020-05-05 stsp free((char *)pe->path);
3194 0208f208 2020-05-05 stsp free(pe->data);
3195 0208f208 2020-05-05 stsp }
3196 fe621944 2020-11-10 stsp
3197 0208f208 2020-05-05 stsp fputc('\n', outfile);
3198 fe621944 2020-11-10 stsp outoff++;
3199 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3200 abd2672a 2018-12-23 stsp done:
3201 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3202 abd2672a 2018-12-23 stsp free(id_str);
3203 5943eee2 2019-08-13 stsp free(logmsg);
3204 8b473291 2019-02-21 stsp free(refs_str);
3205 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3206 7510f233 2020-08-09 stsp if (err) {
3207 ae6a6978 2020-08-09 stsp free(*line_offsets);
3208 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3209 ae6a6978 2020-08-09 stsp *nlines = 0;
3210 7510f233 2020-08-09 stsp }
3211 fe621944 2020-11-10 stsp return err;
3212 abd2672a 2018-12-23 stsp }
3213 abd2672a 2018-12-23 stsp
3214 abd2672a 2018-12-23 stsp static const struct got_error *
3215 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3216 26ed57b2 2018-05-19 stsp {
3217 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3218 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3219 15a94983 2018-12-23 stsp int obj_type;
3220 fe621944 2020-11-10 stsp
3221 fe621944 2020-11-10 stsp free(s->line_offsets);
3222 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3223 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3224 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3225 fe621944 2020-11-10 stsp s->nlines = 0;
3226 26ed57b2 2018-05-19 stsp
3227 511a516b 2018-05-19 stsp f = got_opentemp();
3228 48ae06ee 2018-10-18 stsp if (f == NULL) {
3229 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3230 48ae06ee 2018-10-18 stsp goto done;
3231 48ae06ee 2018-10-18 stsp }
3232 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3233 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3234 fb43ecf1 2019-02-11 stsp goto done;
3235 fb43ecf1 2019-02-11 stsp }
3236 48ae06ee 2018-10-18 stsp s->f = f;
3237 26ed57b2 2018-05-19 stsp
3238 15a94983 2018-12-23 stsp if (s->id1)
3239 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3240 15a94983 2018-12-23 stsp else
3241 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3242 15a94983 2018-12-23 stsp if (err)
3243 15a94983 2018-12-23 stsp goto done;
3244 15a94983 2018-12-23 stsp
3245 15a94983 2018-12-23 stsp switch (obj_type) {
3246 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3247 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3248 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3249 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3250 26ed57b2 2018-05-19 stsp break;
3251 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3252 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3253 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3254 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3255 26ed57b2 2018-05-19 stsp break;
3256 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3257 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3258 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3259 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3260 abd2672a 2018-12-23 stsp
3261 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3262 abd2672a 2018-12-23 stsp if (err)
3263 3ffacbe1 2020-02-02 tracey goto done;
3264 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3265 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3266 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3267 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3268 f44b1f58 2020-02-02 tracey if (err)
3269 f44b1f58 2020-02-02 tracey goto done;
3270 f44b1f58 2020-02-02 tracey } else {
3271 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3272 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3273 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3274 fe621944 2020-11-10 stsp err = write_commit_info(
3275 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3276 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3277 f44b1f58 2020-02-02 tracey if (err)
3278 f44b1f58 2020-02-02 tracey goto done;
3279 f5404e4e 2020-02-02 tracey break;
3280 15a087fe 2019-02-21 stsp }
3281 abd2672a 2018-12-23 stsp }
3282 abd2672a 2018-12-23 stsp }
3283 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3284 abd2672a 2018-12-23 stsp
3285 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3286 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3287 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3288 26ed57b2 2018-05-19 stsp break;
3289 abd2672a 2018-12-23 stsp }
3290 26ed57b2 2018-05-19 stsp default:
3291 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3292 48ae06ee 2018-10-18 stsp break;
3293 26ed57b2 2018-05-19 stsp }
3294 f44b1f58 2020-02-02 tracey if (err)
3295 f44b1f58 2020-02-02 tracey goto done;
3296 48ae06ee 2018-10-18 stsp done:
3297 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3298 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3299 48ae06ee 2018-10-18 stsp return err;
3300 48ae06ee 2018-10-18 stsp }
3301 26ed57b2 2018-05-19 stsp
3302 f5215bb9 2019-02-22 stsp static void
3303 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3304 f5215bb9 2019-02-22 stsp {
3305 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3306 f5215bb9 2019-02-22 stsp update_panels();
3307 f5215bb9 2019-02-22 stsp doupdate();
3308 f44b1f58 2020-02-02 tracey }
3309 f44b1f58 2020-02-02 tracey
3310 f44b1f58 2020-02-02 tracey static const struct got_error *
3311 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3312 f44b1f58 2020-02-02 tracey {
3313 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3314 f44b1f58 2020-02-02 tracey
3315 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3316 f44b1f58 2020-02-02 tracey return NULL;
3317 f44b1f58 2020-02-02 tracey }
3318 f44b1f58 2020-02-02 tracey
3319 f44b1f58 2020-02-02 tracey static const struct got_error *
3320 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3321 f44b1f58 2020-02-02 tracey {
3322 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3323 f44b1f58 2020-02-02 tracey int lineno;
3324 f44b1f58 2020-02-02 tracey
3325 f44b1f58 2020-02-02 tracey if (!view->searching) {
3326 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3327 f44b1f58 2020-02-02 tracey return NULL;
3328 f44b1f58 2020-02-02 tracey }
3329 f44b1f58 2020-02-02 tracey
3330 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3331 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3332 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3333 f44b1f58 2020-02-02 tracey else
3334 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3335 f44b1f58 2020-02-02 tracey } else {
3336 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3337 f44b1f58 2020-02-02 tracey lineno = 1;
3338 f44b1f58 2020-02-02 tracey else
3339 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3340 f44b1f58 2020-02-02 tracey }
3341 f44b1f58 2020-02-02 tracey
3342 f44b1f58 2020-02-02 tracey while (1) {
3343 f44b1f58 2020-02-02 tracey char *line = NULL;
3344 f44b1f58 2020-02-02 tracey off_t offset;
3345 f44b1f58 2020-02-02 tracey size_t len;
3346 f44b1f58 2020-02-02 tracey
3347 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3348 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3349 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3350 f44b1f58 2020-02-02 tracey free(line);
3351 f44b1f58 2020-02-02 tracey break;
3352 f44b1f58 2020-02-02 tracey }
3353 f44b1f58 2020-02-02 tracey
3354 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3355 f44b1f58 2020-02-02 tracey lineno = 1;
3356 f44b1f58 2020-02-02 tracey else
3357 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3358 f44b1f58 2020-02-02 tracey }
3359 f44b1f58 2020-02-02 tracey
3360 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3361 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3362 f44b1f58 2020-02-02 tracey free(line);
3363 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3364 f44b1f58 2020-02-02 tracey }
3365 f44b1f58 2020-02-02 tracey free(line);
3366 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3367 41605754 2020-11-12 stsp if (line &&
3368 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3369 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3370 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3371 f44b1f58 2020-02-02 tracey free(line);
3372 f44b1f58 2020-02-02 tracey break;
3373 f44b1f58 2020-02-02 tracey }
3374 f44b1f58 2020-02-02 tracey free(line);
3375 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3376 f44b1f58 2020-02-02 tracey lineno++;
3377 f44b1f58 2020-02-02 tracey else
3378 f44b1f58 2020-02-02 tracey lineno--;
3379 f44b1f58 2020-02-02 tracey }
3380 f44b1f58 2020-02-02 tracey
3381 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3382 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3383 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3384 f44b1f58 2020-02-02 tracey }
3385 f44b1f58 2020-02-02 tracey
3386 f44b1f58 2020-02-02 tracey return NULL;
3387 f5215bb9 2019-02-22 stsp }
3388 f5215bb9 2019-02-22 stsp
3389 48ae06ee 2018-10-18 stsp static const struct got_error *
3390 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3391 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3392 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3393 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3394 48ae06ee 2018-10-18 stsp {
3395 48ae06ee 2018-10-18 stsp const struct got_error *err;
3396 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3397 5dc9f4bc 2018-08-04 stsp
3398 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
3399 78756c87 2020-11-24 stsp
3400 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3401 15a94983 2018-12-23 stsp int type1, type2;
3402 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3403 15a94983 2018-12-23 stsp if (err)
3404 15a94983 2018-12-23 stsp return err;
3405 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3406 15a94983 2018-12-23 stsp if (err)
3407 15a94983 2018-12-23 stsp return err;
3408 15a94983 2018-12-23 stsp
3409 15a94983 2018-12-23 stsp if (type1 != type2)
3410 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3411 15a94983 2018-12-23 stsp }
3412 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3413 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3414 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3415 f44b1f58 2020-02-02 tracey s->repo = repo;
3416 f44b1f58 2020-02-02 tracey s->id1 = id1;
3417 f44b1f58 2020-02-02 tracey s->id2 = id2;
3418 3dbaef42 2020-11-24 stsp s->label1 = label1;
3419 3dbaef42 2020-11-24 stsp s->label2 = label2;
3420 48ae06ee 2018-10-18 stsp
3421 15a94983 2018-12-23 stsp if (id1) {
3422 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3423 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3424 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3425 48ae06ee 2018-10-18 stsp } else
3426 5465d566 2020-02-01 tracey s->id1 = NULL;
3427 48ae06ee 2018-10-18 stsp
3428 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3429 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3430 5465d566 2020-02-01 tracey free(s->id1);
3431 5465d566 2020-02-01 tracey s->id1 = NULL;
3432 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3433 48ae06ee 2018-10-18 stsp }
3434 5465d566 2020-02-01 tracey s->f = NULL;
3435 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3436 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3437 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3438 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3439 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3440 5465d566 2020-02-01 tracey s->log_view = log_view;
3441 5465d566 2020-02-01 tracey s->repo = repo;
3442 6d17833f 2019-11-08 stsp
3443 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3444 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3445 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3446 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3447 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3448 6d17833f 2019-11-08 stsp if (err)
3449 6d17833f 2019-11-08 stsp return err;
3450 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3451 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3452 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3453 6d17833f 2019-11-08 stsp if (err) {
3454 5465d566 2020-02-01 tracey free_colors(&s->colors);
3455 6d17833f 2019-11-08 stsp return err;
3456 6d17833f 2019-11-08 stsp }
3457 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3458 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3459 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3460 6d17833f 2019-11-08 stsp if (err) {
3461 5465d566 2020-02-01 tracey free_colors(&s->colors);
3462 6d17833f 2019-11-08 stsp return err;
3463 6d17833f 2019-11-08 stsp }
3464 6d17833f 2019-11-08 stsp
3465 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3466 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3467 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3468 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3469 6d17833f 2019-11-08 stsp if (err) {
3470 5465d566 2020-02-01 tracey free_colors(&s->colors);
3471 6d17833f 2019-11-08 stsp return err;
3472 6d17833f 2019-11-08 stsp }
3473 11b20872 2019-11-08 stsp
3474 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3475 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3476 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3477 11b20872 2019-11-08 stsp if (err) {
3478 5465d566 2020-02-01 tracey free_colors(&s->colors);
3479 11b20872 2019-11-08 stsp return err;
3480 11b20872 2019-11-08 stsp }
3481 11b20872 2019-11-08 stsp
3482 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3483 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3484 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3485 11b20872 2019-11-08 stsp if (err) {
3486 5465d566 2020-02-01 tracey free_colors(&s->colors);
3487 11b20872 2019-11-08 stsp return err;
3488 11b20872 2019-11-08 stsp }
3489 6d17833f 2019-11-08 stsp }
3490 5dc9f4bc 2018-08-04 stsp
3491 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3492 78756c87 2020-11-24 stsp if (err) {
3493 78756c87 2020-11-24 stsp free(s->id1);
3494 78756c87 2020-11-24 stsp s->id1 = NULL;
3495 78756c87 2020-11-24 stsp free(s->id2);
3496 78756c87 2020-11-24 stsp s->id2 = NULL;
3497 78756c87 2020-11-24 stsp free_colors(&s->colors);
3498 78756c87 2020-11-24 stsp return err;
3499 78756c87 2020-11-24 stsp }
3500 78756c87 2020-11-24 stsp
3501 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3502 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3503 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3504 f5215bb9 2019-02-22 stsp
3505 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3506 fe621944 2020-11-10 stsp s->nlines = 0;
3507 5465d566 2020-02-01 tracey err = create_diff(s);
3508 48ae06ee 2018-10-18 stsp if (err) {
3509 5465d566 2020-02-01 tracey free(s->id1);
3510 5465d566 2020-02-01 tracey s->id1 = NULL;
3511 5465d566 2020-02-01 tracey free(s->id2);
3512 5465d566 2020-02-01 tracey s->id2 = NULL;
3513 78756c87 2020-11-24 stsp free_colors(&s->colors);
3514 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3515 48ae06ee 2018-10-18 stsp return err;
3516 48ae06ee 2018-10-18 stsp }
3517 48ae06ee 2018-10-18 stsp
3518 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3519 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3520 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3521 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3522 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3523 e5a0f69f 2018-08-18 stsp
3524 5dc9f4bc 2018-08-04 stsp return NULL;
3525 5dc9f4bc 2018-08-04 stsp }
3526 5dc9f4bc 2018-08-04 stsp
3527 e5a0f69f 2018-08-18 stsp static const struct got_error *
3528 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3529 5dc9f4bc 2018-08-04 stsp {
3530 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3531 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3532 5465d566 2020-02-01 tracey
3533 5465d566 2020-02-01 tracey free(s->id1);
3534 5465d566 2020-02-01 tracey s->id1 = NULL;
3535 5465d566 2020-02-01 tracey free(s->id2);
3536 5465d566 2020-02-01 tracey s->id2 = NULL;
3537 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3538 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3539 5465d566 2020-02-01 tracey free_colors(&s->colors);
3540 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3541 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3542 fe621944 2020-11-10 stsp s->nlines = 0;
3543 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3544 e5a0f69f 2018-08-18 stsp return err;
3545 5dc9f4bc 2018-08-04 stsp }
3546 5dc9f4bc 2018-08-04 stsp
3547 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3548 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3549 5dc9f4bc 2018-08-04 stsp {
3550 a3404814 2018-09-02 stsp const struct got_error *err;
3551 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3552 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3553 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3554 a3404814 2018-09-02 stsp
3555 a3404814 2018-09-02 stsp if (s->id1) {
3556 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3557 a3404814 2018-09-02 stsp if (err)
3558 a3404814 2018-09-02 stsp return err;
3559 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3560 3dbaef42 2020-11-24 stsp } else
3561 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3562 3dbaef42 2020-11-24 stsp
3563 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3564 a3404814 2018-09-02 stsp if (err)
3565 a3404814 2018-09-02 stsp return err;
3566 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3567 26ed57b2 2018-05-19 stsp
3568 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3570 a3404814 2018-09-02 stsp free(id_str1);
3571 a3404814 2018-09-02 stsp free(id_str2);
3572 a3404814 2018-09-02 stsp return err;
3573 a3404814 2018-09-02 stsp }
3574 a3404814 2018-09-02 stsp free(id_str1);
3575 a3404814 2018-09-02 stsp free(id_str2);
3576 a3404814 2018-09-02 stsp
3577 89f1a395 2020-12-01 naddy return draw_file(view, header);
3578 15a087fe 2019-02-21 stsp }
3579 15a087fe 2019-02-21 stsp
3580 15a087fe 2019-02-21 stsp static const struct got_error *
3581 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3582 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3583 15a087fe 2019-02-21 stsp {
3584 d7a04538 2019-02-21 stsp const struct got_error *err;
3585 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3586 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3587 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3588 15a087fe 2019-02-21 stsp
3589 15a087fe 2019-02-21 stsp free(s->id2);
3590 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3591 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3592 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3593 15a087fe 2019-02-21 stsp
3594 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3595 d7a04538 2019-02-21 stsp if (err)
3596 d7a04538 2019-02-21 stsp return err;
3597 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3598 15a087fe 2019-02-21 stsp free(s->id1);
3599 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3600 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3601 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3602 15a087fe 2019-02-21 stsp return NULL;
3603 0cf4efb1 2018-09-29 stsp }
3604 0cf4efb1 2018-09-29 stsp
3605 0cf4efb1 2018-09-29 stsp static const struct got_error *
3606 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3607 e5a0f69f 2018-08-18 stsp {
3608 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3609 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3610 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3611 e5a0f69f 2018-08-18 stsp int i;
3612 e5a0f69f 2018-08-18 stsp
3613 e5a0f69f 2018-08-18 stsp switch (ch) {
3614 64453f7e 2020-11-21 stsp case 'a':
3615 3dbaef42 2020-11-24 stsp case 'w':
3616 3dbaef42 2020-11-24 stsp if (ch == 'a')
3617 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3618 3dbaef42 2020-11-24 stsp if (ch == 'w')
3619 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3620 64453f7e 2020-11-21 stsp wclear(view->window);
3621 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3622 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3623 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3624 64453f7e 2020-11-21 stsp err = create_diff(s);
3625 64453f7e 2020-11-21 stsp break;
3626 1e37a5c2 2019-05-12 jcs case 'k':
3627 1e37a5c2 2019-05-12 jcs case KEY_UP:
3628 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3629 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3630 1e37a5c2 2019-05-12 jcs break;
3631 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3632 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3633 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3634 26ed57b2 2018-05-19 stsp break;
3635 1e37a5c2 2019-05-12 jcs i = 0;
3636 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3637 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3638 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3639 1e37a5c2 2019-05-12 jcs break;
3640 1e37a5c2 2019-05-12 jcs case 'j':
3641 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3642 1e37a5c2 2019-05-12 jcs if (!s->eof)
3643 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3644 1e37a5c2 2019-05-12 jcs break;
3645 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3646 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3647 1e37a5c2 2019-05-12 jcs case ' ':
3648 00ba99a7 2019-05-12 jcs if (s->eof)
3649 1e37a5c2 2019-05-12 jcs break;
3650 1e37a5c2 2019-05-12 jcs i = 0;
3651 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3652 1e37a5c2 2019-05-12 jcs char *line;
3653 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3654 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3655 1e37a5c2 2019-05-12 jcs if (line == NULL)
3656 34bc9ec9 2019-02-22 stsp break;
3657 1e37a5c2 2019-05-12 jcs }
3658 1e37a5c2 2019-05-12 jcs break;
3659 1e37a5c2 2019-05-12 jcs case '[':
3660 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3661 1e37a5c2 2019-05-12 jcs s->diff_context--;
3662 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3663 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3664 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3665 27829c9e 2020-11-21 stsp s->nlines) {
3666 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3667 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3668 27829c9e 2020-11-21 stsp }
3669 1e37a5c2 2019-05-12 jcs }
3670 1e37a5c2 2019-05-12 jcs break;
3671 1e37a5c2 2019-05-12 jcs case ']':
3672 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3673 1e37a5c2 2019-05-12 jcs s->diff_context++;
3674 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3675 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3676 1e37a5c2 2019-05-12 jcs }
3677 1e37a5c2 2019-05-12 jcs break;
3678 1e37a5c2 2019-05-12 jcs case '<':
3679 1e37a5c2 2019-05-12 jcs case ',':
3680 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3681 48ae06ee 2018-10-18 stsp break;
3682 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3683 6524637e 2019-02-21 stsp
3684 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3685 1e37a5c2 2019-05-12 jcs if (err)
3686 1e37a5c2 2019-05-12 jcs break;
3687 15a087fe 2019-02-21 stsp
3688 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3689 1e37a5c2 2019-05-12 jcs if (err)
3690 1e37a5c2 2019-05-12 jcs break;
3691 15a087fe 2019-02-21 stsp
3692 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3693 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3694 15a087fe 2019-02-21 stsp
3695 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3696 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3697 1e37a5c2 2019-05-12 jcs break;
3698 1e37a5c2 2019-05-12 jcs case '>':
3699 1e37a5c2 2019-05-12 jcs case '.':
3700 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3701 15a087fe 2019-02-21 stsp break;
3702 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3703 5e224a3e 2019-02-22 stsp
3704 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3705 1e37a5c2 2019-05-12 jcs if (err)
3706 1e37a5c2 2019-05-12 jcs break;
3707 15a087fe 2019-02-21 stsp
3708 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3709 1e37a5c2 2019-05-12 jcs if (err)
3710 1e37a5c2 2019-05-12 jcs break;
3711 15a087fe 2019-02-21 stsp
3712 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3713 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3714 1e37a5c2 2019-05-12 jcs
3715 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3716 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3717 1e37a5c2 2019-05-12 jcs break;
3718 1e37a5c2 2019-05-12 jcs default:
3719 1e37a5c2 2019-05-12 jcs break;
3720 26ed57b2 2018-05-19 stsp }
3721 e5a0f69f 2018-08-18 stsp
3722 bcbd79e2 2018-08-19 stsp return err;
3723 26ed57b2 2018-05-19 stsp }
3724 26ed57b2 2018-05-19 stsp
3725 4ed7e80c 2018-05-20 stsp static const struct got_error *
3726 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3727 9f7d7167 2018-04-29 stsp {
3728 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3729 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3730 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3731 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3732 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3733 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3734 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3735 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3736 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3737 3dbaef42 2020-11-24 stsp const char *errstr;
3738 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3739 70ac5f84 2019-03-28 stsp
3740 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3741 26ed57b2 2018-05-19 stsp switch (ch) {
3742 64453f7e 2020-11-21 stsp case 'a':
3743 64453f7e 2020-11-21 stsp force_text_diff = 1;
3744 3dbaef42 2020-11-24 stsp break;
3745 3dbaef42 2020-11-24 stsp case 'C':
3746 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3747 3dbaef42 2020-11-24 stsp &errstr);
3748 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3749 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3750 64453f7e 2020-11-21 stsp break;
3751 09b5bff8 2020-02-23 naddy case 'r':
3752 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3753 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3754 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3755 09b5bff8 2020-02-23 naddy optarg);
3756 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3757 09b5bff8 2020-02-23 naddy break;
3758 3dbaef42 2020-11-24 stsp case 'w':
3759 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3760 3dbaef42 2020-11-24 stsp break;
3761 26ed57b2 2018-05-19 stsp default:
3762 17020d27 2019-03-07 stsp usage_diff();
3763 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3764 26ed57b2 2018-05-19 stsp }
3765 26ed57b2 2018-05-19 stsp }
3766 26ed57b2 2018-05-19 stsp
3767 26ed57b2 2018-05-19 stsp argc -= optind;
3768 26ed57b2 2018-05-19 stsp argv += optind;
3769 26ed57b2 2018-05-19 stsp
3770 26ed57b2 2018-05-19 stsp if (argc == 0) {
3771 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3772 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3773 15a94983 2018-12-23 stsp id_str1 = argv[0];
3774 15a94983 2018-12-23 stsp id_str2 = argv[1];
3775 26ed57b2 2018-05-19 stsp } else
3776 26ed57b2 2018-05-19 stsp usage_diff();
3777 a915003a 2019-02-05 stsp
3778 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3779 a273ac94 2020-02-23 naddy if (cwd == NULL)
3780 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3781 eb6600df 2019-01-04 stsp
3782 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3783 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3784 a273ac94 2020-02-23 naddy goto done;
3785 a273ac94 2020-02-23 naddy
3786 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3787 a273ac94 2020-02-23 naddy if (worktree)
3788 a273ac94 2020-02-23 naddy repo_path =
3789 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3790 a273ac94 2020-02-23 naddy else
3791 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3792 a273ac94 2020-02-23 naddy }
3793 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3794 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3795 a273ac94 2020-02-23 naddy goto done;
3796 a273ac94 2020-02-23 naddy }
3797 a273ac94 2020-02-23 naddy
3798 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3799 eb6600df 2019-01-04 stsp if (error)
3800 eb6600df 2019-01-04 stsp goto done;
3801 26ed57b2 2018-05-19 stsp
3802 a273ac94 2020-02-23 naddy init_curses();
3803 a273ac94 2020-02-23 naddy
3804 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3805 26ed57b2 2018-05-19 stsp if (error)
3806 26ed57b2 2018-05-19 stsp goto done;
3807 26ed57b2 2018-05-19 stsp
3808 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3809 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3810 26ed57b2 2018-05-19 stsp if (error)
3811 26ed57b2 2018-05-19 stsp goto done;
3812 26ed57b2 2018-05-19 stsp
3813 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3814 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3815 26ed57b2 2018-05-19 stsp if (error)
3816 26ed57b2 2018-05-19 stsp goto done;
3817 26ed57b2 2018-05-19 stsp
3818 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3819 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3820 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3821 ea5e7bb5 2018-08-01 stsp goto done;
3822 ea5e7bb5 2018-08-01 stsp }
3823 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3824 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3825 5dc9f4bc 2018-08-04 stsp if (error)
3826 5dc9f4bc 2018-08-04 stsp goto done;
3827 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3828 26ed57b2 2018-05-19 stsp done:
3829 3dbaef42 2020-11-24 stsp free(label1);
3830 3dbaef42 2020-11-24 stsp free(label2);
3831 c02c541e 2019-03-29 stsp free(repo_path);
3832 a273ac94 2020-02-23 naddy free(cwd);
3833 921be706 2019-06-28 stsp if (repo)
3834 921be706 2019-06-28 stsp got_repo_close(repo);
3835 a273ac94 2020-02-23 naddy if (worktree)
3836 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3837 26ed57b2 2018-05-19 stsp return error;
3838 9f7d7167 2018-04-29 stsp }
3839 9f7d7167 2018-04-29 stsp
3840 4ed7e80c 2018-05-20 stsp __dead static void
3841 9f7d7167 2018-04-29 stsp usage_blame(void)
3842 9f7d7167 2018-04-29 stsp {
3843 80ddbec8 2018-04-29 stsp endwin();
3844 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3845 9f7d7167 2018-04-29 stsp getprogname());
3846 9f7d7167 2018-04-29 stsp exit(1);
3847 9f7d7167 2018-04-29 stsp }
3848 84451b3e 2018-07-10 stsp
3849 84451b3e 2018-07-10 stsp struct tog_blame_line {
3850 84451b3e 2018-07-10 stsp int annotated;
3851 84451b3e 2018-07-10 stsp struct got_object_id *id;
3852 84451b3e 2018-07-10 stsp };
3853 9f7d7167 2018-04-29 stsp
3854 4ed7e80c 2018-05-20 stsp static const struct got_error *
3855 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3856 84451b3e 2018-07-10 stsp {
3857 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3858 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3859 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3860 84451b3e 2018-07-10 stsp const struct got_error *err;
3861 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3862 84451b3e 2018-07-10 stsp char *line;
3863 84451b3e 2018-07-10 stsp size_t len;
3864 84451b3e 2018-07-10 stsp wchar_t *wline;
3865 27a741e5 2019-09-11 stsp int width;
3866 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3867 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3868 ab089a2a 2018-07-12 stsp char *id_str;
3869 11b20872 2019-11-08 stsp struct tog_color *tc;
3870 ab089a2a 2018-07-12 stsp
3871 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3872 ab089a2a 2018-07-12 stsp if (err)
3873 ab089a2a 2018-07-12 stsp return err;
3874 84451b3e 2018-07-10 stsp
3875 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3876 f7d12f7e 2018-08-01 stsp werase(view->window);
3877 84451b3e 2018-07-10 stsp
3878 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3879 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3880 ab089a2a 2018-07-12 stsp free(id_str);
3881 ab089a2a 2018-07-12 stsp return err;
3882 ab089a2a 2018-07-12 stsp }
3883 ab089a2a 2018-07-12 stsp
3884 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3885 ab089a2a 2018-07-12 stsp free(line);
3886 2550e4c3 2018-07-13 stsp line = NULL;
3887 1cae65b4 2019-09-22 stsp if (err)
3888 1cae65b4 2019-09-22 stsp return err;
3889 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3890 a3404814 2018-09-02 stsp wstandout(view->window);
3891 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3892 11b20872 2019-11-08 stsp if (tc)
3893 11b20872 2019-11-08 stsp wattr_on(view->window,
3894 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3895 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3896 11b20872 2019-11-08 stsp if (tc)
3897 11b20872 2019-11-08 stsp wattr_off(view->window,
3898 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3899 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3900 a3404814 2018-09-02 stsp wstandend(view->window);
3901 2550e4c3 2018-07-13 stsp free(wline);
3902 2550e4c3 2018-07-13 stsp wline = NULL;
3903 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3904 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3905 ab089a2a 2018-07-12 stsp
3906 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3907 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3908 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3909 ab089a2a 2018-07-12 stsp free(id_str);
3910 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3911 ab089a2a 2018-07-12 stsp }
3912 ab089a2a 2018-07-12 stsp free(id_str);
3913 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3914 3f60a8ef 2018-07-10 stsp free(line);
3915 2550e4c3 2018-07-13 stsp line = NULL;
3916 3f60a8ef 2018-07-10 stsp if (err)
3917 3f60a8ef 2018-07-10 stsp return err;
3918 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3919 2550e4c3 2018-07-13 stsp free(wline);
3920 2550e4c3 2018-07-13 stsp wline = NULL;
3921 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3922 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3923 3f60a8ef 2018-07-10 stsp
3924 4f7c3e5e 2020-12-01 naddy s->eof = 0;
3925 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
3926 4f7c3e5e 2020-12-01 naddy line = parse_next_line(blame->f, &len);
3927 84451b3e 2018-07-10 stsp if (line == NULL) {
3928 4f7c3e5e 2020-12-01 naddy s->eof = 1;
3929 84451b3e 2018-07-10 stsp break;
3930 84451b3e 2018-07-10 stsp }
3931 4f7c3e5e 2020-12-01 naddy if (++lineno < s->first_displayed_line) {
3932 84451b3e 2018-07-10 stsp free(line);
3933 84451b3e 2018-07-10 stsp continue;
3934 84451b3e 2018-07-10 stsp }
3935 84451b3e 2018-07-10 stsp
3936 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3937 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3938 b700b5d6 2018-07-10 stsp
3939 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
3940 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
3941 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3942 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3943 27a741e5 2019-09-11 stsp !(view->focussed &&
3944 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
3945 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3946 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3947 8d0fe45a 2019-08-12 stsp char *id_str;
3948 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3949 8d0fe45a 2019-08-12 stsp if (err) {
3950 8d0fe45a 2019-08-12 stsp free(line);
3951 8d0fe45a 2019-08-12 stsp return err;
3952 8d0fe45a 2019-08-12 stsp }
3953 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3954 11b20872 2019-11-08 stsp if (tc)
3955 11b20872 2019-11-08 stsp wattr_on(view->window,
3956 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3957 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3958 11b20872 2019-11-08 stsp if (tc)
3959 11b20872 2019-11-08 stsp wattr_off(view->window,
3960 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3961 8d0fe45a 2019-08-12 stsp free(id_str);
3962 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3963 8d0fe45a 2019-08-12 stsp } else {
3964 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3965 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3966 84451b3e 2018-07-10 stsp }
3967 ee41ec32 2018-07-10 stsp } else {
3968 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3969 ee41ec32 2018-07-10 stsp prev_id = NULL;
3970 ee41ec32 2018-07-10 stsp }
3971 84451b3e 2018-07-10 stsp
3972 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3973 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3974 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3975 27a741e5 2019-09-11 stsp
3976 41605754 2020-11-12 stsp if (view->ncols <= 9) {
3977 41605754 2020-11-12 stsp width = 9;
3978 41605754 2020-11-12 stsp wline = wcsdup(L"");
3979 41605754 2020-11-12 stsp if (wline == NULL) {
3980 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
3981 41605754 2020-11-12 stsp free(line);
3982 41605754 2020-11-12 stsp return err;
3983 41605754 2020-11-12 stsp }
3984 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
3985 4f7c3e5e 2020-12-01 naddy s->matched_line &&
3986 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3987 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
3988 41605754 2020-11-12 stsp view->window, regmatch);
3989 41605754 2020-11-12 stsp if (err) {
3990 41605754 2020-11-12 stsp free(line);
3991 41605754 2020-11-12 stsp return err;
3992 41605754 2020-11-12 stsp }
3993 41605754 2020-11-12 stsp width += 9;
3994 41605754 2020-11-12 stsp } else {
3995 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
3996 41605754 2020-11-12 stsp view->ncols - 9, 9);
3997 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3998 41605754 2020-11-12 stsp free(wline);
3999 41605754 2020-11-12 stsp wline = NULL;
4000 41605754 2020-11-12 stsp width += 9;
4001 41605754 2020-11-12 stsp }
4002 41605754 2020-11-12 stsp
4003 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4004 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4005 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4006 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4007 84451b3e 2018-07-10 stsp free(line);
4008 84451b3e 2018-07-10 stsp }
4009 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4010 84451b3e 2018-07-10 stsp
4011 1a57306a 2018-09-02 stsp view_vborder(view);
4012 84451b3e 2018-07-10 stsp
4013 84451b3e 2018-07-10 stsp return NULL;
4014 84451b3e 2018-07-10 stsp }
4015 84451b3e 2018-07-10 stsp
4016 84451b3e 2018-07-10 stsp static const struct got_error *
4017 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4018 84451b3e 2018-07-10 stsp {
4019 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4020 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4021 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4022 1a76625f 2018-10-22 stsp int errcode;
4023 84451b3e 2018-07-10 stsp
4024 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4025 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4026 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4027 84451b3e 2018-07-10 stsp
4028 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4029 1a76625f 2018-10-22 stsp if (errcode)
4030 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4031 84451b3e 2018-07-10 stsp
4032 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4033 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4034 d68a0a7d 2018-07-10 stsp goto done;
4035 d68a0a7d 2018-07-10 stsp }
4036 d68a0a7d 2018-07-10 stsp
4037 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4038 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4039 d68a0a7d 2018-07-10 stsp
4040 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4041 d68a0a7d 2018-07-10 stsp if (line->annotated)
4042 d68a0a7d 2018-07-10 stsp goto done;
4043 d68a0a7d 2018-07-10 stsp
4044 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4045 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4046 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4047 84451b3e 2018-07-10 stsp goto done;
4048 84451b3e 2018-07-10 stsp }
4049 84451b3e 2018-07-10 stsp line->annotated = 1;
4050 84451b3e 2018-07-10 stsp done:
4051 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4052 1a76625f 2018-10-22 stsp if (errcode)
4053 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4054 84451b3e 2018-07-10 stsp return err;
4055 84451b3e 2018-07-10 stsp }
4056 84451b3e 2018-07-10 stsp
4057 84451b3e 2018-07-10 stsp static void *
4058 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4059 84451b3e 2018-07-10 stsp {
4060 18430de3 2018-07-10 stsp const struct got_error *err;
4061 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4062 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4063 1a76625f 2018-10-22 stsp int errcode;
4064 18430de3 2018-07-10 stsp
4065 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4066 61266923 2020-01-14 stsp if (err)
4067 61266923 2020-01-14 stsp return (void *)err;
4068 61266923 2020-01-14 stsp
4069 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4070 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4071 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4072 fc06ba56 2019-08-22 stsp err = NULL;
4073 18430de3 2018-07-10 stsp
4074 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4075 1a76625f 2018-10-22 stsp if (errcode)
4076 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4077 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4078 18430de3 2018-07-10 stsp
4079 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4080 c9beca56 2018-07-22 stsp ta->repo = NULL;
4081 c9beca56 2018-07-22 stsp *ta->complete = 1;
4082 18430de3 2018-07-10 stsp
4083 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4084 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4085 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4086 18430de3 2018-07-10 stsp
4087 18430de3 2018-07-10 stsp return (void *)err;
4088 84451b3e 2018-07-10 stsp }
4089 84451b3e 2018-07-10 stsp
4090 245d91c1 2018-07-12 stsp static struct got_object_id *
4091 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4092 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4093 245d91c1 2018-07-12 stsp {
4094 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4095 8d0fe45a 2019-08-12 stsp
4096 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4097 8d0fe45a 2019-08-12 stsp return NULL;
4098 b880a918 2018-07-10 stsp
4099 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4100 245d91c1 2018-07-12 stsp if (!line->annotated)
4101 245d91c1 2018-07-12 stsp return NULL;
4102 245d91c1 2018-07-12 stsp
4103 245d91c1 2018-07-12 stsp return line->id;
4104 b880a918 2018-07-10 stsp }
4105 245d91c1 2018-07-12 stsp
4106 b880a918 2018-07-10 stsp static const struct got_error *
4107 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4108 a70480e0 2018-06-23 stsp {
4109 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4110 245d91c1 2018-07-12 stsp int i;
4111 245d91c1 2018-07-12 stsp
4112 245d91c1 2018-07-12 stsp if (blame->thread) {
4113 1a76625f 2018-10-22 stsp int errcode;
4114 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4115 1a76625f 2018-10-22 stsp if (errcode)
4116 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4117 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4118 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4119 1a76625f 2018-10-22 stsp if (errcode)
4120 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4121 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4122 1a76625f 2018-10-22 stsp if (errcode)
4123 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4124 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4125 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4126 245d91c1 2018-07-12 stsp err = NULL;
4127 245d91c1 2018-07-12 stsp blame->thread = NULL;
4128 245d91c1 2018-07-12 stsp }
4129 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4130 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4131 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4132 245d91c1 2018-07-12 stsp }
4133 245d91c1 2018-07-12 stsp if (blame->f) {
4134 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4135 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4136 245d91c1 2018-07-12 stsp blame->f = NULL;
4137 245d91c1 2018-07-12 stsp }
4138 57670559 2018-12-23 stsp if (blame->lines) {
4139 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4140 57670559 2018-12-23 stsp free(blame->lines[i].id);
4141 57670559 2018-12-23 stsp free(blame->lines);
4142 57670559 2018-12-23 stsp blame->lines = NULL;
4143 57670559 2018-12-23 stsp }
4144 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4145 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4146 245d91c1 2018-07-12 stsp
4147 245d91c1 2018-07-12 stsp return err;
4148 245d91c1 2018-07-12 stsp }
4149 245d91c1 2018-07-12 stsp
4150 245d91c1 2018-07-12 stsp static const struct got_error *
4151 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4152 fc06ba56 2019-08-22 stsp {
4153 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4154 fc06ba56 2019-08-22 stsp int *done = arg;
4155 fc06ba56 2019-08-22 stsp int errcode;
4156 fc06ba56 2019-08-22 stsp
4157 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4158 fc06ba56 2019-08-22 stsp if (errcode)
4159 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4160 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4161 fc06ba56 2019-08-22 stsp
4162 fc06ba56 2019-08-22 stsp if (*done)
4163 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4164 fc06ba56 2019-08-22 stsp
4165 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4166 fc06ba56 2019-08-22 stsp if (errcode)
4167 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4168 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4169 fc06ba56 2019-08-22 stsp
4170 fc06ba56 2019-08-22 stsp return err;
4171 fc06ba56 2019-08-22 stsp }
4172 fc06ba56 2019-08-22 stsp
4173 fc06ba56 2019-08-22 stsp static const struct got_error *
4174 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4175 245d91c1 2018-07-12 stsp {
4176 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4177 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4178 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4179 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4180 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4181 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4182 15a94983 2018-12-23 stsp int obj_type;
4183 a70480e0 2018-06-23 stsp
4184 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4185 a5388363 2020-12-01 naddy s->path);
4186 27d434c2 2018-09-15 stsp if (err)
4187 15a94983 2018-12-23 stsp return err;
4188 27d434c2 2018-09-15 stsp
4189 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4190 84451b3e 2018-07-10 stsp if (err)
4191 84451b3e 2018-07-10 stsp goto done;
4192 27d434c2 2018-09-15 stsp
4193 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4194 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4195 84451b3e 2018-07-10 stsp goto done;
4196 84451b3e 2018-07-10 stsp }
4197 a70480e0 2018-06-23 stsp
4198 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4199 a70480e0 2018-06-23 stsp if (err)
4200 a70480e0 2018-06-23 stsp goto done;
4201 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4202 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4203 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4204 84451b3e 2018-07-10 stsp goto done;
4205 84451b3e 2018-07-10 stsp }
4206 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4207 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4208 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4209 84451b3e 2018-07-10 stsp goto done;
4210 b02560ec 2019-08-19 stsp
4211 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4212 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4213 b02560ec 2019-08-19 stsp blame->nlines--;
4214 a70480e0 2018-06-23 stsp
4215 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4216 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4217 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4218 84451b3e 2018-07-10 stsp goto done;
4219 84451b3e 2018-07-10 stsp }
4220 a70480e0 2018-06-23 stsp
4221 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4222 bd24772e 2018-07-11 stsp if (err)
4223 bd24772e 2018-07-11 stsp goto done;
4224 bd24772e 2018-07-11 stsp
4225 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4226 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4227 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4228 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4229 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4230 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4231 245d91c1 2018-07-12 stsp goto done;
4232 245d91c1 2018-07-12 stsp }
4233 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4234 245d91c1 2018-07-12 stsp
4235 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4236 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4237 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4238 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4239 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4240 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4241 a5388363 2020-12-01 naddy s->blame_complete = 0;
4242 245d91c1 2018-07-12 stsp
4243 245d91c1 2018-07-12 stsp done:
4244 245d91c1 2018-07-12 stsp if (blob)
4245 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4246 27d434c2 2018-09-15 stsp free(obj_id);
4247 245d91c1 2018-07-12 stsp if (err)
4248 245d91c1 2018-07-12 stsp stop_blame(blame);
4249 245d91c1 2018-07-12 stsp return err;
4250 245d91c1 2018-07-12 stsp }
4251 245d91c1 2018-07-12 stsp
4252 245d91c1 2018-07-12 stsp static const struct got_error *
4253 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4254 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4255 245d91c1 2018-07-12 stsp {
4256 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4257 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4258 dbc6a6b6 2018-07-12 stsp
4259 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4260 245d91c1 2018-07-12 stsp
4261 c4843652 2019-08-12 stsp s->path = strdup(path);
4262 c4843652 2019-08-12 stsp if (s->path == NULL)
4263 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4264 c4843652 2019-08-12 stsp
4265 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4266 c4843652 2019-08-12 stsp if (err) {
4267 c4843652 2019-08-12 stsp free(s->path);
4268 7cbe629d 2018-08-04 stsp return err;
4269 c4843652 2019-08-12 stsp }
4270 245d91c1 2018-07-12 stsp
4271 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4272 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4273 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4274 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4275 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4276 fb2756b9 2018-08-04 stsp s->repo = repo;
4277 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4278 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4279 7cbe629d 2018-08-04 stsp
4280 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4281 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4282 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4283 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4284 11b20872 2019-11-08 stsp if (err)
4285 11b20872 2019-11-08 stsp return err;
4286 11b20872 2019-11-08 stsp }
4287 11b20872 2019-11-08 stsp
4288 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4289 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4290 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4291 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4292 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4293 e5a0f69f 2018-08-18 stsp
4294 a5388363 2020-12-01 naddy return run_blame(view);
4295 7cbe629d 2018-08-04 stsp }
4296 7cbe629d 2018-08-04 stsp
4297 e5a0f69f 2018-08-18 stsp static const struct got_error *
4298 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4299 7cbe629d 2018-08-04 stsp {
4300 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4301 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4302 7cbe629d 2018-08-04 stsp
4303 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4304 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4305 e5a0f69f 2018-08-18 stsp
4306 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4307 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4308 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4309 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4310 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4311 7cbe629d 2018-08-04 stsp }
4312 e5a0f69f 2018-08-18 stsp
4313 e5a0f69f 2018-08-18 stsp free(s->path);
4314 11b20872 2019-11-08 stsp free_colors(&s->colors);
4315 e5a0f69f 2018-08-18 stsp
4316 e5a0f69f 2018-08-18 stsp return err;
4317 7cbe629d 2018-08-04 stsp }
4318 7cbe629d 2018-08-04 stsp
4319 7cbe629d 2018-08-04 stsp static const struct got_error *
4320 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4321 6c4c42e0 2019-06-24 stsp {
4322 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4323 6c4c42e0 2019-06-24 stsp
4324 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4325 6c4c42e0 2019-06-24 stsp return NULL;
4326 6c4c42e0 2019-06-24 stsp }
4327 6c4c42e0 2019-06-24 stsp
4328 6c4c42e0 2019-06-24 stsp static const struct got_error *
4329 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4330 6c4c42e0 2019-06-24 stsp {
4331 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4332 6c4c42e0 2019-06-24 stsp int lineno;
4333 6c4c42e0 2019-06-24 stsp
4334 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4335 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4336 6c4c42e0 2019-06-24 stsp return NULL;
4337 6c4c42e0 2019-06-24 stsp }
4338 6c4c42e0 2019-06-24 stsp
4339 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4340 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4341 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4342 6c4c42e0 2019-06-24 stsp else
4343 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4344 6c4c42e0 2019-06-24 stsp } else {
4345 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4346 6c4c42e0 2019-06-24 stsp lineno = 1;
4347 6c4c42e0 2019-06-24 stsp else
4348 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4349 6c4c42e0 2019-06-24 stsp }
4350 6c4c42e0 2019-06-24 stsp
4351 6c4c42e0 2019-06-24 stsp while (1) {
4352 6c4c42e0 2019-06-24 stsp char *line = NULL;
4353 6c4c42e0 2019-06-24 stsp off_t offset;
4354 6c4c42e0 2019-06-24 stsp size_t len;
4355 6c4c42e0 2019-06-24 stsp
4356 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4357 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4358 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4359 6c4c42e0 2019-06-24 stsp free(line);
4360 6c4c42e0 2019-06-24 stsp break;
4361 6c4c42e0 2019-06-24 stsp }
4362 2246482e 2019-06-25 stsp
4363 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4364 6c4c42e0 2019-06-24 stsp lineno = 1;
4365 6c4c42e0 2019-06-24 stsp else
4366 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4367 6c4c42e0 2019-06-24 stsp }
4368 6c4c42e0 2019-06-24 stsp
4369 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4370 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4371 6c4c42e0 2019-06-24 stsp free(line);
4372 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4373 6c4c42e0 2019-06-24 stsp }
4374 6c4c42e0 2019-06-24 stsp free(line);
4375 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4376 41605754 2020-11-12 stsp if (line &&
4377 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4378 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4379 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4380 6c4c42e0 2019-06-24 stsp free(line);
4381 6c4c42e0 2019-06-24 stsp break;
4382 6c4c42e0 2019-06-24 stsp }
4383 6c4c42e0 2019-06-24 stsp free(line);
4384 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4385 6c4c42e0 2019-06-24 stsp lineno++;
4386 6c4c42e0 2019-06-24 stsp else
4387 6c4c42e0 2019-06-24 stsp lineno--;
4388 6c4c42e0 2019-06-24 stsp }
4389 6c4c42e0 2019-06-24 stsp
4390 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4391 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4392 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4393 6c4c42e0 2019-06-24 stsp }
4394 6c4c42e0 2019-06-24 stsp
4395 6c4c42e0 2019-06-24 stsp return NULL;
4396 6c4c42e0 2019-06-24 stsp }
4397 6c4c42e0 2019-06-24 stsp
4398 6c4c42e0 2019-06-24 stsp static const struct got_error *
4399 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4400 7cbe629d 2018-08-04 stsp {
4401 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4402 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4403 2b380cc8 2018-10-24 stsp int errcode;
4404 2b380cc8 2018-10-24 stsp
4405 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4406 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4407 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4408 2b380cc8 2018-10-24 stsp if (errcode)
4409 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4410 51fe7530 2019-08-19 stsp
4411 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4412 2b380cc8 2018-10-24 stsp }
4413 e5a0f69f 2018-08-18 stsp
4414 51fe7530 2019-08-19 stsp if (s->blame_complete)
4415 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4416 51fe7530 2019-08-19 stsp
4417 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4418 e5a0f69f 2018-08-18 stsp
4419 669b5ffa 2018-10-07 stsp view_vborder(view);
4420 e5a0f69f 2018-08-18 stsp return err;
4421 e5a0f69f 2018-08-18 stsp }
4422 e5a0f69f 2018-08-18 stsp
4423 e5a0f69f 2018-08-18 stsp static const struct got_error *
4424 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4425 e5a0f69f 2018-08-18 stsp {
4426 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4427 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4428 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4429 669b5ffa 2018-10-07 stsp int begin_x = 0;
4430 7cbe629d 2018-08-04 stsp
4431 e5a0f69f 2018-08-18 stsp switch (ch) {
4432 1e37a5c2 2019-05-12 jcs case 'q':
4433 1e37a5c2 2019-05-12 jcs s->done = 1;
4434 1e37a5c2 2019-05-12 jcs break;
4435 1e37a5c2 2019-05-12 jcs case 'k':
4436 1e37a5c2 2019-05-12 jcs case KEY_UP:
4437 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4438 1e37a5c2 2019-05-12 jcs s->selected_line--;
4439 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4440 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4441 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4442 1e37a5c2 2019-05-12 jcs break;
4443 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4444 ea025d1d 2020-02-22 naddy case CTRL('b'):
4445 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4446 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4447 e5a0f69f 2018-08-18 stsp break;
4448 1e37a5c2 2019-05-12 jcs }
4449 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4450 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4451 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4452 1e37a5c2 2019-05-12 jcs else
4453 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4454 1e37a5c2 2019-05-12 jcs break;
4455 1e37a5c2 2019-05-12 jcs case 'j':
4456 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4457 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4458 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4459 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4460 1e37a5c2 2019-05-12 jcs s->selected_line++;
4461 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4462 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4463 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4464 1e37a5c2 2019-05-12 jcs break;
4465 1e37a5c2 2019-05-12 jcs case 'b':
4466 1e37a5c2 2019-05-12 jcs case 'p': {
4467 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4468 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4469 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4470 1e37a5c2 2019-05-12 jcs if (id == NULL)
4471 e5a0f69f 2018-08-18 stsp break;
4472 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4473 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4474 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4475 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4476 1e37a5c2 2019-05-12 jcs int obj_type;
4477 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4478 1e37a5c2 2019-05-12 jcs s->repo, id);
4479 e5a0f69f 2018-08-18 stsp if (err)
4480 e5a0f69f 2018-08-18 stsp break;
4481 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4482 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4483 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4484 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4485 e5a0f69f 2018-08-18 stsp break;
4486 e5a0f69f 2018-08-18 stsp }
4487 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4488 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4489 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4490 e5a0f69f 2018-08-18 stsp if (err) {
4491 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4492 1e37a5c2 2019-05-12 jcs err = NULL;
4493 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4494 e5a0f69f 2018-08-18 stsp break;
4495 e5a0f69f 2018-08-18 stsp }
4496 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4497 1e37a5c2 2019-05-12 jcs blob_id);
4498 1e37a5c2 2019-05-12 jcs free(blob_id);
4499 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4500 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4501 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4502 e5a0f69f 2018-08-18 stsp break;
4503 1e37a5c2 2019-05-12 jcs }
4504 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4505 1e37a5c2 2019-05-12 jcs pid->id);
4506 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4507 1e37a5c2 2019-05-12 jcs } else {
4508 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4509 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4510 1e37a5c2 2019-05-12 jcs break;
4511 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4512 1e37a5c2 2019-05-12 jcs id);
4513 1e37a5c2 2019-05-12 jcs }
4514 1e37a5c2 2019-05-12 jcs if (err)
4515 e5a0f69f 2018-08-18 stsp break;
4516 1e37a5c2 2019-05-12 jcs s->done = 1;
4517 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4518 1e37a5c2 2019-05-12 jcs s->done = 0;
4519 1e37a5c2 2019-05-12 jcs if (thread_err)
4520 1e37a5c2 2019-05-12 jcs break;
4521 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4522 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4523 a5388363 2020-12-01 naddy err = run_blame(view);
4524 1e37a5c2 2019-05-12 jcs if (err)
4525 1e37a5c2 2019-05-12 jcs break;
4526 1e37a5c2 2019-05-12 jcs break;
4527 1e37a5c2 2019-05-12 jcs }
4528 1e37a5c2 2019-05-12 jcs case 'B': {
4529 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4530 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4531 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4532 1e37a5c2 2019-05-12 jcs break;
4533 1e37a5c2 2019-05-12 jcs s->done = 1;
4534 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4535 1e37a5c2 2019-05-12 jcs s->done = 0;
4536 1e37a5c2 2019-05-12 jcs if (thread_err)
4537 1e37a5c2 2019-05-12 jcs break;
4538 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4539 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4540 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4541 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4542 a5388363 2020-12-01 naddy err = run_blame(view);
4543 1e37a5c2 2019-05-12 jcs if (err)
4544 1e37a5c2 2019-05-12 jcs break;
4545 1e37a5c2 2019-05-12 jcs break;
4546 1e37a5c2 2019-05-12 jcs }
4547 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4548 1e37a5c2 2019-05-12 jcs case '\r': {
4549 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4550 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4551 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4552 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4553 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4554 1e37a5c2 2019-05-12 jcs if (id == NULL)
4555 1e37a5c2 2019-05-12 jcs break;
4556 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4557 1e37a5c2 2019-05-12 jcs if (err)
4558 1e37a5c2 2019-05-12 jcs break;
4559 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4560 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4561 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4562 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4563 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4564 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4565 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4566 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4567 1e37a5c2 2019-05-12 jcs break;
4568 15a94983 2018-12-23 stsp }
4569 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4570 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4571 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4572 1e37a5c2 2019-05-12 jcs if (err) {
4573 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4574 1e37a5c2 2019-05-12 jcs break;
4575 1e37a5c2 2019-05-12 jcs }
4576 e78dc838 2020-12-04 stsp view->focussed = 0;
4577 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4578 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4579 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4580 1e37a5c2 2019-05-12 jcs if (err)
4581 34bc9ec9 2019-02-22 stsp break;
4582 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4583 e78dc838 2020-12-04 stsp view->focus_child = 1;
4584 1e37a5c2 2019-05-12 jcs } else
4585 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4586 1e37a5c2 2019-05-12 jcs if (err)
4587 e5a0f69f 2018-08-18 stsp break;
4588 1e37a5c2 2019-05-12 jcs break;
4589 1e37a5c2 2019-05-12 jcs }
4590 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4591 ea025d1d 2020-02-22 naddy case CTRL('f'):
4592 1e37a5c2 2019-05-12 jcs case ' ':
4593 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4594 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4595 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4596 e5a0f69f 2018-08-18 stsp break;
4597 1e37a5c2 2019-05-12 jcs }
4598 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4599 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4600 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4601 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4602 e5a0f69f 2018-08-18 stsp break;
4603 1e37a5c2 2019-05-12 jcs }
4604 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4605 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4606 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4607 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4608 1e37a5c2 2019-05-12 jcs else
4609 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4610 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4611 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4612 1e37a5c2 2019-05-12 jcs break;
4613 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4614 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4615 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4616 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4617 1e37a5c2 2019-05-12 jcs }
4618 1e37a5c2 2019-05-12 jcs break;
4619 1e37a5c2 2019-05-12 jcs default:
4620 1e37a5c2 2019-05-12 jcs break;
4621 a70480e0 2018-06-23 stsp }
4622 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4623 a70480e0 2018-06-23 stsp }
4624 a70480e0 2018-06-23 stsp
4625 a70480e0 2018-06-23 stsp static const struct got_error *
4626 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4627 9f7d7167 2018-04-29 stsp {
4628 a70480e0 2018-06-23 stsp const struct got_error *error;
4629 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4630 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4631 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4632 0587e10c 2020-07-23 stsp char *link_target = NULL;
4633 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4634 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4635 a70480e0 2018-06-23 stsp int ch;
4636 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4637 a70480e0 2018-06-23 stsp
4638 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4639 a70480e0 2018-06-23 stsp switch (ch) {
4640 a70480e0 2018-06-23 stsp case 'c':
4641 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4642 a70480e0 2018-06-23 stsp break;
4643 69069811 2018-08-02 stsp case 'r':
4644 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4645 69069811 2018-08-02 stsp if (repo_path == NULL)
4646 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4647 9ba1d308 2019-10-21 stsp optarg);
4648 69069811 2018-08-02 stsp break;
4649 a70480e0 2018-06-23 stsp default:
4650 17020d27 2019-03-07 stsp usage_blame();
4651 a70480e0 2018-06-23 stsp /* NOTREACHED */
4652 a70480e0 2018-06-23 stsp }
4653 a70480e0 2018-06-23 stsp }
4654 a70480e0 2018-06-23 stsp
4655 a70480e0 2018-06-23 stsp argc -= optind;
4656 a70480e0 2018-06-23 stsp argv += optind;
4657 a70480e0 2018-06-23 stsp
4658 f135c941 2020-02-20 stsp if (argc != 1)
4659 a70480e0 2018-06-23 stsp usage_blame();
4660 69069811 2018-08-02 stsp
4661 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4662 6962eb72 2020-02-20 stsp if (cwd == NULL)
4663 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4664 6962eb72 2020-02-20 stsp
4665 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4666 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4667 f135c941 2020-02-20 stsp goto done;
4668 f135c941 2020-02-20 stsp
4669 69069811 2018-08-02 stsp if (repo_path == NULL) {
4670 f135c941 2020-02-20 stsp if (worktree)
4671 eb41ed75 2019-02-05 stsp repo_path =
4672 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4673 f135c941 2020-02-20 stsp else
4674 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4675 69069811 2018-08-02 stsp }
4676 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4677 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4678 f135c941 2020-02-20 stsp goto done;
4679 f135c941 2020-02-20 stsp }
4680 a915003a 2019-02-05 stsp
4681 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4682 c02c541e 2019-03-29 stsp if (error != NULL)
4683 8e94dd5b 2019-01-04 stsp goto done;
4684 69069811 2018-08-02 stsp
4685 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4686 f135c941 2020-02-20 stsp worktree);
4687 c02c541e 2019-03-29 stsp if (error)
4688 92205607 2019-01-04 stsp goto done;
4689 69069811 2018-08-02 stsp
4690 f135c941 2020-02-20 stsp init_curses();
4691 f135c941 2020-02-20 stsp
4692 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4693 eb41ed75 2019-02-05 stsp if (error)
4694 69069811 2018-08-02 stsp goto done;
4695 a70480e0 2018-06-23 stsp
4696 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4697 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4698 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4699 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4700 a70480e0 2018-06-23 stsp if (error != NULL)
4701 66b4983c 2018-06-23 stsp goto done;
4702 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4703 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4704 a70480e0 2018-06-23 stsp } else {
4705 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4706 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4707 a70480e0 2018-06-23 stsp }
4708 a19e88aa 2018-06-23 stsp if (error != NULL)
4709 8b473291 2019-02-21 stsp goto done;
4710 8b473291 2019-02-21 stsp
4711 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4712 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4713 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4714 e1cd8fed 2018-08-01 stsp goto done;
4715 e1cd8fed 2018-08-01 stsp }
4716 0587e10c 2020-07-23 stsp
4717 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4718 0587e10c 2020-07-23 stsp commit_id, repo);
4719 7cbe629d 2018-08-04 stsp if (error)
4720 7cbe629d 2018-08-04 stsp goto done;
4721 0587e10c 2020-07-23 stsp
4722 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4723 78756c87 2020-11-24 stsp commit_id, repo);
4724 0587e10c 2020-07-23 stsp if (error)
4725 0587e10c 2020-07-23 stsp goto done;
4726 12314ad4 2019-08-31 stsp if (worktree) {
4727 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4728 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4729 12314ad4 2019-08-31 stsp worktree = NULL;
4730 12314ad4 2019-08-31 stsp }
4731 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4732 a70480e0 2018-06-23 stsp done:
4733 69069811 2018-08-02 stsp free(repo_path);
4734 f135c941 2020-02-20 stsp free(in_repo_path);
4735 0587e10c 2020-07-23 stsp free(link_target);
4736 69069811 2018-08-02 stsp free(cwd);
4737 a70480e0 2018-06-23 stsp free(commit_id);
4738 eb41ed75 2019-02-05 stsp if (worktree)
4739 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4740 a70480e0 2018-06-23 stsp if (repo)
4741 a70480e0 2018-06-23 stsp got_repo_close(repo);
4742 a70480e0 2018-06-23 stsp return error;
4743 ffd1d5e5 2018-06-23 stsp }
4744 ffd1d5e5 2018-06-23 stsp
4745 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4746 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4747 ffd1d5e5 2018-06-23 stsp {
4748 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4749 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4750 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4751 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4752 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4753 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4754 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4755 ffd1d5e5 2018-06-23 stsp
4756 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4757 ffd1d5e5 2018-06-23 stsp
4758 f7d12f7e 2018-08-01 stsp werase(view->window);
4759 ffd1d5e5 2018-06-23 stsp
4760 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4761 ffd1d5e5 2018-06-23 stsp return NULL;
4762 ffd1d5e5 2018-06-23 stsp
4763 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4764 ffd1d5e5 2018-06-23 stsp if (err)
4765 ffd1d5e5 2018-06-23 stsp return err;
4766 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4767 a3404814 2018-09-02 stsp wstandout(view->window);
4768 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4769 11b20872 2019-11-08 stsp if (tc)
4770 11b20872 2019-11-08 stsp wattr_on(view->window,
4771 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4772 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4773 11b20872 2019-11-08 stsp if (tc)
4774 11b20872 2019-11-08 stsp wattr_off(view->window,
4775 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4776 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4777 a3404814 2018-09-02 stsp wstandend(view->window);
4778 2550e4c3 2018-07-13 stsp free(wline);
4779 2550e4c3 2018-07-13 stsp wline = NULL;
4780 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4781 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4782 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4783 ffd1d5e5 2018-06-23 stsp return NULL;
4784 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4785 ce52c690 2018-06-23 stsp if (err)
4786 ce52c690 2018-06-23 stsp return err;
4787 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4788 2550e4c3 2018-07-13 stsp free(wline);
4789 2550e4c3 2018-07-13 stsp wline = NULL;
4790 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4791 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4792 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4793 ffd1d5e5 2018-06-23 stsp return NULL;
4794 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4795 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4796 a1eca9bb 2018-06-23 stsp return NULL;
4797 ffd1d5e5 2018-06-23 stsp
4798 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4799 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4800 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4801 0cf4efb1 2018-09-29 stsp if (view->focussed)
4802 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4803 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4804 ffd1d5e5 2018-06-23 stsp }
4805 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4806 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4807 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4808 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4809 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4810 ffd1d5e5 2018-06-23 stsp return NULL;
4811 ffd1d5e5 2018-06-23 stsp n = 1;
4812 ffd1d5e5 2018-06-23 stsp } else {
4813 ffd1d5e5 2018-06-23 stsp n = 0;
4814 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4815 ffd1d5e5 2018-06-23 stsp }
4816 ffd1d5e5 2018-06-23 stsp
4817 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4818 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4819 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4820 848d6979 2019-08-12 stsp const char *modestr = "";
4821 56e0773d 2019-11-28 stsp mode_t mode;
4822 1d13200f 2018-07-12 stsp
4823 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4824 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4825 56e0773d 2019-11-28 stsp
4826 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4827 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4828 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4829 1d13200f 2018-07-12 stsp if (err)
4830 638f9024 2019-05-13 stsp return got_error_from_errno(
4831 230a42bd 2019-05-11 jcs "got_object_id_str");
4832 1d13200f 2018-07-12 stsp }
4833 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4834 63c5ca5d 2019-08-24 stsp modestr = "$";
4835 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4836 0d6c6ee3 2020-05-20 stsp int i;
4837 0d6c6ee3 2020-05-20 stsp
4838 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4839 d86d3b18 2020-12-01 naddy te, s->repo);
4840 0d6c6ee3 2020-05-20 stsp if (err) {
4841 0d6c6ee3 2020-05-20 stsp free(id_str);
4842 0d6c6ee3 2020-05-20 stsp return err;
4843 0d6c6ee3 2020-05-20 stsp }
4844 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4845 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4846 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4847 0d6c6ee3 2020-05-20 stsp }
4848 848d6979 2019-08-12 stsp modestr = "@";
4849 0d6c6ee3 2020-05-20 stsp }
4850 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4851 848d6979 2019-08-12 stsp modestr = "/";
4852 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4853 848d6979 2019-08-12 stsp modestr = "*";
4854 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4855 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4856 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4857 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4858 1d13200f 2018-07-12 stsp free(id_str);
4859 0d6c6ee3 2020-05-20 stsp free(link_target);
4860 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4861 1d13200f 2018-07-12 stsp }
4862 1d13200f 2018-07-12 stsp free(id_str);
4863 0d6c6ee3 2020-05-20 stsp free(link_target);
4864 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4865 ffd1d5e5 2018-06-23 stsp if (err) {
4866 ffd1d5e5 2018-06-23 stsp free(line);
4867 ffd1d5e5 2018-06-23 stsp break;
4868 ffd1d5e5 2018-06-23 stsp }
4869 d86d3b18 2020-12-01 naddy if (n == s->selected) {
4870 0cf4efb1 2018-09-29 stsp if (view->focussed)
4871 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4872 d86d3b18 2020-12-01 naddy s->selected_entry = te;
4873 ffd1d5e5 2018-06-23 stsp }
4874 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
4875 f26dddb7 2019-11-08 stsp if (tc)
4876 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4877 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4878 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4879 f26dddb7 2019-11-08 stsp if (tc)
4880 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4881 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4882 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4883 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4884 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
4885 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4886 ffd1d5e5 2018-06-23 stsp free(line);
4887 2550e4c3 2018-07-13 stsp free(wline);
4888 2550e4c3 2018-07-13 stsp wline = NULL;
4889 ffd1d5e5 2018-06-23 stsp n++;
4890 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4891 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
4892 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4893 ffd1d5e5 2018-06-23 stsp break;
4894 ffd1d5e5 2018-06-23 stsp }
4895 ffd1d5e5 2018-06-23 stsp
4896 ffd1d5e5 2018-06-23 stsp return err;
4897 ffd1d5e5 2018-06-23 stsp }
4898 ffd1d5e5 2018-06-23 stsp
4899 ffd1d5e5 2018-06-23 stsp static void
4900 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4901 ffd1d5e5 2018-06-23 stsp {
4902 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4903 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4904 fa86c4bf 2020-11-29 stsp int i = 0;
4905 ffd1d5e5 2018-06-23 stsp
4906 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4907 ffd1d5e5 2018-06-23 stsp return;
4908 ffd1d5e5 2018-06-23 stsp
4909 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4910 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4911 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4912 fa86c4bf 2020-11-29 stsp if (!isroot)
4913 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4914 fa86c4bf 2020-11-29 stsp break;
4915 fa86c4bf 2020-11-29 stsp }
4916 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4917 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4918 ffd1d5e5 2018-06-23 stsp }
4919 ffd1d5e5 2018-06-23 stsp }
4920 ffd1d5e5 2018-06-23 stsp
4921 fa86c4bf 2020-11-29 stsp static void
4922 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4923 ffd1d5e5 2018-06-23 stsp {
4924 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4925 ffd1d5e5 2018-06-23 stsp int n = 0;
4926 ffd1d5e5 2018-06-23 stsp
4927 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4928 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4929 694d3271 2020-12-01 naddy s->first_displayed_entry);
4930 694d3271 2020-12-01 naddy else
4931 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4932 56e0773d 2019-11-28 stsp
4933 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4934 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4935 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4936 768394f3 2019-01-24 stsp if (last) {
4937 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4938 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4939 768394f3 2019-01-24 stsp }
4940 ffd1d5e5 2018-06-23 stsp }
4941 ffd1d5e5 2018-06-23 stsp }
4942 ffd1d5e5 2018-06-23 stsp
4943 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4944 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4945 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4946 ffd1d5e5 2018-06-23 stsp {
4947 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4948 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4949 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4950 ffd1d5e5 2018-06-23 stsp
4951 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4952 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4953 56e0773d 2019-11-28 stsp + 1 /* slash */;
4954 ce52c690 2018-06-23 stsp if (te)
4955 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4956 ce52c690 2018-06-23 stsp
4957 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4958 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4959 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4960 ffd1d5e5 2018-06-23 stsp
4961 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4962 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4963 d9765a41 2018-06-23 stsp while (pt) {
4964 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4965 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4966 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4967 cb2ebc8a 2018-06-23 stsp goto done;
4968 cb2ebc8a 2018-06-23 stsp }
4969 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4970 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4971 cb2ebc8a 2018-06-23 stsp goto done;
4972 cb2ebc8a 2018-06-23 stsp }
4973 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4974 ffd1d5e5 2018-06-23 stsp }
4975 ce52c690 2018-06-23 stsp if (te) {
4976 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4977 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4978 ce52c690 2018-06-23 stsp goto done;
4979 ce52c690 2018-06-23 stsp }
4980 cb2ebc8a 2018-06-23 stsp }
4981 ce52c690 2018-06-23 stsp done:
4982 ce52c690 2018-06-23 stsp if (err) {
4983 ce52c690 2018-06-23 stsp free(*path);
4984 ce52c690 2018-06-23 stsp *path = NULL;
4985 ce52c690 2018-06-23 stsp }
4986 ce52c690 2018-06-23 stsp return err;
4987 ce52c690 2018-06-23 stsp }
4988 ce52c690 2018-06-23 stsp
4989 ce52c690 2018-06-23 stsp static const struct got_error *
4990 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4991 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4992 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4993 ce52c690 2018-06-23 stsp {
4994 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4995 ce52c690 2018-06-23 stsp char *path;
4996 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4997 a0de39f3 2019-08-09 stsp
4998 a0de39f3 2019-08-09 stsp *new_view = NULL;
4999 69efd4c4 2018-07-18 stsp
5000 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5001 ce52c690 2018-06-23 stsp if (err)
5002 ce52c690 2018-06-23 stsp return err;
5003 ffd1d5e5 2018-06-23 stsp
5004 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5005 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5006 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5007 83ce39e3 2019-08-12 stsp goto done;
5008 83ce39e3 2019-08-12 stsp }
5009 cdf1ee82 2018-08-01 stsp
5010 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5011 e5a0f69f 2018-08-18 stsp if (err) {
5012 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5013 fc06ba56 2019-08-22 stsp err = NULL;
5014 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5015 e5a0f69f 2018-08-18 stsp } else
5016 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5017 83ce39e3 2019-08-12 stsp done:
5018 83ce39e3 2019-08-12 stsp free(path);
5019 69efd4c4 2018-07-18 stsp return err;
5020 69efd4c4 2018-07-18 stsp }
5021 69efd4c4 2018-07-18 stsp
5022 69efd4c4 2018-07-18 stsp static const struct got_error *
5023 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
5024 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5025 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5026 69efd4c4 2018-07-18 stsp {
5027 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5028 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5029 69efd4c4 2018-07-18 stsp char *path;
5030 a0de39f3 2019-08-09 stsp
5031 a0de39f3 2019-08-09 stsp *new_view = NULL;
5032 69efd4c4 2018-07-18 stsp
5033 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5034 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5035 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5036 e5a0f69f 2018-08-18 stsp
5037 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
5038 69efd4c4 2018-07-18 stsp if (err)
5039 69efd4c4 2018-07-18 stsp return err;
5040 69efd4c4 2018-07-18 stsp
5041 78756c87 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5042 ba4f502b 2018-08-04 stsp if (err)
5043 e5a0f69f 2018-08-18 stsp view_close(log_view);
5044 e5a0f69f 2018-08-18 stsp else
5045 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5046 cb2ebc8a 2018-06-23 stsp free(path);
5047 cb2ebc8a 2018-06-23 stsp return err;
5048 ffd1d5e5 2018-06-23 stsp }
5049 ffd1d5e5 2018-06-23 stsp
5050 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5051 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5052 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5053 ffd1d5e5 2018-06-23 stsp {
5054 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5055 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5056 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5057 ffd1d5e5 2018-06-23 stsp
5058 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5059 ffd1d5e5 2018-06-23 stsp
5060 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5061 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5062 ffd1d5e5 2018-06-23 stsp goto done;
5063 ffd1d5e5 2018-06-23 stsp
5064 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5065 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5066 ffd1d5e5 2018-06-23 stsp goto done;
5067 ffd1d5e5 2018-06-23 stsp }
5068 ffd1d5e5 2018-06-23 stsp
5069 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5070 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5071 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5072 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5073 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5074 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5075 6484ec90 2018-09-29 stsp goto done;
5076 6484ec90 2018-09-29 stsp }
5077 fb2756b9 2018-08-04 stsp s->repo = repo;
5078 c0b01bdb 2019-11-08 stsp
5079 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5080 c0b01bdb 2019-11-08 stsp
5081 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5082 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5083 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5084 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5085 c0b01bdb 2019-11-08 stsp if (err)
5086 c0b01bdb 2019-11-08 stsp goto done;
5087 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5088 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5089 c0b01bdb 2019-11-08 stsp if (err) {
5090 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5091 c0b01bdb 2019-11-08 stsp goto done;
5092 c0b01bdb 2019-11-08 stsp }
5093 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5094 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5095 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5096 c0b01bdb 2019-11-08 stsp if (err) {
5097 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5098 c0b01bdb 2019-11-08 stsp goto done;
5099 c0b01bdb 2019-11-08 stsp }
5100 e5a0f69f 2018-08-18 stsp
5101 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5102 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5103 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5104 11b20872 2019-11-08 stsp if (err) {
5105 11b20872 2019-11-08 stsp free_colors(&s->colors);
5106 11b20872 2019-11-08 stsp goto done;
5107 11b20872 2019-11-08 stsp }
5108 11b20872 2019-11-08 stsp
5109 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5110 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5111 c0b01bdb 2019-11-08 stsp if (err) {
5112 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5113 c0b01bdb 2019-11-08 stsp goto done;
5114 c0b01bdb 2019-11-08 stsp }
5115 c0b01bdb 2019-11-08 stsp }
5116 c0b01bdb 2019-11-08 stsp
5117 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5118 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5119 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5120 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5121 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5122 ad80ab7b 2018-08-04 stsp done:
5123 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5124 6484ec90 2018-09-29 stsp if (err) {
5125 fb2756b9 2018-08-04 stsp free(s->tree_label);
5126 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5127 6484ec90 2018-09-29 stsp }
5128 ad80ab7b 2018-08-04 stsp return err;
5129 ad80ab7b 2018-08-04 stsp }
5130 ad80ab7b 2018-08-04 stsp
5131 e5a0f69f 2018-08-18 stsp static const struct got_error *
5132 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5133 ad80ab7b 2018-08-04 stsp {
5134 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5135 ad80ab7b 2018-08-04 stsp
5136 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5137 fb2756b9 2018-08-04 stsp free(s->tree_label);
5138 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5139 6484ec90 2018-09-29 stsp free(s->commit_id);
5140 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5141 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5142 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5143 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5144 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5145 ad80ab7b 2018-08-04 stsp free(parent);
5146 ad80ab7b 2018-08-04 stsp
5147 ad80ab7b 2018-08-04 stsp }
5148 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5149 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5150 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5151 7c32bd05 2019-06-22 stsp return NULL;
5152 7c32bd05 2019-06-22 stsp }
5153 7c32bd05 2019-06-22 stsp
5154 7c32bd05 2019-06-22 stsp static const struct got_error *
5155 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5156 7c32bd05 2019-06-22 stsp {
5157 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5158 7c32bd05 2019-06-22 stsp
5159 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5160 7c32bd05 2019-06-22 stsp return NULL;
5161 7c32bd05 2019-06-22 stsp }
5162 7c32bd05 2019-06-22 stsp
5163 7c32bd05 2019-06-22 stsp static int
5164 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5165 7c32bd05 2019-06-22 stsp {
5166 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5167 7c32bd05 2019-06-22 stsp
5168 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5169 56e0773d 2019-11-28 stsp 0) == 0;
5170 7c32bd05 2019-06-22 stsp }
5171 7c32bd05 2019-06-22 stsp
5172 7c32bd05 2019-06-22 stsp static const struct got_error *
5173 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5174 7c32bd05 2019-06-22 stsp {
5175 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5176 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5177 7c32bd05 2019-06-22 stsp
5178 7c32bd05 2019-06-22 stsp if (!view->searching) {
5179 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5180 7c32bd05 2019-06-22 stsp return NULL;
5181 7c32bd05 2019-06-22 stsp }
5182 7c32bd05 2019-06-22 stsp
5183 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5184 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5185 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5186 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5187 56e0773d 2019-11-28 stsp s->selected_entry);
5188 7c32bd05 2019-06-22 stsp else
5189 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5190 56e0773d 2019-11-28 stsp } else {
5191 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5192 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5193 56e0773d 2019-11-28 stsp else
5194 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5195 56e0773d 2019-11-28 stsp s->selected_entry);
5196 7c32bd05 2019-06-22 stsp }
5197 7c32bd05 2019-06-22 stsp } else {
5198 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5199 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5200 56e0773d 2019-11-28 stsp else
5201 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5202 7c32bd05 2019-06-22 stsp }
5203 7c32bd05 2019-06-22 stsp
5204 7c32bd05 2019-06-22 stsp while (1) {
5205 56e0773d 2019-11-28 stsp if (te == NULL) {
5206 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5207 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5208 ac66afb8 2019-06-24 stsp return NULL;
5209 ac66afb8 2019-06-24 stsp }
5210 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5211 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5212 56e0773d 2019-11-28 stsp else
5213 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5214 7c32bd05 2019-06-22 stsp }
5215 7c32bd05 2019-06-22 stsp
5216 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5217 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5218 56e0773d 2019-11-28 stsp s->matched_entry = te;
5219 7c32bd05 2019-06-22 stsp break;
5220 7c32bd05 2019-06-22 stsp }
5221 7c32bd05 2019-06-22 stsp
5222 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5223 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5224 56e0773d 2019-11-28 stsp else
5225 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5226 7c32bd05 2019-06-22 stsp }
5227 e5a0f69f 2018-08-18 stsp
5228 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5229 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5230 7c32bd05 2019-06-22 stsp s->selected = 0;
5231 7c32bd05 2019-06-22 stsp }
5232 7c32bd05 2019-06-22 stsp
5233 e5a0f69f 2018-08-18 stsp return NULL;
5234 ad80ab7b 2018-08-04 stsp }
5235 ad80ab7b 2018-08-04 stsp
5236 ad80ab7b 2018-08-04 stsp static const struct got_error *
5237 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5238 ad80ab7b 2018-08-04 stsp {
5239 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5240 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5241 e5a0f69f 2018-08-18 stsp char *parent_path;
5242 ad80ab7b 2018-08-04 stsp
5243 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5244 e5a0f69f 2018-08-18 stsp if (err)
5245 e5a0f69f 2018-08-18 stsp return err;
5246 ffd1d5e5 2018-06-23 stsp
5247 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5248 e5a0f69f 2018-08-18 stsp free(parent_path);
5249 669b5ffa 2018-10-07 stsp
5250 669b5ffa 2018-10-07 stsp view_vborder(view);
5251 e5a0f69f 2018-08-18 stsp return err;
5252 e5a0f69f 2018-08-18 stsp }
5253 ce52c690 2018-06-23 stsp
5254 e5a0f69f 2018-08-18 stsp static const struct got_error *
5255 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5256 e5a0f69f 2018-08-18 stsp {
5257 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5258 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5259 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5260 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5261 ffd1d5e5 2018-06-23 stsp
5262 e5a0f69f 2018-08-18 stsp switch (ch) {
5263 1e37a5c2 2019-05-12 jcs case 'i':
5264 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5265 1e37a5c2 2019-05-12 jcs break;
5266 1e37a5c2 2019-05-12 jcs case 'l':
5267 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5268 ffd1d5e5 2018-06-23 stsp break;
5269 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5270 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5271 78756c87 2020-11-24 stsp err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5272 78756c87 2020-11-24 stsp &s->parents, s->commit_id, s->repo);
5273 e78dc838 2020-12-04 stsp view->focussed = 0;
5274 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5275 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5276 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5277 1e37a5c2 2019-05-12 jcs if (err)
5278 1e37a5c2 2019-05-12 jcs return err;
5279 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5280 e78dc838 2020-12-04 stsp view->focus_child = 1;
5281 1e37a5c2 2019-05-12 jcs } else
5282 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5283 152c1c93 2020-11-29 stsp break;
5284 152c1c93 2020-11-29 stsp case 'r':
5285 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5286 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5287 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5288 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5289 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5290 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5291 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5292 152c1c93 2020-11-29 stsp if (err) {
5293 152c1c93 2020-11-29 stsp view_close(ref_view);
5294 152c1c93 2020-11-29 stsp return err;
5295 152c1c93 2020-11-29 stsp }
5296 e78dc838 2020-12-04 stsp view->focussed = 0;
5297 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5298 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5299 152c1c93 2020-11-29 stsp err = view_close_child(view);
5300 152c1c93 2020-11-29 stsp if (err)
5301 152c1c93 2020-11-29 stsp return err;
5302 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5303 e78dc838 2020-12-04 stsp view->focus_child = 1;
5304 152c1c93 2020-11-29 stsp } else
5305 152c1c93 2020-11-29 stsp *new_view = ref_view;
5306 1e37a5c2 2019-05-12 jcs break;
5307 1e37a5c2 2019-05-12 jcs case 'k':
5308 1e37a5c2 2019-05-12 jcs case KEY_UP:
5309 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5310 1e37a5c2 2019-05-12 jcs s->selected--;
5311 fa86c4bf 2020-11-29 stsp break;
5312 1e37a5c2 2019-05-12 jcs }
5313 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5314 1e37a5c2 2019-05-12 jcs break;
5315 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5316 ea025d1d 2020-02-22 naddy case CTRL('b'):
5317 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5318 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5319 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5320 fa86c4bf 2020-11-29 stsp s->selected = 0;
5321 fa86c4bf 2020-11-29 stsp } else {
5322 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5323 fa86c4bf 2020-11-29 stsp s->selected = 0;
5324 fa86c4bf 2020-11-29 stsp }
5325 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5326 1e37a5c2 2019-05-12 jcs break;
5327 1e37a5c2 2019-05-12 jcs case 'j':
5328 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5329 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5330 1e37a5c2 2019-05-12 jcs s->selected++;
5331 1e37a5c2 2019-05-12 jcs break;
5332 1e37a5c2 2019-05-12 jcs }
5333 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5334 56e0773d 2019-11-28 stsp == NULL)
5335 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5336 1e37a5c2 2019-05-12 jcs break;
5337 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5338 1e37a5c2 2019-05-12 jcs break;
5339 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5340 ea025d1d 2020-02-22 naddy case CTRL('f'):
5341 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5342 1e37a5c2 2019-05-12 jcs == NULL) {
5343 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5344 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5345 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5346 1e37a5c2 2019-05-12 jcs break;
5347 1e37a5c2 2019-05-12 jcs }
5348 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5349 1e37a5c2 2019-05-12 jcs break;
5350 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5351 1e37a5c2 2019-05-12 jcs case '\r':
5352 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5353 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5354 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5355 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5356 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5357 1e37a5c2 2019-05-12 jcs break;
5358 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5359 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5360 1e37a5c2 2019-05-12 jcs entry);
5361 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5362 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5363 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5364 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5365 1e37a5c2 2019-05-12 jcs s->selected_entry =
5366 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5367 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5368 1e37a5c2 2019-05-12 jcs free(parent);
5369 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5370 56e0773d 2019-11-28 stsp s->selected_entry))) {
5371 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5372 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5373 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5374 1e37a5c2 2019-05-12 jcs if (err)
5375 1e37a5c2 2019-05-12 jcs break;
5376 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5377 941e9f74 2019-05-21 stsp if (err) {
5378 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5379 1e37a5c2 2019-05-12 jcs break;
5380 1e37a5c2 2019-05-12 jcs }
5381 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5382 56e0773d 2019-11-28 stsp s->selected_entry))) {
5383 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5384 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5385 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5386 1e37a5c2 2019-05-12 jcs
5387 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5388 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5389 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5390 1e37a5c2 2019-05-12 jcs if (err)
5391 1e37a5c2 2019-05-12 jcs break;
5392 e78dc838 2020-12-04 stsp view->focussed = 0;
5393 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5394 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5395 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5396 669b5ffa 2018-10-07 stsp if (err)
5397 669b5ffa 2018-10-07 stsp return err;
5398 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5399 e78dc838 2020-12-04 stsp view->focus_child = 1;
5400 669b5ffa 2018-10-07 stsp } else
5401 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5402 1e37a5c2 2019-05-12 jcs }
5403 1e37a5c2 2019-05-12 jcs break;
5404 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5405 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5406 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5407 1e37a5c2 2019-05-12 jcs break;
5408 1e37a5c2 2019-05-12 jcs default:
5409 1e37a5c2 2019-05-12 jcs break;
5410 ffd1d5e5 2018-06-23 stsp }
5411 e5a0f69f 2018-08-18 stsp
5412 ffd1d5e5 2018-06-23 stsp return err;
5413 9f7d7167 2018-04-29 stsp }
5414 9f7d7167 2018-04-29 stsp
5415 ffd1d5e5 2018-06-23 stsp __dead static void
5416 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5417 ffd1d5e5 2018-06-23 stsp {
5418 ffd1d5e5 2018-06-23 stsp endwin();
5419 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5420 ffd1d5e5 2018-06-23 stsp getprogname());
5421 ffd1d5e5 2018-06-23 stsp exit(1);
5422 ffd1d5e5 2018-06-23 stsp }
5423 ffd1d5e5 2018-06-23 stsp
5424 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5425 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5426 ffd1d5e5 2018-06-23 stsp {
5427 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5428 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5429 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5430 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5431 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5432 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5433 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5434 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5435 ffd1d5e5 2018-06-23 stsp int ch;
5436 5221c383 2018-08-01 stsp struct tog_view *view;
5437 70ac5f84 2019-03-28 stsp
5438 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5439 ffd1d5e5 2018-06-23 stsp switch (ch) {
5440 ffd1d5e5 2018-06-23 stsp case 'c':
5441 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5442 74283ab8 2020-02-07 stsp break;
5443 74283ab8 2020-02-07 stsp case 'r':
5444 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5445 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5446 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5447 74283ab8 2020-02-07 stsp optarg);
5448 ffd1d5e5 2018-06-23 stsp break;
5449 ffd1d5e5 2018-06-23 stsp default:
5450 e99e2d15 2020-11-24 naddy usage_tree();
5451 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5452 ffd1d5e5 2018-06-23 stsp }
5453 ffd1d5e5 2018-06-23 stsp }
5454 ffd1d5e5 2018-06-23 stsp
5455 ffd1d5e5 2018-06-23 stsp argc -= optind;
5456 ffd1d5e5 2018-06-23 stsp argv += optind;
5457 ffd1d5e5 2018-06-23 stsp
5458 55cccc34 2020-02-20 stsp if (argc > 1)
5459 e99e2d15 2020-11-24 naddy usage_tree();
5460 74283ab8 2020-02-07 stsp
5461 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5462 55cccc34 2020-02-20 stsp if (cwd == NULL)
5463 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5464 55cccc34 2020-02-20 stsp
5465 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5466 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5467 55cccc34 2020-02-20 stsp goto done;
5468 55cccc34 2020-02-20 stsp
5469 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5470 55cccc34 2020-02-20 stsp if (worktree)
5471 52185f70 2019-02-05 stsp repo_path =
5472 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5473 55cccc34 2020-02-20 stsp else
5474 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5475 74283ab8 2020-02-07 stsp }
5476 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5477 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5478 55cccc34 2020-02-20 stsp goto done;
5479 55cccc34 2020-02-20 stsp }
5480 a915003a 2019-02-05 stsp
5481 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5482 c02c541e 2019-03-29 stsp if (error != NULL)
5483 52185f70 2019-02-05 stsp goto done;
5484 d188b9a6 2019-01-04 stsp
5485 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5486 55cccc34 2020-02-20 stsp repo, worktree);
5487 55cccc34 2020-02-20 stsp if (error)
5488 55cccc34 2020-02-20 stsp goto done;
5489 55cccc34 2020-02-20 stsp
5490 55cccc34 2020-02-20 stsp init_curses();
5491 55cccc34 2020-02-20 stsp
5492 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5493 c02c541e 2019-03-29 stsp if (error)
5494 52185f70 2019-02-05 stsp goto done;
5495 ffd1d5e5 2018-06-23 stsp
5496 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5497 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5498 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5499 55cccc34 2020-02-20 stsp if (error)
5500 ffd1d5e5 2018-06-23 stsp goto done;
5501 ffd1d5e5 2018-06-23 stsp
5502 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5503 55cccc34 2020-02-20 stsp if (error)
5504 ffd1d5e5 2018-06-23 stsp goto done;
5505 ffd1d5e5 2018-06-23 stsp
5506 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5507 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5508 55cccc34 2020-02-20 stsp if (error)
5509 8b473291 2019-02-21 stsp goto done;
5510 8b473291 2019-02-21 stsp
5511 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5512 5221c383 2018-08-01 stsp if (view == NULL) {
5513 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5514 5221c383 2018-08-01 stsp goto done;
5515 5221c383 2018-08-01 stsp }
5516 78756c87 2020-11-24 stsp error = open_tree_view(view, tree, commit_id, repo);
5517 ad80ab7b 2018-08-04 stsp if (error)
5518 ad80ab7b 2018-08-04 stsp goto done;
5519 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5520 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5521 d91faf3b 2020-12-01 naddy in_repo_path);
5522 55cccc34 2020-02-20 stsp if (error)
5523 55cccc34 2020-02-20 stsp goto done;
5524 55cccc34 2020-02-20 stsp }
5525 55cccc34 2020-02-20 stsp
5526 55cccc34 2020-02-20 stsp if (worktree) {
5527 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5528 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5529 55cccc34 2020-02-20 stsp worktree = NULL;
5530 55cccc34 2020-02-20 stsp }
5531 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5532 ffd1d5e5 2018-06-23 stsp done:
5533 52185f70 2019-02-05 stsp free(repo_path);
5534 e4a0e26d 2020-02-20 stsp free(cwd);
5535 ffd1d5e5 2018-06-23 stsp free(commit_id);
5536 ffd1d5e5 2018-06-23 stsp if (commit)
5537 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5538 ffd1d5e5 2018-06-23 stsp if (tree)
5539 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5540 ffd1d5e5 2018-06-23 stsp if (repo)
5541 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5542 ffd1d5e5 2018-06-23 stsp return error;
5543 6458efa5 2020-11-24 stsp }
5544 6458efa5 2020-11-24 stsp
5545 6458efa5 2020-11-24 stsp static const struct got_error *
5546 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5547 6458efa5 2020-11-24 stsp {
5548 6458efa5 2020-11-24 stsp const struct got_error *err;
5549 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5550 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5551 6458efa5 2020-11-24 stsp
5552 6458efa5 2020-11-24 stsp err = got_ref_list(&s->simplerefs, s->repo, NULL,
5553 6458efa5 2020-11-24 stsp got_ref_cmp_by_name, NULL);
5554 6458efa5 2020-11-24 stsp if (err)
5555 6458efa5 2020-11-24 stsp return err;
5556 6458efa5 2020-11-24 stsp
5557 6458efa5 2020-11-24 stsp s->nrefs = 0;
5558 6458efa5 2020-11-24 stsp SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5559 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5560 6458efa5 2020-11-24 stsp continue;
5561 6458efa5 2020-11-24 stsp
5562 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5563 6458efa5 2020-11-24 stsp if (re == NULL)
5564 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5565 6458efa5 2020-11-24 stsp
5566 6458efa5 2020-11-24 stsp re->ref = sre->ref;
5567 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5568 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5569 6458efa5 2020-11-24 stsp }
5570 6458efa5 2020-11-24 stsp
5571 6458efa5 2020-11-24 stsp return NULL;
5572 6458efa5 2020-11-24 stsp }
5573 6458efa5 2020-11-24 stsp
5574 6458efa5 2020-11-24 stsp void
5575 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5576 6458efa5 2020-11-24 stsp {
5577 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5578 6458efa5 2020-11-24 stsp
5579 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5580 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5581 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5582 6458efa5 2020-11-24 stsp free(re);
5583 6458efa5 2020-11-24 stsp }
5584 6458efa5 2020-11-24 stsp got_ref_list_free(&s->simplerefs);
5585 6458efa5 2020-11-24 stsp }
5586 6458efa5 2020-11-24 stsp
5587 6458efa5 2020-11-24 stsp static const struct got_error *
5588 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5589 6458efa5 2020-11-24 stsp {
5590 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5591 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5592 6458efa5 2020-11-24 stsp
5593 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5594 6458efa5 2020-11-24 stsp s->repo = repo;
5595 6458efa5 2020-11-24 stsp
5596 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->simplerefs);
5597 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5598 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5599 6458efa5 2020-11-24 stsp
5600 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5601 6458efa5 2020-11-24 stsp if (err)
5602 6458efa5 2020-11-24 stsp return err;
5603 34ba6917 2020-11-30 stsp
5604 34ba6917 2020-11-30 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5605 6458efa5 2020-11-24 stsp
5606 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5607 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5608 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5609 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5610 6458efa5 2020-11-24 stsp if (err)
5611 6458efa5 2020-11-24 stsp goto done;
5612 6458efa5 2020-11-24 stsp
5613 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5614 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5615 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5616 6458efa5 2020-11-24 stsp if (err)
5617 6458efa5 2020-11-24 stsp goto done;
5618 6458efa5 2020-11-24 stsp
5619 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5620 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5621 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5622 6458efa5 2020-11-24 stsp if (err)
5623 6458efa5 2020-11-24 stsp goto done;
5624 6458efa5 2020-11-24 stsp }
5625 6458efa5 2020-11-24 stsp
5626 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5627 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5628 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5629 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5630 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5631 6458efa5 2020-11-24 stsp done:
5632 6458efa5 2020-11-24 stsp if (err)
5633 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5634 6458efa5 2020-11-24 stsp return err;
5635 6458efa5 2020-11-24 stsp }
5636 6458efa5 2020-11-24 stsp
5637 6458efa5 2020-11-24 stsp static const struct got_error *
5638 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5639 6458efa5 2020-11-24 stsp {
5640 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5641 6458efa5 2020-11-24 stsp
5642 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5643 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5644 6458efa5 2020-11-24 stsp
5645 6458efa5 2020-11-24 stsp return NULL;
5646 9f7d7167 2018-04-29 stsp }
5647 ce5b7c56 2019-07-09 stsp
5648 6458efa5 2020-11-24 stsp static const struct got_error *
5649 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5650 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5651 6458efa5 2020-11-24 stsp {
5652 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5653 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5654 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5655 6458efa5 2020-11-24 stsp int obj_type;
5656 6458efa5 2020-11-24 stsp
5657 c42c9805 2020-11-24 stsp *commit_id = NULL;
5658 6458efa5 2020-11-24 stsp
5659 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5660 6458efa5 2020-11-24 stsp if (err)
5661 6458efa5 2020-11-24 stsp return err;
5662 6458efa5 2020-11-24 stsp
5663 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5664 6458efa5 2020-11-24 stsp if (err)
5665 6458efa5 2020-11-24 stsp goto done;
5666 6458efa5 2020-11-24 stsp
5667 6458efa5 2020-11-24 stsp switch (obj_type) {
5668 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5669 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5670 6458efa5 2020-11-24 stsp break;
5671 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5672 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5673 6458efa5 2020-11-24 stsp if (err)
5674 6458efa5 2020-11-24 stsp goto done;
5675 c42c9805 2020-11-24 stsp free(obj_id);
5676 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5677 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5678 c42c9805 2020-11-24 stsp if (err)
5679 6458efa5 2020-11-24 stsp goto done;
5680 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5681 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5682 c42c9805 2020-11-24 stsp goto done;
5683 c42c9805 2020-11-24 stsp }
5684 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5685 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5686 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5687 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5688 c42c9805 2020-11-24 stsp goto done;
5689 c42c9805 2020-11-24 stsp }
5690 6458efa5 2020-11-24 stsp break;
5691 6458efa5 2020-11-24 stsp default:
5692 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5693 c42c9805 2020-11-24 stsp break;
5694 6458efa5 2020-11-24 stsp }
5695 6458efa5 2020-11-24 stsp
5696 c42c9805 2020-11-24 stsp done:
5697 c42c9805 2020-11-24 stsp if (tag)
5698 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5699 c42c9805 2020-11-24 stsp if (err) {
5700 c42c9805 2020-11-24 stsp free(*commit_id);
5701 c42c9805 2020-11-24 stsp *commit_id = NULL;
5702 c42c9805 2020-11-24 stsp }
5703 c42c9805 2020-11-24 stsp return err;
5704 c42c9805 2020-11-24 stsp }
5705 c42c9805 2020-11-24 stsp
5706 c42c9805 2020-11-24 stsp static const struct got_error *
5707 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5708 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5709 c42c9805 2020-11-24 stsp {
5710 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5711 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5712 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5713 c42c9805 2020-11-24 stsp
5714 c42c9805 2020-11-24 stsp *new_view = NULL;
5715 c42c9805 2020-11-24 stsp
5716 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5717 c42c9805 2020-11-24 stsp if (err) {
5718 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5719 c42c9805 2020-11-24 stsp return err;
5720 c42c9805 2020-11-24 stsp else
5721 c42c9805 2020-11-24 stsp return NULL;
5722 c42c9805 2020-11-24 stsp }
5723 c42c9805 2020-11-24 stsp
5724 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5725 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5726 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5727 6458efa5 2020-11-24 stsp goto done;
5728 6458efa5 2020-11-24 stsp }
5729 6458efa5 2020-11-24 stsp
5730 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5731 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5732 6458efa5 2020-11-24 stsp done:
5733 6458efa5 2020-11-24 stsp if (err)
5734 6458efa5 2020-11-24 stsp view_close(log_view);
5735 6458efa5 2020-11-24 stsp else
5736 6458efa5 2020-11-24 stsp *new_view = log_view;
5737 c42c9805 2020-11-24 stsp free(commit_id);
5738 6458efa5 2020-11-24 stsp return err;
5739 6458efa5 2020-11-24 stsp }
5740 6458efa5 2020-11-24 stsp
5741 ce5b7c56 2019-07-09 stsp static void
5742 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5743 6458efa5 2020-11-24 stsp {
5744 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5745 34ba6917 2020-11-30 stsp int i = 0;
5746 6458efa5 2020-11-24 stsp
5747 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5748 6458efa5 2020-11-24 stsp return;
5749 6458efa5 2020-11-24 stsp
5750 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5751 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5752 34ba6917 2020-11-30 stsp if (re == NULL)
5753 34ba6917 2020-11-30 stsp break;
5754 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5755 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5756 6458efa5 2020-11-24 stsp }
5757 6458efa5 2020-11-24 stsp }
5758 6458efa5 2020-11-24 stsp
5759 34ba6917 2020-11-30 stsp static void
5760 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5761 6458efa5 2020-11-24 stsp {
5762 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5763 6458efa5 2020-11-24 stsp int n = 0;
5764 6458efa5 2020-11-24 stsp
5765 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5766 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5767 6458efa5 2020-11-24 stsp else
5768 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5769 6458efa5 2020-11-24 stsp
5770 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5771 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5772 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5773 6458efa5 2020-11-24 stsp if (last) {
5774 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5775 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5776 6458efa5 2020-11-24 stsp }
5777 6458efa5 2020-11-24 stsp }
5778 6458efa5 2020-11-24 stsp }
5779 6458efa5 2020-11-24 stsp
5780 6458efa5 2020-11-24 stsp static const struct got_error *
5781 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5782 6458efa5 2020-11-24 stsp {
5783 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5784 6458efa5 2020-11-24 stsp
5785 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5786 6458efa5 2020-11-24 stsp return NULL;
5787 6458efa5 2020-11-24 stsp }
5788 6458efa5 2020-11-24 stsp
5789 6458efa5 2020-11-24 stsp static int
5790 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5791 6458efa5 2020-11-24 stsp {
5792 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5793 6458efa5 2020-11-24 stsp
5794 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5795 6458efa5 2020-11-24 stsp 0) == 0;
5796 6458efa5 2020-11-24 stsp }
5797 6458efa5 2020-11-24 stsp
5798 6458efa5 2020-11-24 stsp static const struct got_error *
5799 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5800 6458efa5 2020-11-24 stsp {
5801 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5802 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5803 6458efa5 2020-11-24 stsp
5804 6458efa5 2020-11-24 stsp if (!view->searching) {
5805 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5806 6458efa5 2020-11-24 stsp return NULL;
5807 6458efa5 2020-11-24 stsp }
5808 6458efa5 2020-11-24 stsp
5809 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5810 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5811 6458efa5 2020-11-24 stsp if (s->selected_entry)
5812 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5813 6458efa5 2020-11-24 stsp else
5814 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5815 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5816 6458efa5 2020-11-24 stsp } else {
5817 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5818 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5819 6458efa5 2020-11-24 stsp else
5820 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5821 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5822 6458efa5 2020-11-24 stsp }
5823 6458efa5 2020-11-24 stsp } else {
5824 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5825 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5826 6458efa5 2020-11-24 stsp else
5827 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5828 6458efa5 2020-11-24 stsp }
5829 6458efa5 2020-11-24 stsp
5830 6458efa5 2020-11-24 stsp while (1) {
5831 6458efa5 2020-11-24 stsp if (re == NULL) {
5832 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5833 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5834 6458efa5 2020-11-24 stsp return NULL;
5835 6458efa5 2020-11-24 stsp }
5836 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5837 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5838 6458efa5 2020-11-24 stsp else
5839 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5840 6458efa5 2020-11-24 stsp }
5841 6458efa5 2020-11-24 stsp
5842 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5843 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5844 6458efa5 2020-11-24 stsp s->matched_entry = re;
5845 6458efa5 2020-11-24 stsp break;
5846 6458efa5 2020-11-24 stsp }
5847 6458efa5 2020-11-24 stsp
5848 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5849 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5850 6458efa5 2020-11-24 stsp else
5851 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5852 6458efa5 2020-11-24 stsp }
5853 6458efa5 2020-11-24 stsp
5854 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5855 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5856 6458efa5 2020-11-24 stsp s->selected = 0;
5857 6458efa5 2020-11-24 stsp }
5858 6458efa5 2020-11-24 stsp
5859 6458efa5 2020-11-24 stsp return NULL;
5860 6458efa5 2020-11-24 stsp }
5861 6458efa5 2020-11-24 stsp
5862 6458efa5 2020-11-24 stsp static const struct got_error *
5863 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5864 6458efa5 2020-11-24 stsp {
5865 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5866 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5867 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5868 6458efa5 2020-11-24 stsp char *line = NULL;
5869 6458efa5 2020-11-24 stsp wchar_t *wline;
5870 6458efa5 2020-11-24 stsp struct tog_color *tc;
5871 6458efa5 2020-11-24 stsp int width, n;
5872 6458efa5 2020-11-24 stsp int limit = view->nlines;
5873 6458efa5 2020-11-24 stsp
5874 6458efa5 2020-11-24 stsp werase(view->window);
5875 6458efa5 2020-11-24 stsp
5876 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5877 6458efa5 2020-11-24 stsp
5878 6458efa5 2020-11-24 stsp if (limit == 0)
5879 6458efa5 2020-11-24 stsp return NULL;
5880 6458efa5 2020-11-24 stsp
5881 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5882 6458efa5 2020-11-24 stsp
5883 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5884 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5885 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5886 6458efa5 2020-11-24 stsp
5887 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5888 6458efa5 2020-11-24 stsp if (err) {
5889 6458efa5 2020-11-24 stsp free(line);
5890 6458efa5 2020-11-24 stsp return err;
5891 6458efa5 2020-11-24 stsp }
5892 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5893 6458efa5 2020-11-24 stsp wstandout(view->window);
5894 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5895 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5896 6458efa5 2020-11-24 stsp wstandend(view->window);
5897 6458efa5 2020-11-24 stsp free(wline);
5898 6458efa5 2020-11-24 stsp wline = NULL;
5899 6458efa5 2020-11-24 stsp free(line);
5900 6458efa5 2020-11-24 stsp line = NULL;
5901 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5902 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5903 6458efa5 2020-11-24 stsp if (--limit <= 0)
5904 6458efa5 2020-11-24 stsp return NULL;
5905 6458efa5 2020-11-24 stsp
5906 6458efa5 2020-11-24 stsp n = 0;
5907 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5908 6458efa5 2020-11-24 stsp char *line = NULL;
5909 6458efa5 2020-11-24 stsp
5910 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5911 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5912 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5913 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5914 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5915 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5916 6458efa5 2020-11-24 stsp struct got_object_id *id;
5917 6458efa5 2020-11-24 stsp char *id_str;
5918 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5919 6458efa5 2020-11-24 stsp if (err)
5920 6458efa5 2020-11-24 stsp return err;
5921 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5922 6458efa5 2020-11-24 stsp if (err) {
5923 6458efa5 2020-11-24 stsp free(id);
5924 6458efa5 2020-11-24 stsp return err;
5925 6458efa5 2020-11-24 stsp }
5926 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
5927 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
5928 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
5929 6458efa5 2020-11-24 stsp free(id);
5930 6458efa5 2020-11-24 stsp free(id_str);
5931 6458efa5 2020-11-24 stsp return err;
5932 6458efa5 2020-11-24 stsp }
5933 6458efa5 2020-11-24 stsp free(id);
5934 6458efa5 2020-11-24 stsp free(id_str);
5935 6458efa5 2020-11-24 stsp } else {
5936 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
5937 6458efa5 2020-11-24 stsp if (line == NULL)
5938 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
5939 6458efa5 2020-11-24 stsp }
5940 6458efa5 2020-11-24 stsp
5941 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5942 6458efa5 2020-11-24 stsp if (err) {
5943 6458efa5 2020-11-24 stsp free(line);
5944 6458efa5 2020-11-24 stsp return err;
5945 6458efa5 2020-11-24 stsp }
5946 6458efa5 2020-11-24 stsp if (n == s->selected) {
5947 6458efa5 2020-11-24 stsp if (view->focussed)
5948 6458efa5 2020-11-24 stsp wstandout(view->window);
5949 6458efa5 2020-11-24 stsp s->selected_entry = re;
5950 6458efa5 2020-11-24 stsp }
5951 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
5952 6458efa5 2020-11-24 stsp if (tc)
5953 6458efa5 2020-11-24 stsp wattr_on(view->window,
5954 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
5955 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5956 6458efa5 2020-11-24 stsp if (tc)
5957 6458efa5 2020-11-24 stsp wattr_off(view->window,
5958 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
5959 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5960 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5961 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
5962 6458efa5 2020-11-24 stsp wstandend(view->window);
5963 6458efa5 2020-11-24 stsp free(line);
5964 6458efa5 2020-11-24 stsp free(wline);
5965 6458efa5 2020-11-24 stsp wline = NULL;
5966 6458efa5 2020-11-24 stsp n++;
5967 6458efa5 2020-11-24 stsp s->ndisplayed++;
5968 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
5969 6458efa5 2020-11-24 stsp
5970 6458efa5 2020-11-24 stsp limit--;
5971 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5972 6458efa5 2020-11-24 stsp }
5973 6458efa5 2020-11-24 stsp
5974 6458efa5 2020-11-24 stsp view_vborder(view);
5975 6458efa5 2020-11-24 stsp return err;
5976 6458efa5 2020-11-24 stsp }
5977 6458efa5 2020-11-24 stsp
5978 6458efa5 2020-11-24 stsp static const struct got_error *
5979 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
5980 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5981 c42c9805 2020-11-24 stsp {
5982 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5983 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
5984 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
5985 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
5986 c42c9805 2020-11-24 stsp
5987 c42c9805 2020-11-24 stsp *new_view = NULL;
5988 c42c9805 2020-11-24 stsp
5989 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5990 c42c9805 2020-11-24 stsp if (err) {
5991 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5992 c42c9805 2020-11-24 stsp return err;
5993 c42c9805 2020-11-24 stsp else
5994 c42c9805 2020-11-24 stsp return NULL;
5995 c42c9805 2020-11-24 stsp }
5996 c42c9805 2020-11-24 stsp
5997 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
5998 c42c9805 2020-11-24 stsp if (err)
5999 c42c9805 2020-11-24 stsp goto done;
6000 c42c9805 2020-11-24 stsp
6001 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6002 c42c9805 2020-11-24 stsp if (err)
6003 c42c9805 2020-11-24 stsp goto done;
6004 c42c9805 2020-11-24 stsp
6005 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6006 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6007 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6008 c42c9805 2020-11-24 stsp goto done;
6009 c42c9805 2020-11-24 stsp }
6010 c42c9805 2020-11-24 stsp
6011 c42c9805 2020-11-24 stsp err = open_tree_view(tree_view, tree, commit_id, repo);
6012 c42c9805 2020-11-24 stsp if (err)
6013 c42c9805 2020-11-24 stsp goto done;
6014 c42c9805 2020-11-24 stsp
6015 c42c9805 2020-11-24 stsp *new_view = tree_view;
6016 c42c9805 2020-11-24 stsp done:
6017 c42c9805 2020-11-24 stsp free(commit_id);
6018 c42c9805 2020-11-24 stsp free(tree_id);
6019 c42c9805 2020-11-24 stsp if (err) {
6020 c42c9805 2020-11-24 stsp if (tree)
6021 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6022 c42c9805 2020-11-24 stsp }
6023 c42c9805 2020-11-24 stsp return err;
6024 c42c9805 2020-11-24 stsp }
6025 c42c9805 2020-11-24 stsp static const struct got_error *
6026 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6027 6458efa5 2020-11-24 stsp {
6028 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6029 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6030 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6031 34ba6917 2020-11-30 stsp int begin_x = 0;
6032 6458efa5 2020-11-24 stsp
6033 6458efa5 2020-11-24 stsp switch (ch) {
6034 6458efa5 2020-11-24 stsp case 'i':
6035 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6036 6458efa5 2020-11-24 stsp break;
6037 6458efa5 2020-11-24 stsp case KEY_ENTER:
6038 6458efa5 2020-11-24 stsp case '\r':
6039 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6040 6458efa5 2020-11-24 stsp break;
6041 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6042 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6043 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6044 6458efa5 2020-11-24 stsp s->repo);
6045 e78dc838 2020-12-04 stsp view->focussed = 0;
6046 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6047 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6048 6458efa5 2020-11-24 stsp err = view_close_child(view);
6049 6458efa5 2020-11-24 stsp if (err)
6050 6458efa5 2020-11-24 stsp return err;
6051 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6052 e78dc838 2020-12-04 stsp view->focus_child = 1;
6053 6458efa5 2020-11-24 stsp } else
6054 6458efa5 2020-11-24 stsp *new_view = log_view;
6055 6458efa5 2020-11-24 stsp break;
6056 c42c9805 2020-11-24 stsp case 't':
6057 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6058 c42c9805 2020-11-24 stsp break;
6059 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6060 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6061 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6062 c42c9805 2020-11-24 stsp s->repo);
6063 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6064 c42c9805 2020-11-24 stsp break;
6065 e78dc838 2020-12-04 stsp view->focussed = 0;
6066 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6067 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6068 c42c9805 2020-11-24 stsp err = view_close_child(view);
6069 c42c9805 2020-11-24 stsp if (err)
6070 c42c9805 2020-11-24 stsp return err;
6071 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6072 e78dc838 2020-12-04 stsp view->focus_child = 1;
6073 c42c9805 2020-11-24 stsp } else
6074 c42c9805 2020-11-24 stsp *new_view = tree_view;
6075 c42c9805 2020-11-24 stsp break;
6076 6458efa5 2020-11-24 stsp case 'k':
6077 6458efa5 2020-11-24 stsp case KEY_UP:
6078 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6079 6458efa5 2020-11-24 stsp s->selected--;
6080 6458efa5 2020-11-24 stsp break;
6081 34ba6917 2020-11-30 stsp }
6082 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6083 6458efa5 2020-11-24 stsp break;
6084 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6085 6458efa5 2020-11-24 stsp case CTRL('b'):
6086 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6087 34ba6917 2020-11-30 stsp s->selected = 0;
6088 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6089 6458efa5 2020-11-24 stsp break;
6090 6458efa5 2020-11-24 stsp case 'j':
6091 6458efa5 2020-11-24 stsp case KEY_DOWN:
6092 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6093 6458efa5 2020-11-24 stsp s->selected++;
6094 6458efa5 2020-11-24 stsp break;
6095 6458efa5 2020-11-24 stsp }
6096 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6097 6458efa5 2020-11-24 stsp /* can't scroll any further */
6098 6458efa5 2020-11-24 stsp break;
6099 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6100 6458efa5 2020-11-24 stsp break;
6101 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6102 6458efa5 2020-11-24 stsp case CTRL('f'):
6103 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6104 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6105 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6106 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6107 6458efa5 2020-11-24 stsp break;
6108 6458efa5 2020-11-24 stsp }
6109 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6110 6458efa5 2020-11-24 stsp break;
6111 6458efa5 2020-11-24 stsp case CTRL('l'):
6112 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6113 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6114 6458efa5 2020-11-24 stsp break;
6115 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6116 6458efa5 2020-11-24 stsp if (s->selected > view->nlines)
6117 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6118 6458efa5 2020-11-24 stsp break;
6119 6458efa5 2020-11-24 stsp default:
6120 6458efa5 2020-11-24 stsp break;
6121 6458efa5 2020-11-24 stsp }
6122 6458efa5 2020-11-24 stsp
6123 6458efa5 2020-11-24 stsp return err;
6124 6458efa5 2020-11-24 stsp }
6125 6458efa5 2020-11-24 stsp
6126 6458efa5 2020-11-24 stsp __dead static void
6127 6458efa5 2020-11-24 stsp usage_ref(void)
6128 6458efa5 2020-11-24 stsp {
6129 6458efa5 2020-11-24 stsp endwin();
6130 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6131 6458efa5 2020-11-24 stsp getprogname());
6132 6458efa5 2020-11-24 stsp exit(1);
6133 6458efa5 2020-11-24 stsp }
6134 6458efa5 2020-11-24 stsp
6135 6458efa5 2020-11-24 stsp static const struct got_error *
6136 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6137 6458efa5 2020-11-24 stsp {
6138 6458efa5 2020-11-24 stsp const struct got_error *error;
6139 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6140 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6141 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6142 6458efa5 2020-11-24 stsp int ch;
6143 6458efa5 2020-11-24 stsp struct tog_view *view;
6144 6458efa5 2020-11-24 stsp
6145 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6146 6458efa5 2020-11-24 stsp switch (ch) {
6147 6458efa5 2020-11-24 stsp case 'r':
6148 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6149 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6150 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6151 6458efa5 2020-11-24 stsp optarg);
6152 6458efa5 2020-11-24 stsp break;
6153 6458efa5 2020-11-24 stsp default:
6154 e99e2d15 2020-11-24 naddy usage_ref();
6155 6458efa5 2020-11-24 stsp /* NOTREACHED */
6156 6458efa5 2020-11-24 stsp }
6157 6458efa5 2020-11-24 stsp }
6158 6458efa5 2020-11-24 stsp
6159 6458efa5 2020-11-24 stsp argc -= optind;
6160 6458efa5 2020-11-24 stsp argv += optind;
6161 6458efa5 2020-11-24 stsp
6162 6458efa5 2020-11-24 stsp if (argc > 1)
6163 e99e2d15 2020-11-24 naddy usage_ref();
6164 6458efa5 2020-11-24 stsp
6165 6458efa5 2020-11-24 stsp cwd = getcwd(NULL, 0);
6166 6458efa5 2020-11-24 stsp if (cwd == NULL)
6167 6458efa5 2020-11-24 stsp return got_error_from_errno("getcwd");
6168 6458efa5 2020-11-24 stsp
6169 6458efa5 2020-11-24 stsp error = got_worktree_open(&worktree, cwd);
6170 6458efa5 2020-11-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6171 6458efa5 2020-11-24 stsp goto done;
6172 6458efa5 2020-11-24 stsp
6173 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6174 6458efa5 2020-11-24 stsp if (worktree)
6175 6458efa5 2020-11-24 stsp repo_path =
6176 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6177 6458efa5 2020-11-24 stsp else
6178 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6179 6458efa5 2020-11-24 stsp }
6180 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6181 6458efa5 2020-11-24 stsp error = got_error_from_errno("strdup");
6182 6458efa5 2020-11-24 stsp goto done;
6183 6458efa5 2020-11-24 stsp }
6184 6458efa5 2020-11-24 stsp
6185 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6186 6458efa5 2020-11-24 stsp if (error != NULL)
6187 6458efa5 2020-11-24 stsp goto done;
6188 6458efa5 2020-11-24 stsp
6189 6458efa5 2020-11-24 stsp init_curses();
6190 6458efa5 2020-11-24 stsp
6191 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6192 6458efa5 2020-11-24 stsp if (error)
6193 6458efa5 2020-11-24 stsp goto done;
6194 6458efa5 2020-11-24 stsp
6195 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6196 6458efa5 2020-11-24 stsp if (view == NULL) {
6197 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6198 6458efa5 2020-11-24 stsp goto done;
6199 6458efa5 2020-11-24 stsp }
6200 6458efa5 2020-11-24 stsp
6201 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6202 6458efa5 2020-11-24 stsp if (error)
6203 6458efa5 2020-11-24 stsp goto done;
6204 6458efa5 2020-11-24 stsp
6205 6458efa5 2020-11-24 stsp if (worktree) {
6206 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6207 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6208 6458efa5 2020-11-24 stsp worktree = NULL;
6209 6458efa5 2020-11-24 stsp }
6210 6458efa5 2020-11-24 stsp error = view_loop(view);
6211 6458efa5 2020-11-24 stsp done:
6212 6458efa5 2020-11-24 stsp free(repo_path);
6213 6458efa5 2020-11-24 stsp free(cwd);
6214 6458efa5 2020-11-24 stsp if (repo)
6215 6458efa5 2020-11-24 stsp got_repo_close(repo);
6216 6458efa5 2020-11-24 stsp return error;
6217 6458efa5 2020-11-24 stsp }
6218 6458efa5 2020-11-24 stsp
6219 6458efa5 2020-11-24 stsp static void
6220 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6221 ce5b7c56 2019-07-09 stsp {
6222 ce5b7c56 2019-07-09 stsp int i;
6223 9f7d7167 2018-04-29 stsp
6224 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6225 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6226 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6227 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6228 ce5b7c56 2019-07-09 stsp }
6229 6879ba42 2020-10-01 naddy fputc('\n', fp);
6230 ce5b7c56 2019-07-09 stsp }
6231 ce5b7c56 2019-07-09 stsp
6232 4ed7e80c 2018-05-20 stsp __dead static void
6233 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6234 9f7d7167 2018-04-29 stsp {
6235 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6236 6879ba42 2020-10-01 naddy
6237 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6238 6879ba42 2020-10-01 naddy getprogname());
6239 6879ba42 2020-10-01 naddy if (hflag) {
6240 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6241 6879ba42 2020-10-01 naddy list_commands(fp);
6242 ee85c5e8 2020-02-29 stsp }
6243 6879ba42 2020-10-01 naddy exit(status);
6244 9f7d7167 2018-04-29 stsp }
6245 9f7d7167 2018-04-29 stsp
6246 c2301be8 2018-04-30 stsp static char **
6247 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6248 c2301be8 2018-04-30 stsp {
6249 ee85c5e8 2020-02-29 stsp va_list ap;
6250 c2301be8 2018-04-30 stsp char **argv;
6251 ee85c5e8 2020-02-29 stsp int i;
6252 c2301be8 2018-04-30 stsp
6253 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6254 ee85c5e8 2020-02-29 stsp
6255 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6256 c2301be8 2018-04-30 stsp if (argv == NULL)
6257 c2301be8 2018-04-30 stsp err(1, "calloc");
6258 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6259 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6260 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6261 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6262 c2301be8 2018-04-30 stsp }
6263 c2301be8 2018-04-30 stsp
6264 ee85c5e8 2020-02-29 stsp va_end(ap);
6265 c2301be8 2018-04-30 stsp return argv;
6266 ee85c5e8 2020-02-29 stsp }
6267 ee85c5e8 2020-02-29 stsp
6268 ee85c5e8 2020-02-29 stsp /*
6269 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6270 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6271 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6272 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6273 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6274 ee85c5e8 2020-02-29 stsp */
6275 ee85c5e8 2020-02-29 stsp static const struct got_error *
6276 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6277 ee85c5e8 2020-02-29 stsp {
6278 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6279 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6280 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6281 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6282 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6283 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6284 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6285 ee85c5e8 2020-02-29 stsp
6286 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6287 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6288 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6289 ee85c5e8 2020-02-29 stsp
6290 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6291 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6292 ee85c5e8 2020-02-29 stsp goto done;
6293 ee85c5e8 2020-02-29 stsp
6294 ee85c5e8 2020-02-29 stsp if (worktree)
6295 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6296 ee85c5e8 2020-02-29 stsp else
6297 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6298 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6299 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6300 ee85c5e8 2020-02-29 stsp goto done;
6301 ee85c5e8 2020-02-29 stsp }
6302 ee85c5e8 2020-02-29 stsp
6303 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6304 ee85c5e8 2020-02-29 stsp if (error != NULL)
6305 ee85c5e8 2020-02-29 stsp goto done;
6306 ee85c5e8 2020-02-29 stsp
6307 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6308 ee85c5e8 2020-02-29 stsp repo, worktree);
6309 ee85c5e8 2020-02-29 stsp if (error)
6310 ee85c5e8 2020-02-29 stsp goto done;
6311 ee85c5e8 2020-02-29 stsp
6312 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6313 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6314 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
6315 ee85c5e8 2020-02-29 stsp if (error)
6316 ee85c5e8 2020-02-29 stsp goto done;
6317 ee85c5e8 2020-02-29 stsp
6318 ee85c5e8 2020-02-29 stsp if (worktree) {
6319 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6320 ee85c5e8 2020-02-29 stsp worktree = NULL;
6321 ee85c5e8 2020-02-29 stsp }
6322 ee85c5e8 2020-02-29 stsp
6323 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6324 ee85c5e8 2020-02-29 stsp if (error) {
6325 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6326 ee85c5e8 2020-02-29 stsp goto done;
6327 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6328 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6329 6879ba42 2020-10-01 naddy usage(1, 1);
6330 ee85c5e8 2020-02-29 stsp /* not reached */
6331 ee85c5e8 2020-02-29 stsp }
6332 ee85c5e8 2020-02-29 stsp
6333 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6334 ee85c5e8 2020-02-29 stsp repo = NULL;
6335 ee85c5e8 2020-02-29 stsp
6336 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6337 ee85c5e8 2020-02-29 stsp if (error)
6338 ee85c5e8 2020-02-29 stsp goto done;
6339 ee85c5e8 2020-02-29 stsp
6340 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6341 ee85c5e8 2020-02-29 stsp argc = 4;
6342 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6343 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6344 ee85c5e8 2020-02-29 stsp done:
6345 ee85c5e8 2020-02-29 stsp if (repo)
6346 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6347 ee85c5e8 2020-02-29 stsp if (worktree)
6348 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6349 ee85c5e8 2020-02-29 stsp free(id);
6350 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6351 ee85c5e8 2020-02-29 stsp free(commit_id);
6352 ee85c5e8 2020-02-29 stsp free(cwd);
6353 ee85c5e8 2020-02-29 stsp free(repo_path);
6354 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6355 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6356 ee85c5e8 2020-02-29 stsp int i;
6357 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6358 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6359 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6360 ee85c5e8 2020-02-29 stsp }
6361 ee85c5e8 2020-02-29 stsp return error;
6362 c2301be8 2018-04-30 stsp }
6363 c2301be8 2018-04-30 stsp
6364 9f7d7167 2018-04-29 stsp int
6365 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6366 9f7d7167 2018-04-29 stsp {
6367 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6368 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6369 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6370 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6371 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6372 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6373 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6374 83cd27f8 2020-01-13 stsp };
6375 9f7d7167 2018-04-29 stsp
6376 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6377 9f7d7167 2018-04-29 stsp
6378 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6379 9f7d7167 2018-04-29 stsp switch (ch) {
6380 9f7d7167 2018-04-29 stsp case 'h':
6381 9f7d7167 2018-04-29 stsp hflag = 1;
6382 9f7d7167 2018-04-29 stsp break;
6383 53ccebc2 2019-07-30 stsp case 'V':
6384 53ccebc2 2019-07-30 stsp Vflag = 1;
6385 53ccebc2 2019-07-30 stsp break;
6386 9f7d7167 2018-04-29 stsp default:
6387 6879ba42 2020-10-01 naddy usage(hflag, 1);
6388 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6389 9f7d7167 2018-04-29 stsp }
6390 9f7d7167 2018-04-29 stsp }
6391 9f7d7167 2018-04-29 stsp
6392 9f7d7167 2018-04-29 stsp argc -= optind;
6393 9f7d7167 2018-04-29 stsp argv += optind;
6394 9814e6a3 2020-09-27 naddy optind = 1;
6395 c2301be8 2018-04-30 stsp optreset = 1;
6396 9f7d7167 2018-04-29 stsp
6397 53ccebc2 2019-07-30 stsp if (Vflag) {
6398 53ccebc2 2019-07-30 stsp got_version_print_str();
6399 6879ba42 2020-10-01 naddy return 0;
6400 53ccebc2 2019-07-30 stsp }
6401 4010e238 2020-12-04 stsp
6402 4010e238 2020-12-04 stsp #ifndef PROFILE
6403 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6404 4010e238 2020-12-04 stsp NULL) == -1)
6405 4010e238 2020-12-04 stsp err(1, "pledge");
6406 4010e238 2020-12-04 stsp #endif
6407 53ccebc2 2019-07-30 stsp
6408 c2301be8 2018-04-30 stsp if (argc == 0) {
6409 f29d3e89 2018-06-23 stsp if (hflag)
6410 6879ba42 2020-10-01 naddy usage(hflag, 0);
6411 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6412 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6413 c2301be8 2018-04-30 stsp argc = 1;
6414 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6415 c2301be8 2018-04-30 stsp } else {
6416 9f7d7167 2018-04-29 stsp int i;
6417 9f7d7167 2018-04-29 stsp
6418 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6419 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6420 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6421 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6422 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6423 9f7d7167 2018-04-29 stsp break;
6424 9f7d7167 2018-04-29 stsp }
6425 9f7d7167 2018-04-29 stsp }
6426 ee85c5e8 2020-02-29 stsp }
6427 3642c4c6 2019-07-09 stsp
6428 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6429 ee85c5e8 2020-02-29 stsp if (argc != 1)
6430 6879ba42 2020-10-01 naddy usage(0, 1);
6431 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6432 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6433 ee85c5e8 2020-02-29 stsp } else {
6434 ee85c5e8 2020-02-29 stsp if (hflag)
6435 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6436 ee85c5e8 2020-02-29 stsp else
6437 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6438 9f7d7167 2018-04-29 stsp }
6439 9f7d7167 2018-04-29 stsp
6440 9f7d7167 2018-04-29 stsp endwin();
6441 b46c1e04 2020-09-20 naddy putchar('\n');
6442 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6443 a2f4a359 2020-02-28 stsp int i;
6444 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6445 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6446 a2f4a359 2020-02-28 stsp free(cmd_argv);
6447 a2f4a359 2020-02-28 stsp }
6448 a2f4a359 2020-02-28 stsp
6449 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6450 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6451 9f7d7167 2018-04-29 stsp return 0;
6452 9f7d7167 2018-04-29 stsp }