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