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