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