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