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