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