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