2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.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>
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
108 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
110 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
111 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
112 ba4f502b 2018-08-04 stsp struct got_object_id *id;
113 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
116 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 ba4f502b 2018-08-04 stsp struct commit_queue {
118 ba4f502b 2018-08-04 stsp int ncommits;
119 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
122 f26dddb7 2019-11-08 stsp struct tog_color {
123 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
124 6d17833f 2019-11-08 stsp regex_t regex;
125 6d17833f 2019-11-08 stsp short colorpair;
127 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
129 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
132 cc488aa7 2022-01-23 stsp static const struct got_error *
133 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 cc488aa7 2022-01-23 stsp struct got_reference* re2)
136 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
137 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
138 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
140 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
141 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
145 cc488aa7 2022-01-23 stsp return NULL;
146 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
148 cc488aa7 2022-01-23 stsp return NULL;
151 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 cc488aa7 2022-01-23 stsp return NULL;
155 11b20872 2019-11-08 stsp static const struct got_error *
156 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
158 51a10b52 2020-12-26 stsp const struct got_error *err;
160 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
164 51a10b52 2020-12-26 stsp return err;
166 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
170 51a10b52 2020-12-26 stsp static void
171 51a10b52 2020-12-26 stsp tog_free_refs(void)
173 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
174 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
175 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
177 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
180 51a10b52 2020-12-26 stsp static const struct got_error *
181 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
182 11b20872 2019-11-08 stsp int idx, short color)
184 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
185 11b20872 2019-11-08 stsp struct tog_color *tc;
186 11b20872 2019-11-08 stsp int regerr = 0;
188 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
189 11b20872 2019-11-08 stsp return NULL;
191 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
193 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
194 11b20872 2019-11-08 stsp if (tc == NULL)
195 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
196 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
197 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 11b20872 2019-11-08 stsp if (regerr) {
199 11b20872 2019-11-08 stsp static char regerr_msg[512];
200 11b20872 2019-11-08 stsp static char err_msg[512];
201 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
202 11b20872 2019-11-08 stsp sizeof(regerr_msg));
203 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 11b20872 2019-11-08 stsp regerr_msg);
205 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
207 11b20872 2019-11-08 stsp return err;
209 11b20872 2019-11-08 stsp tc->colorpair = idx;
210 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
211 11b20872 2019-11-08 stsp return NULL;
214 11b20872 2019-11-08 stsp static void
215 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
217 11b20872 2019-11-08 stsp struct tog_color *tc;
219 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
220 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
221 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
222 11b20872 2019-11-08 stsp regfree(&tc->regex);
227 11b20872 2019-11-08 stsp struct tog_color *
228 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
230 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
232 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
233 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
237 11b20872 2019-11-08 stsp return NULL;
241 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
243 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
245 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 11b20872 2019-11-08 stsp return COLOR_CYAN;
247 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 11b20872 2019-11-08 stsp return COLOR_YELLOW;
249 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 11b20872 2019-11-08 stsp return COLOR_GREEN;
251 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
253 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
255 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 91b8c405 2020-01-25 stsp return COLOR_CYAN;
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 11b20872 2019-11-08 stsp return COLOR_GREEN;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 11b20872 2019-11-08 stsp return COLOR_GREEN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 11b20872 2019-11-08 stsp return COLOR_CYAN;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 11b20872 2019-11-08 stsp return COLOR_YELLOW;
265 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 6458efa5 2020-11-24 stsp return COLOR_GREEN;
267 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
269 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
271 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
278 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
280 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
282 11b20872 2019-11-08 stsp if (val == NULL)
283 11b20872 2019-11-08 stsp return default_color_value(envvar);
285 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
286 11b20872 2019-11-08 stsp return COLOR_BLACK;
287 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
288 11b20872 2019-11-08 stsp return COLOR_RED;
289 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
290 11b20872 2019-11-08 stsp return COLOR_GREEN;
291 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
292 11b20872 2019-11-08 stsp return COLOR_YELLOW;
293 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
294 11b20872 2019-11-08 stsp return COLOR_BLUE;
295 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
296 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
297 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
298 11b20872 2019-11-08 stsp return COLOR_CYAN;
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
300 11b20872 2019-11-08 stsp return COLOR_WHITE;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
304 11b20872 2019-11-08 stsp return default_color_value(envvar);
308 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
309 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
310 3dbaef42 2020-11-24 stsp const char *label1, *label2;
311 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
312 15a087fe 2019-02-21 stsp int first_displayed_line;
313 15a087fe 2019-02-21 stsp int last_displayed_line;
315 15a087fe 2019-02-21 stsp int diff_context;
316 3dbaef42 2020-11-24 stsp int ignore_whitespace;
317 64453f7e 2020-11-21 stsp int force_text_diff;
318 15a087fe 2019-02-21 stsp struct got_repository *repo;
319 bddb1296 2019-11-08 stsp struct tog_colors colors;
320 fe621944 2020-11-10 stsp size_t nlines;
321 f44b1f58 2020-02-02 tracey off_t *line_offsets;
322 f44b1f58 2020-02-02 tracey int matched_line;
323 f44b1f58 2020-02-02 tracey int selected_line;
325 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
326 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
329 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
332 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
333 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
334 1a76625f 2018-10-22 stsp int commits_needed;
335 fb280deb 2021-08-30 stsp int load_all;
336 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
337 1a76625f 2018-10-22 stsp struct commit_queue *commits;
338 1a76625f 2018-10-22 stsp const char *in_repo_path;
339 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
340 1a76625f 2018-10-22 stsp struct got_repository *repo;
341 74467cc8 2022-06-15 stsp int *pack_fds;
342 1a76625f 2018-10-22 stsp int log_complete;
343 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
344 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
345 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
346 13add988 2019-10-15 stsp int *searching;
347 13add988 2019-10-15 stsp int *search_next_done;
348 13add988 2019-10-15 stsp regex_t *regex;
351 1a76625f 2018-10-22 stsp struct tog_log_view_state {
352 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
353 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
354 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
355 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
356 b01e7d3b 2018-08-04 stsp int selected;
357 b01e7d3b 2018-08-04 stsp char *in_repo_path;
358 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
359 b672a97a 2020-01-27 stsp int log_branches;
360 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
361 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
362 1a76625f 2018-10-22 stsp sig_atomic_t quit;
363 1a76625f 2018-10-22 stsp pthread_t thread;
364 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
365 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
366 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
367 11b20872 2019-11-08 stsp struct tog_colors colors;
370 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
371 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
372 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
374 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
375 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
376 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
377 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
378 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
379 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
380 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
381 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
382 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
383 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
384 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
386 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
387 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
388 e9424729 2018-08-04 stsp int nlines;
390 e9424729 2018-08-04 stsp struct tog_view *view;
391 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
395 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
396 e9424729 2018-08-04 stsp const char *path;
397 e9424729 2018-08-04 stsp struct got_repository *repo;
398 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
399 e9424729 2018-08-04 stsp int *complete;
400 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
401 fc06ba56 2019-08-22 stsp void *cancel_arg;
404 e9424729 2018-08-04 stsp struct tog_blame {
406 be659d10 2020-11-18 stsp off_t filesize;
407 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
408 6fcac457 2018-11-19 stsp int nlines;
409 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
410 e9424729 2018-08-04 stsp pthread_t thread;
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
412 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
413 e9424729 2018-08-04 stsp const char *path;
414 0ae84acc 2022-06-15 tracey int *pack_fds;
417 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
418 7cbe629d 2018-08-04 stsp int first_displayed_line;
419 7cbe629d 2018-08-04 stsp int last_displayed_line;
420 7cbe629d 2018-08-04 stsp int selected_line;
421 7cbe629d 2018-08-04 stsp int blame_complete;
424 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
425 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
426 e5a0f69f 2018-08-18 stsp char *path;
427 7cbe629d 2018-08-04 stsp struct got_repository *repo;
428 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
429 e9424729 2018-08-04 stsp struct tog_blame blame;
430 6c4c42e0 2019-06-24 stsp int matched_line;
431 11b20872 2019-11-08 stsp struct tog_colors colors;
434 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
435 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
436 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
437 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
438 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
439 ad80ab7b 2018-08-04 stsp int selected;
442 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
445 ad80ab7b 2018-08-04 stsp char *tree_label;
446 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
447 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
448 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
453 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
455 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
456 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
457 6458efa5 2020-11-24 stsp struct tog_colors colors;
460 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
461 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
462 6458efa5 2020-11-24 stsp struct got_reference *ref;
466 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
469 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
471 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
472 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
473 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
474 6458efa5 2020-11-24 stsp struct got_repository *repo;
475 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
476 bddb1296 2019-11-08 stsp struct tog_colors colors;
480 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
482 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
483 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
484 669b5ffa 2018-10-07 stsp * there is enough screen estate.
486 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
487 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
489 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
490 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
491 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
493 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
494 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
496 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
498 cc3c9aac 2018-08-01 stsp struct tog_view {
499 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
500 26ed57b2 2018-05-19 stsp WINDOW *window;
501 26ed57b2 2018-05-19 stsp PANEL *panel;
502 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
503 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
504 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
505 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
507 669b5ffa 2018-10-07 stsp struct tog_view *parent;
508 669b5ffa 2018-10-07 stsp struct tog_view *child;
511 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
512 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
513 e78dc838 2020-12-04 stsp * between parent and child.
514 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
515 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
516 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
517 e78dc838 2020-12-04 stsp * situations.
519 e78dc838 2020-12-04 stsp int focus_child;
521 5dc9f4bc 2018-08-04 stsp /* type-specific state */
522 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
524 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
525 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
526 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
527 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
528 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
531 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
532 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
533 e78dc838 2020-12-04 stsp struct tog_view *, int);
534 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
536 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
537 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
538 c0c4acc8 2021-01-24 stsp int search_started;
539 60493ae3 2019-06-20 stsp int searching;
540 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
541 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
542 60493ae3 2019-06-20 stsp int search_next_done;
543 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
544 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
545 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
546 1803e47f 2019-06-22 stsp regex_t regex;
547 41605754 2020-11-12 stsp regmatch_t regmatch;
550 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
551 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
552 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
553 78756c87 2020-11-24 stsp struct got_repository *);
554 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
555 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
556 e78dc838 2020-12-04 stsp struct tog_view *, int);
557 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
558 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
559 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
561 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
562 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
563 78756c87 2020-11-24 stsp const char *, const char *, int);
564 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
565 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
566 e78dc838 2020-12-04 stsp struct tog_view *, int);
567 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
568 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
569 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
572 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
573 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
574 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
575 e78dc838 2020-12-04 stsp struct tog_view *, int);
576 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
577 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
578 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
580 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
581 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
582 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
584 e78dc838 2020-12-04 stsp struct tog_view *, int);
585 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
586 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
587 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
589 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
590 6458efa5 2020-11-24 stsp struct got_repository *);
591 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
592 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
593 e78dc838 2020-12-04 stsp struct tog_view *, int);
594 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
595 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
596 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
598 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
599 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
600 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
601 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
602 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
604 25791caa 2018-10-24 stsp static void
605 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
607 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
610 83baff54 2019-08-12 stsp static void
611 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
613 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
616 61266923 2020-01-14 stsp static void
617 61266923 2020-01-14 stsp tog_sigcont(int signo)
619 61266923 2020-01-14 stsp tog_sigcont_received = 1;
622 2497f032 2022-05-31 stsp static void
623 2497f032 2022-05-31 stsp tog_sigint(int signo)
625 2497f032 2022-05-31 stsp tog_sigint_received = 1;
628 2497f032 2022-05-31 stsp static void
629 2497f032 2022-05-31 stsp tog_sigterm(int signo)
631 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
635 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
637 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
638 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
642 e5a0f69f 2018-08-18 stsp static const struct got_error *
643 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
645 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
647 669b5ffa 2018-10-07 stsp if (view->child) {
648 669b5ffa 2018-10-07 stsp view_close(view->child);
649 669b5ffa 2018-10-07 stsp view->child = NULL;
651 e5a0f69f 2018-08-18 stsp if (view->close)
652 e5a0f69f 2018-08-18 stsp err = view->close(view);
653 ea5e7bb5 2018-08-01 stsp if (view->panel)
654 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
655 ea5e7bb5 2018-08-01 stsp if (view->window)
656 ea5e7bb5 2018-08-01 stsp delwin(view->window);
657 ea5e7bb5 2018-08-01 stsp free(view);
658 e5a0f69f 2018-08-18 stsp return err;
661 ea5e7bb5 2018-08-01 stsp static struct tog_view *
662 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
663 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
665 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
667 ea5e7bb5 2018-08-01 stsp if (view == NULL)
668 ea5e7bb5 2018-08-01 stsp return NULL;
670 d6b05b5b 2018-08-04 stsp view->type = type;
671 f7d12f7e 2018-08-01 stsp view->lines = LINES;
672 f7d12f7e 2018-08-01 stsp view->cols = COLS;
673 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
674 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
675 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
676 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
677 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
678 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
679 96a765a8 2018-08-04 stsp view_close(view);
680 ea5e7bb5 2018-08-01 stsp return NULL;
682 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
683 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
684 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
685 96a765a8 2018-08-04 stsp view_close(view);
686 ea5e7bb5 2018-08-01 stsp return NULL;
689 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
690 ea5e7bb5 2018-08-01 stsp return view;
694 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
696 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
698 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
701 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
703 5c60c32a 2018-10-18 stsp static const struct got_error *
704 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
706 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
708 5c60c32a 2018-10-18 stsp view->begin_y = 0;
709 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
710 5c60c32a 2018-10-18 stsp view->nlines = LINES;
711 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
712 5c60c32a 2018-10-18 stsp view->lines = LINES;
713 5c60c32a 2018-10-18 stsp view->cols = COLS;
714 5c60c32a 2018-10-18 stsp err = view_resize(view);
716 5c60c32a 2018-10-18 stsp return err;
718 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
721 5c60c32a 2018-10-18 stsp return NULL;
724 5c60c32a 2018-10-18 stsp static const struct got_error *
725 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
727 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
729 5c60c32a 2018-10-18 stsp view->begin_x = 0;
730 5c60c32a 2018-10-18 stsp view->begin_y = 0;
731 5c60c32a 2018-10-18 stsp view->nlines = LINES;
732 5c60c32a 2018-10-18 stsp view->ncols = COLS;
733 5c60c32a 2018-10-18 stsp view->lines = LINES;
734 5c60c32a 2018-10-18 stsp view->cols = COLS;
735 5c60c32a 2018-10-18 stsp err = view_resize(view);
737 5c60c32a 2018-10-18 stsp return err;
739 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
742 5c60c32a 2018-10-18 stsp return NULL;
746 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
748 5c60c32a 2018-10-18 stsp return view->parent == NULL;
751 4d8c2215 2018-08-19 stsp static const struct got_error *
752 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
754 f7d12f7e 2018-08-01 stsp int nlines, ncols;
756 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
757 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
759 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
761 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
762 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
764 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
766 6e3e5d9c 2018-10-18 stsp if (view->child) {
767 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
768 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
769 0dbbbe90 2022-06-17 op ncols = COLS;
771 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
772 5c60c32a 2018-10-18 stsp if (view->child->focussed)
773 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
775 5c60c32a 2018-10-18 stsp show_panel(view->panel);
777 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
779 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
780 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
782 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
783 0dbbbe90 2022-06-17 op ncols = COLS;
785 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
786 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
787 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
788 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
789 0dbbbe90 2022-06-17 op wclear(view->window);
791 0dbbbe90 2022-06-17 op view->nlines = nlines;
792 0dbbbe90 2022-06-17 op view->ncols = ncols;
793 0dbbbe90 2022-06-17 op view->lines = LINES;
794 0dbbbe90 2022-06-17 op view->cols = COLS;
796 5c60c32a 2018-10-18 stsp return NULL;
799 669b5ffa 2018-10-07 stsp static const struct got_error *
800 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
802 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
804 669b5ffa 2018-10-07 stsp if (view->child == NULL)
805 669b5ffa 2018-10-07 stsp return NULL;
807 669b5ffa 2018-10-07 stsp err = view_close(view->child);
808 669b5ffa 2018-10-07 stsp view->child = NULL;
809 669b5ffa 2018-10-07 stsp return err;
812 0dbbbe90 2022-06-17 op static const struct got_error *
813 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
815 669b5ffa 2018-10-07 stsp view->child = child;
816 669b5ffa 2018-10-07 stsp child->parent = view;
818 0dbbbe90 2022-06-17 op return view_resize(view);
822 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
824 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
827 34bc9ec9 2019-02-22 stsp static void
828 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
830 25791caa 2018-10-24 stsp int cols, lines;
831 25791caa 2018-10-24 stsp struct winsize size;
833 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 25791caa 2018-10-24 stsp cols = 80; /* Default */
835 25791caa 2018-10-24 stsp lines = 24;
837 25791caa 2018-10-24 stsp cols = size.ws_col;
838 25791caa 2018-10-24 stsp lines = size.ws_row;
840 25791caa 2018-10-24 stsp resize_term(lines, cols);
843 2b49a8ae 2019-06-22 stsp static const struct got_error *
844 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
846 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
847 2b49a8ae 2019-06-22 stsp char pattern[1024];
850 c0c4acc8 2021-01-24 stsp if (view->search_started) {
851 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
852 c0c4acc8 2021-01-24 stsp view->searching = 0;
853 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
855 c0c4acc8 2021-01-24 stsp view->search_started = 0;
857 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
858 2b49a8ae 2019-06-22 stsp return NULL;
860 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
863 2b49a8ae 2019-06-22 stsp nocbreak();
865 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
868 2b49a8ae 2019-06-22 stsp if (ret == ERR)
869 2b49a8ae 2019-06-22 stsp return NULL;
871 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 7c32bd05 2019-06-22 stsp err = view->search_start(view);
874 7c32bd05 2019-06-22 stsp regfree(&view->regex);
875 7c32bd05 2019-06-22 stsp return err;
877 c0c4acc8 2021-01-24 stsp view->search_started = 1;
878 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
879 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
880 2b49a8ae 2019-06-22 stsp view->search_next(view);
883 2b49a8ae 2019-06-22 stsp return NULL;
886 0cf4efb1 2018-09-29 stsp static const struct got_error *
887 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
888 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
890 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
891 669b5ffa 2018-10-07 stsp struct tog_view *v;
892 1a76625f 2018-10-22 stsp int ch, errcode;
894 e5a0f69f 2018-08-18 stsp *new = NULL;
896 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
897 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
902 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
903 82954512 2020-02-03 stsp if (errcode)
904 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
905 82954512 2020-02-03 stsp "pthread_mutex_unlock");
906 3da8ef85 2021-09-21 stsp sched_yield();
907 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
908 82954512 2020-02-03 stsp if (errcode)
909 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
910 82954512 2020-02-03 stsp "pthread_mutex_lock");
911 60493ae3 2019-06-20 stsp view->search_next(view);
912 60493ae3 2019-06-20 stsp return NULL;
915 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
916 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
917 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
918 1a76625f 2018-10-22 stsp if (errcode)
919 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
921 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
922 1a76625f 2018-10-22 stsp if (errcode)
923 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
924 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
926 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
927 25791caa 2018-10-24 stsp tog_resizeterm();
928 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
929 61266923 2020-01-14 stsp tog_sigcont_received = 0;
930 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
931 25791caa 2018-10-24 stsp err = view_resize(v);
933 25791caa 2018-10-24 stsp return err;
934 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
936 25791caa 2018-10-24 stsp return err;
937 cdfcfb03 2020-12-06 stsp if (v->child) {
938 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
940 cdfcfb03 2020-12-06 stsp return err;
941 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
942 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
944 cdfcfb03 2020-12-06 stsp return err;
949 e5a0f69f 2018-08-18 stsp switch (ch) {
951 1e37a5c2 2019-05-12 jcs if (view->child) {
952 e78dc838 2020-12-04 stsp view->focussed = 0;
953 e78dc838 2020-12-04 stsp view->child->focussed = 1;
954 e78dc838 2020-12-04 stsp view->focus_child = 1;
955 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
956 e78dc838 2020-12-04 stsp view->focussed = 0;
957 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
958 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
962 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
963 9970f7fc 2020-12-03 stsp view->dying = 1;
969 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
970 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
972 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
973 e78dc838 2020-12-04 stsp view->focussed = 0;
974 e78dc838 2020-12-04 stsp view->child->focussed = 1;
975 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
977 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
980 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
981 9970f7fc 2020-12-03 stsp KEY_RESIZE);
983 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
984 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
985 e78dc838 2020-12-04 stsp view->focussed = 1;
986 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
988 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
992 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
995 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
998 60493ae3 2019-06-20 stsp if (view->search_start)
999 2b49a8ae 2019-06-22 stsp view_search_start(view);
1001 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1005 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1006 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1007 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1009 60493ae3 2019-06-20 stsp view->search_next(view);
1011 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1014 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1018 e5a0f69f 2018-08-18 stsp return err;
1022 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1024 0cf4efb1 2018-09-29 stsp PANEL *panel;
1025 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1027 669b5ffa 2018-10-07 stsp if (view->parent)
1028 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1030 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1031 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1034 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1035 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1040 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1042 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1043 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1045 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1047 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1050 669b5ffa 2018-10-07 stsp return view->focussed;
1053 bcbd79e2 2018-08-19 stsp static const struct got_error *
1054 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1056 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1057 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1058 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1059 fd823528 2018-10-22 stsp int fast_refresh = 10;
1060 1a76625f 2018-10-22 stsp int done = 0, errcode;
1062 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1063 1a76625f 2018-10-22 stsp if (errcode)
1064 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1066 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1067 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1069 1004088d 2018-09-29 stsp view->focussed = 1;
1070 878940b7 2018-09-29 stsp err = view->show(view);
1072 0cf4efb1 2018-09-29 stsp return err;
1073 0cf4efb1 2018-09-29 stsp update_panels();
1074 0cf4efb1 2018-09-29 stsp doupdate();
1075 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1077 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1078 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1080 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1083 9970f7fc 2020-12-03 stsp if (view->dying) {
1084 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1086 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1087 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1089 e78dc838 2020-12-04 stsp else if (view->parent)
1090 669b5ffa 2018-10-07 stsp prev = view->parent;
1092 e78dc838 2020-12-04 stsp if (view->parent) {
1093 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1094 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1096 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1100 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1102 9970f7fc 2020-12-03 stsp err = view_close(view);
1104 e5a0f69f 2018-08-18 stsp goto done;
1106 e78dc838 2020-12-04 stsp view = NULL;
1107 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1108 e78dc838 2020-12-04 stsp if (v->focussed)
1111 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1112 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1114 e78dc838 2020-12-04 stsp view = prev;
1115 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1116 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1117 e78dc838 2020-12-04 stsp tog_view_list_head);
1119 e78dc838 2020-12-04 stsp if (view) {
1120 e78dc838 2020-12-04 stsp if (view->focus_child) {
1121 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1122 e78dc838 2020-12-04 stsp view = view->child;
1124 e78dc838 2020-12-04 stsp view->focussed = 1;
1128 bcbd79e2 2018-08-19 stsp if (new_view) {
1129 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1130 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1131 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1132 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1134 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1135 86c66b02 2018-10-18 stsp err = view_close(v);
1137 86c66b02 2018-10-18 stsp goto done;
1140 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1141 fed7eaa8 2018-10-24 stsp view = new_view;
1143 669b5ffa 2018-10-07 stsp if (view) {
1144 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1145 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1146 e78dc838 2020-12-04 stsp view = view->child;
1148 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1149 e78dc838 2020-12-04 stsp view = view->parent;
1151 e78dc838 2020-12-04 stsp show_panel(view->panel);
1152 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1153 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1154 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1155 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1157 1a76625f 2018-10-22 stsp goto done;
1159 669b5ffa 2018-10-07 stsp err = view->show(view);
1161 1a76625f 2018-10-22 stsp goto done;
1162 669b5ffa 2018-10-07 stsp if (view->child) {
1163 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1165 1a76625f 2018-10-22 stsp goto done;
1167 1a76625f 2018-10-22 stsp update_panels();
1168 1a76625f 2018-10-22 stsp doupdate();
1172 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1173 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1174 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1175 e5a0f69f 2018-08-18 stsp view_close(view);
1178 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1179 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1180 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1182 e5a0f69f 2018-08-18 stsp return err;
1185 4ed7e80c 2018-05-20 stsp __dead static void
1186 9f7d7167 2018-04-29 stsp usage_log(void)
1189 c70c5802 2018-08-01 stsp fprintf(stderr,
1190 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1191 9f7d7167 2018-04-29 stsp getprogname());
1195 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1196 80ddbec8 2018-04-29 stsp static const struct got_error *
1197 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1199 00dfcb92 2018-06-11 stsp char *vis = NULL;
1200 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1202 963b370f 2018-05-20 stsp *ws = NULL;
1203 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1204 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1205 00dfcb92 2018-06-11 stsp int vislen;
1206 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1207 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1209 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1210 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1212 00dfcb92 2018-06-11 stsp return err;
1213 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1214 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1215 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1216 a7f50699 2018-06-11 stsp goto done;
1220 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1221 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1222 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1223 a7f50699 2018-06-11 stsp goto done;
1226 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1227 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1229 00dfcb92 2018-06-11 stsp free(vis);
1230 963b370f 2018-05-20 stsp if (err) {
1231 963b370f 2018-05-20 stsp free(*ws);
1232 963b370f 2018-05-20 stsp *ws = NULL;
1233 963b370f 2018-05-20 stsp *wlen = 0;
1235 963b370f 2018-05-20 stsp return err;
1238 145b6838 2022-06-16 stsp static const struct got_error *
1239 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1241 145b6838 2022-06-16 stsp char *dst;
1242 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1244 145b6838 2022-06-16 stsp *ptr = NULL;
1245 145b6838 2022-06-16 stsp n = len = strlen(src);
1246 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1247 145b6838 2022-06-16 stsp if (dst == NULL)
1248 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1250 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1251 145b6838 2022-06-16 stsp const char c = src[idx];
1253 145b6838 2022-06-16 stsp if (c == '\t') {
1254 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1257 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1258 6e1c41ad 2022-06-16 mark if (p == NULL) {
1259 6e1c41ad 2022-06-16 mark free(dst);
1260 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1265 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1268 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1272 145b6838 2022-06-16 stsp dst[sz] = '\0';
1273 145b6838 2022-06-16 stsp *ptr = dst;
1274 145b6838 2022-06-16 stsp return NULL;
1278 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1279 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1280 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1284 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1286 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1288 4e4a9ac8 2022-06-17 op if (n == 0) {
1289 4e4a9ac8 2022-06-17 op *rcol = cols;
1293 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1294 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1295 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1297 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1299 4e4a9ac8 2022-06-17 op if (width == -1) {
1301 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1304 4e4a9ac8 2022-06-17 op if (cols + width > n)
1306 4e4a9ac8 2022-06-17 op cols += width;
1309 4e4a9ac8 2022-06-17 op *rcol = cols;
1314 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1315 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1316 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1318 ccda2f4d 2022-06-16 stsp static const struct got_error *
1319 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1320 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1322 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1324 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1325 145b6838 2022-06-16 stsp char *exstr = NULL;
1326 963b370f 2018-05-20 stsp size_t wlen;
1327 4e4a9ac8 2022-06-17 op int i, scrollx;
1329 963b370f 2018-05-20 stsp *wlinep = NULL;
1330 b700b5d6 2018-07-10 stsp *widthp = 0;
1332 145b6838 2022-06-16 stsp if (expand) {
1333 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1335 145b6838 2022-06-16 stsp return err;
1338 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1339 145b6838 2022-06-16 stsp free(exstr);
1341 963b370f 2018-05-20 stsp return err;
1343 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1345 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1346 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1349 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1350 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1354 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1355 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1357 b700b5d6 2018-07-10 stsp if (widthp)
1358 b700b5d6 2018-07-10 stsp *widthp = cols;
1359 ccda2f4d 2022-06-16 stsp if (scrollxp)
1360 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1362 963b370f 2018-05-20 stsp free(wline);
1364 963b370f 2018-05-20 stsp *wlinep = wline;
1365 963b370f 2018-05-20 stsp return err;
1368 8b473291 2019-02-21 stsp static const struct got_error*
1369 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1370 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1372 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1373 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1375 8b473291 2019-02-21 stsp const char *name;
1377 8b473291 2019-02-21 stsp *refs_str = NULL;
1379 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1380 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1381 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1384 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1385 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1387 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1388 8b473291 2019-02-21 stsp name += 5;
1389 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1390 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1392 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1393 8b473291 2019-02-21 stsp name += 6;
1394 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1395 8b473291 2019-02-21 stsp name += 8;
1396 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1397 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1400 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1403 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1404 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1405 5d844a1e 2019-08-13 stsp if (err) {
1406 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1407 48cae60d 2020-09-22 stsp free(ref_id);
1410 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1411 5d844a1e 2019-08-13 stsp err = NULL;
1412 5d844a1e 2019-08-13 stsp tag = NULL;
1415 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1416 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1417 48cae60d 2020-09-22 stsp free(ref_id);
1419 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1420 52b5abe1 2019-08-13 stsp if (cmp != 0)
1422 8b473291 2019-02-21 stsp s = *refs_str;
1423 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1424 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1425 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1427 8b473291 2019-02-21 stsp *refs_str = NULL;
1433 8b473291 2019-02-21 stsp return err;
1436 963b370f 2018-05-20 stsp static const struct got_error *
1437 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1438 27a741e5 2019-09-11 stsp int col_tab_align)
1440 e6b8b890 2020-12-29 naddy char *smallerthan;
1442 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1443 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1444 5813d178 2019-03-09 stsp author = smallerthan + 1;
1445 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1446 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1447 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1450 5813d178 2019-03-09 stsp static const struct got_error *
1451 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1452 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1453 8fdc79fe 2020-12-01 naddy int author_display_cols)
1455 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1456 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1457 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1458 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1459 5813d178 2019-03-09 stsp char *author = NULL;
1460 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1461 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1462 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1463 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1464 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1465 ccb26ccd 2018-11-05 stsp struct tm tm;
1466 45d799e2 2018-12-23 stsp time_t committer_time;
1467 11b20872 2019-11-08 stsp struct tog_color *tc;
1469 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1470 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1471 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1472 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1473 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1475 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1476 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1478 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1479 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1481 11b20872 2019-11-08 stsp wattr_on(view->window,
1482 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1483 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1485 11b20872 2019-11-08 stsp wattr_off(view->window,
1486 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1487 27a741e5 2019-09-11 stsp col = limit;
1488 b39d25c7 2018-07-10 stsp if (col > avail)
1489 b39d25c7 2018-07-10 stsp goto done;
1491 6570a66d 2019-11-08 stsp if (avail >= 120) {
1492 6570a66d 2019-11-08 stsp char *id_str;
1493 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1495 6570a66d 2019-11-08 stsp goto done;
1496 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1498 11b20872 2019-11-08 stsp wattr_on(view->window,
1499 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1500 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1502 11b20872 2019-11-08 stsp wattr_off(view->window,
1503 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1504 6570a66d 2019-11-08 stsp free(id_str);
1506 6570a66d 2019-11-08 stsp if (col > avail)
1507 6570a66d 2019-11-08 stsp goto done;
1510 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1511 5813d178 2019-03-09 stsp if (author == NULL) {
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1513 80ddbec8 2018-04-29 stsp goto done;
1515 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1517 bb737323 2018-05-20 stsp goto done;
1518 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1520 11b20872 2019-11-08 stsp wattr_on(view->window,
1521 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1522 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1524 11b20872 2019-11-08 stsp wattr_off(view->window,
1525 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1526 bb737323 2018-05-20 stsp col += author_width;
1527 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1528 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1530 bb737323 2018-05-20 stsp author_width++;
1532 9c2eaf34 2018-05-20 stsp if (col > avail)
1533 9c2eaf34 2018-05-20 stsp goto done;
1535 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1537 6d9fbc00 2018-04-29 stsp goto done;
1538 bb737323 2018-05-20 stsp logmsg = logmsg0;
1539 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1541 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1542 bb737323 2018-05-20 stsp if (newline)
1543 bb737323 2018-05-20 stsp *newline = '\0';
1544 ccda2f4d 2022-06-16 stsp limit = avail - col;
1545 4d1f6af3 2022-06-17 op if (view->child && limit > 0)
1546 4d1f6af3 2022-06-17 op limit--; /* for the border */
1547 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1548 ccda2f4d 2022-06-16 stsp limit, col, 1);
1550 29688b02 2022-06-16 stsp goto done;
1551 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1552 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1553 27a741e5 2019-09-11 stsp while (col < avail) {
1554 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1558 80ddbec8 2018-04-29 stsp free(logmsg0);
1559 bb737323 2018-05-20 stsp free(wlogmsg);
1560 5813d178 2019-03-09 stsp free(author);
1561 bb737323 2018-05-20 stsp free(wauthor);
1562 80ddbec8 2018-04-29 stsp free(line);
1563 80ddbec8 2018-04-29 stsp return err;
1566 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1567 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1568 899d86c2 2018-05-10 stsp struct got_object_id *id)
1570 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1572 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1573 80ddbec8 2018-04-29 stsp if (entry == NULL)
1574 899d86c2 2018-05-10 stsp return NULL;
1576 899d86c2 2018-05-10 stsp entry->id = id;
1577 99db9666 2018-05-07 stsp entry->commit = commit;
1578 899d86c2 2018-05-10 stsp return entry;
1581 99db9666 2018-05-07 stsp static void
1582 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1584 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1586 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1587 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1588 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1589 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1590 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1591 99db9666 2018-05-07 stsp free(entry);
1594 99db9666 2018-05-07 stsp static void
1595 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1597 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1598 99db9666 2018-05-07 stsp pop_commit(commits);
1601 c4972b91 2018-05-07 stsp static const struct got_error *
1602 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1603 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1605 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1606 13add988 2019-10-15 stsp regmatch_t regmatch;
1607 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1609 13add988 2019-10-15 stsp *have_match = 0;
1611 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1613 13add988 2019-10-15 stsp return err;
1615 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1617 13add988 2019-10-15 stsp goto done;
1619 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1620 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1621 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1622 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1623 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1624 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1625 13add988 2019-10-15 stsp *have_match = 1;
1627 13add988 2019-10-15 stsp free(id_str);
1628 13add988 2019-10-15 stsp free(logmsg);
1629 13add988 2019-10-15 stsp return err;
1632 13add988 2019-10-15 stsp static const struct got_error *
1633 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1635 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1638 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1639 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1640 1a76625f 2018-10-22 stsp * while updating the display.
1643 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1644 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1645 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1646 1a76625f 2018-10-22 stsp int errcode;
1648 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1649 4e0d2870 2020-12-07 naddy NULL, NULL);
1650 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1653 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1656 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1657 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1658 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1662 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1663 1a76625f 2018-10-22 stsp if (errcode) {
1664 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1665 13add988 2019-10-15 stsp "pthread_mutex_lock");
1669 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1670 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1671 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1673 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1674 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1675 7c1452c1 2020-03-26 stsp int have_match;
1676 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1679 7c1452c1 2020-03-26 stsp if (have_match)
1680 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1683 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1684 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1685 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1686 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1689 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1691 9ba79e04 2018-06-11 stsp return err;
1694 2b779855 2020-12-05 naddy static void
1695 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1697 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1698 2b779855 2020-12-05 naddy int ncommits = 0;
1700 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1701 2b779855 2020-12-05 naddy while (entry) {
1702 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1703 2b779855 2020-12-05 naddy s->selected_entry = entry;
1706 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1707 2b779855 2020-12-05 naddy ncommits++;
1711 0553a4e3 2018-04-30 stsp static const struct got_error *
1712 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1714 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1715 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1716 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1717 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1718 60493ae3 2019-06-20 stsp int width;
1719 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1720 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1721 8b473291 2019-02-21 stsp char *refs_str = NULL;
1722 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1723 11b20872 2019-11-08 stsp struct tog_color *tc;
1724 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1726 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1727 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1728 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1729 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1731 ecb28ae0 2018-07-16 stsp return err;
1732 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1733 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1734 d2075bf3 2020-12-25 stsp if (refs) {
1735 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1736 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1738 d2075bf3 2020-12-25 stsp goto done;
1742 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1743 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1745 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1746 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1747 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1748 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1749 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1750 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1751 8f4ed634 2020-03-26 stsp goto done;
1754 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1756 f9686aa5 2020-03-27 stsp if (view->searching) {
1757 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1758 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1759 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1760 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1761 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1762 f9686aa5 2020-03-27 stsp search_str = "searching...";
1765 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1766 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1767 f9686aa5 2020-03-27 stsp search_str ? search_str :
1768 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1769 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1770 8f4ed634 2020-03-26 stsp goto done;
1774 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1775 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1776 87411fa9 2022-06-16 stsp "........................................",
1777 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1778 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1779 1a76625f 2018-10-22 stsp header = NULL;
1780 1a76625f 2018-10-22 stsp goto done;
1782 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1783 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1784 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1785 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1786 1a76625f 2018-10-22 stsp header = NULL;
1787 1a76625f 2018-10-22 stsp goto done;
1789 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1791 1a76625f 2018-10-22 stsp goto done;
1793 2814baeb 2018-08-01 stsp werase(view->window);
1795 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1796 a3404814 2018-09-02 stsp wstandout(view->window);
1797 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1799 11b20872 2019-11-08 stsp wattr_on(view->window,
1800 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1801 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1803 11b20872 2019-11-08 stsp wattr_off(view->window,
1804 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1805 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1806 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1809 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1810 a3404814 2018-09-02 stsp wstandend(view->window);
1811 ecb28ae0 2018-07-16 stsp free(wline);
1812 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1813 1a76625f 2018-10-22 stsp goto done;
1815 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
1816 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1817 5813d178 2019-03-09 stsp ncommits = 0;
1818 145b6838 2022-06-16 stsp view->maxx = 0;
1819 5813d178 2019-03-09 stsp while (entry) {
1820 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
1821 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
1822 5813d178 2019-03-09 stsp int width;
1823 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1825 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1826 5813d178 2019-03-09 stsp if (author == NULL) {
1827 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1828 5813d178 2019-03-09 stsp goto done;
1830 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1831 27a741e5 2019-09-11 stsp date_display_cols);
1832 5813d178 2019-03-09 stsp if (author_cols < width)
1833 5813d178 2019-03-09 stsp author_cols = width;
1834 5813d178 2019-03-09 stsp free(wauthor);
1835 5813d178 2019-03-09 stsp free(author);
1836 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
1838 145b6838 2022-06-16 stsp goto done;
1839 145b6838 2022-06-16 stsp msg = msg0;
1840 145b6838 2022-06-16 stsp while (*msg == '\n')
1842 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
1843 29688b02 2022-06-16 stsp *eol = '\0';
1844 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1845 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
1847 29688b02 2022-06-16 stsp goto done;
1848 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
1849 145b6838 2022-06-16 stsp free(msg0);
1850 29688b02 2022-06-16 stsp free(wmsg);
1851 7ca04879 2019-10-19 stsp ncommits++;
1852 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1855 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1856 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1857 867c6645 2018-07-10 stsp ncommits = 0;
1858 899d86c2 2018-05-10 stsp while (entry) {
1859 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1861 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1862 2814baeb 2018-08-01 stsp wstandout(view->window);
1863 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1864 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1865 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1866 2814baeb 2018-08-01 stsp wstandend(view->window);
1868 60493ae3 2019-06-20 stsp goto done;
1869 0553a4e3 2018-04-30 stsp ncommits++;
1870 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1871 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1874 1a57306a 2018-09-02 stsp view_vborder(view);
1876 1a76625f 2018-10-22 stsp free(id_str);
1877 8b473291 2019-02-21 stsp free(refs_str);
1878 1a76625f 2018-10-22 stsp free(ncommits_str);
1879 1a76625f 2018-10-22 stsp free(header);
1880 80ddbec8 2018-04-29 stsp return err;
1883 07b55e75 2018-05-10 stsp static void
1884 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1886 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1887 07b55e75 2018-05-10 stsp int nscrolled = 0;
1889 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1890 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1893 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1894 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1895 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1896 07b55e75 2018-05-10 stsp if (entry) {
1897 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1898 07b55e75 2018-05-10 stsp nscrolled++;
1903 aa075928 2018-05-10 stsp static const struct got_error *
1904 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1906 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1907 5e224a3e 2019-02-22 stsp int errcode;
1909 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1911 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1912 ffe38506 2020-12-01 naddy if (ta->log_complete)
1915 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1916 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1917 7aafa0d1 2019-02-22 stsp if (errcode)
1918 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1919 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1922 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1923 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1925 7c1452c1 2020-03-26 stsp if (!wait)
1928 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1929 ffe38506 2020-12-01 naddy show_log_view(view);
1930 7c1452c1 2020-03-26 stsp update_panels();
1931 7c1452c1 2020-03-26 stsp doupdate();
1933 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1934 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1935 82954512 2020-02-03 stsp if (errcode)
1936 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1937 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1939 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1940 ffe38506 2020-12-01 naddy show_log_view(view);
1941 7c1452c1 2020-03-26 stsp update_panels();
1942 7c1452c1 2020-03-26 stsp doupdate();
1945 5e224a3e 2019-02-22 stsp return NULL;
1948 5e224a3e 2019-02-22 stsp static const struct got_error *
1949 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1951 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1952 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1953 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1954 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1956 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1957 5e224a3e 2019-02-22 stsp return NULL;
1959 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1960 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1961 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1963 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1965 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1966 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1968 5e224a3e 2019-02-22 stsp return err;
1972 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1973 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1976 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1978 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1979 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1981 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1982 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1984 dd0a52c1 2018-05-20 stsp return err;
1987 cd0acaa7 2018-05-20 stsp static const struct got_error *
1988 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1989 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1990 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1992 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1993 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1994 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1996 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1997 15a94983 2018-12-23 stsp if (diff_view == NULL)
1998 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2000 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2001 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2002 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2003 e5a0f69f 2018-08-18 stsp if (err == NULL)
2004 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2005 cd0acaa7 2018-05-20 stsp return err;
2008 80ddbec8 2018-04-29 stsp static const struct got_error *
2009 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2010 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2012 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2014 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2015 941e9f74 2019-05-21 stsp if (parent == NULL)
2016 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2018 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2019 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2020 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2021 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2022 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2023 941e9f74 2019-05-21 stsp s->tree = subtree;
2024 941e9f74 2019-05-21 stsp s->selected = 0;
2025 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2026 941e9f74 2019-05-21 stsp return NULL;
2029 941e9f74 2019-05-21 stsp static const struct got_error *
2030 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2031 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2033 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2034 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2035 941e9f74 2019-05-21 stsp const char *p;
2036 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2038 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2040 941e9f74 2019-05-21 stsp while (*p) {
2041 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2042 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2043 56e0773d 2019-11-28 stsp char *te_name;
2045 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2048 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2049 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2050 941e9f74 2019-05-21 stsp if (slash == NULL)
2051 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2053 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2054 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2055 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2058 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2059 56e0773d 2019-11-28 stsp if (te == NULL) {
2060 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2061 56e0773d 2019-11-28 stsp free(te_name);
2064 56e0773d 2019-11-28 stsp free(te_name);
2065 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2067 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2068 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2070 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2071 941e9f74 2019-05-21 stsp if (slash)
2072 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2074 941e9f74 2019-05-21 stsp subpath = strdup(path);
2075 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2076 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2080 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2085 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2086 941e9f74 2019-05-21 stsp free(tree_id);
2090 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2091 941e9f74 2019-05-21 stsp if (err) {
2092 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2095 941e9f74 2019-05-21 stsp if (slash == NULL)
2097 941e9f74 2019-05-21 stsp free(subpath);
2098 941e9f74 2019-05-21 stsp subpath = NULL;
2099 941e9f74 2019-05-21 stsp p = slash;
2102 941e9f74 2019-05-21 stsp free(subpath);
2103 1a76625f 2018-10-22 stsp return err;
2106 61266923 2020-01-14 stsp static const struct got_error *
2107 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2108 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2109 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2111 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2112 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2113 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2115 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2116 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2117 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2119 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2121 55cccc34 2020-02-20 stsp return err;
2122 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2124 55cccc34 2020-02-20 stsp *new_view = tree_view;
2126 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2127 55cccc34 2020-02-20 stsp return NULL;
2129 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2132 55cccc34 2020-02-20 stsp static const struct got_error *
2133 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2135 61266923 2020-01-14 stsp sigset_t sigset;
2136 61266923 2020-01-14 stsp int errcode;
2138 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2139 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2141 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2142 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2143 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2144 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2145 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2146 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2147 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2148 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2149 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2151 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2152 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2153 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2155 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2156 61266923 2020-01-14 stsp if (errcode)
2157 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2159 61266923 2020-01-14 stsp return NULL;
2162 1a76625f 2018-10-22 stsp static void *
2163 1a76625f 2018-10-22 stsp log_thread(void *arg)
2165 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2166 1a76625f 2018-10-22 stsp int errcode = 0;
2167 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2168 1a76625f 2018-10-22 stsp int done = 0;
2170 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2172 61266923 2020-01-14 stsp return (void *)err;
2174 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2175 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2176 1a76625f 2018-10-22 stsp if (err) {
2177 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2178 1a76625f 2018-10-22 stsp return (void *)err;
2179 1a76625f 2018-10-22 stsp err = NULL;
2181 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2182 1a76625f 2018-10-22 stsp a->commits_needed--;
2184 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2185 3abe8080 2019-04-10 stsp if (errcode) {
2186 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2187 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2189 3abe8080 2019-04-10 stsp } else if (*a->quit)
2191 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2192 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2193 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2194 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2197 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2198 7c1452c1 2020-03-26 stsp if (errcode) {
2199 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2200 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2201 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2206 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2208 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2209 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2210 7c1452c1 2020-03-26 stsp &tog_mutex);
2211 7c1452c1 2020-03-26 stsp if (errcode)
2212 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2213 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2214 21355643 2020-12-06 stsp if (*a->quit)
2219 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2220 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2221 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2222 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2224 3abe8080 2019-04-10 stsp a->log_complete = 1;
2225 1a76625f 2018-10-22 stsp return (void *)err;
2228 1a76625f 2018-10-22 stsp static const struct got_error *
2229 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2231 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2232 1a76625f 2018-10-22 stsp int errcode;
2234 1a76625f 2018-10-22 stsp if (s->thread) {
2235 1a76625f 2018-10-22 stsp s->quit = 1;
2236 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2237 1a76625f 2018-10-22 stsp if (errcode)
2238 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2239 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2240 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2241 1a76625f 2018-10-22 stsp if (errcode)
2242 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2243 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2244 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2245 1a76625f 2018-10-22 stsp if (errcode)
2246 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2247 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2248 1a76625f 2018-10-22 stsp if (errcode)
2249 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2250 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2251 1a76625f 2018-10-22 stsp s->thread = NULL;
2254 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2255 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2256 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2259 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2260 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2261 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2262 74467cc8 2022-06-15 stsp if (err == NULL)
2263 74467cc8 2022-06-15 stsp err = pack_err;
2264 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2267 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2268 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2269 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2272 9343a5fb 2018-06-23 stsp return err;
2275 9343a5fb 2018-06-23 stsp static const struct got_error *
2276 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2278 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2279 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2280 276b94a1 2020-11-13 naddy int errcode;
2282 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2284 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2285 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2286 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2288 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2289 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2290 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2292 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2293 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2294 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2295 1a76625f 2018-10-22 stsp free(s->start_id);
2296 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2297 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2298 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2299 1a76625f 2018-10-22 stsp return err;
2302 1a76625f 2018-10-22 stsp static const struct got_error *
2303 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2305 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2307 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2308 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2309 60493ae3 2019-06-20 stsp return NULL;
2312 60493ae3 2019-06-20 stsp static const struct got_error *
2313 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2315 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2316 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2317 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2319 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2320 f9686aa5 2020-03-27 stsp show_log_view(view);
2321 f9686aa5 2020-03-27 stsp update_panels();
2322 f9686aa5 2020-03-27 stsp doupdate();
2324 96e2b566 2019-07-08 stsp if (s->search_entry) {
2325 13add988 2019-10-15 stsp int errcode, ch;
2326 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2327 13add988 2019-10-15 stsp if (errcode)
2328 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2329 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2330 13add988 2019-10-15 stsp ch = wgetch(view->window);
2331 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2332 13add988 2019-10-15 stsp if (errcode)
2333 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2334 13add988 2019-10-15 stsp "pthread_mutex_lock");
2335 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2336 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2337 678cbce5 2019-07-28 stsp return NULL;
2339 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2340 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2342 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2343 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2344 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2345 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2346 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2349 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2350 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2351 f704b35c 2022-06-18 stsp * might have changed.
2353 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2354 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2355 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2357 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2359 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2360 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2361 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2363 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2364 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2367 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2370 60493ae3 2019-06-20 stsp while (1) {
2371 13add988 2019-10-15 stsp int have_match = 0;
2373 60493ae3 2019-06-20 stsp if (entry == NULL) {
2374 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2375 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2376 f9967bca 2020-03-27 stsp view->search_next_done =
2377 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2378 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2379 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2380 f801134a 2019-06-25 stsp return NULL;
2383 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2384 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2385 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2387 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2388 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2391 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2392 13add988 2019-10-15 stsp &view->regex);
2395 13add988 2019-10-15 stsp if (have_match) {
2396 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2397 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2401 96e2b566 2019-07-08 stsp s->search_entry = entry;
2402 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2403 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2405 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2408 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2409 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2410 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2411 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2413 60493ae3 2019-06-20 stsp return err;
2416 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2417 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2419 60493ae3 2019-06-20 stsp return err;
2424 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2426 60493ae3 2019-06-20 stsp return NULL;
2429 60493ae3 2019-06-20 stsp static const struct got_error *
2430 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2431 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2432 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2434 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2435 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2436 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2437 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2438 1a76625f 2018-10-22 stsp int errcode;
2440 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2441 f135c941 2020-02-20 stsp free(s->in_repo_path);
2442 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2443 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2444 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2447 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2448 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2449 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2451 fb2756b9 2018-08-04 stsp s->repo = repo;
2452 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2453 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2454 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2455 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2456 9cd7cbd1 2020-12-07 stsp goto done;
2459 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2460 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2461 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2462 5036bf37 2018-09-24 stsp goto done;
2464 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2466 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2467 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2468 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2469 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2471 11b20872 2019-11-08 stsp goto done;
2472 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2473 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2474 11b20872 2019-11-08 stsp if (err) {
2475 11b20872 2019-11-08 stsp free_colors(&s->colors);
2476 11b20872 2019-11-08 stsp goto done;
2478 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2479 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2480 11b20872 2019-11-08 stsp if (err) {
2481 11b20872 2019-11-08 stsp free_colors(&s->colors);
2482 11b20872 2019-11-08 stsp goto done;
2486 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2487 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2488 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2489 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2490 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2492 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2493 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2495 74467cc8 2022-06-15 stsp goto done;
2497 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2498 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2499 0ae84acc 2022-06-15 tracey if (err)
2500 0ae84acc 2022-06-15 tracey goto done;
2501 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2502 b672a97a 2020-01-27 stsp !s->log_branches);
2504 1a76625f 2018-10-22 stsp goto done;
2505 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2506 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2508 c5b78334 2020-01-12 stsp goto done;
2510 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2511 1a76625f 2018-10-22 stsp if (errcode) {
2512 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2513 1a76625f 2018-10-22 stsp goto done;
2515 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2516 7c1452c1 2020-03-26 stsp if (errcode) {
2517 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2518 7c1452c1 2020-03-26 stsp goto done;
2521 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2522 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2523 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2524 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2525 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2526 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2527 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2528 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2529 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2530 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2531 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2532 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2533 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2536 1a76625f 2018-10-22 stsp close_log_view(view);
2537 ba4f502b 2018-08-04 stsp return err;
2540 e5a0f69f 2018-08-18 stsp static const struct got_error *
2541 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2543 f2f6d207 2020-11-24 stsp const struct got_error *err;
2544 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2546 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2547 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2548 2b380cc8 2018-10-24 stsp &s->thread_args);
2549 2b380cc8 2018-10-24 stsp if (errcode)
2550 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2551 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2552 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2554 f2f6d207 2020-11-24 stsp return err;
2558 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2561 e5a0f69f 2018-08-18 stsp static const struct got_error *
2562 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2564 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2565 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2566 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2567 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2568 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2569 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
2571 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2572 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2573 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2574 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2575 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2576 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2577 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2578 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2579 fb280deb 2021-08-30 stsp select_commit(s);
2581 528dedf3 2021-08-30 stsp return NULL;
2584 528dedf3 2021-08-30 stsp switch (ch) {
2586 1e37a5c2 2019-05-12 jcs s->quit = 1;
2589 145b6838 2022-06-16 stsp view->x = 0;
2592 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2594 145b6838 2022-06-16 stsp case KEY_RIGHT:
2596 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2597 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2599 145b6838 2022-06-16 stsp case KEY_LEFT:
2601 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2604 1e37a5c2 2019-05-12 jcs case KEY_UP:
2607 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2608 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2610 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2611 1e37a5c2 2019-05-12 jcs s->selected--;
2613 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2614 912a3f79 2021-08-30 j select_commit(s);
2617 912a3f79 2021-08-30 j case KEY_HOME:
2618 912a3f79 2021-08-30 j s->selected = 0;
2619 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2620 2b779855 2020-12-05 naddy select_commit(s);
2622 83cc4199 2022-06-13 stsp case CTRL('u'):
2624 83cc4199 2022-06-13 stsp nscroll /= 2;
2625 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2626 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2627 a4292ac5 2019-05-12 jcs case CTRL('b'):
2628 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2630 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2631 83cc4199 2022-06-13 stsp s->selected = MAX(0, s->selected - nscroll - 1);
2633 83cc4199 2022-06-13 stsp log_scroll_up(s, nscroll);
2634 2b779855 2020-12-05 naddy select_commit(s);
2637 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2640 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2641 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2643 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2644 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2645 1e37a5c2 2019-05-12 jcs s->selected++;
2647 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2651 912a3f79 2021-08-30 j select_commit(s);
2654 912a3f79 2021-08-30 j case KEY_END: {
2655 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2656 912a3f79 2021-08-30 j * traverse them all. */
2657 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2658 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2659 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2662 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2663 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2664 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2665 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2667 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2668 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2670 f3bc9f1d 2021-09-05 naddy if (n > 0)
2671 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2672 2b779855 2020-12-05 naddy select_commit(s);
2675 80b7e8da 2022-06-11 stsp case CTRL('d'):
2677 83cc4199 2022-06-13 stsp nscroll /= 2;
2678 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2679 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2680 83cc4199 2022-06-13 stsp case CTRL('f'): {
2681 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2682 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2683 1e37a5c2 2019-05-12 jcs if (first == NULL)
2685 83cc4199 2022-06-13 stsp err = log_scroll_down(view, nscroll);
2688 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2689 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2690 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2691 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2692 83cc4199 2022-06-13 stsp s->selected += MIN(s->last_displayed_entry->idx -
2693 83cc4199 2022-06-13 stsp s->selected_entry->idx, nscroll + 1);
2695 2b779855 2020-12-05 naddy select_commit(s);
2698 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2699 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2700 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2701 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2702 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2703 2b779855 2020-12-05 naddy select_commit(s);
2704 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2705 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2706 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2707 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2708 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2711 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2714 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2716 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2717 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2718 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2719 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2720 78756c87 2020-11-24 stsp view, s->repo);
2723 e78dc838 2020-12-04 stsp view->focussed = 0;
2724 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2725 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2726 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2728 1e37a5c2 2019-05-12 jcs return err;
2729 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
2732 e78dc838 2020-12-04 stsp view->focus_child = 1;
2734 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2737 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2739 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2740 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2741 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2742 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2746 e78dc838 2020-12-04 stsp view->focussed = 0;
2747 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2748 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2749 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2751 1e37a5c2 2019-05-12 jcs return err;
2752 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
2755 e78dc838 2020-12-04 stsp view->focus_child = 1;
2757 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2759 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2760 21355643 2020-12-06 stsp case CTRL('l'):
2762 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2763 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2765 21355643 2020-12-06 stsp err = stop_log_thread(s);
2767 74cfe85e 2020-10-20 stsp return err;
2768 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2769 21355643 2020-12-06 stsp char *parent_path;
2770 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2772 21355643 2020-12-06 stsp return err;
2773 21355643 2020-12-06 stsp free(s->in_repo_path);
2774 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2775 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2776 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2777 21355643 2020-12-06 stsp struct got_object_id *start_id;
2778 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2779 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2780 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2782 21355643 2020-12-06 stsp return err;
2783 21355643 2020-12-06 stsp free(s->start_id);
2784 21355643 2020-12-06 stsp s->start_id = start_id;
2785 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2786 21355643 2020-12-06 stsp } else /* 'B' */
2787 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2789 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
2790 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2792 b0dd8d27 2022-06-16 stsp return err;
2794 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
2795 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
2796 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2798 21355643 2020-12-06 stsp return err;
2799 51a10b52 2020-12-26 stsp tog_free_refs();
2800 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
2802 ca51c541 2020-12-07 stsp return err;
2803 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2804 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2806 d01904d4 2019-06-25 stsp return err;
2807 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2808 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2810 b672a97a 2020-01-27 stsp return err;
2811 21355643 2020-12-06 stsp free_commits(&s->commits);
2812 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2813 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2814 21355643 2020-12-06 stsp s->selected_entry = NULL;
2815 21355643 2020-12-06 stsp s->selected = 0;
2816 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2817 21355643 2020-12-06 stsp s->quit = 0;
2818 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2819 dfee752e 2022-06-18 op s->matched_entry = NULL;
2820 dfee752e 2022-06-18 op s->search_entry = NULL;
2823 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2824 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2825 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2826 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2827 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2828 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2829 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2830 6458efa5 2020-11-24 stsp if (err) {
2831 6458efa5 2020-11-24 stsp view_close(ref_view);
2832 6458efa5 2020-11-24 stsp return err;
2834 e78dc838 2020-12-04 stsp view->focussed = 0;
2835 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2836 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2837 6458efa5 2020-11-24 stsp err = view_close_child(view);
2839 6458efa5 2020-11-24 stsp return err;
2840 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
2843 e78dc838 2020-12-04 stsp view->focus_child = 1;
2845 6458efa5 2020-11-24 stsp *new_view = ref_view;
2851 80ddbec8 2018-04-29 stsp return err;
2854 4ed7e80c 2018-05-20 stsp static const struct got_error *
2855 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2857 c2db6724 2019-01-04 stsp const struct got_error *error;
2859 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2860 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2861 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2863 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2864 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2866 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2867 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2869 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2870 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2872 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2873 c2db6724 2019-01-04 stsp if (error != NULL)
2874 c2db6724 2019-01-04 stsp return error;
2876 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2877 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2879 c2db6724 2019-01-04 stsp return NULL;
2882 a915003a 2019-02-05 stsp static void
2883 a915003a 2019-02-05 stsp init_curses(void)
2886 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
2887 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
2888 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
2890 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
2891 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
2892 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
2893 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
2894 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
2896 a915003a 2019-02-05 stsp initscr();
2898 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2901 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2902 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2903 a915003a 2019-02-05 stsp curs_set(0);
2904 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2905 6d17833f 2019-11-08 stsp start_color();
2906 6d17833f 2019-11-08 stsp use_default_colors();
2910 c2db6724 2019-01-04 stsp static const struct got_error *
2911 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2912 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2914 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2916 f135c941 2020-02-20 stsp if (argc == 0) {
2917 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2918 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2919 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2920 f135c941 2020-02-20 stsp return NULL;
2923 f135c941 2020-02-20 stsp if (worktree) {
2924 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2927 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2929 f135c941 2020-02-20 stsp return err;
2930 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2931 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2932 bfd61697 2020-11-14 stsp p) == -1) {
2933 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2934 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2938 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2940 f135c941 2020-02-20 stsp return err;
2943 f135c941 2020-02-20 stsp static const struct got_error *
2944 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2946 80ddbec8 2018-04-29 stsp const struct got_error *error;
2947 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2948 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2949 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2950 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2951 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2952 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2953 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2954 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2955 04cc582a 2018-08-01 stsp struct tog_view *view;
2956 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
2958 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2959 80ddbec8 2018-04-29 stsp switch (ch) {
2961 b672a97a 2020-01-27 stsp log_branches = 1;
2964 80ddbec8 2018-04-29 stsp start_commit = optarg;
2967 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2968 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2969 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2973 17020d27 2019-03-07 stsp usage_log();
2974 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2978 80ddbec8 2018-04-29 stsp argc -= optind;
2979 80ddbec8 2018-04-29 stsp argv += optind;
2981 f135c941 2020-02-20 stsp if (argc > 1)
2982 f135c941 2020-02-20 stsp usage_log();
2984 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
2985 0ae84acc 2022-06-15 tracey if (error != NULL)
2986 0ae84acc 2022-06-15 tracey goto done;
2988 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2989 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2990 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2991 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2992 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2993 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2994 c156c7a4 2020-12-18 stsp goto done;
2995 a1fbf39a 2019-08-11 stsp if (worktree)
2996 6962eb72 2020-02-20 stsp repo_path =
2997 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2999 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3000 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3001 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3002 c156c7a4 2020-12-18 stsp goto done;
3006 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3007 80ddbec8 2018-04-29 stsp if (error != NULL)
3008 ecb28ae0 2018-07-16 stsp goto done;
3010 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3011 f135c941 2020-02-20 stsp repo, worktree);
3012 f135c941 2020-02-20 stsp if (error)
3013 f135c941 2020-02-20 stsp goto done;
3015 f135c941 2020-02-20 stsp init_curses();
3017 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3018 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3019 c02c541e 2019-03-29 stsp if (error)
3020 c02c541e 2019-03-29 stsp goto done;
3022 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3023 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3024 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3025 87670572 2020-12-26 naddy if (error)
3026 87670572 2020-12-26 naddy goto done;
3029 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3030 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3031 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3032 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3033 d8f38dc4 2020-12-05 stsp if (error)
3034 d8f38dc4 2020-12-05 stsp goto done;
3035 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3037 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3038 d8f38dc4 2020-12-05 stsp if (error == NULL)
3039 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3040 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3041 d8f38dc4 2020-12-05 stsp goto done;
3042 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3043 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3044 d8f38dc4 2020-12-05 stsp if (error)
3045 d8f38dc4 2020-12-05 stsp goto done;
3048 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3049 04cc582a 2018-08-01 stsp if (view == NULL) {
3050 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3051 04cc582a 2018-08-01 stsp goto done;
3053 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3054 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3055 ba4f502b 2018-08-04 stsp if (error)
3056 ba4f502b 2018-08-04 stsp goto done;
3057 2fc00ff4 2019-08-31 stsp if (worktree) {
3058 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3059 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3060 2fc00ff4 2019-08-31 stsp worktree = NULL;
3062 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3064 f135c941 2020-02-20 stsp free(in_repo_path);
3065 ecb28ae0 2018-07-16 stsp free(repo_path);
3066 ecb28ae0 2018-07-16 stsp free(cwd);
3067 899d86c2 2018-05-10 stsp free(start_id);
3068 d8f38dc4 2020-12-05 stsp free(label);
3070 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3071 1d0f4054 2021-06-17 stsp if (repo) {
3072 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3073 1d0f4054 2021-06-17 stsp if (error == NULL)
3074 1d0f4054 2021-06-17 stsp error = close_err;
3076 ec142235 2019-03-07 stsp if (worktree)
3077 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3078 0ae84acc 2022-06-15 tracey if (pack_fds) {
3079 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3080 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3081 0ae84acc 2022-06-15 tracey if (error == NULL)
3082 0ae84acc 2022-06-15 tracey error = pack_err;
3084 51a10b52 2020-12-26 stsp tog_free_refs();
3085 80ddbec8 2018-04-29 stsp return error;
3088 4ed7e80c 2018-05-20 stsp __dead static void
3089 9f7d7167 2018-04-29 stsp usage_diff(void)
3092 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3093 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3097 6d17833f 2019-11-08 stsp static int
3098 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3099 41605754 2020-11-12 stsp regmatch_t *regmatch)
3101 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3104 f26dddb7 2019-11-08 stsp struct tog_color *
3105 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3107 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3109 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3110 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3111 6d17833f 2019-11-08 stsp return tc;
3114 6d17833f 2019-11-08 stsp return NULL;
3117 4ed7e80c 2018-05-20 stsp static const struct got_error *
3118 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3119 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3121 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3122 44a87665 2022-06-16 stsp char *exstr = NULL;
3123 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3124 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3125 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3126 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3128 41605754 2020-11-12 stsp *wtotal = 0;
3130 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3131 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3133 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3135 44a87665 2022-06-16 stsp return err;
3137 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3138 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3139 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3140 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3141 44a87665 2022-06-16 stsp goto done;
3143 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3144 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3145 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3146 1853e0f4 2022-06-16 stsp goto done;
3148 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3149 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3150 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3151 1853e0f4 2022-06-16 stsp goto done;
3154 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3155 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3156 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3158 1853e0f4 2022-06-16 stsp goto done;
3159 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3161 1853e0f4 2022-06-16 stsp free(wline);
3162 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3163 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3165 1853e0f4 2022-06-16 stsp goto done;
3166 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3167 1853e0f4 2022-06-16 stsp wlimit -= width;
3168 1853e0f4 2022-06-16 stsp *wtotal += width;
3171 41605754 2020-11-12 stsp if (wlimit > 0) {
3172 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3173 1853e0f4 2022-06-16 stsp size_t wlen;
3175 1853e0f4 2022-06-16 stsp free(wline);
3176 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3177 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3179 1853e0f4 2022-06-16 stsp goto done;
3180 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3181 1853e0f4 2022-06-16 stsp while (i < wlen) {
3182 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3183 1853e0f4 2022-06-16 stsp if (width == -1) {
3184 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3185 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3186 1853e0f4 2022-06-16 stsp goto done;
3188 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3190 1853e0f4 2022-06-16 stsp w += width;
3193 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3194 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3195 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3196 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3197 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3198 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3199 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3203 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3204 1853e0f4 2022-06-16 stsp free(wline);
3205 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3206 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3207 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3208 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3210 1853e0f4 2022-06-16 stsp goto done;
3211 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3213 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3214 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3216 1853e0f4 2022-06-16 stsp goto done;
3217 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3219 1853e0f4 2022-06-16 stsp *wtotal += width2;
3222 145b6838 2022-06-16 stsp free(wline);
3223 44a87665 2022-06-16 stsp free(exstr);
3224 1853e0f4 2022-06-16 stsp free(seg0);
3225 1853e0f4 2022-06-16 stsp free(seg1);
3226 1853e0f4 2022-06-16 stsp free(seg2);
3227 1853e0f4 2022-06-16 stsp return err;
3230 41605754 2020-11-12 stsp static const struct got_error *
3231 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3233 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3234 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3235 61e69b96 2018-05-20 stsp const struct got_error *err;
3236 fe621944 2020-11-10 stsp int nprinted = 0;
3237 b304db33 2018-05-20 stsp char *line;
3238 826082fe 2020-12-10 stsp size_t linesize = 0;
3239 826082fe 2020-12-10 stsp ssize_t linelen;
3240 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3241 61e69b96 2018-05-20 stsp wchar_t *wline;
3242 e0b650dd 2018-05-20 stsp int width;
3243 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3244 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3245 fe621944 2020-11-10 stsp off_t line_offset;
3247 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3248 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3249 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3251 f7d12f7e 2018-08-01 stsp werase(view->window);
3253 a3404814 2018-09-02 stsp if (header) {
3254 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3255 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3256 135a2da0 2020-11-11 stsp header) == -1)
3257 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3258 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3260 135a2da0 2020-11-11 stsp free(line);
3262 a3404814 2018-09-02 stsp return err;
3264 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3265 a3404814 2018-09-02 stsp wstandout(view->window);
3266 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3267 e54cc94a 2020-11-11 stsp free(wline);
3268 e54cc94a 2020-11-11 stsp wline = NULL;
3269 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3270 a3404814 2018-09-02 stsp wstandend(view->window);
3271 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3272 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3274 a3404814 2018-09-02 stsp if (max_lines <= 1)
3275 a3404814 2018-09-02 stsp return NULL;
3276 a3404814 2018-09-02 stsp max_lines--;
3279 89f1a395 2020-12-01 naddy s->eof = 0;
3280 145b6838 2022-06-16 stsp view->maxx = 0;
3281 826082fe 2020-12-10 stsp line = NULL;
3282 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3283 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3284 826082fe 2020-12-10 stsp if (linelen == -1) {
3285 826082fe 2020-12-10 stsp if (feof(s->f)) {
3286 826082fe 2020-12-10 stsp s->eof = 1;
3289 826082fe 2020-12-10 stsp free(line);
3290 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3293 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3294 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3295 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3296 1853e0f4 2022-06-16 stsp if (err) {
3297 1853e0f4 2022-06-16 stsp free(line);
3298 1853e0f4 2022-06-16 stsp return err;
3300 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3301 1853e0f4 2022-06-16 stsp free(wline);
3302 1853e0f4 2022-06-16 stsp wline = NULL;
3304 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3306 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3307 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3308 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3309 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3310 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3311 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3312 41605754 2020-11-12 stsp if (err) {
3313 41605754 2020-11-12 stsp free(line);
3314 41605754 2020-11-12 stsp return err;
3318 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3319 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3320 969c159c 2022-06-16 stsp if (err) {
3321 969c159c 2022-06-16 stsp free(line);
3322 969c159c 2022-06-16 stsp return err;
3324 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3325 969c159c 2022-06-16 stsp free(wline);
3326 41605754 2020-11-12 stsp wline = NULL;
3329 6d17833f 2019-11-08 stsp wattr_off(view->window,
3330 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3331 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3332 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3333 fe621944 2020-11-10 stsp nprinted++;
3335 826082fe 2020-12-10 stsp free(line);
3336 fe621944 2020-11-10 stsp if (nprinted >= 1)
3337 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3338 89f1a395 2020-12-01 naddy (nprinted - 1);
3340 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3342 1a57306a 2018-09-02 stsp view_vborder(view);
3344 89f1a395 2020-12-01 naddy if (s->eof) {
3345 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3346 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3347 c3e9aa98 2019-05-13 jcs nprinted++;
3350 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3351 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3353 c3e9aa98 2019-05-13 jcs return err;
3356 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3357 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3358 e54cc94a 2020-11-11 stsp free(wline);
3359 e54cc94a 2020-11-11 stsp wline = NULL;
3360 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3363 26ed57b2 2018-05-19 stsp return NULL;
3366 abd2672a 2018-12-23 stsp static char *
3367 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3369 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3370 09867e48 2019-08-13 stsp char *p, *s;
3372 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3373 09867e48 2019-08-13 stsp if (tm == NULL)
3374 09867e48 2019-08-13 stsp return NULL;
3375 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3376 09867e48 2019-08-13 stsp if (s == NULL)
3377 09867e48 2019-08-13 stsp return NULL;
3378 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3380 abd2672a 2018-12-23 stsp *p = '\0';
3384 4ed7e80c 2018-05-20 stsp static const struct got_error *
3385 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3386 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3388 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3389 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3390 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3391 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3393 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3394 0208f208 2020-05-05 stsp if (qid != NULL) {
3395 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3396 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3397 d7b5a0e8 2022-04-20 stsp &qid->id);
3399 0208f208 2020-05-05 stsp return err;
3401 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3402 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3403 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3404 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3405 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3407 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3411 0208f208 2020-05-05 stsp if (tree_id1) {
3412 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3414 0208f208 2020-05-05 stsp goto done;
3417 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3418 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3420 0208f208 2020-05-05 stsp goto done;
3422 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3423 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3425 0208f208 2020-05-05 stsp if (tree1)
3426 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3427 0208f208 2020-05-05 stsp if (tree2)
3428 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3429 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3430 0208f208 2020-05-05 stsp return err;
3433 0208f208 2020-05-05 stsp static const struct got_error *
3434 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3438 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3439 fe621944 2020-11-10 stsp if (p == NULL)
3440 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3441 fe621944 2020-11-10 stsp *line_offsets = p;
3442 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3443 fe621944 2020-11-10 stsp (*nlines)++;
3444 fe621944 2020-11-10 stsp return NULL;
3447 fe621944 2020-11-10 stsp static const struct got_error *
3448 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3449 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3450 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3452 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3453 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3454 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3455 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3456 45d799e2 2018-12-23 stsp time_t committer_time;
3457 45d799e2 2018-12-23 stsp const char *author, *committer;
3458 8b473291 2019-02-21 stsp char *refs_str = NULL;
3459 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3460 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3461 fe621944 2020-11-10 stsp off_t outoff = 0;
3464 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3466 8b473291 2019-02-21 stsp if (refs) {
3467 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3469 8b473291 2019-02-21 stsp return err;
3472 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3474 abd2672a 2018-12-23 stsp return err;
3476 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3477 15a94983 2018-12-23 stsp if (err) {
3478 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3479 15a94983 2018-12-23 stsp goto done;
3482 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3484 fe621944 2020-11-10 stsp goto done;
3486 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3487 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3488 fe621944 2020-11-10 stsp if (n < 0) {
3489 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3490 abd2672a 2018-12-23 stsp goto done;
3492 fe621944 2020-11-10 stsp outoff += n;
3493 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3495 fe621944 2020-11-10 stsp goto done;
3497 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3498 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3499 fe621944 2020-11-10 stsp if (n < 0) {
3500 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3501 abd2672a 2018-12-23 stsp goto done;
3503 fe621944 2020-11-10 stsp outoff += n;
3504 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3506 fe621944 2020-11-10 stsp goto done;
3508 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3509 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3510 fe621944 2020-11-10 stsp if (datestr) {
3511 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3512 fe621944 2020-11-10 stsp if (n < 0) {
3513 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3514 fe621944 2020-11-10 stsp goto done;
3516 fe621944 2020-11-10 stsp outoff += n;
3517 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3519 fe621944 2020-11-10 stsp goto done;
3521 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3522 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3523 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3524 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3525 fe621944 2020-11-10 stsp if (n < 0) {
3526 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3527 fe621944 2020-11-10 stsp goto done;
3529 fe621944 2020-11-10 stsp outoff += n;
3530 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3532 fe621944 2020-11-10 stsp goto done;
3534 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3535 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3536 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3537 9f98ca05 2021-09-24 stsp int pn = 1;
3538 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3539 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3540 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3542 9f98ca05 2021-09-24 stsp goto done;
3543 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3544 9f98ca05 2021-09-24 stsp if (n < 0) {
3545 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3546 9f98ca05 2021-09-24 stsp goto done;
3548 9f98ca05 2021-09-24 stsp outoff += n;
3549 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3551 9f98ca05 2021-09-24 stsp goto done;
3552 9f98ca05 2021-09-24 stsp free(id_str);
3553 9f98ca05 2021-09-24 stsp id_str = NULL;
3557 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3559 5943eee2 2019-08-13 stsp goto done;
3560 fe621944 2020-11-10 stsp s = logmsg;
3561 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3562 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3563 fe621944 2020-11-10 stsp if (n < 0) {
3564 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3565 fe621944 2020-11-10 stsp goto done;
3567 fe621944 2020-11-10 stsp outoff += n;
3568 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3570 fe621944 2020-11-10 stsp goto done;
3573 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3575 0208f208 2020-05-05 stsp goto done;
3576 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3577 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3578 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3579 fe621944 2020-11-10 stsp if (n < 0) {
3580 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3581 fe621944 2020-11-10 stsp goto done;
3583 fe621944 2020-11-10 stsp outoff += n;
3584 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3586 fe621944 2020-11-10 stsp goto done;
3587 0208f208 2020-05-05 stsp free((char *)pe->path);
3588 0208f208 2020-05-05 stsp free(pe->data);
3591 0208f208 2020-05-05 stsp fputc('\n', outfile);
3593 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3595 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3596 abd2672a 2018-12-23 stsp free(id_str);
3597 5943eee2 2019-08-13 stsp free(logmsg);
3598 8b473291 2019-02-21 stsp free(refs_str);
3599 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3600 7510f233 2020-08-09 stsp if (err) {
3601 ae6a6978 2020-08-09 stsp free(*line_offsets);
3602 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3603 ae6a6978 2020-08-09 stsp *nlines = 0;
3605 fe621944 2020-11-10 stsp return err;
3608 abd2672a 2018-12-23 stsp static const struct got_error *
3609 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3611 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3612 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3613 15a94983 2018-12-23 stsp int obj_type;
3615 fe621944 2020-11-10 stsp free(s->line_offsets);
3616 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3617 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3618 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3619 fe621944 2020-11-10 stsp s->nlines = 0;
3621 511a516b 2018-05-19 stsp f = got_opentemp();
3622 48ae06ee 2018-10-18 stsp if (f == NULL) {
3623 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3624 48ae06ee 2018-10-18 stsp goto done;
3626 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3627 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3628 fb43ecf1 2019-02-11 stsp goto done;
3632 15a94983 2018-12-23 stsp if (s->id1)
3633 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3635 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3637 15a94983 2018-12-23 stsp goto done;
3639 15a94983 2018-12-23 stsp switch (obj_type) {
3640 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3641 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3642 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3643 b72706c3 2022-06-01 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3644 b72706c3 2022-06-01 stsp s->repo, s->f);
3646 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3647 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3648 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3649 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3651 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3652 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3653 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3654 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3655 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3657 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3659 3ffacbe1 2020-02-02 tracey goto done;
3660 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3661 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3662 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3663 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3664 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3665 f44b1f58 2020-02-02 tracey if (err)
3666 f44b1f58 2020-02-02 tracey goto done;
3667 f44b1f58 2020-02-02 tracey } else {
3668 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3669 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3670 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3671 fe621944 2020-11-10 stsp err = write_commit_info(
3672 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3673 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3674 f44b1f58 2020-02-02 tracey if (err)
3675 f44b1f58 2020-02-02 tracey goto done;
3680 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3682 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3683 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3684 b72706c3 2022-06-01 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3688 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3691 f44b1f58 2020-02-02 tracey if (err)
3692 f44b1f58 2020-02-02 tracey goto done;
3694 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3695 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3696 48ae06ee 2018-10-18 stsp return err;
3699 f5215bb9 2019-02-22 stsp static void
3700 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3702 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3703 f5215bb9 2019-02-22 stsp update_panels();
3704 f5215bb9 2019-02-22 stsp doupdate();
3707 f44b1f58 2020-02-02 tracey static const struct got_error *
3708 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3710 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3712 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3713 f44b1f58 2020-02-02 tracey return NULL;
3716 f44b1f58 2020-02-02 tracey static const struct got_error *
3717 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3719 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3720 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
3721 f44b1f58 2020-02-02 tracey int lineno;
3722 cb713507 2022-06-16 stsp char *line = NULL;
3723 826082fe 2020-12-10 stsp size_t linesize = 0;
3724 826082fe 2020-12-10 stsp ssize_t linelen;
3726 f44b1f58 2020-02-02 tracey if (!view->searching) {
3727 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3728 f44b1f58 2020-02-02 tracey return NULL;
3731 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3732 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3733 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3735 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3737 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
3739 f44b1f58 2020-02-02 tracey while (1) {
3740 f44b1f58 2020-02-02 tracey off_t offset;
3742 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3743 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3744 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3748 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3749 f44b1f58 2020-02-02 tracey lineno = 1;
3751 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3754 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3755 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3756 f44b1f58 2020-02-02 tracey free(line);
3757 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3759 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3760 cb713507 2022-06-16 stsp if (linelen != -1) {
3761 cb713507 2022-06-16 stsp char *exstr;
3762 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
3765 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
3766 cb713507 2022-06-16 stsp &view->regmatch)) {
3767 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3768 cb713507 2022-06-16 stsp s->matched_line = lineno;
3769 cb713507 2022-06-16 stsp free(exstr);
3772 cb713507 2022-06-16 stsp free(exstr);
3774 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3775 f44b1f58 2020-02-02 tracey lineno++;
3777 f44b1f58 2020-02-02 tracey lineno--;
3779 826082fe 2020-12-10 stsp free(line);
3781 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3782 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3783 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3786 145b6838 2022-06-16 stsp return err;
3789 48ae06ee 2018-10-18 stsp static const struct got_error *
3790 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
3792 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
3793 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
3795 b72706c3 2022-06-01 stsp free(s->id1);
3796 b72706c3 2022-06-01 stsp s->id1 = NULL;
3797 b72706c3 2022-06-01 stsp free(s->id2);
3798 b72706c3 2022-06-01 stsp s->id2 = NULL;
3799 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
3800 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3801 b72706c3 2022-06-01 stsp s->f = NULL;
3802 b72706c3 2022-06-01 stsp if (s->f1 && fclose(s->f1) == EOF)
3803 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3804 b72706c3 2022-06-01 stsp s->f1 = NULL;
3805 b72706c3 2022-06-01 stsp if (s->f2 && fclose(s->f2) == EOF)
3806 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3807 b72706c3 2022-06-01 stsp s->f2 = NULL;
3808 b72706c3 2022-06-01 stsp free_colors(&s->colors);
3809 b72706c3 2022-06-01 stsp free(s->line_offsets);
3810 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
3811 b72706c3 2022-06-01 stsp s->nlines = 0;
3812 b72706c3 2022-06-01 stsp return err;
3815 b72706c3 2022-06-01 stsp static const struct got_error *
3816 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3817 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3818 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3819 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3821 48ae06ee 2018-10-18 stsp const struct got_error *err;
3822 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3824 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
3826 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3827 15a94983 2018-12-23 stsp int type1, type2;
3828 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3830 15a94983 2018-12-23 stsp return err;
3831 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3833 15a94983 2018-12-23 stsp return err;
3835 15a94983 2018-12-23 stsp if (type1 != type2)
3836 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3838 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3839 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3840 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3841 f44b1f58 2020-02-02 tracey s->repo = repo;
3842 f44b1f58 2020-02-02 tracey s->id1 = id1;
3843 f44b1f58 2020-02-02 tracey s->id2 = id2;
3844 3dbaef42 2020-11-24 stsp s->label1 = label1;
3845 3dbaef42 2020-11-24 stsp s->label2 = label2;
3847 15a94983 2018-12-23 stsp if (id1) {
3848 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3849 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3850 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3852 5465d566 2020-02-01 tracey s->id1 = NULL;
3854 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3855 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3856 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
3857 b72706c3 2022-06-01 stsp goto done;
3860 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
3861 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
3862 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
3863 a00719e9 2022-06-17 stsp goto done;
3866 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
3867 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
3868 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3869 b72706c3 2022-06-01 stsp goto done;
3872 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3873 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3874 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3875 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3876 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3877 5465d566 2020-02-01 tracey s->log_view = log_view;
3878 5465d566 2020-02-01 tracey s->repo = repo;
3880 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3881 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3882 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3883 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3884 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3886 b72706c3 2022-06-01 stsp goto done;
3887 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3888 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3889 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3891 b72706c3 2022-06-01 stsp goto done;
3892 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3893 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3894 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3896 b72706c3 2022-06-01 stsp goto done;
3898 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3899 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3900 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3901 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3903 b72706c3 2022-06-01 stsp goto done;
3905 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3906 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3907 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3909 b72706c3 2022-06-01 stsp goto done;
3911 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3912 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3913 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3915 b72706c3 2022-06-01 stsp goto done;
3918 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3919 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3920 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3922 5465d566 2020-02-01 tracey err = create_diff(s);
3924 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3925 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3926 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3927 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3928 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3931 b72706c3 2022-06-01 stsp close_diff_view(view);
3932 e5a0f69f 2018-08-18 stsp return err;
3935 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3936 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3938 a3404814 2018-09-02 stsp const struct got_error *err;
3939 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3940 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3941 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3943 a3404814 2018-09-02 stsp if (s->id1) {
3944 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3946 a3404814 2018-09-02 stsp return err;
3947 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3949 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3951 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3953 a3404814 2018-09-02 stsp return err;
3954 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3956 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3957 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3958 a3404814 2018-09-02 stsp free(id_str1);
3959 a3404814 2018-09-02 stsp free(id_str2);
3960 a3404814 2018-09-02 stsp return err;
3962 a3404814 2018-09-02 stsp free(id_str1);
3963 a3404814 2018-09-02 stsp free(id_str2);
3965 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3966 267bb3b8 2021-08-01 stsp free(header);
3967 267bb3b8 2021-08-01 stsp return err;
3970 15a087fe 2019-02-21 stsp static const struct got_error *
3971 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3972 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3974 d7a04538 2019-02-21 stsp const struct got_error *err;
3975 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3976 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3977 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3979 15a087fe 2019-02-21 stsp free(s->id2);
3980 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3981 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3982 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3984 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3986 d7a04538 2019-02-21 stsp return err;
3987 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3988 15a087fe 2019-02-21 stsp free(s->id1);
3989 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3990 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3991 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3992 15a087fe 2019-02-21 stsp return NULL;
3995 0cf4efb1 2018-09-29 stsp static const struct got_error *
3996 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3998 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3999 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4000 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4001 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4002 826082fe 2020-12-10 stsp char *line = NULL;
4003 826082fe 2020-12-10 stsp size_t linesize = 0;
4004 826082fe 2020-12-10 stsp ssize_t linelen;
4005 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4007 e5a0f69f 2018-08-18 stsp switch (ch) {
4009 145b6838 2022-06-16 stsp view->x = 0;
4012 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4014 145b6838 2022-06-16 stsp case KEY_RIGHT:
4016 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4017 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4019 145b6838 2022-06-16 stsp case KEY_LEFT:
4021 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4025 3dbaef42 2020-11-24 stsp if (ch == 'a')
4026 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4027 3dbaef42 2020-11-24 stsp if (ch == 'w')
4028 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4029 64453f7e 2020-11-21 stsp wclear(view->window);
4030 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
4031 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4032 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4033 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
4034 64453f7e 2020-11-21 stsp err = create_diff(s);
4037 912a3f79 2021-08-30 j case KEY_HOME:
4038 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4041 912a3f79 2021-08-30 j case KEY_END:
4045 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4049 1e37a5c2 2019-05-12 jcs case KEY_UP:
4050 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4051 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4052 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4054 83cc4199 2022-06-13 stsp case CTRL('u'):
4056 83cc4199 2022-06-13 stsp nscroll /= 2;
4057 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4058 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4059 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4060 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
4063 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4064 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4067 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4068 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4069 1e37a5c2 2019-05-12 jcs if (!s->eof)
4070 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4072 83cc4199 2022-06-13 stsp case CTRL('d'):
4074 83cc4199 2022-06-13 stsp nscroll /= 2;
4075 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4076 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4077 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4079 00ba99a7 2019-05-12 jcs if (s->eof)
4082 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4083 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4084 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4085 826082fe 2020-12-10 stsp if (linelen == -1) {
4086 826082fe 2020-12-10 stsp if (feof(s->f)) {
4087 826082fe 2020-12-10 stsp s->eof = 1;
4089 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4093 826082fe 2020-12-10 stsp free(line);
4096 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4097 1e37a5c2 2019-05-12 jcs s->diff_context--;
4098 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4099 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4100 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4101 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4102 27829c9e 2020-11-21 stsp s->nlines) {
4103 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4104 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4109 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4110 1e37a5c2 2019-05-12 jcs s->diff_context++;
4111 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4112 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4113 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4118 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4120 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4121 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4123 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4127 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4130 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4134 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4135 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4136 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4137 145b6838 2022-06-16 stsp view->x = 0;
4139 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4140 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4144 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4146 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4147 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4149 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4153 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4156 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4160 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4161 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4162 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4163 145b6838 2022-06-16 stsp view->x = 0;
4165 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4166 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4172 bcbd79e2 2018-08-19 stsp return err;
4175 4ed7e80c 2018-05-20 stsp static const struct got_error *
4176 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4178 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4179 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4180 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4181 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4182 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4183 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4184 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4185 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4186 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4187 3dbaef42 2020-11-24 stsp const char *errstr;
4188 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4189 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4191 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4192 26ed57b2 2018-05-19 stsp switch (ch) {
4194 64453f7e 2020-11-21 stsp force_text_diff = 1;
4197 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4199 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4200 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4201 5a20d08d 2022-02-09 op errstr, errstr);
4203 09b5bff8 2020-02-23 naddy case 'r':
4204 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4205 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4206 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4208 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4211 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4214 17020d27 2019-03-07 stsp usage_diff();
4215 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4219 26ed57b2 2018-05-19 stsp argc -= optind;
4220 26ed57b2 2018-05-19 stsp argv += optind;
4222 26ed57b2 2018-05-19 stsp if (argc == 0) {
4223 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4224 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4225 15a94983 2018-12-23 stsp id_str1 = argv[0];
4226 15a94983 2018-12-23 stsp id_str2 = argv[1];
4228 26ed57b2 2018-05-19 stsp usage_diff();
4230 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4231 0ae84acc 2022-06-15 tracey if (error)
4232 0ae84acc 2022-06-15 tracey goto done;
4234 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4235 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4236 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4237 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4238 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4239 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4240 c156c7a4 2020-12-18 stsp goto done;
4241 a273ac94 2020-02-23 naddy if (worktree)
4242 a273ac94 2020-02-23 naddy repo_path =
4243 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4245 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4246 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4247 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4248 c156c7a4 2020-12-18 stsp goto done;
4252 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4253 eb6600df 2019-01-04 stsp if (error)
4254 eb6600df 2019-01-04 stsp goto done;
4256 a273ac94 2020-02-23 naddy init_curses();
4258 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4259 51a10b52 2020-12-26 stsp if (error)
4260 51a10b52 2020-12-26 stsp goto done;
4262 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4263 26ed57b2 2018-05-19 stsp if (error)
4264 26ed57b2 2018-05-19 stsp goto done;
4266 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4267 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4268 26ed57b2 2018-05-19 stsp if (error)
4269 26ed57b2 2018-05-19 stsp goto done;
4271 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4272 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4273 26ed57b2 2018-05-19 stsp if (error)
4274 26ed57b2 2018-05-19 stsp goto done;
4276 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4277 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4278 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4279 ea5e7bb5 2018-08-01 stsp goto done;
4281 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4282 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4283 5dc9f4bc 2018-08-04 stsp if (error)
4284 5dc9f4bc 2018-08-04 stsp goto done;
4285 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4287 3dbaef42 2020-11-24 stsp free(label1);
4288 3dbaef42 2020-11-24 stsp free(label2);
4289 c02c541e 2019-03-29 stsp free(repo_path);
4290 a273ac94 2020-02-23 naddy free(cwd);
4291 1d0f4054 2021-06-17 stsp if (repo) {
4292 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4293 1d0f4054 2021-06-17 stsp if (error == NULL)
4294 1d0f4054 2021-06-17 stsp error = close_err;
4296 a273ac94 2020-02-23 naddy if (worktree)
4297 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4298 0ae84acc 2022-06-15 tracey if (pack_fds) {
4299 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4300 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4301 0ae84acc 2022-06-15 tracey if (error == NULL)
4302 0ae84acc 2022-06-15 tracey error = pack_err;
4304 51a10b52 2020-12-26 stsp tog_free_refs();
4305 26ed57b2 2018-05-19 stsp return error;
4308 4ed7e80c 2018-05-20 stsp __dead static void
4309 9f7d7167 2018-04-29 stsp usage_blame(void)
4312 87411fa9 2022-06-16 stsp fprintf(stderr,
4313 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4314 9f7d7167 2018-04-29 stsp getprogname());
4318 84451b3e 2018-07-10 stsp struct tog_blame_line {
4319 84451b3e 2018-07-10 stsp int annotated;
4320 84451b3e 2018-07-10 stsp struct got_object_id *id;
4323 4ed7e80c 2018-05-20 stsp static const struct got_error *
4324 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4326 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4327 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4328 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4329 84451b3e 2018-07-10 stsp const struct got_error *err;
4330 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4331 826082fe 2020-12-10 stsp char *line = NULL;
4332 826082fe 2020-12-10 stsp size_t linesize = 0;
4333 826082fe 2020-12-10 stsp ssize_t linelen;
4334 84451b3e 2018-07-10 stsp wchar_t *wline;
4335 27a741e5 2019-09-11 stsp int width;
4336 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4337 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4338 ab089a2a 2018-07-12 stsp char *id_str;
4339 11b20872 2019-11-08 stsp struct tog_color *tc;
4341 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4343 ab089a2a 2018-07-12 stsp return err;
4345 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4346 f7d12f7e 2018-08-01 stsp werase(view->window);
4348 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4349 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4350 ab089a2a 2018-07-12 stsp free(id_str);
4351 ab089a2a 2018-07-12 stsp return err;
4354 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4355 ab089a2a 2018-07-12 stsp free(line);
4356 2550e4c3 2018-07-13 stsp line = NULL;
4358 1cae65b4 2019-09-22 stsp return err;
4359 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4360 a3404814 2018-09-02 stsp wstandout(view->window);
4361 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4363 11b20872 2019-11-08 stsp wattr_on(view->window,
4364 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4365 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4367 11b20872 2019-11-08 stsp wattr_off(view->window,
4368 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4369 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4370 a3404814 2018-09-02 stsp wstandend(view->window);
4371 2550e4c3 2018-07-13 stsp free(wline);
4372 2550e4c3 2018-07-13 stsp wline = NULL;
4373 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4374 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4376 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4377 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4378 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4379 ab089a2a 2018-07-12 stsp free(id_str);
4380 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4382 ab089a2a 2018-07-12 stsp free(id_str);
4383 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4384 3f60a8ef 2018-07-10 stsp free(line);
4385 2550e4c3 2018-07-13 stsp line = NULL;
4387 3f60a8ef 2018-07-10 stsp return err;
4388 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4389 2550e4c3 2018-07-13 stsp free(wline);
4390 2550e4c3 2018-07-13 stsp wline = NULL;
4391 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4392 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4394 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4395 145b6838 2022-06-16 stsp view->maxx = 0;
4396 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4397 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4398 826082fe 2020-12-10 stsp if (linelen == -1) {
4399 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4400 826082fe 2020-12-10 stsp s->eof = 1;
4403 84451b3e 2018-07-10 stsp free(line);
4404 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4406 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4409 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4410 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4411 1853e0f4 2022-06-16 stsp if (err) {
4412 1853e0f4 2022-06-16 stsp free(line);
4413 1853e0f4 2022-06-16 stsp return err;
4415 1853e0f4 2022-06-16 stsp free(wline);
4416 1853e0f4 2022-06-16 stsp wline = NULL;
4417 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4419 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4420 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4422 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4423 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4424 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4425 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4426 27a741e5 2019-09-11 stsp !(view->focussed &&
4427 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4428 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4429 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4430 8d0fe45a 2019-08-12 stsp char *id_str;
4431 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4432 87411fa9 2022-06-16 stsp blame_line->id);
4433 8d0fe45a 2019-08-12 stsp if (err) {
4434 8d0fe45a 2019-08-12 stsp free(line);
4435 8d0fe45a 2019-08-12 stsp return err;
4437 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4439 11b20872 2019-11-08 stsp wattr_on(view->window,
4440 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4441 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4443 11b20872 2019-11-08 stsp wattr_off(view->window,
4444 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4445 8d0fe45a 2019-08-12 stsp free(id_str);
4446 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4448 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4449 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4452 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4453 ee41ec32 2018-07-10 stsp prev_id = NULL;
4456 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4457 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4458 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4460 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4461 41605754 2020-11-12 stsp width = 9;
4462 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4463 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4464 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4465 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4466 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4467 41605754 2020-11-12 stsp if (err) {
4468 41605754 2020-11-12 stsp free(line);
4469 41605754 2020-11-12 stsp return err;
4471 41605754 2020-11-12 stsp width += 9;
4474 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4475 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4476 4cb9d869 2022-06-16 stsp if (err) {
4477 4cb9d869 2022-06-16 stsp free(line);
4478 4cb9d869 2022-06-16 stsp return err;
4480 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4481 a75b3e2d 2022-06-16 stsp width += 9;
4482 41605754 2020-11-12 stsp free(wline);
4483 41605754 2020-11-12 stsp wline = NULL;
4486 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4487 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4488 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4489 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4491 826082fe 2020-12-10 stsp free(line);
4492 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4494 1a57306a 2018-09-02 stsp view_vborder(view);
4496 84451b3e 2018-07-10 stsp return NULL;
4499 84451b3e 2018-07-10 stsp static const struct got_error *
4500 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4501 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4503 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4504 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4505 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4506 1a76625f 2018-10-22 stsp int errcode;
4508 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4509 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4510 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4513 1a76625f 2018-10-22 stsp if (errcode)
4514 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4516 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4517 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4518 d68a0a7d 2018-07-10 stsp goto done;
4521 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4522 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4524 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4525 d68a0a7d 2018-07-10 stsp if (line->annotated)
4526 d68a0a7d 2018-07-10 stsp goto done;
4528 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4529 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4530 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4531 84451b3e 2018-07-10 stsp goto done;
4533 84451b3e 2018-07-10 stsp line->annotated = 1;
4535 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4536 1a76625f 2018-10-22 stsp if (errcode)
4537 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4538 84451b3e 2018-07-10 stsp return err;
4541 84451b3e 2018-07-10 stsp static void *
4542 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4544 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4545 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4546 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4547 1a76625f 2018-10-22 stsp int errcode;
4549 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4551 61266923 2020-01-14 stsp return (void *)err;
4553 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4554 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4555 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4556 fc06ba56 2019-08-22 stsp err = NULL;
4558 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4559 1a76625f 2018-10-22 stsp if (errcode)
4560 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4561 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4563 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4564 1d0f4054 2021-06-17 stsp if (err == NULL)
4565 1d0f4054 2021-06-17 stsp err = close_err;
4566 c9beca56 2018-07-22 stsp ta->repo = NULL;
4567 c9beca56 2018-07-22 stsp *ta->complete = 1;
4569 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4570 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4571 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4573 18430de3 2018-07-10 stsp return (void *)err;
4576 245d91c1 2018-07-12 stsp static struct got_object_id *
4577 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4578 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4580 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4582 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4583 8d0fe45a 2019-08-12 stsp return NULL;
4585 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4586 245d91c1 2018-07-12 stsp if (!line->annotated)
4587 245d91c1 2018-07-12 stsp return NULL;
4589 245d91c1 2018-07-12 stsp return line->id;
4592 b880a918 2018-07-10 stsp static const struct got_error *
4593 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4595 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4598 245d91c1 2018-07-12 stsp if (blame->thread) {
4599 1a76625f 2018-10-22 stsp int errcode;
4600 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4601 1a76625f 2018-10-22 stsp if (errcode)
4602 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4603 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4604 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4605 1a76625f 2018-10-22 stsp if (errcode)
4606 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4607 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4608 1a76625f 2018-10-22 stsp if (errcode)
4609 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4610 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4611 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4612 245d91c1 2018-07-12 stsp err = NULL;
4613 245d91c1 2018-07-12 stsp blame->thread = NULL;
4615 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4616 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4617 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4618 1d0f4054 2021-06-17 stsp if (err == NULL)
4619 1d0f4054 2021-06-17 stsp err = close_err;
4620 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4622 245d91c1 2018-07-12 stsp if (blame->f) {
4623 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4624 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4625 245d91c1 2018-07-12 stsp blame->f = NULL;
4627 57670559 2018-12-23 stsp if (blame->lines) {
4628 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4629 57670559 2018-12-23 stsp free(blame->lines[i].id);
4630 57670559 2018-12-23 stsp free(blame->lines);
4631 57670559 2018-12-23 stsp blame->lines = NULL;
4633 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4634 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4635 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
4636 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4637 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
4638 0ae84acc 2022-06-15 tracey if (err == NULL)
4639 0ae84acc 2022-06-15 tracey err = pack_err;
4640 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
4642 245d91c1 2018-07-12 stsp return err;
4645 245d91c1 2018-07-12 stsp static const struct got_error *
4646 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4648 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4649 fc06ba56 2019-08-22 stsp int *done = arg;
4650 fc06ba56 2019-08-22 stsp int errcode;
4652 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4653 fc06ba56 2019-08-22 stsp if (errcode)
4654 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4655 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4657 fc06ba56 2019-08-22 stsp if (*done)
4658 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4660 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4661 fc06ba56 2019-08-22 stsp if (errcode)
4662 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4663 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4665 fc06ba56 2019-08-22 stsp return err;
4668 fc06ba56 2019-08-22 stsp static const struct got_error *
4669 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4671 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4672 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4673 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4674 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4675 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4676 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4677 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4678 15a94983 2018-12-23 stsp int obj_type;
4679 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4681 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
4682 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
4684 15a94983 2018-12-23 stsp return err;
4686 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4688 a44927cc 2022-04-07 stsp goto done;
4690 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4692 84451b3e 2018-07-10 stsp goto done;
4694 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4695 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4696 84451b3e 2018-07-10 stsp goto done;
4699 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4701 a70480e0 2018-06-23 stsp goto done;
4702 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4703 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4704 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4705 84451b3e 2018-07-10 stsp goto done;
4707 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4708 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4710 1fddf795 2021-01-20 stsp goto done;
4711 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4712 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4713 84451b3e 2018-07-10 stsp goto done;
4716 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4717 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4718 b02560ec 2019-08-19 stsp blame->nlines--;
4720 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4721 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4722 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4723 84451b3e 2018-07-10 stsp goto done;
4726 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
4728 bd24772e 2018-07-11 stsp goto done;
4729 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4730 0ae84acc 2022-06-15 tracey pack_fds);
4731 0ae84acc 2022-06-15 tracey if (err)
4732 0ae84acc 2022-06-15 tracey goto done;
4734 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
4735 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4736 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4737 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4738 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4739 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4740 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4741 245d91c1 2018-07-12 stsp goto done;
4743 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4745 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4746 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4747 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4748 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4749 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4750 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4751 a5388363 2020-12-01 naddy s->blame_complete = 0;
4753 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4754 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4755 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4756 f5a09613 2020-12-13 naddy s->selected_line = 1;
4758 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4761 a44927cc 2022-04-07 stsp if (commit)
4762 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4764 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4765 27d434c2 2018-09-15 stsp free(obj_id);
4767 245d91c1 2018-07-12 stsp stop_blame(blame);
4768 245d91c1 2018-07-12 stsp return err;
4771 245d91c1 2018-07-12 stsp static const struct got_error *
4772 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4773 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4775 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4776 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4778 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4780 c4843652 2019-08-12 stsp s->path = strdup(path);
4781 c4843652 2019-08-12 stsp if (s->path == NULL)
4782 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4784 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4785 c4843652 2019-08-12 stsp if (err) {
4786 c4843652 2019-08-12 stsp free(s->path);
4787 7cbe629d 2018-08-04 stsp return err;
4790 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4791 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4792 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4793 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4794 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4795 fb2756b9 2018-08-04 stsp s->repo = repo;
4796 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4797 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4799 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4800 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4801 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4802 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4804 11b20872 2019-11-08 stsp return err;
4807 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4808 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4809 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4810 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4811 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4813 a5388363 2020-12-01 naddy return run_blame(view);
4816 e5a0f69f 2018-08-18 stsp static const struct got_error *
4817 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4819 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4820 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4822 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4823 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4825 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4826 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4827 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4828 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4829 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4832 e5a0f69f 2018-08-18 stsp free(s->path);
4833 11b20872 2019-11-08 stsp free_colors(&s->colors);
4834 e5a0f69f 2018-08-18 stsp return err;
4837 7cbe629d 2018-08-04 stsp static const struct got_error *
4838 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4840 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4842 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4843 6c4c42e0 2019-06-24 stsp return NULL;
4846 6c4c42e0 2019-06-24 stsp static const struct got_error *
4847 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4849 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4850 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4851 6c4c42e0 2019-06-24 stsp int lineno;
4852 cb713507 2022-06-16 stsp char *line = NULL;
4853 826082fe 2020-12-10 stsp size_t linesize = 0;
4854 826082fe 2020-12-10 stsp ssize_t linelen;
4856 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4857 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4858 6c4c42e0 2019-06-24 stsp return NULL;
4861 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4862 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4863 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4865 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4867 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
4869 6c4c42e0 2019-06-24 stsp while (1) {
4870 6c4c42e0 2019-06-24 stsp off_t offset;
4872 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4873 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4874 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4878 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4879 6c4c42e0 2019-06-24 stsp lineno = 1;
4881 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4884 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4885 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4886 6c4c42e0 2019-06-24 stsp free(line);
4887 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4889 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4890 cb713507 2022-06-16 stsp if (linelen != -1) {
4891 cb713507 2022-06-16 stsp char *exstr;
4892 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4895 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4896 cb713507 2022-06-16 stsp &view->regmatch)) {
4897 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4898 cb713507 2022-06-16 stsp s->matched_line = lineno;
4899 cb713507 2022-06-16 stsp free(exstr);
4902 cb713507 2022-06-16 stsp free(exstr);
4904 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4909 826082fe 2020-12-10 stsp free(line);
4911 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4912 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4913 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4916 145b6838 2022-06-16 stsp return err;
4919 6c4c42e0 2019-06-24 stsp static const struct got_error *
4920 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4922 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4923 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4924 2b380cc8 2018-10-24 stsp int errcode;
4926 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4927 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4928 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4929 2b380cc8 2018-10-24 stsp if (errcode)
4930 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4932 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4935 51fe7530 2019-08-19 stsp if (s->blame_complete)
4936 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4938 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4940 669b5ffa 2018-10-07 stsp view_vborder(view);
4941 e5a0f69f 2018-08-18 stsp return err;
4944 e5a0f69f 2018-08-18 stsp static const struct got_error *
4945 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4947 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4948 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4949 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4950 83cc4199 2022-06-13 stsp int begin_x = 0, nscroll = view->nlines - 2;
4952 e5a0f69f 2018-08-18 stsp switch (ch) {
4954 145b6838 2022-06-16 stsp view->x = 0;
4957 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4959 145b6838 2022-06-16 stsp case KEY_RIGHT:
4961 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4962 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4964 145b6838 2022-06-16 stsp case KEY_LEFT:
4966 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4969 1e37a5c2 2019-05-12 jcs s->done = 1;
4971 4deef56f 2021-09-02 naddy case 'g':
4972 4deef56f 2021-09-02 naddy case KEY_HOME:
4973 4deef56f 2021-09-02 naddy s->selected_line = 1;
4974 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4976 4deef56f 2021-09-02 naddy case 'G':
4977 4deef56f 2021-09-02 naddy case KEY_END:
4978 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4979 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4980 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4982 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4983 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4984 4deef56f 2021-09-02 naddy (view->nlines - 3);
4988 1e37a5c2 2019-05-12 jcs case KEY_UP:
4989 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4990 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4991 1e37a5c2 2019-05-12 jcs s->selected_line--;
4992 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4993 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4994 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4996 83cc4199 2022-06-13 stsp case CTRL('u'):
4998 83cc4199 2022-06-13 stsp nscroll /= 2;
4999 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5000 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5001 ea025d1d 2020-02-22 naddy case CTRL('b'):
5002 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5003 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5006 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5007 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5009 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5012 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5013 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5014 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
5015 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
5016 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5017 1e37a5c2 2019-05-12 jcs s->selected_line++;
5018 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
5019 1e37a5c2 2019-05-12 jcs s->blame.nlines)
5020 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5023 1e37a5c2 2019-05-12 jcs case 'p': {
5024 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5025 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5026 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5027 1e37a5c2 2019-05-12 jcs if (id == NULL)
5029 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5030 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5031 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5032 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5033 1e37a5c2 2019-05-12 jcs int obj_type;
5034 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5035 1e37a5c2 2019-05-12 jcs s->repo, id);
5038 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5039 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5040 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5041 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5044 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5045 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5046 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5049 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5050 a44927cc 2022-04-07 stsp pcommit, s->path);
5051 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5052 e5a0f69f 2018-08-18 stsp if (err) {
5053 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5054 1e37a5c2 2019-05-12 jcs err = NULL;
5055 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5058 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5060 1e37a5c2 2019-05-12 jcs free(blob_id);
5061 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5062 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5063 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5066 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5067 d7b5a0e8 2022-04-20 stsp &pid->id);
5068 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5070 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5071 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5073 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5078 1e37a5c2 2019-05-12 jcs s->done = 1;
5079 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5080 1e37a5c2 2019-05-12 jcs s->done = 0;
5081 1e37a5c2 2019-05-12 jcs if (thread_err)
5083 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5084 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5085 a5388363 2020-12-01 naddy err = run_blame(view);
5090 1e37a5c2 2019-05-12 jcs case 'B': {
5091 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5092 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5093 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5095 1e37a5c2 2019-05-12 jcs s->done = 1;
5096 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5097 1e37a5c2 2019-05-12 jcs s->done = 0;
5098 1e37a5c2 2019-05-12 jcs if (thread_err)
5100 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5101 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5102 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5103 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5104 a5388363 2020-12-01 naddy err = run_blame(view);
5109 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5110 1e37a5c2 2019-05-12 jcs case '\r': {
5111 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5112 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5113 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5114 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5115 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5116 1e37a5c2 2019-05-12 jcs if (id == NULL)
5118 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5121 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5122 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
5123 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5124 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5125 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5126 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5127 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5128 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5131 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5132 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5133 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5135 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5138 e78dc838 2020-12-04 stsp view->focussed = 0;
5139 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5140 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5141 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5144 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5147 e78dc838 2020-12-04 stsp view->focus_child = 1;
5149 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5154 83cc4199 2022-06-13 stsp case CTRL('d'):
5156 83cc4199 2022-06-13 stsp nscroll /= 2;
5157 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5158 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5159 ea025d1d 2020-02-22 naddy case CTRL('f'):
5161 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5162 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5163 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5166 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5167 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5168 83cc4199 2022-06-13 stsp s->selected_line +=
5169 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5170 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5172 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5173 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5175 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5176 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5178 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5179 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5180 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5181 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5187 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5190 a70480e0 2018-06-23 stsp static const struct got_error *
5191 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5193 a70480e0 2018-06-23 stsp const struct got_error *error;
5194 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5195 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5196 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5197 0587e10c 2020-07-23 stsp char *link_target = NULL;
5198 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5199 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5200 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5202 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5203 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5205 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5206 a70480e0 2018-06-23 stsp switch (ch) {
5208 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5211 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5212 69069811 2018-08-02 stsp if (repo_path == NULL)
5213 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5217 17020d27 2019-03-07 stsp usage_blame();
5218 a70480e0 2018-06-23 stsp /* NOTREACHED */
5222 a70480e0 2018-06-23 stsp argc -= optind;
5223 a70480e0 2018-06-23 stsp argv += optind;
5225 f135c941 2020-02-20 stsp if (argc != 1)
5226 a70480e0 2018-06-23 stsp usage_blame();
5228 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5229 0ae84acc 2022-06-15 tracey if (error != NULL)
5230 0ae84acc 2022-06-15 tracey goto done;
5232 69069811 2018-08-02 stsp if (repo_path == NULL) {
5233 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5234 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5235 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5236 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5237 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5238 c156c7a4 2020-12-18 stsp goto done;
5239 f135c941 2020-02-20 stsp if (worktree)
5240 eb41ed75 2019-02-05 stsp repo_path =
5241 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5243 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5244 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5245 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5246 c156c7a4 2020-12-18 stsp goto done;
5250 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5251 c02c541e 2019-03-29 stsp if (error != NULL)
5252 8e94dd5b 2019-01-04 stsp goto done;
5254 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5255 f135c941 2020-02-20 stsp worktree);
5256 c02c541e 2019-03-29 stsp if (error)
5257 92205607 2019-01-04 stsp goto done;
5259 f135c941 2020-02-20 stsp init_curses();
5261 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5262 51a10b52 2020-12-26 stsp if (error)
5263 51a10b52 2020-12-26 stsp goto done;
5265 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5266 eb41ed75 2019-02-05 stsp if (error)
5267 69069811 2018-08-02 stsp goto done;
5269 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5270 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5271 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5272 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5273 a70480e0 2018-06-23 stsp if (error != NULL)
5274 66b4983c 2018-06-23 stsp goto done;
5275 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5276 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5278 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5279 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5281 a19e88aa 2018-06-23 stsp if (error != NULL)
5282 8b473291 2019-02-21 stsp goto done;
5284 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5285 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5286 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5287 e1cd8fed 2018-08-01 stsp goto done;
5290 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5291 a44927cc 2022-04-07 stsp if (error)
5292 a44927cc 2022-04-07 stsp goto done;
5294 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5295 a44927cc 2022-04-07 stsp commit, repo);
5296 7cbe629d 2018-08-04 stsp if (error)
5297 7cbe629d 2018-08-04 stsp goto done;
5299 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5300 78756c87 2020-11-24 stsp commit_id, repo);
5301 0587e10c 2020-07-23 stsp if (error)
5302 0587e10c 2020-07-23 stsp goto done;
5303 12314ad4 2019-08-31 stsp if (worktree) {
5304 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5305 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5306 12314ad4 2019-08-31 stsp worktree = NULL;
5308 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5310 69069811 2018-08-02 stsp free(repo_path);
5311 f135c941 2020-02-20 stsp free(in_repo_path);
5312 0587e10c 2020-07-23 stsp free(link_target);
5313 69069811 2018-08-02 stsp free(cwd);
5314 a70480e0 2018-06-23 stsp free(commit_id);
5315 a44927cc 2022-04-07 stsp if (commit)
5316 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5317 eb41ed75 2019-02-05 stsp if (worktree)
5318 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5319 1d0f4054 2021-06-17 stsp if (repo) {
5320 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5321 1d0f4054 2021-06-17 stsp if (error == NULL)
5322 1d0f4054 2021-06-17 stsp error = close_err;
5324 0ae84acc 2022-06-15 tracey if (pack_fds) {
5325 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5326 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5327 0ae84acc 2022-06-15 tracey if (error == NULL)
5328 0ae84acc 2022-06-15 tracey error = pack_err;
5330 51a10b52 2020-12-26 stsp tog_free_refs();
5331 a70480e0 2018-06-23 stsp return error;
5334 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5335 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5337 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5338 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5339 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5340 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5341 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5342 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5343 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5345 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5347 f7d12f7e 2018-08-01 stsp werase(view->window);
5349 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5350 ffd1d5e5 2018-06-23 stsp return NULL;
5352 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5355 ffd1d5e5 2018-06-23 stsp return err;
5356 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5357 a3404814 2018-09-02 stsp wstandout(view->window);
5358 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5360 11b20872 2019-11-08 stsp wattr_on(view->window,
5361 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5362 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5364 11b20872 2019-11-08 stsp wattr_off(view->window,
5365 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5366 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5367 a3404814 2018-09-02 stsp wstandend(view->window);
5368 2550e4c3 2018-07-13 stsp free(wline);
5369 2550e4c3 2018-07-13 stsp wline = NULL;
5370 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5371 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5372 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5373 ffd1d5e5 2018-06-23 stsp return NULL;
5374 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5377 ce52c690 2018-06-23 stsp return err;
5378 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5379 2550e4c3 2018-07-13 stsp free(wline);
5380 2550e4c3 2018-07-13 stsp wline = NULL;
5381 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5382 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5383 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5384 ffd1d5e5 2018-06-23 stsp return NULL;
5385 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5386 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5387 a1eca9bb 2018-06-23 stsp return NULL;
5389 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5390 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5391 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5392 0cf4efb1 2018-09-29 stsp if (view->focussed)
5393 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5394 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5396 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5397 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5398 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5399 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5400 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5401 ffd1d5e5 2018-06-23 stsp return NULL;
5405 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5408 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5409 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5410 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5411 848d6979 2019-08-12 stsp const char *modestr = "";
5412 56e0773d 2019-11-28 stsp mode_t mode;
5414 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5415 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5417 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5418 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5419 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5421 638f9024 2019-05-13 stsp return got_error_from_errno(
5422 230a42bd 2019-05-11 jcs "got_object_id_str");
5424 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5425 63c5ca5d 2019-08-24 stsp modestr = "$";
5426 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5429 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5430 d86d3b18 2020-12-01 naddy te, s->repo);
5431 0d6c6ee3 2020-05-20 stsp if (err) {
5432 0d6c6ee3 2020-05-20 stsp free(id_str);
5433 0d6c6ee3 2020-05-20 stsp return err;
5435 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5436 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5437 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5439 848d6979 2019-08-12 stsp modestr = "@";
5441 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5442 848d6979 2019-08-12 stsp modestr = "/";
5443 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5444 848d6979 2019-08-12 stsp modestr = "*";
5445 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5446 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5447 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5448 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5449 1d13200f 2018-07-12 stsp free(id_str);
5450 0d6c6ee3 2020-05-20 stsp free(link_target);
5451 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5453 1d13200f 2018-07-12 stsp free(id_str);
5454 0d6c6ee3 2020-05-20 stsp free(link_target);
5455 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5457 ffd1d5e5 2018-06-23 stsp if (err) {
5458 ffd1d5e5 2018-06-23 stsp free(line);
5461 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5462 0cf4efb1 2018-09-29 stsp if (view->focussed)
5463 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5464 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5466 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5468 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5469 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5470 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5472 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5473 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5474 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5475 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5476 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5477 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5478 ffd1d5e5 2018-06-23 stsp free(line);
5479 2550e4c3 2018-07-13 stsp free(wline);
5480 2550e4c3 2018-07-13 stsp wline = NULL;
5482 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5483 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5484 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5488 ffd1d5e5 2018-06-23 stsp return err;
5491 ffd1d5e5 2018-06-23 stsp static void
5492 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5494 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5495 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5496 fa86c4bf 2020-11-29 stsp int i = 0;
5498 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5501 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5502 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5503 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5504 fa86c4bf 2020-11-29 stsp if (!isroot)
5505 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5508 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5509 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5513 fa86c4bf 2020-11-29 stsp static void
5514 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5516 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5517 ffd1d5e5 2018-06-23 stsp int n = 0;
5519 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5520 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5521 694d3271 2020-12-01 naddy s->first_displayed_entry);
5523 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5525 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5526 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5527 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5528 768394f3 2019-01-24 stsp if (last) {
5529 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5530 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5535 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5536 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5537 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5539 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5540 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5541 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5543 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5544 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5545 56e0773d 2019-11-28 stsp + 1 /* slash */;
5547 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5549 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5550 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5551 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5553 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5554 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5555 d9765a41 2018-06-23 stsp while (pt) {
5556 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5557 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5558 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5559 cb2ebc8a 2018-06-23 stsp goto done;
5561 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5562 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5563 cb2ebc8a 2018-06-23 stsp goto done;
5565 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5568 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5569 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5570 ce52c690 2018-06-23 stsp goto done;
5574 ce52c690 2018-06-23 stsp if (err) {
5575 ce52c690 2018-06-23 stsp free(*path);
5576 ce52c690 2018-06-23 stsp *path = NULL;
5578 ce52c690 2018-06-23 stsp return err;
5581 ce52c690 2018-06-23 stsp static const struct got_error *
5582 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5583 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5584 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5586 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5587 ce52c690 2018-06-23 stsp char *path;
5588 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5590 a0de39f3 2019-08-09 stsp *new_view = NULL;
5592 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5594 ce52c690 2018-06-23 stsp return err;
5596 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5597 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5598 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5599 83ce39e3 2019-08-12 stsp goto done;
5602 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5603 e5a0f69f 2018-08-18 stsp if (err) {
5604 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5605 fc06ba56 2019-08-22 stsp err = NULL;
5606 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5608 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5610 83ce39e3 2019-08-12 stsp free(path);
5611 69efd4c4 2018-07-18 stsp return err;
5614 69efd4c4 2018-07-18 stsp static const struct got_error *
5615 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5616 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5618 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5619 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5620 69efd4c4 2018-07-18 stsp char *path;
5622 a0de39f3 2019-08-09 stsp *new_view = NULL;
5624 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5625 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5626 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5628 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5630 69efd4c4 2018-07-18 stsp return err;
5632 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5635 e5a0f69f 2018-08-18 stsp view_close(log_view);
5637 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5638 cb2ebc8a 2018-06-23 stsp free(path);
5639 cb2ebc8a 2018-06-23 stsp return err;
5642 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5643 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5644 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5646 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5647 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5648 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5649 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5651 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5652 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5654 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5655 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5656 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5658 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5660 bc573f3b 2021-07-10 stsp goto done;
5663 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5664 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5665 bc573f3b 2021-07-10 stsp * closed on demand.
5667 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5668 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5670 bc573f3b 2021-07-10 stsp goto done;
5671 bc573f3b 2021-07-10 stsp s->tree = s->root;
5673 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5674 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5675 ffd1d5e5 2018-06-23 stsp goto done;
5677 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5678 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5679 ffd1d5e5 2018-06-23 stsp goto done;
5682 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5683 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5684 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5685 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5686 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5687 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5688 9cd7cbd1 2020-12-07 stsp goto done;
5691 fb2756b9 2018-08-04 stsp s->repo = repo;
5693 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5694 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5695 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5696 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5698 c0b01bdb 2019-11-08 stsp goto done;
5699 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5700 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5702 c0b01bdb 2019-11-08 stsp goto done;
5703 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5704 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5705 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5707 c0b01bdb 2019-11-08 stsp goto done;
5709 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5710 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5711 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5713 11b20872 2019-11-08 stsp goto done;
5715 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5716 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5718 c0b01bdb 2019-11-08 stsp goto done;
5721 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5722 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5723 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5724 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5725 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5727 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5728 bc573f3b 2021-07-10 stsp if (commit)
5729 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5731 bc573f3b 2021-07-10 stsp close_tree_view(view);
5732 ad80ab7b 2018-08-04 stsp return err;
5735 e5a0f69f 2018-08-18 stsp static const struct got_error *
5736 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5738 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5740 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5741 fb2756b9 2018-08-04 stsp free(s->tree_label);
5742 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5743 6484ec90 2018-09-29 stsp free(s->commit_id);
5744 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5745 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5746 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5747 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5748 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5749 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5750 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5751 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5752 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5753 ad80ab7b 2018-08-04 stsp free(parent);
5756 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5757 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5758 bc573f3b 2021-07-10 stsp if (s->root)
5759 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5760 7c32bd05 2019-06-22 stsp return NULL;
5763 7c32bd05 2019-06-22 stsp static const struct got_error *
5764 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5766 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5768 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5769 7c32bd05 2019-06-22 stsp return NULL;
5772 7c32bd05 2019-06-22 stsp static int
5773 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5775 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5777 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5781 7c32bd05 2019-06-22 stsp static const struct got_error *
5782 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5784 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5785 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5787 7c32bd05 2019-06-22 stsp if (!view->searching) {
5788 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5789 7c32bd05 2019-06-22 stsp return NULL;
5792 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5793 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5794 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5795 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5796 56e0773d 2019-11-28 stsp s->selected_entry);
5798 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5800 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5801 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5803 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5804 56e0773d 2019-11-28 stsp s->selected_entry);
5807 487cd7d2 2021-12-17 stsp if (s->selected_entry)
5808 487cd7d2 2021-12-17 stsp te = s->selected_entry;
5809 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
5810 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5812 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5815 7c32bd05 2019-06-22 stsp while (1) {
5816 56e0773d 2019-11-28 stsp if (te == NULL) {
5817 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5818 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5819 ac66afb8 2019-06-24 stsp return NULL;
5821 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5822 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5824 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5827 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5828 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5829 56e0773d 2019-11-28 stsp s->matched_entry = te;
5833 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5834 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5836 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5839 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5840 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5841 7c32bd05 2019-06-22 stsp s->selected = 0;
5844 e5a0f69f 2018-08-18 stsp return NULL;
5847 ad80ab7b 2018-08-04 stsp static const struct got_error *
5848 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5850 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5851 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5852 e5a0f69f 2018-08-18 stsp char *parent_path;
5854 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5856 e5a0f69f 2018-08-18 stsp return err;
5858 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5859 e5a0f69f 2018-08-18 stsp free(parent_path);
5861 669b5ffa 2018-10-07 stsp view_vborder(view);
5862 e5a0f69f 2018-08-18 stsp return err;
5865 e5a0f69f 2018-08-18 stsp static const struct got_error *
5866 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5868 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5869 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5870 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5871 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5872 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
5874 e5a0f69f 2018-08-18 stsp switch (ch) {
5876 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5879 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5881 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5882 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5883 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5884 e78dc838 2020-12-04 stsp view->focussed = 0;
5885 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5886 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5887 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5889 1e37a5c2 2019-05-12 jcs return err;
5890 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
5893 e78dc838 2020-12-04 stsp view->focus_child = 1;
5895 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5898 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5899 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5900 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5901 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5902 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5903 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5904 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5905 152c1c93 2020-11-29 stsp if (err) {
5906 152c1c93 2020-11-29 stsp view_close(ref_view);
5907 152c1c93 2020-11-29 stsp return err;
5909 e78dc838 2020-12-04 stsp view->focussed = 0;
5910 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5911 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5912 152c1c93 2020-11-29 stsp err = view_close_child(view);
5914 152c1c93 2020-11-29 stsp return err;
5915 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
5918 e78dc838 2020-12-04 stsp view->focus_child = 1;
5920 152c1c93 2020-11-29 stsp *new_view = ref_view;
5922 e4526bf5 2021-09-03 naddy case 'g':
5923 e4526bf5 2021-09-03 naddy case KEY_HOME:
5924 e4526bf5 2021-09-03 naddy s->selected = 0;
5925 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5926 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5927 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5929 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5931 e4526bf5 2021-09-03 naddy case 'G':
5932 e4526bf5 2021-09-03 naddy case KEY_END:
5933 e4526bf5 2021-09-03 naddy s->selected = 0;
5934 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5935 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5936 e4526bf5 2021-09-03 naddy if (te == NULL) {
5937 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5938 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5943 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5944 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5946 e4526bf5 2021-09-03 naddy if (n > 0)
5947 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5950 1e37a5c2 2019-05-12 jcs case KEY_UP:
5951 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5952 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5953 1e37a5c2 2019-05-12 jcs s->selected--;
5956 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5958 83cc4199 2022-06-13 stsp case CTRL('u'):
5960 83cc4199 2022-06-13 stsp nscroll /= 2;
5961 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5962 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5963 ea025d1d 2020-02-22 naddy case CTRL('b'):
5964 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5965 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5966 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5967 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5969 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5970 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5972 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
5975 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5976 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5977 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5978 1e37a5c2 2019-05-12 jcs s->selected++;
5981 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5983 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5985 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5987 83cc4199 2022-06-13 stsp case CTRL('d'):
5989 83cc4199 2022-06-13 stsp nscroll /= 2;
5990 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5991 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5992 ea025d1d 2020-02-22 naddy case CTRL('f'):
5993 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5995 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5996 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5997 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
5998 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6001 83cc4199 2022-06-13 stsp tree_scroll_down(s, nscroll);
6003 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6005 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6006 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6007 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6008 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6009 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
6011 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6012 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6014 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6015 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6016 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6017 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6018 1e37a5c2 2019-05-12 jcs s->selected_entry =
6019 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6020 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6021 1e37a5c2 2019-05-12 jcs free(parent);
6022 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6023 56e0773d 2019-11-28 stsp s->selected_entry))) {
6024 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6025 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6026 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6029 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6030 941e9f74 2019-05-21 stsp if (err) {
6031 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6034 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6035 56e0773d 2019-11-28 stsp s->selected_entry))) {
6036 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6037 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
6038 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
6040 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
6041 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6042 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6045 e78dc838 2020-12-04 stsp view->focussed = 0;
6046 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6047 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6048 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6050 669b5ffa 2018-10-07 stsp return err;
6051 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6054 e78dc838 2020-12-04 stsp view->focus_child = 1;
6056 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6059 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6060 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6061 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6067 ffd1d5e5 2018-06-23 stsp return err;
6070 ffd1d5e5 2018-06-23 stsp __dead static void
6071 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6074 87411fa9 2022-06-16 stsp fprintf(stderr,
6075 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6076 ffd1d5e5 2018-06-23 stsp getprogname());
6080 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6081 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6083 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6084 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6085 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6086 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6087 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6088 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6089 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6090 4e97c21c 2020-12-06 stsp char *label = NULL;
6091 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6092 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6094 5221c383 2018-08-01 stsp struct tog_view *view;
6095 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6097 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6098 ffd1d5e5 2018-06-23 stsp switch (ch) {
6100 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6103 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6104 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6105 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6109 e99e2d15 2020-11-24 naddy usage_tree();
6110 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6114 ffd1d5e5 2018-06-23 stsp argc -= optind;
6115 ffd1d5e5 2018-06-23 stsp argv += optind;
6117 55cccc34 2020-02-20 stsp if (argc > 1)
6118 e99e2d15 2020-11-24 naddy usage_tree();
6120 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6121 0ae84acc 2022-06-15 tracey if (error != NULL)
6122 0ae84acc 2022-06-15 tracey goto done;
6124 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6125 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6126 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6127 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6128 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6129 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6130 c156c7a4 2020-12-18 stsp goto done;
6131 55cccc34 2020-02-20 stsp if (worktree)
6132 52185f70 2019-02-05 stsp repo_path =
6133 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6135 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6136 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6137 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6138 c156c7a4 2020-12-18 stsp goto done;
6142 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6143 c02c541e 2019-03-29 stsp if (error != NULL)
6144 52185f70 2019-02-05 stsp goto done;
6146 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6147 55cccc34 2020-02-20 stsp repo, worktree);
6148 55cccc34 2020-02-20 stsp if (error)
6149 55cccc34 2020-02-20 stsp goto done;
6151 55cccc34 2020-02-20 stsp init_curses();
6153 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6154 c02c541e 2019-03-29 stsp if (error)
6155 52185f70 2019-02-05 stsp goto done;
6157 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6158 51a10b52 2020-12-26 stsp if (error)
6159 51a10b52 2020-12-26 stsp goto done;
6161 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6162 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6163 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6164 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6165 4e97c21c 2020-12-06 stsp if (error)
6166 4e97c21c 2020-12-06 stsp goto done;
6167 4e97c21c 2020-12-06 stsp head_ref_name = label;
6169 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6170 4e97c21c 2020-12-06 stsp if (error == NULL)
6171 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6172 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6173 4e97c21c 2020-12-06 stsp goto done;
6174 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6175 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6176 4e97c21c 2020-12-06 stsp if (error)
6177 4e97c21c 2020-12-06 stsp goto done;
6180 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6181 a44927cc 2022-04-07 stsp if (error)
6182 a44927cc 2022-04-07 stsp goto done;
6184 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6185 5221c383 2018-08-01 stsp if (view == NULL) {
6186 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6187 5221c383 2018-08-01 stsp goto done;
6189 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6190 ad80ab7b 2018-08-04 stsp if (error)
6191 ad80ab7b 2018-08-04 stsp goto done;
6192 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6193 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6194 d91faf3b 2020-12-01 naddy in_repo_path);
6195 55cccc34 2020-02-20 stsp if (error)
6196 55cccc34 2020-02-20 stsp goto done;
6199 55cccc34 2020-02-20 stsp if (worktree) {
6200 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6201 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6202 55cccc34 2020-02-20 stsp worktree = NULL;
6204 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6206 52185f70 2019-02-05 stsp free(repo_path);
6207 e4a0e26d 2020-02-20 stsp free(cwd);
6208 ffd1d5e5 2018-06-23 stsp free(commit_id);
6209 4e97c21c 2020-12-06 stsp free(label);
6211 486cd271 2020-12-06 stsp got_ref_close(ref);
6212 1d0f4054 2021-06-17 stsp if (repo) {
6213 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6214 1d0f4054 2021-06-17 stsp if (error == NULL)
6215 1d0f4054 2021-06-17 stsp error = close_err;
6217 0ae84acc 2022-06-15 tracey if (pack_fds) {
6218 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6219 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6220 0ae84acc 2022-06-15 tracey if (error == NULL)
6221 0ae84acc 2022-06-15 tracey error = pack_err;
6223 51a10b52 2020-12-26 stsp tog_free_refs();
6224 ffd1d5e5 2018-06-23 stsp return error;
6227 6458efa5 2020-11-24 stsp static const struct got_error *
6228 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6230 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6231 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6233 6458efa5 2020-11-24 stsp s->nrefs = 0;
6234 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6235 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6236 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6237 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6238 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6241 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6242 6458efa5 2020-11-24 stsp if (re == NULL)
6243 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6245 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6246 8924d611 2020-12-26 stsp if (re->ref == NULL)
6247 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6248 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6249 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6252 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6253 6458efa5 2020-11-24 stsp return NULL;
6257 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6259 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6261 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6262 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6263 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6264 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6269 6458efa5 2020-11-24 stsp static const struct got_error *
6270 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6272 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6273 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6275 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6276 6458efa5 2020-11-24 stsp s->repo = repo;
6278 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6279 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6281 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6283 6458efa5 2020-11-24 stsp return err;
6285 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6286 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6287 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6288 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6290 6458efa5 2020-11-24 stsp goto done;
6292 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6293 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6294 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6296 6458efa5 2020-11-24 stsp goto done;
6298 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6299 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6300 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6302 cc488aa7 2022-01-23 stsp goto done;
6304 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6305 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6306 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6308 6458efa5 2020-11-24 stsp goto done;
6311 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6312 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6313 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6314 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6315 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6318 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6319 6458efa5 2020-11-24 stsp return err;
6322 6458efa5 2020-11-24 stsp static const struct got_error *
6323 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6325 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6327 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6328 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6330 6458efa5 2020-11-24 stsp return NULL;
6333 6458efa5 2020-11-24 stsp static const struct got_error *
6334 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6335 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6337 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6338 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6339 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6340 6458efa5 2020-11-24 stsp int obj_type;
6342 c42c9805 2020-11-24 stsp *commit_id = NULL;
6344 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6346 6458efa5 2020-11-24 stsp return err;
6348 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6350 6458efa5 2020-11-24 stsp goto done;
6352 6458efa5 2020-11-24 stsp switch (obj_type) {
6353 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6354 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6356 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6357 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6359 6458efa5 2020-11-24 stsp goto done;
6360 c42c9805 2020-11-24 stsp free(obj_id);
6361 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6362 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6364 6458efa5 2020-11-24 stsp goto done;
6365 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6366 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6367 c42c9805 2020-11-24 stsp goto done;
6369 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6370 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6371 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6372 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6373 c42c9805 2020-11-24 stsp goto done;
6377 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6383 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6384 c42c9805 2020-11-24 stsp if (err) {
6385 c42c9805 2020-11-24 stsp free(*commit_id);
6386 c42c9805 2020-11-24 stsp *commit_id = NULL;
6388 c42c9805 2020-11-24 stsp return err;
6391 c42c9805 2020-11-24 stsp static const struct got_error *
6392 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6393 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6395 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6396 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6397 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6399 c42c9805 2020-11-24 stsp *new_view = NULL;
6401 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6402 c42c9805 2020-11-24 stsp if (err) {
6403 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6404 c42c9805 2020-11-24 stsp return err;
6406 c42c9805 2020-11-24 stsp return NULL;
6409 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6410 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6411 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6412 6458efa5 2020-11-24 stsp goto done;
6415 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6416 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6419 6458efa5 2020-11-24 stsp view_close(log_view);
6421 6458efa5 2020-11-24 stsp *new_view = log_view;
6422 c42c9805 2020-11-24 stsp free(commit_id);
6423 6458efa5 2020-11-24 stsp return err;
6426 ce5b7c56 2019-07-09 stsp static void
6427 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6429 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6430 34ba6917 2020-11-30 stsp int i = 0;
6432 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6435 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6436 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6437 34ba6917 2020-11-30 stsp if (re == NULL)
6439 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6440 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6444 34ba6917 2020-11-30 stsp static void
6445 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6447 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6448 6458efa5 2020-11-24 stsp int n = 0;
6450 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6451 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6453 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6455 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6456 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6457 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6458 6458efa5 2020-11-24 stsp if (last) {
6459 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6460 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6465 6458efa5 2020-11-24 stsp static const struct got_error *
6466 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6468 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6470 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6471 6458efa5 2020-11-24 stsp return NULL;
6474 6458efa5 2020-11-24 stsp static int
6475 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6477 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6479 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6483 6458efa5 2020-11-24 stsp static const struct got_error *
6484 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6486 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6487 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6489 6458efa5 2020-11-24 stsp if (!view->searching) {
6490 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6491 6458efa5 2020-11-24 stsp return NULL;
6494 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6495 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6496 6458efa5 2020-11-24 stsp if (s->selected_entry)
6497 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6499 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6500 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6502 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6503 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6505 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6506 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6509 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6510 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6511 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6512 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6514 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6517 6458efa5 2020-11-24 stsp while (1) {
6518 6458efa5 2020-11-24 stsp if (re == NULL) {
6519 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6520 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6521 6458efa5 2020-11-24 stsp return NULL;
6523 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6524 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6526 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6529 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6530 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6531 6458efa5 2020-11-24 stsp s->matched_entry = re;
6535 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6536 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6538 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6541 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6542 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6543 6458efa5 2020-11-24 stsp s->selected = 0;
6546 6458efa5 2020-11-24 stsp return NULL;
6549 6458efa5 2020-11-24 stsp static const struct got_error *
6550 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6552 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6553 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6554 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6555 6458efa5 2020-11-24 stsp char *line = NULL;
6556 6458efa5 2020-11-24 stsp wchar_t *wline;
6557 6458efa5 2020-11-24 stsp struct tog_color *tc;
6558 6458efa5 2020-11-24 stsp int width, n;
6559 6458efa5 2020-11-24 stsp int limit = view->nlines;
6561 6458efa5 2020-11-24 stsp werase(view->window);
6563 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6565 6458efa5 2020-11-24 stsp if (limit == 0)
6566 6458efa5 2020-11-24 stsp return NULL;
6568 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6570 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6571 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6572 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6574 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6575 6458efa5 2020-11-24 stsp if (err) {
6576 6458efa5 2020-11-24 stsp free(line);
6577 6458efa5 2020-11-24 stsp return err;
6579 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6580 6458efa5 2020-11-24 stsp wstandout(view->window);
6581 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6582 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6583 6458efa5 2020-11-24 stsp wstandend(view->window);
6584 6458efa5 2020-11-24 stsp free(wline);
6585 6458efa5 2020-11-24 stsp wline = NULL;
6586 6458efa5 2020-11-24 stsp free(line);
6587 6458efa5 2020-11-24 stsp line = NULL;
6588 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6589 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6590 6458efa5 2020-11-24 stsp if (--limit <= 0)
6591 6458efa5 2020-11-24 stsp return NULL;
6594 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6595 6458efa5 2020-11-24 stsp char *line = NULL;
6596 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6598 b4996bee 2022-06-16 stsp if (s->show_date) {
6599 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
6600 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
6601 b4996bee 2022-06-16 stsp struct got_object_id *id;
6602 b4996bee 2022-06-16 stsp struct tm tm;
6605 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6607 b4996bee 2022-06-16 stsp return err;
6608 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
6609 b4996bee 2022-06-16 stsp if (err) {
6610 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6612 b4996bee 2022-06-16 stsp return err;
6614 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
6616 b4996bee 2022-06-16 stsp if (err) {
6618 b4996bee 2022-06-16 stsp return err;
6620 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
6621 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
6623 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
6624 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
6627 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
6628 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
6629 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6630 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
6632 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6633 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
6634 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
6635 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6636 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6637 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6638 6458efa5 2020-11-24 stsp struct got_object_id *id;
6639 6458efa5 2020-11-24 stsp char *id_str;
6640 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6642 6458efa5 2020-11-24 stsp return err;
6643 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6644 6458efa5 2020-11-24 stsp if (err) {
6646 6458efa5 2020-11-24 stsp return err;
6648 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6649 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6650 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6652 6458efa5 2020-11-24 stsp free(id_str);
6653 6458efa5 2020-11-24 stsp return err;
6656 6458efa5 2020-11-24 stsp free(id_str);
6657 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6658 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
6659 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
6661 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6663 6458efa5 2020-11-24 stsp if (err) {
6664 6458efa5 2020-11-24 stsp free(line);
6665 6458efa5 2020-11-24 stsp return err;
6667 6458efa5 2020-11-24 stsp if (n == s->selected) {
6668 6458efa5 2020-11-24 stsp if (view->focussed)
6669 6458efa5 2020-11-24 stsp wstandout(view->window);
6670 6458efa5 2020-11-24 stsp s->selected_entry = re;
6672 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6674 6458efa5 2020-11-24 stsp wattr_on(view->window,
6675 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6676 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6678 6458efa5 2020-11-24 stsp wattr_off(view->window,
6679 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6680 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6681 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6682 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6683 6458efa5 2020-11-24 stsp wstandend(view->window);
6684 6458efa5 2020-11-24 stsp free(line);
6685 6458efa5 2020-11-24 stsp free(wline);
6686 6458efa5 2020-11-24 stsp wline = NULL;
6688 6458efa5 2020-11-24 stsp s->ndisplayed++;
6689 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6692 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6695 6458efa5 2020-11-24 stsp view_vborder(view);
6696 6458efa5 2020-11-24 stsp return err;
6699 6458efa5 2020-11-24 stsp static const struct got_error *
6700 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6701 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6703 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6704 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6705 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6707 c42c9805 2020-11-24 stsp *new_view = NULL;
6709 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6710 c42c9805 2020-11-24 stsp if (err) {
6711 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6712 c42c9805 2020-11-24 stsp return err;
6714 c42c9805 2020-11-24 stsp return NULL;
6718 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6719 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6720 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6721 c42c9805 2020-11-24 stsp goto done;
6724 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6725 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6727 c42c9805 2020-11-24 stsp goto done;
6729 c42c9805 2020-11-24 stsp *new_view = tree_view;
6731 c42c9805 2020-11-24 stsp free(commit_id);
6732 c42c9805 2020-11-24 stsp return err;
6734 c42c9805 2020-11-24 stsp static const struct got_error *
6735 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6737 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6738 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6739 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6740 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6741 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
6743 6458efa5 2020-11-24 stsp switch (ch) {
6745 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6748 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
6751 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
6752 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6753 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
6754 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
6757 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
6758 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
6759 50617b77 2021-11-20 stsp &tog_refs, s->repo);
6762 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
6763 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
6765 6458efa5 2020-11-24 stsp case KEY_ENTER:
6766 6458efa5 2020-11-24 stsp case '\r':
6767 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6769 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6770 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6771 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6773 e78dc838 2020-12-04 stsp view->focussed = 0;
6774 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6775 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6776 6458efa5 2020-11-24 stsp err = view_close_child(view);
6778 6458efa5 2020-11-24 stsp return err;
6779 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6782 e78dc838 2020-12-04 stsp view->focus_child = 1;
6784 6458efa5 2020-11-24 stsp *new_view = log_view;
6787 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6789 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6790 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6791 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6793 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6795 e78dc838 2020-12-04 stsp view->focussed = 0;
6796 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6797 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6798 c42c9805 2020-11-24 stsp err = view_close_child(view);
6800 c42c9805 2020-11-24 stsp return err;
6801 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
6804 e78dc838 2020-12-04 stsp view->focus_child = 1;
6806 c42c9805 2020-11-24 stsp *new_view = tree_view;
6808 e4526bf5 2021-09-03 naddy case 'g':
6809 e4526bf5 2021-09-03 naddy case KEY_HOME:
6810 e4526bf5 2021-09-03 naddy s->selected = 0;
6811 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6813 e4526bf5 2021-09-03 naddy case 'G':
6814 e4526bf5 2021-09-03 naddy case KEY_END:
6815 e4526bf5 2021-09-03 naddy s->selected = 0;
6816 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6817 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6818 e4526bf5 2021-09-03 naddy if (re == NULL)
6820 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6821 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6823 e4526bf5 2021-09-03 naddy if (n > 0)
6824 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6827 6458efa5 2020-11-24 stsp case KEY_UP:
6828 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6829 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6830 6458efa5 2020-11-24 stsp s->selected--;
6833 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6835 83cc4199 2022-06-13 stsp case CTRL('u'):
6837 83cc4199 2022-06-13 stsp nscroll /= 2;
6838 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6839 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6840 6458efa5 2020-11-24 stsp case CTRL('b'):
6841 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6842 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
6843 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
6846 6458efa5 2020-11-24 stsp case KEY_DOWN:
6847 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6848 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6849 6458efa5 2020-11-24 stsp s->selected++;
6852 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6853 6458efa5 2020-11-24 stsp /* can't scroll any further */
6855 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6857 83cc4199 2022-06-13 stsp case CTRL('d'):
6859 83cc4199 2022-06-13 stsp nscroll /= 2;
6860 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6861 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6862 6458efa5 2020-11-24 stsp case CTRL('f'):
6863 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6864 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6865 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6866 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6867 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6870 83cc4199 2022-06-13 stsp ref_scroll_down(s, nscroll);
6872 6458efa5 2020-11-24 stsp case CTRL('l'):
6873 8924d611 2020-12-26 stsp tog_free_refs();
6874 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
6877 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6878 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6880 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6881 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6882 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6888 6458efa5 2020-11-24 stsp return err;
6891 6458efa5 2020-11-24 stsp __dead static void
6892 6458efa5 2020-11-24 stsp usage_ref(void)
6895 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6896 6458efa5 2020-11-24 stsp getprogname());
6900 6458efa5 2020-11-24 stsp static const struct got_error *
6901 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6903 6458efa5 2020-11-24 stsp const struct got_error *error;
6904 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6905 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6906 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6908 6458efa5 2020-11-24 stsp struct tog_view *view;
6909 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6911 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6912 6458efa5 2020-11-24 stsp switch (ch) {
6914 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6915 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6916 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6920 e99e2d15 2020-11-24 naddy usage_ref();
6921 6458efa5 2020-11-24 stsp /* NOTREACHED */
6925 6458efa5 2020-11-24 stsp argc -= optind;
6926 6458efa5 2020-11-24 stsp argv += optind;
6928 6458efa5 2020-11-24 stsp if (argc > 1)
6929 e99e2d15 2020-11-24 naddy usage_ref();
6931 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6932 0ae84acc 2022-06-15 tracey if (error != NULL)
6933 0ae84acc 2022-06-15 tracey goto done;
6935 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6936 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6937 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6938 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6939 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6940 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6941 c156c7a4 2020-12-18 stsp goto done;
6942 6458efa5 2020-11-24 stsp if (worktree)
6943 6458efa5 2020-11-24 stsp repo_path =
6944 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6946 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6947 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6948 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6949 c156c7a4 2020-12-18 stsp goto done;
6953 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6954 6458efa5 2020-11-24 stsp if (error != NULL)
6955 6458efa5 2020-11-24 stsp goto done;
6957 6458efa5 2020-11-24 stsp init_curses();
6959 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6960 51a10b52 2020-12-26 stsp if (error)
6961 51a10b52 2020-12-26 stsp goto done;
6963 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6964 6458efa5 2020-11-24 stsp if (error)
6965 6458efa5 2020-11-24 stsp goto done;
6967 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6968 6458efa5 2020-11-24 stsp if (view == NULL) {
6969 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6970 6458efa5 2020-11-24 stsp goto done;
6973 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6974 6458efa5 2020-11-24 stsp if (error)
6975 6458efa5 2020-11-24 stsp goto done;
6977 6458efa5 2020-11-24 stsp if (worktree) {
6978 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6979 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6980 6458efa5 2020-11-24 stsp worktree = NULL;
6982 6458efa5 2020-11-24 stsp error = view_loop(view);
6984 6458efa5 2020-11-24 stsp free(repo_path);
6985 6458efa5 2020-11-24 stsp free(cwd);
6986 1d0f4054 2021-06-17 stsp if (repo) {
6987 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6988 1d0f4054 2021-06-17 stsp if (close_err)
6989 1d0f4054 2021-06-17 stsp error = close_err;
6991 0ae84acc 2022-06-15 tracey if (pack_fds) {
6992 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6993 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6994 0ae84acc 2022-06-15 tracey if (error == NULL)
6995 0ae84acc 2022-06-15 tracey error = pack_err;
6997 51a10b52 2020-12-26 stsp tog_free_refs();
6998 6458efa5 2020-11-24 stsp return error;
7001 6458efa5 2020-11-24 stsp static void
7002 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7006 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7007 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7008 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7009 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7011 6879ba42 2020-10-01 naddy fputc('\n', fp);
7014 4ed7e80c 2018-05-20 stsp __dead static void
7015 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7017 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7019 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7020 6879ba42 2020-10-01 naddy getprogname());
7021 6879ba42 2020-10-01 naddy if (hflag) {
7022 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7023 6879ba42 2020-10-01 naddy list_commands(fp);
7025 6879ba42 2020-10-01 naddy exit(status);
7028 c2301be8 2018-04-30 stsp static char **
7029 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7031 ee85c5e8 2020-02-29 stsp va_list ap;
7032 c2301be8 2018-04-30 stsp char **argv;
7035 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7037 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7038 c2301be8 2018-04-30 stsp if (argv == NULL)
7039 c2301be8 2018-04-30 stsp err(1, "calloc");
7040 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7041 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7042 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7043 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7046 ee85c5e8 2020-02-29 stsp va_end(ap);
7047 c2301be8 2018-04-30 stsp return argv;
7051 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7052 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7053 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7054 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7055 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7057 ee85c5e8 2020-02-29 stsp static const struct got_error *
7058 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7060 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7061 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7062 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7063 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7064 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7065 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7066 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7067 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7068 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7070 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7071 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7072 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7074 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7075 0ae84acc 2022-06-15 tracey if (error != NULL)
7076 0ae84acc 2022-06-15 tracey goto done;
7078 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7079 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7080 ee85c5e8 2020-02-29 stsp goto done;
7082 ee85c5e8 2020-02-29 stsp if (worktree)
7083 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7085 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7086 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7087 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7088 ee85c5e8 2020-02-29 stsp goto done;
7091 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7092 ee85c5e8 2020-02-29 stsp if (error != NULL)
7093 ee85c5e8 2020-02-29 stsp goto done;
7095 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7096 ee85c5e8 2020-02-29 stsp repo, worktree);
7097 ee85c5e8 2020-02-29 stsp if (error)
7098 ee85c5e8 2020-02-29 stsp goto done;
7100 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7101 84de9106 2020-12-26 stsp if (error)
7102 84de9106 2020-12-26 stsp goto done;
7103 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7104 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7105 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7106 ee85c5e8 2020-02-29 stsp if (error)
7107 ee85c5e8 2020-02-29 stsp goto done;
7109 ee85c5e8 2020-02-29 stsp if (worktree) {
7110 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7111 ee85c5e8 2020-02-29 stsp worktree = NULL;
7114 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7115 a44927cc 2022-04-07 stsp if (error)
7116 a44927cc 2022-04-07 stsp goto done;
7118 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7119 ee85c5e8 2020-02-29 stsp if (error) {
7120 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7121 ee85c5e8 2020-02-29 stsp goto done;
7122 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7123 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7124 6879ba42 2020-10-01 naddy usage(1, 1);
7125 ee85c5e8 2020-02-29 stsp /* not reached */
7128 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7129 1d0f4054 2021-06-17 stsp if (error == NULL)
7130 1d0f4054 2021-06-17 stsp error = close_err;
7131 ee85c5e8 2020-02-29 stsp repo = NULL;
7133 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7134 ee85c5e8 2020-02-29 stsp if (error)
7135 ee85c5e8 2020-02-29 stsp goto done;
7137 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7139 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7140 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7142 1d0f4054 2021-06-17 stsp if (repo) {
7143 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7144 1d0f4054 2021-06-17 stsp if (error == NULL)
7145 1d0f4054 2021-06-17 stsp error = close_err;
7147 a44927cc 2022-04-07 stsp if (commit)
7148 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7149 ee85c5e8 2020-02-29 stsp if (worktree)
7150 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7151 0ae84acc 2022-06-15 tracey if (pack_fds) {
7152 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7153 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7154 0ae84acc 2022-06-15 tracey if (error == NULL)
7155 0ae84acc 2022-06-15 tracey error = pack_err;
7158 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7159 ee85c5e8 2020-02-29 stsp free(commit_id);
7160 ee85c5e8 2020-02-29 stsp free(cwd);
7161 ee85c5e8 2020-02-29 stsp free(repo_path);
7162 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7163 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7165 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7166 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7167 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7169 87670572 2020-12-26 naddy tog_free_refs();
7170 ee85c5e8 2020-02-29 stsp return error;
7174 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7176 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7177 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7178 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7179 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7180 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7181 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7182 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7185 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7187 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7188 9f7d7167 2018-04-29 stsp switch (ch) {
7190 9f7d7167 2018-04-29 stsp hflag = 1;
7193 53ccebc2 2019-07-30 stsp Vflag = 1;
7196 6879ba42 2020-10-01 naddy usage(hflag, 1);
7197 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7201 9f7d7167 2018-04-29 stsp argc -= optind;
7202 9f7d7167 2018-04-29 stsp argv += optind;
7203 9814e6a3 2020-09-27 naddy optind = 1;
7204 c2301be8 2018-04-30 stsp optreset = 1;
7206 53ccebc2 2019-07-30 stsp if (Vflag) {
7207 53ccebc2 2019-07-30 stsp got_version_print_str();
7208 6879ba42 2020-10-01 naddy return 0;
7211 4010e238 2020-12-04 stsp #ifndef PROFILE
7212 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7213 4010e238 2020-12-04 stsp NULL) == -1)
7214 4010e238 2020-12-04 stsp err(1, "pledge");
7217 c2301be8 2018-04-30 stsp if (argc == 0) {
7218 f29d3e89 2018-06-23 stsp if (hflag)
7219 6879ba42 2020-10-01 naddy usage(hflag, 0);
7220 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7221 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7223 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7227 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7228 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7229 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7230 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7231 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7237 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7238 ee85c5e8 2020-02-29 stsp if (argc != 1)
7239 6879ba42 2020-10-01 naddy usage(0, 1);
7240 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7241 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7243 ee85c5e8 2020-02-29 stsp if (hflag)
7244 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7246 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7250 b46c1e04 2020-09-20 naddy putchar('\n');
7251 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7253 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7254 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7255 a2f4a359 2020-02-28 stsp free(cmd_argv);
7258 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7259 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);