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;
752 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
754 6131ff18 2022-06-20 mark return view->begin_x > 0;
758 4d8c2215 2018-08-19 stsp static const struct got_error *
759 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
761 f7d12f7e 2018-08-01 stsp int nlines, ncols;
763 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
764 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
766 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
768 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
769 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
771 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
773 6131ff18 2022-06-20 mark if (view->child && view_is_splitscreen(view->child)) {
774 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
775 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
776 0dbbbe90 2022-06-17 op ncols = COLS;
778 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
779 5c60c32a 2018-10-18 stsp if (view->child->focussed)
780 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
782 5c60c32a 2018-10-18 stsp show_panel(view->panel);
784 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
786 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
787 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
789 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
790 0dbbbe90 2022-06-17 op ncols = COLS;
792 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
793 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
794 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
795 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
796 0dbbbe90 2022-06-17 op wclear(view->window);
798 0dbbbe90 2022-06-17 op view->nlines = nlines;
799 0dbbbe90 2022-06-17 op view->ncols = ncols;
800 0dbbbe90 2022-06-17 op view->lines = LINES;
801 0dbbbe90 2022-06-17 op view->cols = COLS;
803 5c60c32a 2018-10-18 stsp return NULL;
806 669b5ffa 2018-10-07 stsp static const struct got_error *
807 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
809 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
811 669b5ffa 2018-10-07 stsp if (view->child == NULL)
812 669b5ffa 2018-10-07 stsp return NULL;
814 669b5ffa 2018-10-07 stsp err = view_close(view->child);
815 669b5ffa 2018-10-07 stsp view->child = NULL;
816 669b5ffa 2018-10-07 stsp return err;
819 0dbbbe90 2022-06-17 op static const struct got_error *
820 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
822 669b5ffa 2018-10-07 stsp view->child = child;
823 669b5ffa 2018-10-07 stsp child->parent = view;
825 0dbbbe90 2022-06-17 op return view_resize(view);
828 34bc9ec9 2019-02-22 stsp static void
829 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
831 25791caa 2018-10-24 stsp int cols, lines;
832 25791caa 2018-10-24 stsp struct winsize size;
834 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
835 25791caa 2018-10-24 stsp cols = 80; /* Default */
836 25791caa 2018-10-24 stsp lines = 24;
838 25791caa 2018-10-24 stsp cols = size.ws_col;
839 25791caa 2018-10-24 stsp lines = size.ws_row;
841 25791caa 2018-10-24 stsp resize_term(lines, cols);
844 2b49a8ae 2019-06-22 stsp static const struct got_error *
845 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
847 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
848 2b49a8ae 2019-06-22 stsp char pattern[1024];
851 c0c4acc8 2021-01-24 stsp if (view->search_started) {
852 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
853 c0c4acc8 2021-01-24 stsp view->searching = 0;
854 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
856 c0c4acc8 2021-01-24 stsp view->search_started = 0;
858 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
859 2b49a8ae 2019-06-22 stsp return NULL;
861 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
862 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
864 2b49a8ae 2019-06-22 stsp nocbreak();
866 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
869 2b49a8ae 2019-06-22 stsp if (ret == ERR)
870 2b49a8ae 2019-06-22 stsp return NULL;
872 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
873 7c32bd05 2019-06-22 stsp err = view->search_start(view);
875 7c32bd05 2019-06-22 stsp regfree(&view->regex);
876 7c32bd05 2019-06-22 stsp return err;
878 c0c4acc8 2021-01-24 stsp view->search_started = 1;
879 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
880 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
881 2b49a8ae 2019-06-22 stsp view->search_next(view);
884 2b49a8ae 2019-06-22 stsp return NULL;
887 0cf4efb1 2018-09-29 stsp static const struct got_error *
888 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
889 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
891 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
892 669b5ffa 2018-10-07 stsp struct tog_view *v;
893 1a76625f 2018-10-22 stsp int ch, errcode;
895 e5a0f69f 2018-08-18 stsp *new = NULL;
897 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
898 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
899 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
900 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
902 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
903 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
904 82954512 2020-02-03 stsp if (errcode)
905 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
906 82954512 2020-02-03 stsp "pthread_mutex_unlock");
907 3da8ef85 2021-09-21 stsp sched_yield();
908 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
909 82954512 2020-02-03 stsp if (errcode)
910 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
911 82954512 2020-02-03 stsp "pthread_mutex_lock");
912 60493ae3 2019-06-20 stsp view->search_next(view);
913 60493ae3 2019-06-20 stsp return NULL;
916 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
917 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
918 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
919 1a76625f 2018-10-22 stsp if (errcode)
920 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
921 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
922 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
923 1a76625f 2018-10-22 stsp if (errcode)
924 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
925 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
927 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
928 25791caa 2018-10-24 stsp tog_resizeterm();
929 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
930 61266923 2020-01-14 stsp tog_sigcont_received = 0;
931 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
932 25791caa 2018-10-24 stsp err = view_resize(v);
934 25791caa 2018-10-24 stsp return err;
935 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
937 25791caa 2018-10-24 stsp return err;
938 cdfcfb03 2020-12-06 stsp if (v->child) {
939 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
941 cdfcfb03 2020-12-06 stsp return err;
942 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
943 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
945 cdfcfb03 2020-12-06 stsp return err;
950 e5a0f69f 2018-08-18 stsp switch (ch) {
952 1e37a5c2 2019-05-12 jcs if (view->child) {
953 e78dc838 2020-12-04 stsp view->focussed = 0;
954 e78dc838 2020-12-04 stsp view->child->focussed = 1;
955 e78dc838 2020-12-04 stsp view->focus_child = 1;
956 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
957 e78dc838 2020-12-04 stsp view->focussed = 0;
958 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
959 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
960 6131ff18 2022-06-20 mark if (!view_is_splitscreen(view))
961 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
965 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
966 9970f7fc 2020-12-03 stsp view->dying = 1;
972 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
973 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
975 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
976 e78dc838 2020-12-04 stsp view->focussed = 0;
977 e78dc838 2020-12-04 stsp view->child->focussed = 1;
978 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
980 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
983 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
984 9970f7fc 2020-12-03 stsp KEY_RESIZE);
986 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
987 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
988 e78dc838 2020-12-04 stsp view->focussed = 1;
989 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
991 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
993 6131ff18 2022-06-20 mark err = view_resize(view->parent);
997 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1000 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1003 60493ae3 2019-06-20 stsp if (view->search_start)
1004 2b49a8ae 2019-06-22 stsp view_search_start(view);
1006 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1010 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1011 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1012 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1013 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1014 60493ae3 2019-06-20 stsp view->search_next(view);
1016 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1019 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1023 e5a0f69f 2018-08-18 stsp return err;
1027 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1029 0cf4efb1 2018-09-29 stsp PANEL *panel;
1030 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1032 669b5ffa 2018-10-07 stsp if (view->parent)
1033 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1035 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1036 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1039 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1040 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1041 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1045 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1047 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1048 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1050 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1052 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1055 669b5ffa 2018-10-07 stsp return view->focussed;
1058 bcbd79e2 2018-08-19 stsp static const struct got_error *
1059 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1061 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1062 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1063 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1064 fd823528 2018-10-22 stsp int fast_refresh = 10;
1065 1a76625f 2018-10-22 stsp int done = 0, errcode;
1067 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1068 1a76625f 2018-10-22 stsp if (errcode)
1069 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1071 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1072 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1074 1004088d 2018-09-29 stsp view->focussed = 1;
1075 878940b7 2018-09-29 stsp err = view->show(view);
1077 0cf4efb1 2018-09-29 stsp return err;
1078 0cf4efb1 2018-09-29 stsp update_panels();
1079 0cf4efb1 2018-09-29 stsp doupdate();
1080 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1081 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1082 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1083 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1085 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1088 9970f7fc 2020-12-03 stsp if (view->dying) {
1089 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1091 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1092 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1094 e78dc838 2020-12-04 stsp else if (view->parent)
1095 669b5ffa 2018-10-07 stsp prev = view->parent;
1097 e78dc838 2020-12-04 stsp if (view->parent) {
1098 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1099 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1101 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1105 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1107 9970f7fc 2020-12-03 stsp err = view_close(view);
1109 e5a0f69f 2018-08-18 stsp goto done;
1111 e78dc838 2020-12-04 stsp view = NULL;
1112 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1113 e78dc838 2020-12-04 stsp if (v->focussed)
1116 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1117 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1119 e78dc838 2020-12-04 stsp view = prev;
1120 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1121 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1122 e78dc838 2020-12-04 stsp tog_view_list_head);
1124 e78dc838 2020-12-04 stsp if (view) {
1125 e78dc838 2020-12-04 stsp if (view->focus_child) {
1126 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1127 e78dc838 2020-12-04 stsp view = view->child;
1129 e78dc838 2020-12-04 stsp view->focussed = 1;
1133 bcbd79e2 2018-08-19 stsp if (new_view) {
1134 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1135 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1136 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1137 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1139 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1140 86c66b02 2018-10-18 stsp err = view_close(v);
1142 86c66b02 2018-10-18 stsp goto done;
1145 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1146 fed7eaa8 2018-10-24 stsp view = new_view;
1148 669b5ffa 2018-10-07 stsp if (view) {
1149 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1150 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1151 e78dc838 2020-12-04 stsp view = view->child;
1153 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1154 e78dc838 2020-12-04 stsp view = view->parent;
1156 e78dc838 2020-12-04 stsp show_panel(view->panel);
1157 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1158 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1159 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1160 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1162 1a76625f 2018-10-22 stsp goto done;
1164 669b5ffa 2018-10-07 stsp err = view->show(view);
1166 1a76625f 2018-10-22 stsp goto done;
1167 669b5ffa 2018-10-07 stsp if (view->child) {
1168 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1170 1a76625f 2018-10-22 stsp goto done;
1172 1a76625f 2018-10-22 stsp update_panels();
1173 1a76625f 2018-10-22 stsp doupdate();
1177 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1178 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1179 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1180 e5a0f69f 2018-08-18 stsp view_close(view);
1183 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1184 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1185 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1187 e5a0f69f 2018-08-18 stsp return err;
1190 4ed7e80c 2018-05-20 stsp __dead static void
1191 9f7d7167 2018-04-29 stsp usage_log(void)
1194 c70c5802 2018-08-01 stsp fprintf(stderr,
1195 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1196 9f7d7167 2018-04-29 stsp getprogname());
1200 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1201 80ddbec8 2018-04-29 stsp static const struct got_error *
1202 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1204 00dfcb92 2018-06-11 stsp char *vis = NULL;
1205 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1207 963b370f 2018-05-20 stsp *ws = NULL;
1208 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1209 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1210 00dfcb92 2018-06-11 stsp int vislen;
1211 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1212 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1214 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1215 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1217 00dfcb92 2018-06-11 stsp return err;
1218 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1219 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1220 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1221 a7f50699 2018-06-11 stsp goto done;
1225 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1226 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1227 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1228 a7f50699 2018-06-11 stsp goto done;
1231 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1232 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1234 00dfcb92 2018-06-11 stsp free(vis);
1235 963b370f 2018-05-20 stsp if (err) {
1236 963b370f 2018-05-20 stsp free(*ws);
1237 963b370f 2018-05-20 stsp *ws = NULL;
1238 963b370f 2018-05-20 stsp *wlen = 0;
1240 963b370f 2018-05-20 stsp return err;
1243 145b6838 2022-06-16 stsp static const struct got_error *
1244 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1246 145b6838 2022-06-16 stsp char *dst;
1247 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1249 145b6838 2022-06-16 stsp *ptr = NULL;
1250 145b6838 2022-06-16 stsp n = len = strlen(src);
1251 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1252 145b6838 2022-06-16 stsp if (dst == NULL)
1253 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1255 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1256 145b6838 2022-06-16 stsp const char c = src[idx];
1258 145b6838 2022-06-16 stsp if (c == '\t') {
1259 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1262 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1263 6e1c41ad 2022-06-16 mark if (p == NULL) {
1264 6e1c41ad 2022-06-16 mark free(dst);
1265 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1270 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1273 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1277 145b6838 2022-06-16 stsp dst[sz] = '\0';
1278 145b6838 2022-06-16 stsp *ptr = dst;
1279 145b6838 2022-06-16 stsp return NULL;
1283 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1284 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1285 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1289 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1291 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1293 4e4a9ac8 2022-06-17 op if (n == 0) {
1294 4e4a9ac8 2022-06-17 op *rcol = cols;
1298 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1299 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1300 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1302 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1304 4e4a9ac8 2022-06-17 op if (width == -1) {
1306 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1309 4e4a9ac8 2022-06-17 op if (cols + width > n)
1311 4e4a9ac8 2022-06-17 op cols += width;
1314 4e4a9ac8 2022-06-17 op *rcol = cols;
1319 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1320 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1321 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1323 ccda2f4d 2022-06-16 stsp static const struct got_error *
1324 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1325 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1327 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1329 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1330 145b6838 2022-06-16 stsp char *exstr = NULL;
1331 963b370f 2018-05-20 stsp size_t wlen;
1332 4e4a9ac8 2022-06-17 op int i, scrollx;
1334 963b370f 2018-05-20 stsp *wlinep = NULL;
1335 b700b5d6 2018-07-10 stsp *widthp = 0;
1337 145b6838 2022-06-16 stsp if (expand) {
1338 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1340 145b6838 2022-06-16 stsp return err;
1343 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1344 145b6838 2022-06-16 stsp free(exstr);
1346 963b370f 2018-05-20 stsp return err;
1348 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1350 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1351 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1354 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1355 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1359 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1360 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1362 b700b5d6 2018-07-10 stsp if (widthp)
1363 b700b5d6 2018-07-10 stsp *widthp = cols;
1364 ccda2f4d 2022-06-16 stsp if (scrollxp)
1365 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1367 963b370f 2018-05-20 stsp free(wline);
1369 963b370f 2018-05-20 stsp *wlinep = wline;
1370 963b370f 2018-05-20 stsp return err;
1373 8b473291 2019-02-21 stsp static const struct got_error*
1374 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1375 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1377 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1378 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1380 8b473291 2019-02-21 stsp const char *name;
1382 8b473291 2019-02-21 stsp *refs_str = NULL;
1384 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1385 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1386 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1389 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1390 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1392 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1393 8b473291 2019-02-21 stsp name += 5;
1394 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1395 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1397 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1398 8b473291 2019-02-21 stsp name += 6;
1399 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1400 8b473291 2019-02-21 stsp name += 8;
1401 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1402 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1405 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1408 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1409 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1410 5d844a1e 2019-08-13 stsp if (err) {
1411 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1412 48cae60d 2020-09-22 stsp free(ref_id);
1415 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1416 5d844a1e 2019-08-13 stsp err = NULL;
1417 5d844a1e 2019-08-13 stsp tag = NULL;
1420 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1421 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1422 48cae60d 2020-09-22 stsp free(ref_id);
1424 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1425 52b5abe1 2019-08-13 stsp if (cmp != 0)
1427 8b473291 2019-02-21 stsp s = *refs_str;
1428 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1429 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1430 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1432 8b473291 2019-02-21 stsp *refs_str = NULL;
1438 8b473291 2019-02-21 stsp return err;
1441 963b370f 2018-05-20 stsp static const struct got_error *
1442 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1443 27a741e5 2019-09-11 stsp int col_tab_align)
1445 e6b8b890 2020-12-29 naddy char *smallerthan;
1447 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1448 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1449 5813d178 2019-03-09 stsp author = smallerthan + 1;
1450 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1451 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1452 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1455 5813d178 2019-03-09 stsp static const struct got_error *
1456 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1457 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1458 8fdc79fe 2020-12-01 naddy int author_display_cols)
1460 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1461 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1462 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1463 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1464 5813d178 2019-03-09 stsp char *author = NULL;
1465 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1466 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1467 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1468 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1469 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1470 ccb26ccd 2018-11-05 stsp struct tm tm;
1471 45d799e2 2018-12-23 stsp time_t committer_time;
1472 11b20872 2019-11-08 stsp struct tog_color *tc;
1474 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1475 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1476 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1477 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1478 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1480 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1481 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1483 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1484 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1486 11b20872 2019-11-08 stsp wattr_on(view->window,
1487 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1488 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1490 11b20872 2019-11-08 stsp wattr_off(view->window,
1491 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1492 27a741e5 2019-09-11 stsp col = limit;
1493 b39d25c7 2018-07-10 stsp if (col > avail)
1494 b39d25c7 2018-07-10 stsp goto done;
1496 6570a66d 2019-11-08 stsp if (avail >= 120) {
1497 6570a66d 2019-11-08 stsp char *id_str;
1498 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1500 6570a66d 2019-11-08 stsp goto done;
1501 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1503 11b20872 2019-11-08 stsp wattr_on(view->window,
1504 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1505 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1507 11b20872 2019-11-08 stsp wattr_off(view->window,
1508 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1509 6570a66d 2019-11-08 stsp free(id_str);
1511 6570a66d 2019-11-08 stsp if (col > avail)
1512 6570a66d 2019-11-08 stsp goto done;
1515 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1516 5813d178 2019-03-09 stsp if (author == NULL) {
1517 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1518 80ddbec8 2018-04-29 stsp goto done;
1520 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1522 bb737323 2018-05-20 stsp goto done;
1523 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1525 11b20872 2019-11-08 stsp wattr_on(view->window,
1526 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1527 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1529 11b20872 2019-11-08 stsp wattr_off(view->window,
1530 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1531 bb737323 2018-05-20 stsp col += author_width;
1532 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1533 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1535 bb737323 2018-05-20 stsp author_width++;
1537 9c2eaf34 2018-05-20 stsp if (col > avail)
1538 9c2eaf34 2018-05-20 stsp goto done;
1540 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1542 6d9fbc00 2018-04-29 stsp goto done;
1543 bb737323 2018-05-20 stsp logmsg = logmsg0;
1544 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1546 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1547 bb737323 2018-05-20 stsp if (newline)
1548 bb737323 2018-05-20 stsp *newline = '\0';
1549 ccda2f4d 2022-06-16 stsp limit = avail - col;
1550 63ba1a3a 2022-06-21 op if (view->child && view_is_splitscreen(view->child) && limit > 0)
1551 4d1f6af3 2022-06-17 op limit--; /* for the border */
1552 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1553 ccda2f4d 2022-06-16 stsp limit, col, 1);
1555 29688b02 2022-06-16 stsp goto done;
1556 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1557 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1558 27a741e5 2019-09-11 stsp while (col < avail) {
1559 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1563 80ddbec8 2018-04-29 stsp free(logmsg0);
1564 bb737323 2018-05-20 stsp free(wlogmsg);
1565 5813d178 2019-03-09 stsp free(author);
1566 bb737323 2018-05-20 stsp free(wauthor);
1567 80ddbec8 2018-04-29 stsp free(line);
1568 80ddbec8 2018-04-29 stsp return err;
1571 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1572 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1573 899d86c2 2018-05-10 stsp struct got_object_id *id)
1575 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1577 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1578 80ddbec8 2018-04-29 stsp if (entry == NULL)
1579 899d86c2 2018-05-10 stsp return NULL;
1581 899d86c2 2018-05-10 stsp entry->id = id;
1582 99db9666 2018-05-07 stsp entry->commit = commit;
1583 899d86c2 2018-05-10 stsp return entry;
1586 99db9666 2018-05-07 stsp static void
1587 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1589 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1591 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1592 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1593 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1594 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1595 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1596 99db9666 2018-05-07 stsp free(entry);
1599 99db9666 2018-05-07 stsp static void
1600 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1602 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1603 99db9666 2018-05-07 stsp pop_commit(commits);
1606 c4972b91 2018-05-07 stsp static const struct got_error *
1607 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1608 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1610 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1611 13add988 2019-10-15 stsp regmatch_t regmatch;
1612 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1614 13add988 2019-10-15 stsp *have_match = 0;
1616 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1618 13add988 2019-10-15 stsp return err;
1620 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1622 13add988 2019-10-15 stsp goto done;
1624 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1625 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1626 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1627 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1628 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1629 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1630 13add988 2019-10-15 stsp *have_match = 1;
1632 13add988 2019-10-15 stsp free(id_str);
1633 13add988 2019-10-15 stsp free(logmsg);
1634 13add988 2019-10-15 stsp return err;
1637 13add988 2019-10-15 stsp static const struct got_error *
1638 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1640 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1643 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1644 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1645 1a76625f 2018-10-22 stsp * while updating the display.
1648 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1649 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1650 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1651 1a76625f 2018-10-22 stsp int errcode;
1653 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1654 4e0d2870 2020-12-07 naddy NULL, NULL);
1655 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1658 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1661 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1662 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1663 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1667 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1668 1a76625f 2018-10-22 stsp if (errcode) {
1669 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1670 13add988 2019-10-15 stsp "pthread_mutex_lock");
1674 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1675 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1676 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1678 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1679 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1680 7c1452c1 2020-03-26 stsp int have_match;
1681 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1684 7c1452c1 2020-03-26 stsp if (have_match)
1685 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1688 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1689 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1690 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1691 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1694 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1696 9ba79e04 2018-06-11 stsp return err;
1699 2b779855 2020-12-05 naddy static void
1700 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1702 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1703 2b779855 2020-12-05 naddy int ncommits = 0;
1705 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1706 2b779855 2020-12-05 naddy while (entry) {
1707 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1708 2b779855 2020-12-05 naddy s->selected_entry = entry;
1711 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1712 2b779855 2020-12-05 naddy ncommits++;
1716 0553a4e3 2018-04-30 stsp static const struct got_error *
1717 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1719 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1720 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1721 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1722 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1723 60493ae3 2019-06-20 stsp int width;
1724 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1725 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1726 8b473291 2019-02-21 stsp char *refs_str = NULL;
1727 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1728 11b20872 2019-11-08 stsp struct tog_color *tc;
1729 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1731 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1732 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1733 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1734 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1736 ecb28ae0 2018-07-16 stsp return err;
1737 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1738 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1739 d2075bf3 2020-12-25 stsp if (refs) {
1740 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1741 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1743 d2075bf3 2020-12-25 stsp goto done;
1747 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1748 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1750 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1751 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1752 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1753 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1754 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1755 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1756 8f4ed634 2020-03-26 stsp goto done;
1759 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1761 f9686aa5 2020-03-27 stsp if (view->searching) {
1762 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1763 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1764 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1765 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1766 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1767 f9686aa5 2020-03-27 stsp search_str = "searching...";
1770 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1771 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1772 f9686aa5 2020-03-27 stsp search_str ? search_str :
1773 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1774 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1775 8f4ed634 2020-03-26 stsp goto done;
1779 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1780 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1781 87411fa9 2022-06-16 stsp "........................................",
1782 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1783 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1784 1a76625f 2018-10-22 stsp header = NULL;
1785 1a76625f 2018-10-22 stsp goto done;
1787 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1788 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1789 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1790 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1791 1a76625f 2018-10-22 stsp header = NULL;
1792 1a76625f 2018-10-22 stsp goto done;
1794 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1796 1a76625f 2018-10-22 stsp goto done;
1798 2814baeb 2018-08-01 stsp werase(view->window);
1800 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1801 a3404814 2018-09-02 stsp wstandout(view->window);
1802 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1804 11b20872 2019-11-08 stsp wattr_on(view->window,
1805 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1806 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1808 11b20872 2019-11-08 stsp wattr_off(view->window,
1809 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1810 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1811 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1814 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1815 a3404814 2018-09-02 stsp wstandend(view->window);
1816 ecb28ae0 2018-07-16 stsp free(wline);
1817 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1818 1a76625f 2018-10-22 stsp goto done;
1820 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
1821 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1822 5813d178 2019-03-09 stsp ncommits = 0;
1823 145b6838 2022-06-16 stsp view->maxx = 0;
1824 5813d178 2019-03-09 stsp while (entry) {
1825 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
1826 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
1827 5813d178 2019-03-09 stsp int width;
1828 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1830 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1831 5813d178 2019-03-09 stsp if (author == NULL) {
1832 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1833 5813d178 2019-03-09 stsp goto done;
1835 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1836 27a741e5 2019-09-11 stsp date_display_cols);
1837 5813d178 2019-03-09 stsp if (author_cols < width)
1838 5813d178 2019-03-09 stsp author_cols = width;
1839 5813d178 2019-03-09 stsp free(wauthor);
1840 5813d178 2019-03-09 stsp free(author);
1841 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
1843 145b6838 2022-06-16 stsp goto done;
1844 145b6838 2022-06-16 stsp msg = msg0;
1845 145b6838 2022-06-16 stsp while (*msg == '\n')
1847 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
1848 29688b02 2022-06-16 stsp *eol = '\0';
1849 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1850 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
1852 29688b02 2022-06-16 stsp goto done;
1853 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
1854 145b6838 2022-06-16 stsp free(msg0);
1855 29688b02 2022-06-16 stsp free(wmsg);
1856 7ca04879 2019-10-19 stsp ncommits++;
1857 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1860 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1861 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1862 867c6645 2018-07-10 stsp ncommits = 0;
1863 899d86c2 2018-05-10 stsp while (entry) {
1864 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1866 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1867 2814baeb 2018-08-01 stsp wstandout(view->window);
1868 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1869 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1870 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1871 2814baeb 2018-08-01 stsp wstandend(view->window);
1873 60493ae3 2019-06-20 stsp goto done;
1874 0553a4e3 2018-04-30 stsp ncommits++;
1875 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1876 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1879 1a57306a 2018-09-02 stsp view_vborder(view);
1881 1a76625f 2018-10-22 stsp free(id_str);
1882 8b473291 2019-02-21 stsp free(refs_str);
1883 1a76625f 2018-10-22 stsp free(ncommits_str);
1884 1a76625f 2018-10-22 stsp free(header);
1885 80ddbec8 2018-04-29 stsp return err;
1888 07b55e75 2018-05-10 stsp static void
1889 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1891 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1892 07b55e75 2018-05-10 stsp int nscrolled = 0;
1894 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1895 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1898 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1899 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1900 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1901 07b55e75 2018-05-10 stsp if (entry) {
1902 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1903 07b55e75 2018-05-10 stsp nscrolled++;
1908 aa075928 2018-05-10 stsp static const struct got_error *
1909 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1911 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1912 5e224a3e 2019-02-22 stsp int errcode;
1914 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1916 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1917 ffe38506 2020-12-01 naddy if (ta->log_complete)
1920 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1921 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1922 7aafa0d1 2019-02-22 stsp if (errcode)
1923 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1924 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1927 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1928 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1930 7c1452c1 2020-03-26 stsp if (!wait)
1933 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1934 ffe38506 2020-12-01 naddy show_log_view(view);
1935 7c1452c1 2020-03-26 stsp update_panels();
1936 7c1452c1 2020-03-26 stsp doupdate();
1938 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1939 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1940 82954512 2020-02-03 stsp if (errcode)
1941 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1942 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1944 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1945 ffe38506 2020-12-01 naddy show_log_view(view);
1946 7c1452c1 2020-03-26 stsp update_panels();
1947 7c1452c1 2020-03-26 stsp doupdate();
1950 5e224a3e 2019-02-22 stsp return NULL;
1953 5e224a3e 2019-02-22 stsp static const struct got_error *
1954 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1956 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1957 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1958 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1959 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1961 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1962 5e224a3e 2019-02-22 stsp return NULL;
1964 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1965 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1966 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1968 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1970 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1971 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1973 5e224a3e 2019-02-22 stsp return err;
1977 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1978 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1981 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1983 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1984 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1986 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1987 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1989 dd0a52c1 2018-05-20 stsp return err;
1992 cd0acaa7 2018-05-20 stsp static const struct got_error *
1993 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1994 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1995 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1997 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1998 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1999 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2001 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2002 15a94983 2018-12-23 stsp if (diff_view == NULL)
2003 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2005 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2006 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2007 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2008 e5a0f69f 2018-08-18 stsp if (err == NULL)
2009 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2010 cd0acaa7 2018-05-20 stsp return err;
2013 80ddbec8 2018-04-29 stsp static const struct got_error *
2014 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2015 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2017 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2019 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2020 941e9f74 2019-05-21 stsp if (parent == NULL)
2021 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2023 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2024 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2025 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2026 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2027 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2028 941e9f74 2019-05-21 stsp s->tree = subtree;
2029 941e9f74 2019-05-21 stsp s->selected = 0;
2030 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2031 941e9f74 2019-05-21 stsp return NULL;
2034 941e9f74 2019-05-21 stsp static const struct got_error *
2035 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2036 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2038 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2039 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2040 941e9f74 2019-05-21 stsp const char *p;
2041 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2043 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2045 941e9f74 2019-05-21 stsp while (*p) {
2046 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2047 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2048 56e0773d 2019-11-28 stsp char *te_name;
2050 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2053 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2054 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2055 941e9f74 2019-05-21 stsp if (slash == NULL)
2056 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2058 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2059 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2060 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2063 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2064 56e0773d 2019-11-28 stsp if (te == NULL) {
2065 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2066 56e0773d 2019-11-28 stsp free(te_name);
2069 56e0773d 2019-11-28 stsp free(te_name);
2070 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2072 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2073 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2075 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2076 941e9f74 2019-05-21 stsp if (slash)
2077 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2079 941e9f74 2019-05-21 stsp subpath = strdup(path);
2080 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2081 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2085 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2090 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2091 941e9f74 2019-05-21 stsp free(tree_id);
2095 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2096 941e9f74 2019-05-21 stsp if (err) {
2097 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2100 941e9f74 2019-05-21 stsp if (slash == NULL)
2102 941e9f74 2019-05-21 stsp free(subpath);
2103 941e9f74 2019-05-21 stsp subpath = NULL;
2104 941e9f74 2019-05-21 stsp p = slash;
2107 941e9f74 2019-05-21 stsp free(subpath);
2108 1a76625f 2018-10-22 stsp return err;
2111 61266923 2020-01-14 stsp static const struct got_error *
2112 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2113 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2114 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2116 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2117 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2118 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2120 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2121 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2122 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2124 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2126 55cccc34 2020-02-20 stsp return err;
2127 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2129 55cccc34 2020-02-20 stsp *new_view = tree_view;
2131 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2132 55cccc34 2020-02-20 stsp return NULL;
2134 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2137 55cccc34 2020-02-20 stsp static const struct got_error *
2138 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2140 61266923 2020-01-14 stsp sigset_t sigset;
2141 61266923 2020-01-14 stsp int errcode;
2143 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2144 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2146 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2147 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2148 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2149 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2150 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2151 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2152 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2153 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2154 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2156 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2157 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2158 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2160 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2161 61266923 2020-01-14 stsp if (errcode)
2162 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2164 61266923 2020-01-14 stsp return NULL;
2167 1a76625f 2018-10-22 stsp static void *
2168 1a76625f 2018-10-22 stsp log_thread(void *arg)
2170 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2171 1a76625f 2018-10-22 stsp int errcode = 0;
2172 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2173 1a76625f 2018-10-22 stsp int done = 0;
2175 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2177 61266923 2020-01-14 stsp return (void *)err;
2179 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2180 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2181 1a76625f 2018-10-22 stsp if (err) {
2182 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2183 1a76625f 2018-10-22 stsp return (void *)err;
2184 1a76625f 2018-10-22 stsp err = NULL;
2186 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2187 1a76625f 2018-10-22 stsp a->commits_needed--;
2189 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2190 3abe8080 2019-04-10 stsp if (errcode) {
2191 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2192 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2194 3abe8080 2019-04-10 stsp } else if (*a->quit)
2196 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2197 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2198 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2199 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2202 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2203 7c1452c1 2020-03-26 stsp if (errcode) {
2204 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2205 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2206 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2211 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2213 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2214 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2215 7c1452c1 2020-03-26 stsp &tog_mutex);
2216 7c1452c1 2020-03-26 stsp if (errcode)
2217 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2218 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2219 21355643 2020-12-06 stsp if (*a->quit)
2224 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2225 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2226 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2227 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2229 3abe8080 2019-04-10 stsp a->log_complete = 1;
2230 1a76625f 2018-10-22 stsp return (void *)err;
2233 1a76625f 2018-10-22 stsp static const struct got_error *
2234 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2236 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2237 1a76625f 2018-10-22 stsp int errcode;
2239 1a76625f 2018-10-22 stsp if (s->thread) {
2240 1a76625f 2018-10-22 stsp s->quit = 1;
2241 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2242 1a76625f 2018-10-22 stsp if (errcode)
2243 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2244 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2245 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2246 1a76625f 2018-10-22 stsp if (errcode)
2247 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2248 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2249 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2250 1a76625f 2018-10-22 stsp if (errcode)
2251 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2252 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2253 1a76625f 2018-10-22 stsp if (errcode)
2254 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2255 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2256 1a76625f 2018-10-22 stsp s->thread = NULL;
2259 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2260 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2261 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2264 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2265 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2266 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2267 74467cc8 2022-06-15 stsp if (err == NULL)
2268 74467cc8 2022-06-15 stsp err = pack_err;
2269 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2272 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2273 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2274 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2277 9343a5fb 2018-06-23 stsp return err;
2280 9343a5fb 2018-06-23 stsp static const struct got_error *
2281 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2283 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2284 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2285 276b94a1 2020-11-13 naddy int errcode;
2287 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2289 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2290 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2291 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2293 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2294 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2295 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2297 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2298 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2299 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2300 1a76625f 2018-10-22 stsp free(s->start_id);
2301 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2302 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2303 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2304 1a76625f 2018-10-22 stsp return err;
2307 1a76625f 2018-10-22 stsp static const struct got_error *
2308 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2310 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2312 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2313 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2314 60493ae3 2019-06-20 stsp return NULL;
2317 60493ae3 2019-06-20 stsp static const struct got_error *
2318 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2320 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2321 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2322 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2324 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2325 f9686aa5 2020-03-27 stsp show_log_view(view);
2326 f9686aa5 2020-03-27 stsp update_panels();
2327 f9686aa5 2020-03-27 stsp doupdate();
2329 96e2b566 2019-07-08 stsp if (s->search_entry) {
2330 13add988 2019-10-15 stsp int errcode, ch;
2331 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&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_unlock");
2335 13add988 2019-10-15 stsp ch = wgetch(view->window);
2336 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2337 13add988 2019-10-15 stsp if (errcode)
2338 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2339 13add988 2019-10-15 stsp "pthread_mutex_lock");
2340 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2341 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2342 678cbce5 2019-07-28 stsp return NULL;
2344 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2345 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2347 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2348 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2349 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2350 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2351 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2354 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2355 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2356 f704b35c 2022-06-18 stsp * might have changed.
2358 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2359 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2360 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2362 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2364 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2365 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2366 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2368 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2369 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2372 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2375 60493ae3 2019-06-20 stsp while (1) {
2376 13add988 2019-10-15 stsp int have_match = 0;
2378 60493ae3 2019-06-20 stsp if (entry == NULL) {
2379 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2380 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2381 f9967bca 2020-03-27 stsp view->search_next_done =
2382 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2383 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2384 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2385 f801134a 2019-06-25 stsp return NULL;
2388 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2389 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2390 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2392 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2393 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2396 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2397 13add988 2019-10-15 stsp &view->regex);
2400 13add988 2019-10-15 stsp if (have_match) {
2401 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2402 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2406 96e2b566 2019-07-08 stsp s->search_entry = entry;
2407 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2408 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2410 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2413 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2414 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2415 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2416 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2418 60493ae3 2019-06-20 stsp return err;
2421 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2422 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2424 60493ae3 2019-06-20 stsp return err;
2429 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2431 60493ae3 2019-06-20 stsp return NULL;
2434 60493ae3 2019-06-20 stsp static const struct got_error *
2435 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2436 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2437 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2439 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2440 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2441 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2442 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2443 1a76625f 2018-10-22 stsp int errcode;
2445 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2446 f135c941 2020-02-20 stsp free(s->in_repo_path);
2447 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2448 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2449 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2452 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2453 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2454 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2456 fb2756b9 2018-08-04 stsp s->repo = repo;
2457 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2458 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2459 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2460 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2461 9cd7cbd1 2020-12-07 stsp goto done;
2464 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2465 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2466 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2467 5036bf37 2018-09-24 stsp goto done;
2469 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2471 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2472 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2473 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2474 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2476 11b20872 2019-11-08 stsp goto done;
2477 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2478 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2479 11b20872 2019-11-08 stsp if (err) {
2480 11b20872 2019-11-08 stsp free_colors(&s->colors);
2481 11b20872 2019-11-08 stsp goto done;
2483 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2484 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2485 11b20872 2019-11-08 stsp if (err) {
2486 11b20872 2019-11-08 stsp free_colors(&s->colors);
2487 11b20872 2019-11-08 stsp goto done;
2491 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2492 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2493 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2494 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2495 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2497 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2498 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2500 74467cc8 2022-06-15 stsp goto done;
2502 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2503 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2504 0ae84acc 2022-06-15 tracey if (err)
2505 0ae84acc 2022-06-15 tracey goto done;
2506 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2507 b672a97a 2020-01-27 stsp !s->log_branches);
2509 1a76625f 2018-10-22 stsp goto done;
2510 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2511 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2513 c5b78334 2020-01-12 stsp goto done;
2515 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2516 1a76625f 2018-10-22 stsp if (errcode) {
2517 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2518 1a76625f 2018-10-22 stsp goto done;
2520 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2521 7c1452c1 2020-03-26 stsp if (errcode) {
2522 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2523 7c1452c1 2020-03-26 stsp goto done;
2526 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2527 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2528 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2529 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2530 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2531 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2532 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2533 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2534 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2535 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2536 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2537 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2538 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2541 1a76625f 2018-10-22 stsp close_log_view(view);
2542 ba4f502b 2018-08-04 stsp return err;
2545 e5a0f69f 2018-08-18 stsp static const struct got_error *
2546 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2548 f2f6d207 2020-11-24 stsp const struct got_error *err;
2549 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2551 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2552 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2553 2b380cc8 2018-10-24 stsp &s->thread_args);
2554 2b380cc8 2018-10-24 stsp if (errcode)
2555 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2556 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2557 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2559 f2f6d207 2020-11-24 stsp return err;
2563 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2566 e5a0f69f 2018-08-18 stsp static const struct got_error *
2567 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2569 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2570 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2571 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2572 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2573 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2574 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
2576 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2577 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2578 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2579 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2580 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2581 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2582 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2583 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2584 fb280deb 2021-08-30 stsp select_commit(s);
2586 528dedf3 2021-08-30 stsp return NULL;
2589 528dedf3 2021-08-30 stsp switch (ch) {
2591 1e37a5c2 2019-05-12 jcs s->quit = 1;
2594 145b6838 2022-06-16 stsp view->x = 0;
2597 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2599 145b6838 2022-06-16 stsp case KEY_RIGHT:
2601 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2602 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2604 145b6838 2022-06-16 stsp case KEY_LEFT:
2606 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2609 1e37a5c2 2019-05-12 jcs case KEY_UP:
2612 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2613 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2615 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2616 1e37a5c2 2019-05-12 jcs s->selected--;
2618 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2619 912a3f79 2021-08-30 j select_commit(s);
2622 912a3f79 2021-08-30 j case KEY_HOME:
2623 912a3f79 2021-08-30 j s->selected = 0;
2624 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2625 2b779855 2020-12-05 naddy select_commit(s);
2627 83cc4199 2022-06-13 stsp case CTRL('u'):
2629 83cc4199 2022-06-13 stsp nscroll /= 2;
2630 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2631 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2632 a4292ac5 2019-05-12 jcs case CTRL('b'):
2634 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2636 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2637 83cc4199 2022-06-13 stsp s->selected = MAX(0, s->selected - nscroll - 1);
2639 83cc4199 2022-06-13 stsp log_scroll_up(s, nscroll);
2640 2b779855 2020-12-05 naddy select_commit(s);
2643 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2646 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2647 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2649 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2650 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2651 1e37a5c2 2019-05-12 jcs s->selected++;
2653 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2657 912a3f79 2021-08-30 j select_commit(s);
2660 912a3f79 2021-08-30 j case KEY_END: {
2661 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2662 912a3f79 2021-08-30 j * traverse them all. */
2663 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2664 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2665 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2668 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2669 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2670 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2671 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2673 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2674 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2676 f3bc9f1d 2021-09-05 naddy if (n > 0)
2677 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2678 2b779855 2020-12-05 naddy select_commit(s);
2681 80b7e8da 2022-06-11 stsp case CTRL('d'):
2683 83cc4199 2022-06-13 stsp nscroll /= 2;
2684 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2685 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2686 61417565 2022-06-20 mark case CTRL('f'):
2687 48bb96f0 2022-06-20 naddy case 'f':
2688 48bb96f0 2022-06-20 naddy case ' ': {
2689 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2690 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2691 1e37a5c2 2019-05-12 jcs if (first == NULL)
2693 83cc4199 2022-06-13 stsp err = log_scroll_down(view, nscroll);
2696 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2697 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2698 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2699 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2700 83cc4199 2022-06-13 stsp s->selected += MIN(s->last_displayed_entry->idx -
2701 83cc4199 2022-06-13 stsp s->selected_entry->idx, nscroll + 1);
2703 2b779855 2020-12-05 naddy select_commit(s);
2706 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2707 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2708 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2709 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2710 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2711 2b779855 2020-12-05 naddy select_commit(s);
2712 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2713 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2714 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2715 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2716 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2719 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2721 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2723 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2724 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2725 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2726 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2727 78756c87 2020-11-24 stsp view, s->repo);
2730 e78dc838 2020-12-04 stsp view->focussed = 0;
2731 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2732 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2733 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2735 1e37a5c2 2019-05-12 jcs return err;
2736 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
2739 e78dc838 2020-12-04 stsp view->focus_child = 1;
2741 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2744 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2746 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2747 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2748 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2749 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2753 e78dc838 2020-12-04 stsp view->focussed = 0;
2754 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2755 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2756 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2758 1e37a5c2 2019-05-12 jcs return err;
2759 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
2762 e78dc838 2020-12-04 stsp view->focus_child = 1;
2764 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2766 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2767 21355643 2020-12-06 stsp case CTRL('l'):
2769 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2770 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2772 21355643 2020-12-06 stsp err = stop_log_thread(s);
2774 74cfe85e 2020-10-20 stsp return err;
2775 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2776 21355643 2020-12-06 stsp char *parent_path;
2777 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2779 21355643 2020-12-06 stsp return err;
2780 21355643 2020-12-06 stsp free(s->in_repo_path);
2781 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2782 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2783 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2784 21355643 2020-12-06 stsp struct got_object_id *start_id;
2785 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2786 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2787 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2789 21355643 2020-12-06 stsp return err;
2790 21355643 2020-12-06 stsp free(s->start_id);
2791 21355643 2020-12-06 stsp s->start_id = start_id;
2792 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2793 21355643 2020-12-06 stsp } else /* 'B' */
2794 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2796 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
2797 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2799 b0dd8d27 2022-06-16 stsp return err;
2801 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
2802 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
2803 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2805 21355643 2020-12-06 stsp return err;
2806 51a10b52 2020-12-26 stsp tog_free_refs();
2807 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
2809 ca51c541 2020-12-07 stsp return err;
2810 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2811 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2813 d01904d4 2019-06-25 stsp return err;
2814 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2815 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2817 b672a97a 2020-01-27 stsp return err;
2818 21355643 2020-12-06 stsp free_commits(&s->commits);
2819 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2820 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2821 21355643 2020-12-06 stsp s->selected_entry = NULL;
2822 21355643 2020-12-06 stsp s->selected = 0;
2823 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2824 21355643 2020-12-06 stsp s->quit = 0;
2825 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2826 dfee752e 2022-06-18 op s->matched_entry = NULL;
2827 dfee752e 2022-06-18 op s->search_entry = NULL;
2830 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2831 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2832 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2833 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2834 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2835 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2836 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2837 6458efa5 2020-11-24 stsp if (err) {
2838 6458efa5 2020-11-24 stsp view_close(ref_view);
2839 6458efa5 2020-11-24 stsp return err;
2841 e78dc838 2020-12-04 stsp view->focussed = 0;
2842 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2843 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2844 6458efa5 2020-11-24 stsp err = view_close_child(view);
2846 6458efa5 2020-11-24 stsp return err;
2847 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
2850 e78dc838 2020-12-04 stsp view->focus_child = 1;
2852 6458efa5 2020-11-24 stsp *new_view = ref_view;
2858 80ddbec8 2018-04-29 stsp return err;
2861 4ed7e80c 2018-05-20 stsp static const struct got_error *
2862 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2864 c2db6724 2019-01-04 stsp const struct got_error *error;
2866 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2867 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2868 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2870 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2871 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2873 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2874 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2876 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2877 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2879 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2880 c2db6724 2019-01-04 stsp if (error != NULL)
2881 c2db6724 2019-01-04 stsp return error;
2883 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2884 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2886 c2db6724 2019-01-04 stsp return NULL;
2889 a915003a 2019-02-05 stsp static void
2890 a915003a 2019-02-05 stsp init_curses(void)
2893 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
2894 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
2895 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
2897 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
2898 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
2899 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
2900 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
2901 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
2903 a915003a 2019-02-05 stsp initscr();
2905 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2908 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2909 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2910 a915003a 2019-02-05 stsp curs_set(0);
2911 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2912 6d17833f 2019-11-08 stsp start_color();
2913 6d17833f 2019-11-08 stsp use_default_colors();
2917 c2db6724 2019-01-04 stsp static const struct got_error *
2918 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2919 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2921 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2923 f135c941 2020-02-20 stsp if (argc == 0) {
2924 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2925 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2926 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2927 f135c941 2020-02-20 stsp return NULL;
2930 f135c941 2020-02-20 stsp if (worktree) {
2931 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2934 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2936 f135c941 2020-02-20 stsp return err;
2937 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2938 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2939 bfd61697 2020-11-14 stsp p) == -1) {
2940 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2941 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2945 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2947 f135c941 2020-02-20 stsp return err;
2950 f135c941 2020-02-20 stsp static const struct got_error *
2951 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2953 80ddbec8 2018-04-29 stsp const struct got_error *error;
2954 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2955 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2956 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2957 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2958 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2959 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2960 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2961 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2962 04cc582a 2018-08-01 stsp struct tog_view *view;
2963 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
2965 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2966 80ddbec8 2018-04-29 stsp switch (ch) {
2968 b672a97a 2020-01-27 stsp log_branches = 1;
2971 80ddbec8 2018-04-29 stsp start_commit = optarg;
2974 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2975 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2976 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2980 17020d27 2019-03-07 stsp usage_log();
2981 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2985 80ddbec8 2018-04-29 stsp argc -= optind;
2986 80ddbec8 2018-04-29 stsp argv += optind;
2988 f135c941 2020-02-20 stsp if (argc > 1)
2989 f135c941 2020-02-20 stsp usage_log();
2991 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
2992 0ae84acc 2022-06-15 tracey if (error != NULL)
2993 0ae84acc 2022-06-15 tracey goto done;
2995 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2996 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2997 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2998 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2999 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3000 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3001 c156c7a4 2020-12-18 stsp goto done;
3002 a1fbf39a 2019-08-11 stsp if (worktree)
3003 6962eb72 2020-02-20 stsp repo_path =
3004 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3006 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3007 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3008 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3009 c156c7a4 2020-12-18 stsp goto done;
3013 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3014 80ddbec8 2018-04-29 stsp if (error != NULL)
3015 ecb28ae0 2018-07-16 stsp goto done;
3017 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3018 f135c941 2020-02-20 stsp repo, worktree);
3019 f135c941 2020-02-20 stsp if (error)
3020 f135c941 2020-02-20 stsp goto done;
3022 f135c941 2020-02-20 stsp init_curses();
3024 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3025 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3026 c02c541e 2019-03-29 stsp if (error)
3027 c02c541e 2019-03-29 stsp goto done;
3029 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3030 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3031 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3032 87670572 2020-12-26 naddy if (error)
3033 87670572 2020-12-26 naddy goto done;
3036 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3037 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3038 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3039 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3040 d8f38dc4 2020-12-05 stsp if (error)
3041 d8f38dc4 2020-12-05 stsp goto done;
3042 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3044 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3045 d8f38dc4 2020-12-05 stsp if (error == NULL)
3046 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3047 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3048 d8f38dc4 2020-12-05 stsp goto done;
3049 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3050 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3051 d8f38dc4 2020-12-05 stsp if (error)
3052 d8f38dc4 2020-12-05 stsp goto done;
3055 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3056 04cc582a 2018-08-01 stsp if (view == NULL) {
3057 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3058 04cc582a 2018-08-01 stsp goto done;
3060 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3061 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3062 ba4f502b 2018-08-04 stsp if (error)
3063 ba4f502b 2018-08-04 stsp goto done;
3064 2fc00ff4 2019-08-31 stsp if (worktree) {
3065 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3066 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3067 2fc00ff4 2019-08-31 stsp worktree = NULL;
3069 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3071 f135c941 2020-02-20 stsp free(in_repo_path);
3072 ecb28ae0 2018-07-16 stsp free(repo_path);
3073 ecb28ae0 2018-07-16 stsp free(cwd);
3074 899d86c2 2018-05-10 stsp free(start_id);
3075 d8f38dc4 2020-12-05 stsp free(label);
3077 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3078 1d0f4054 2021-06-17 stsp if (repo) {
3079 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3080 1d0f4054 2021-06-17 stsp if (error == NULL)
3081 1d0f4054 2021-06-17 stsp error = close_err;
3083 ec142235 2019-03-07 stsp if (worktree)
3084 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3085 0ae84acc 2022-06-15 tracey if (pack_fds) {
3086 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3087 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3088 0ae84acc 2022-06-15 tracey if (error == NULL)
3089 0ae84acc 2022-06-15 tracey error = pack_err;
3091 51a10b52 2020-12-26 stsp tog_free_refs();
3092 80ddbec8 2018-04-29 stsp return error;
3095 4ed7e80c 2018-05-20 stsp __dead static void
3096 9f7d7167 2018-04-29 stsp usage_diff(void)
3099 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3100 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3104 6d17833f 2019-11-08 stsp static int
3105 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3106 41605754 2020-11-12 stsp regmatch_t *regmatch)
3108 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3111 f26dddb7 2019-11-08 stsp struct tog_color *
3112 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3114 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3116 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3117 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3118 6d17833f 2019-11-08 stsp return tc;
3121 6d17833f 2019-11-08 stsp return NULL;
3124 4ed7e80c 2018-05-20 stsp static const struct got_error *
3125 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3126 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3128 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3129 44a87665 2022-06-16 stsp char *exstr = NULL;
3130 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3131 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3132 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3133 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3135 41605754 2020-11-12 stsp *wtotal = 0;
3137 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3138 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3140 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3142 44a87665 2022-06-16 stsp return err;
3144 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3145 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3146 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3147 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3148 44a87665 2022-06-16 stsp goto done;
3150 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3151 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3152 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3153 1853e0f4 2022-06-16 stsp goto done;
3155 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3156 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3157 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3158 1853e0f4 2022-06-16 stsp goto done;
3161 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3162 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3163 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3165 1853e0f4 2022-06-16 stsp goto done;
3166 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3168 1853e0f4 2022-06-16 stsp free(wline);
3169 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3170 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3172 1853e0f4 2022-06-16 stsp goto done;
3173 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3174 1853e0f4 2022-06-16 stsp wlimit -= width;
3175 1853e0f4 2022-06-16 stsp *wtotal += width;
3178 41605754 2020-11-12 stsp if (wlimit > 0) {
3179 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3180 1853e0f4 2022-06-16 stsp size_t wlen;
3182 1853e0f4 2022-06-16 stsp free(wline);
3183 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3184 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3186 1853e0f4 2022-06-16 stsp goto done;
3187 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3188 1853e0f4 2022-06-16 stsp while (i < wlen) {
3189 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3190 1853e0f4 2022-06-16 stsp if (width == -1) {
3191 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3192 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3193 1853e0f4 2022-06-16 stsp goto done;
3195 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3197 61417565 2022-06-20 mark w += width;
3200 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3201 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3202 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3203 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3204 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3205 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3206 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3210 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3211 1853e0f4 2022-06-16 stsp free(wline);
3212 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3213 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3214 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3215 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3217 1853e0f4 2022-06-16 stsp goto done;
3218 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3220 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3221 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3223 1853e0f4 2022-06-16 stsp goto done;
3224 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3226 1853e0f4 2022-06-16 stsp *wtotal += width2;
3229 145b6838 2022-06-16 stsp free(wline);
3230 44a87665 2022-06-16 stsp free(exstr);
3231 1853e0f4 2022-06-16 stsp free(seg0);
3232 1853e0f4 2022-06-16 stsp free(seg1);
3233 1853e0f4 2022-06-16 stsp free(seg2);
3234 1853e0f4 2022-06-16 stsp return err;
3237 41605754 2020-11-12 stsp static const struct got_error *
3238 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3240 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3241 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3242 61e69b96 2018-05-20 stsp const struct got_error *err;
3243 fe621944 2020-11-10 stsp int nprinted = 0;
3244 b304db33 2018-05-20 stsp char *line;
3245 826082fe 2020-12-10 stsp size_t linesize = 0;
3246 826082fe 2020-12-10 stsp ssize_t linelen;
3247 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3248 61e69b96 2018-05-20 stsp wchar_t *wline;
3249 e0b650dd 2018-05-20 stsp int width;
3250 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3251 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3252 fe621944 2020-11-10 stsp off_t line_offset;
3254 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3255 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3256 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3258 f7d12f7e 2018-08-01 stsp werase(view->window);
3260 a3404814 2018-09-02 stsp if (header) {
3261 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3262 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3263 135a2da0 2020-11-11 stsp header) == -1)
3264 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3265 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3267 135a2da0 2020-11-11 stsp free(line);
3269 a3404814 2018-09-02 stsp return err;
3271 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3272 a3404814 2018-09-02 stsp wstandout(view->window);
3273 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3274 e54cc94a 2020-11-11 stsp free(wline);
3275 e54cc94a 2020-11-11 stsp wline = NULL;
3276 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3277 a3404814 2018-09-02 stsp wstandend(view->window);
3278 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3279 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3281 a3404814 2018-09-02 stsp if (max_lines <= 1)
3282 a3404814 2018-09-02 stsp return NULL;
3283 a3404814 2018-09-02 stsp max_lines--;
3286 89f1a395 2020-12-01 naddy s->eof = 0;
3287 145b6838 2022-06-16 stsp view->maxx = 0;
3288 826082fe 2020-12-10 stsp line = NULL;
3289 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3290 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3291 826082fe 2020-12-10 stsp if (linelen == -1) {
3292 826082fe 2020-12-10 stsp if (feof(s->f)) {
3293 826082fe 2020-12-10 stsp s->eof = 1;
3296 826082fe 2020-12-10 stsp free(line);
3297 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3300 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3301 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3302 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3303 1853e0f4 2022-06-16 stsp if (err) {
3304 1853e0f4 2022-06-16 stsp free(line);
3305 1853e0f4 2022-06-16 stsp return err;
3307 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3308 1853e0f4 2022-06-16 stsp free(wline);
3309 1853e0f4 2022-06-16 stsp wline = NULL;
3311 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3313 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3314 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3315 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3316 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3317 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3318 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3319 41605754 2020-11-12 stsp if (err) {
3320 41605754 2020-11-12 stsp free(line);
3321 41605754 2020-11-12 stsp return err;
3325 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3326 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3327 969c159c 2022-06-16 stsp if (err) {
3328 969c159c 2022-06-16 stsp free(line);
3329 969c159c 2022-06-16 stsp return err;
3331 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3332 969c159c 2022-06-16 stsp free(wline);
3333 41605754 2020-11-12 stsp wline = NULL;
3336 6d17833f 2019-11-08 stsp wattr_off(view->window,
3337 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3338 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3339 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3340 fe621944 2020-11-10 stsp nprinted++;
3342 826082fe 2020-12-10 stsp free(line);
3343 fe621944 2020-11-10 stsp if (nprinted >= 1)
3344 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3345 89f1a395 2020-12-01 naddy (nprinted - 1);
3347 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3349 1a57306a 2018-09-02 stsp view_vborder(view);
3351 89f1a395 2020-12-01 naddy if (s->eof) {
3352 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3353 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3354 c3e9aa98 2019-05-13 jcs nprinted++;
3357 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3358 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3360 c3e9aa98 2019-05-13 jcs return err;
3363 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3364 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3365 e54cc94a 2020-11-11 stsp free(wline);
3366 e54cc94a 2020-11-11 stsp wline = NULL;
3367 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3370 26ed57b2 2018-05-19 stsp return NULL;
3373 abd2672a 2018-12-23 stsp static char *
3374 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3376 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3377 09867e48 2019-08-13 stsp char *p, *s;
3379 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3380 09867e48 2019-08-13 stsp if (tm == NULL)
3381 09867e48 2019-08-13 stsp return NULL;
3382 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3383 09867e48 2019-08-13 stsp if (s == NULL)
3384 09867e48 2019-08-13 stsp return NULL;
3385 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3387 abd2672a 2018-12-23 stsp *p = '\0';
3391 4ed7e80c 2018-05-20 stsp static const struct got_error *
3392 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3393 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3395 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3396 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3397 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3398 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3400 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3401 0208f208 2020-05-05 stsp if (qid != NULL) {
3402 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3403 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3404 d7b5a0e8 2022-04-20 stsp &qid->id);
3406 0208f208 2020-05-05 stsp return err;
3408 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3409 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3410 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3411 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3412 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3414 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3418 0208f208 2020-05-05 stsp if (tree_id1) {
3419 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3421 0208f208 2020-05-05 stsp goto done;
3424 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3425 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3427 0208f208 2020-05-05 stsp goto done;
3429 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3430 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3432 0208f208 2020-05-05 stsp if (tree1)
3433 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3434 0208f208 2020-05-05 stsp if (tree2)
3435 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3436 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3437 0208f208 2020-05-05 stsp return err;
3440 0208f208 2020-05-05 stsp static const struct got_error *
3441 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3445 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3446 fe621944 2020-11-10 stsp if (p == NULL)
3447 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3448 fe621944 2020-11-10 stsp *line_offsets = p;
3449 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3450 fe621944 2020-11-10 stsp (*nlines)++;
3451 fe621944 2020-11-10 stsp return NULL;
3454 fe621944 2020-11-10 stsp static const struct got_error *
3455 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3456 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3457 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3459 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3460 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3461 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3462 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3463 45d799e2 2018-12-23 stsp time_t committer_time;
3464 45d799e2 2018-12-23 stsp const char *author, *committer;
3465 8b473291 2019-02-21 stsp char *refs_str = NULL;
3466 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3467 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3468 fe621944 2020-11-10 stsp off_t outoff = 0;
3471 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3473 8b473291 2019-02-21 stsp if (refs) {
3474 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3476 8b473291 2019-02-21 stsp return err;
3479 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3481 abd2672a 2018-12-23 stsp return err;
3483 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3484 15a94983 2018-12-23 stsp if (err) {
3485 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3486 15a94983 2018-12-23 stsp goto done;
3489 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3491 fe621944 2020-11-10 stsp goto done;
3493 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3494 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3495 fe621944 2020-11-10 stsp if (n < 0) {
3496 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3497 abd2672a 2018-12-23 stsp goto done;
3499 fe621944 2020-11-10 stsp outoff += n;
3500 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3502 fe621944 2020-11-10 stsp goto done;
3504 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3505 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3506 fe621944 2020-11-10 stsp if (n < 0) {
3507 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3508 abd2672a 2018-12-23 stsp goto done;
3510 fe621944 2020-11-10 stsp outoff += n;
3511 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3513 fe621944 2020-11-10 stsp goto done;
3515 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3516 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3517 fe621944 2020-11-10 stsp if (datestr) {
3518 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3519 fe621944 2020-11-10 stsp if (n < 0) {
3520 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3521 fe621944 2020-11-10 stsp goto done;
3523 fe621944 2020-11-10 stsp outoff += n;
3524 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3526 fe621944 2020-11-10 stsp goto done;
3528 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3529 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3530 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3531 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3532 fe621944 2020-11-10 stsp if (n < 0) {
3533 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3534 fe621944 2020-11-10 stsp goto done;
3536 fe621944 2020-11-10 stsp outoff += n;
3537 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3539 fe621944 2020-11-10 stsp goto done;
3541 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3542 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3543 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3544 9f98ca05 2021-09-24 stsp int pn = 1;
3545 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3546 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3547 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3549 9f98ca05 2021-09-24 stsp goto done;
3550 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3551 9f98ca05 2021-09-24 stsp if (n < 0) {
3552 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3553 9f98ca05 2021-09-24 stsp goto done;
3555 9f98ca05 2021-09-24 stsp outoff += n;
3556 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3558 9f98ca05 2021-09-24 stsp goto done;
3559 9f98ca05 2021-09-24 stsp free(id_str);
3560 9f98ca05 2021-09-24 stsp id_str = NULL;
3564 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3566 5943eee2 2019-08-13 stsp goto done;
3567 fe621944 2020-11-10 stsp s = logmsg;
3568 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3569 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3570 fe621944 2020-11-10 stsp if (n < 0) {
3571 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3572 fe621944 2020-11-10 stsp goto done;
3574 fe621944 2020-11-10 stsp outoff += n;
3575 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3577 fe621944 2020-11-10 stsp goto done;
3580 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3582 0208f208 2020-05-05 stsp goto done;
3583 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3584 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3585 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3586 fe621944 2020-11-10 stsp if (n < 0) {
3587 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3588 fe621944 2020-11-10 stsp goto done;
3590 fe621944 2020-11-10 stsp outoff += n;
3591 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3593 fe621944 2020-11-10 stsp goto done;
3594 0208f208 2020-05-05 stsp free((char *)pe->path);
3595 0208f208 2020-05-05 stsp free(pe->data);
3598 0208f208 2020-05-05 stsp fputc('\n', outfile);
3600 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3602 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3603 abd2672a 2018-12-23 stsp free(id_str);
3604 5943eee2 2019-08-13 stsp free(logmsg);
3605 8b473291 2019-02-21 stsp free(refs_str);
3606 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3607 7510f233 2020-08-09 stsp if (err) {
3608 ae6a6978 2020-08-09 stsp free(*line_offsets);
3609 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3610 ae6a6978 2020-08-09 stsp *nlines = 0;
3612 fe621944 2020-11-10 stsp return err;
3615 abd2672a 2018-12-23 stsp static const struct got_error *
3616 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3618 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3619 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3620 15a94983 2018-12-23 stsp int obj_type;
3622 fe621944 2020-11-10 stsp free(s->line_offsets);
3623 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3624 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3625 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3626 fe621944 2020-11-10 stsp s->nlines = 0;
3628 511a516b 2018-05-19 stsp f = got_opentemp();
3629 48ae06ee 2018-10-18 stsp if (f == NULL) {
3630 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3631 48ae06ee 2018-10-18 stsp goto done;
3633 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3634 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3635 fb43ecf1 2019-02-11 stsp goto done;
3639 15a94983 2018-12-23 stsp if (s->id1)
3640 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3642 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3644 15a94983 2018-12-23 stsp goto done;
3646 15a94983 2018-12-23 stsp switch (obj_type) {
3647 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3648 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3649 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3650 b72706c3 2022-06-01 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3651 b72706c3 2022-06-01 stsp s->repo, s->f);
3653 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3654 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3655 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3656 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3658 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3659 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3660 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3661 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3662 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3664 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3666 3ffacbe1 2020-02-02 tracey goto done;
3667 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3668 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3669 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3670 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3671 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3672 f44b1f58 2020-02-02 tracey if (err)
3673 f44b1f58 2020-02-02 tracey goto done;
3674 f44b1f58 2020-02-02 tracey } else {
3675 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3676 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3677 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3678 fe621944 2020-11-10 stsp err = write_commit_info(
3679 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3680 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3681 f44b1f58 2020-02-02 tracey if (err)
3682 f44b1f58 2020-02-02 tracey goto done;
3687 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3689 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3690 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3691 b72706c3 2022-06-01 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3695 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3698 f44b1f58 2020-02-02 tracey if (err)
3699 f44b1f58 2020-02-02 tracey goto done;
3701 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3702 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3703 48ae06ee 2018-10-18 stsp return err;
3706 f5215bb9 2019-02-22 stsp static void
3707 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3709 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3710 f5215bb9 2019-02-22 stsp update_panels();
3711 f5215bb9 2019-02-22 stsp doupdate();
3714 f44b1f58 2020-02-02 tracey static const struct got_error *
3715 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3717 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3719 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3720 f44b1f58 2020-02-02 tracey return NULL;
3723 f44b1f58 2020-02-02 tracey static const struct got_error *
3724 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3726 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3727 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
3728 f44b1f58 2020-02-02 tracey int lineno;
3729 cb713507 2022-06-16 stsp char *line = NULL;
3730 826082fe 2020-12-10 stsp size_t linesize = 0;
3731 826082fe 2020-12-10 stsp ssize_t linelen;
3733 f44b1f58 2020-02-02 tracey if (!view->searching) {
3734 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3735 f44b1f58 2020-02-02 tracey return NULL;
3738 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3739 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3740 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3742 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3744 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
3746 f44b1f58 2020-02-02 tracey while (1) {
3747 f44b1f58 2020-02-02 tracey off_t offset;
3749 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3750 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3751 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3755 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3756 f44b1f58 2020-02-02 tracey lineno = 1;
3758 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3761 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3762 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3763 f44b1f58 2020-02-02 tracey free(line);
3764 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3766 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3767 cb713507 2022-06-16 stsp if (linelen != -1) {
3768 cb713507 2022-06-16 stsp char *exstr;
3769 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
3772 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
3773 cb713507 2022-06-16 stsp &view->regmatch)) {
3774 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3775 cb713507 2022-06-16 stsp s->matched_line = lineno;
3776 cb713507 2022-06-16 stsp free(exstr);
3779 cb713507 2022-06-16 stsp free(exstr);
3781 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3782 f44b1f58 2020-02-02 tracey lineno++;
3784 f44b1f58 2020-02-02 tracey lineno--;
3786 826082fe 2020-12-10 stsp free(line);
3788 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3789 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3790 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3793 145b6838 2022-06-16 stsp return err;
3796 48ae06ee 2018-10-18 stsp static const struct got_error *
3797 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
3799 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
3800 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
3802 b72706c3 2022-06-01 stsp free(s->id1);
3803 b72706c3 2022-06-01 stsp s->id1 = NULL;
3804 b72706c3 2022-06-01 stsp free(s->id2);
3805 b72706c3 2022-06-01 stsp s->id2 = NULL;
3806 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
3807 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3808 b72706c3 2022-06-01 stsp s->f = NULL;
3809 b72706c3 2022-06-01 stsp if (s->f1 && fclose(s->f1) == EOF)
3810 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3811 b72706c3 2022-06-01 stsp s->f1 = NULL;
3812 b72706c3 2022-06-01 stsp if (s->f2 && fclose(s->f2) == EOF)
3813 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3814 b72706c3 2022-06-01 stsp s->f2 = NULL;
3815 b72706c3 2022-06-01 stsp free_colors(&s->colors);
3816 b72706c3 2022-06-01 stsp free(s->line_offsets);
3817 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
3818 b72706c3 2022-06-01 stsp s->nlines = 0;
3819 b72706c3 2022-06-01 stsp return err;
3822 b72706c3 2022-06-01 stsp static const struct got_error *
3823 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3824 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3825 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3826 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3828 48ae06ee 2018-10-18 stsp const struct got_error *err;
3829 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3831 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
3833 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3834 15a94983 2018-12-23 stsp int type1, type2;
3835 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3837 15a94983 2018-12-23 stsp return err;
3838 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3840 15a94983 2018-12-23 stsp return err;
3842 15a94983 2018-12-23 stsp if (type1 != type2)
3843 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3845 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3846 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3847 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3848 f44b1f58 2020-02-02 tracey s->repo = repo;
3849 f44b1f58 2020-02-02 tracey s->id1 = id1;
3850 f44b1f58 2020-02-02 tracey s->id2 = id2;
3851 3dbaef42 2020-11-24 stsp s->label1 = label1;
3852 3dbaef42 2020-11-24 stsp s->label2 = label2;
3854 15a94983 2018-12-23 stsp if (id1) {
3855 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3856 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3857 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3859 5465d566 2020-02-01 tracey s->id1 = NULL;
3861 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3862 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3863 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
3864 b72706c3 2022-06-01 stsp goto done;
3867 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
3868 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
3869 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
3870 a00719e9 2022-06-17 stsp goto done;
3873 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
3874 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
3875 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3876 b72706c3 2022-06-01 stsp goto done;
3879 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3880 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3881 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3882 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3883 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3884 5465d566 2020-02-01 tracey s->log_view = log_view;
3885 5465d566 2020-02-01 tracey s->repo = repo;
3887 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3888 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3889 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3890 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3891 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3893 b72706c3 2022-06-01 stsp goto done;
3894 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3895 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3896 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3898 b72706c3 2022-06-01 stsp goto done;
3899 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3900 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3901 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3903 b72706c3 2022-06-01 stsp goto done;
3905 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3906 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3907 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3908 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3910 b72706c3 2022-06-01 stsp goto done;
3912 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3913 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3914 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3916 b72706c3 2022-06-01 stsp goto done;
3918 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3919 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3920 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3922 b72706c3 2022-06-01 stsp goto done;
3925 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3926 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3927 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3929 5465d566 2020-02-01 tracey err = create_diff(s);
3931 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3932 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3933 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3934 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3935 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3938 b72706c3 2022-06-01 stsp close_diff_view(view);
3939 e5a0f69f 2018-08-18 stsp return err;
3942 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3943 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3945 a3404814 2018-09-02 stsp const struct got_error *err;
3946 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3947 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3948 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3950 a3404814 2018-09-02 stsp if (s->id1) {
3951 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3953 a3404814 2018-09-02 stsp return err;
3954 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3956 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3958 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3960 a3404814 2018-09-02 stsp return err;
3961 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3963 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3964 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3965 a3404814 2018-09-02 stsp free(id_str1);
3966 a3404814 2018-09-02 stsp free(id_str2);
3967 a3404814 2018-09-02 stsp return err;
3969 a3404814 2018-09-02 stsp free(id_str1);
3970 a3404814 2018-09-02 stsp free(id_str2);
3972 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3973 267bb3b8 2021-08-01 stsp free(header);
3974 267bb3b8 2021-08-01 stsp return err;
3977 15a087fe 2019-02-21 stsp static const struct got_error *
3978 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3979 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3981 d7a04538 2019-02-21 stsp const struct got_error *err;
3982 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3983 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3984 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3986 15a087fe 2019-02-21 stsp free(s->id2);
3987 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3988 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3989 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3991 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3993 d7a04538 2019-02-21 stsp return err;
3994 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3995 15a087fe 2019-02-21 stsp free(s->id1);
3996 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3997 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3998 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3999 15a087fe 2019-02-21 stsp return NULL;
4002 0cf4efb1 2018-09-29 stsp static const struct got_error *
4003 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4005 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4006 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4007 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4008 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4009 826082fe 2020-12-10 stsp char *line = NULL;
4010 826082fe 2020-12-10 stsp size_t linesize = 0;
4011 826082fe 2020-12-10 stsp ssize_t linelen;
4012 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4014 e5a0f69f 2018-08-18 stsp switch (ch) {
4016 145b6838 2022-06-16 stsp view->x = 0;
4019 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4021 145b6838 2022-06-16 stsp case KEY_RIGHT:
4023 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4024 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4026 145b6838 2022-06-16 stsp case KEY_LEFT:
4028 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4032 3dbaef42 2020-11-24 stsp if (ch == 'a')
4033 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4034 3dbaef42 2020-11-24 stsp if (ch == 'w')
4035 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4036 64453f7e 2020-11-21 stsp wclear(view->window);
4037 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
4038 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4039 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4040 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
4041 64453f7e 2020-11-21 stsp err = create_diff(s);
4044 912a3f79 2021-08-30 j case KEY_HOME:
4045 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4048 912a3f79 2021-08-30 j case KEY_END:
4052 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4056 1e37a5c2 2019-05-12 jcs case KEY_UP:
4057 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4058 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4059 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4061 83cc4199 2022-06-13 stsp case CTRL('u'):
4063 83cc4199 2022-06-13 stsp nscroll /= 2;
4064 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4065 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4066 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4068 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
4071 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4072 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4075 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4076 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4077 1e37a5c2 2019-05-12 jcs if (!s->eof)
4078 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4080 83cc4199 2022-06-13 stsp case CTRL('d'):
4082 83cc4199 2022-06-13 stsp nscroll /= 2;
4083 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4084 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4085 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4088 00ba99a7 2019-05-12 jcs if (s->eof)
4091 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4092 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4093 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4094 826082fe 2020-12-10 stsp if (linelen == -1) {
4095 826082fe 2020-12-10 stsp if (feof(s->f)) {
4096 826082fe 2020-12-10 stsp s->eof = 1;
4098 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4102 826082fe 2020-12-10 stsp free(line);
4105 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4106 1e37a5c2 2019-05-12 jcs s->diff_context--;
4107 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4108 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4109 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4110 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4111 27829c9e 2020-11-21 stsp s->nlines) {
4112 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4113 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4118 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4119 1e37a5c2 2019-05-12 jcs s->diff_context++;
4120 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4121 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4122 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4127 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4129 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4130 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4132 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4136 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4139 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4143 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4144 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4145 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4146 145b6838 2022-06-16 stsp view->x = 0;
4148 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4149 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4153 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4155 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4156 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4158 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4162 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4165 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4169 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4170 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4171 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4172 145b6838 2022-06-16 stsp view->x = 0;
4174 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4175 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4181 bcbd79e2 2018-08-19 stsp return err;
4184 4ed7e80c 2018-05-20 stsp static const struct got_error *
4185 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4187 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4188 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4189 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4190 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4191 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4192 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4193 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4194 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4195 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4196 3dbaef42 2020-11-24 stsp const char *errstr;
4197 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4198 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4200 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4201 26ed57b2 2018-05-19 stsp switch (ch) {
4203 64453f7e 2020-11-21 stsp force_text_diff = 1;
4206 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4208 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4209 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4210 5a20d08d 2022-02-09 op errstr, errstr);
4212 09b5bff8 2020-02-23 naddy case 'r':
4213 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4214 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4215 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4217 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4220 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4223 17020d27 2019-03-07 stsp usage_diff();
4224 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4228 26ed57b2 2018-05-19 stsp argc -= optind;
4229 26ed57b2 2018-05-19 stsp argv += optind;
4231 26ed57b2 2018-05-19 stsp if (argc == 0) {
4232 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4233 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4234 15a94983 2018-12-23 stsp id_str1 = argv[0];
4235 15a94983 2018-12-23 stsp id_str2 = argv[1];
4237 26ed57b2 2018-05-19 stsp usage_diff();
4239 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4240 0ae84acc 2022-06-15 tracey if (error)
4241 0ae84acc 2022-06-15 tracey goto done;
4243 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4244 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4245 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4246 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4247 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4248 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4249 c156c7a4 2020-12-18 stsp goto done;
4250 a273ac94 2020-02-23 naddy if (worktree)
4251 a273ac94 2020-02-23 naddy repo_path =
4252 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4254 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4255 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4256 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4257 c156c7a4 2020-12-18 stsp goto done;
4261 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4262 eb6600df 2019-01-04 stsp if (error)
4263 eb6600df 2019-01-04 stsp goto done;
4265 a273ac94 2020-02-23 naddy init_curses();
4267 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4268 51a10b52 2020-12-26 stsp if (error)
4269 51a10b52 2020-12-26 stsp goto done;
4271 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4272 26ed57b2 2018-05-19 stsp if (error)
4273 26ed57b2 2018-05-19 stsp goto done;
4275 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4276 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4277 26ed57b2 2018-05-19 stsp if (error)
4278 26ed57b2 2018-05-19 stsp goto done;
4280 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4281 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4282 26ed57b2 2018-05-19 stsp if (error)
4283 26ed57b2 2018-05-19 stsp goto done;
4285 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4286 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4287 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4288 ea5e7bb5 2018-08-01 stsp goto done;
4290 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4291 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4292 5dc9f4bc 2018-08-04 stsp if (error)
4293 5dc9f4bc 2018-08-04 stsp goto done;
4294 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4296 3dbaef42 2020-11-24 stsp free(label1);
4297 3dbaef42 2020-11-24 stsp free(label2);
4298 c02c541e 2019-03-29 stsp free(repo_path);
4299 a273ac94 2020-02-23 naddy free(cwd);
4300 1d0f4054 2021-06-17 stsp if (repo) {
4301 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4302 1d0f4054 2021-06-17 stsp if (error == NULL)
4303 1d0f4054 2021-06-17 stsp error = close_err;
4305 a273ac94 2020-02-23 naddy if (worktree)
4306 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4307 0ae84acc 2022-06-15 tracey if (pack_fds) {
4308 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4309 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4310 0ae84acc 2022-06-15 tracey if (error == NULL)
4311 0ae84acc 2022-06-15 tracey error = pack_err;
4313 51a10b52 2020-12-26 stsp tog_free_refs();
4314 26ed57b2 2018-05-19 stsp return error;
4317 4ed7e80c 2018-05-20 stsp __dead static void
4318 9f7d7167 2018-04-29 stsp usage_blame(void)
4321 87411fa9 2022-06-16 stsp fprintf(stderr,
4322 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4323 9f7d7167 2018-04-29 stsp getprogname());
4327 84451b3e 2018-07-10 stsp struct tog_blame_line {
4328 84451b3e 2018-07-10 stsp int annotated;
4329 84451b3e 2018-07-10 stsp struct got_object_id *id;
4332 4ed7e80c 2018-05-20 stsp static const struct got_error *
4333 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4335 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4336 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4337 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4338 84451b3e 2018-07-10 stsp const struct got_error *err;
4339 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4340 826082fe 2020-12-10 stsp char *line = NULL;
4341 826082fe 2020-12-10 stsp size_t linesize = 0;
4342 826082fe 2020-12-10 stsp ssize_t linelen;
4343 84451b3e 2018-07-10 stsp wchar_t *wline;
4344 27a741e5 2019-09-11 stsp int width;
4345 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4346 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4347 ab089a2a 2018-07-12 stsp char *id_str;
4348 11b20872 2019-11-08 stsp struct tog_color *tc;
4350 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4352 ab089a2a 2018-07-12 stsp return err;
4354 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4355 f7d12f7e 2018-08-01 stsp werase(view->window);
4357 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4358 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4359 ab089a2a 2018-07-12 stsp free(id_str);
4360 ab089a2a 2018-07-12 stsp return err;
4363 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4364 ab089a2a 2018-07-12 stsp free(line);
4365 2550e4c3 2018-07-13 stsp line = NULL;
4367 1cae65b4 2019-09-22 stsp return err;
4368 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4369 a3404814 2018-09-02 stsp wstandout(view->window);
4370 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4372 11b20872 2019-11-08 stsp wattr_on(view->window,
4373 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4374 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4376 11b20872 2019-11-08 stsp wattr_off(view->window,
4377 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4378 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4379 a3404814 2018-09-02 stsp wstandend(view->window);
4380 2550e4c3 2018-07-13 stsp free(wline);
4381 2550e4c3 2018-07-13 stsp wline = NULL;
4382 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4383 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4385 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4386 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4387 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4388 ab089a2a 2018-07-12 stsp free(id_str);
4389 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4391 ab089a2a 2018-07-12 stsp free(id_str);
4392 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4393 3f60a8ef 2018-07-10 stsp free(line);
4394 2550e4c3 2018-07-13 stsp line = NULL;
4396 3f60a8ef 2018-07-10 stsp return err;
4397 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4398 2550e4c3 2018-07-13 stsp free(wline);
4399 2550e4c3 2018-07-13 stsp wline = NULL;
4400 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4401 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4403 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4404 145b6838 2022-06-16 stsp view->maxx = 0;
4405 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4406 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4407 826082fe 2020-12-10 stsp if (linelen == -1) {
4408 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4409 826082fe 2020-12-10 stsp s->eof = 1;
4412 84451b3e 2018-07-10 stsp free(line);
4413 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4415 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4418 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4419 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4420 1853e0f4 2022-06-16 stsp if (err) {
4421 1853e0f4 2022-06-16 stsp free(line);
4422 1853e0f4 2022-06-16 stsp return err;
4424 1853e0f4 2022-06-16 stsp free(wline);
4425 1853e0f4 2022-06-16 stsp wline = NULL;
4426 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4428 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4429 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4431 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4432 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4433 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4434 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4435 27a741e5 2019-09-11 stsp !(view->focussed &&
4436 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4437 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4438 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4439 8d0fe45a 2019-08-12 stsp char *id_str;
4440 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4441 87411fa9 2022-06-16 stsp blame_line->id);
4442 8d0fe45a 2019-08-12 stsp if (err) {
4443 8d0fe45a 2019-08-12 stsp free(line);
4444 8d0fe45a 2019-08-12 stsp return err;
4446 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4448 11b20872 2019-11-08 stsp wattr_on(view->window,
4449 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4450 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4452 11b20872 2019-11-08 stsp wattr_off(view->window,
4453 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4454 8d0fe45a 2019-08-12 stsp free(id_str);
4455 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4457 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4458 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4461 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4462 ee41ec32 2018-07-10 stsp prev_id = NULL;
4465 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4466 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4467 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4469 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4470 41605754 2020-11-12 stsp width = 9;
4471 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4472 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4473 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4474 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4475 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4476 41605754 2020-11-12 stsp if (err) {
4477 41605754 2020-11-12 stsp free(line);
4478 41605754 2020-11-12 stsp return err;
4480 41605754 2020-11-12 stsp width += 9;
4483 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4484 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4485 4cb9d869 2022-06-16 stsp if (err) {
4486 4cb9d869 2022-06-16 stsp free(line);
4487 4cb9d869 2022-06-16 stsp return err;
4489 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4490 a75b3e2d 2022-06-16 stsp width += 9;
4491 41605754 2020-11-12 stsp free(wline);
4492 41605754 2020-11-12 stsp wline = NULL;
4495 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4496 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4497 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4498 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4500 826082fe 2020-12-10 stsp free(line);
4501 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4503 1a57306a 2018-09-02 stsp view_vborder(view);
4505 84451b3e 2018-07-10 stsp return NULL;
4508 84451b3e 2018-07-10 stsp static const struct got_error *
4509 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4510 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4512 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4513 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4514 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4515 1a76625f 2018-10-22 stsp int errcode;
4517 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4518 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4519 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4521 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4522 1a76625f 2018-10-22 stsp if (errcode)
4523 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4525 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4526 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4527 d68a0a7d 2018-07-10 stsp goto done;
4530 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4531 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4533 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4534 d68a0a7d 2018-07-10 stsp if (line->annotated)
4535 d68a0a7d 2018-07-10 stsp goto done;
4537 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4538 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4539 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4540 84451b3e 2018-07-10 stsp goto done;
4542 84451b3e 2018-07-10 stsp line->annotated = 1;
4544 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4545 1a76625f 2018-10-22 stsp if (errcode)
4546 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4547 84451b3e 2018-07-10 stsp return err;
4550 84451b3e 2018-07-10 stsp static void *
4551 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4553 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4554 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4555 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4556 1a76625f 2018-10-22 stsp int errcode;
4558 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4560 61266923 2020-01-14 stsp return (void *)err;
4562 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4563 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4564 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4565 fc06ba56 2019-08-22 stsp err = NULL;
4567 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4568 1a76625f 2018-10-22 stsp if (errcode)
4569 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4570 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4572 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4573 1d0f4054 2021-06-17 stsp if (err == NULL)
4574 1d0f4054 2021-06-17 stsp err = close_err;
4575 c9beca56 2018-07-22 stsp ta->repo = NULL;
4576 c9beca56 2018-07-22 stsp *ta->complete = 1;
4578 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4579 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4580 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4582 18430de3 2018-07-10 stsp return (void *)err;
4585 245d91c1 2018-07-12 stsp static struct got_object_id *
4586 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4587 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4589 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4591 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4592 8d0fe45a 2019-08-12 stsp return NULL;
4594 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4595 245d91c1 2018-07-12 stsp if (!line->annotated)
4596 245d91c1 2018-07-12 stsp return NULL;
4598 245d91c1 2018-07-12 stsp return line->id;
4601 b880a918 2018-07-10 stsp static const struct got_error *
4602 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4604 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4607 245d91c1 2018-07-12 stsp if (blame->thread) {
4608 1a76625f 2018-10-22 stsp int errcode;
4609 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4610 1a76625f 2018-10-22 stsp if (errcode)
4611 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4612 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4613 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4614 1a76625f 2018-10-22 stsp if (errcode)
4615 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4616 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4617 1a76625f 2018-10-22 stsp if (errcode)
4618 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4619 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4620 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4621 245d91c1 2018-07-12 stsp err = NULL;
4622 245d91c1 2018-07-12 stsp blame->thread = NULL;
4624 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4625 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4626 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4627 1d0f4054 2021-06-17 stsp if (err == NULL)
4628 1d0f4054 2021-06-17 stsp err = close_err;
4629 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4631 245d91c1 2018-07-12 stsp if (blame->f) {
4632 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4633 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4634 245d91c1 2018-07-12 stsp blame->f = NULL;
4636 57670559 2018-12-23 stsp if (blame->lines) {
4637 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4638 57670559 2018-12-23 stsp free(blame->lines[i].id);
4639 57670559 2018-12-23 stsp free(blame->lines);
4640 57670559 2018-12-23 stsp blame->lines = NULL;
4642 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4643 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4644 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
4645 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4646 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
4647 0ae84acc 2022-06-15 tracey if (err == NULL)
4648 0ae84acc 2022-06-15 tracey err = pack_err;
4649 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
4651 245d91c1 2018-07-12 stsp return err;
4654 245d91c1 2018-07-12 stsp static const struct got_error *
4655 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4657 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4658 fc06ba56 2019-08-22 stsp int *done = arg;
4659 fc06ba56 2019-08-22 stsp int errcode;
4661 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4662 fc06ba56 2019-08-22 stsp if (errcode)
4663 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4664 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4666 fc06ba56 2019-08-22 stsp if (*done)
4667 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4669 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4670 fc06ba56 2019-08-22 stsp if (errcode)
4671 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4672 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4674 fc06ba56 2019-08-22 stsp return err;
4677 fc06ba56 2019-08-22 stsp static const struct got_error *
4678 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4680 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4681 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4682 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4683 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4684 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4685 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4686 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4687 15a94983 2018-12-23 stsp int obj_type;
4688 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4690 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
4691 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
4693 15a94983 2018-12-23 stsp return err;
4695 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4697 a44927cc 2022-04-07 stsp goto done;
4699 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4701 84451b3e 2018-07-10 stsp goto done;
4703 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4704 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4705 84451b3e 2018-07-10 stsp goto done;
4708 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4710 a70480e0 2018-06-23 stsp goto done;
4711 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4712 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4713 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4714 84451b3e 2018-07-10 stsp goto done;
4716 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4717 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4719 1fddf795 2021-01-20 stsp goto done;
4720 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4721 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4722 84451b3e 2018-07-10 stsp goto done;
4725 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4726 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4727 b02560ec 2019-08-19 stsp blame->nlines--;
4729 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4730 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4731 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4732 84451b3e 2018-07-10 stsp goto done;
4735 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
4737 bd24772e 2018-07-11 stsp goto done;
4738 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4739 0ae84acc 2022-06-15 tracey pack_fds);
4740 0ae84acc 2022-06-15 tracey if (err)
4741 0ae84acc 2022-06-15 tracey goto done;
4743 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
4744 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4745 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4746 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4747 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4748 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4749 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4750 245d91c1 2018-07-12 stsp goto done;
4752 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4754 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4755 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4756 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4757 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4758 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4759 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4760 a5388363 2020-12-01 naddy s->blame_complete = 0;
4762 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4763 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4764 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4765 f5a09613 2020-12-13 naddy s->selected_line = 1;
4767 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4770 a44927cc 2022-04-07 stsp if (commit)
4771 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4773 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4774 27d434c2 2018-09-15 stsp free(obj_id);
4776 245d91c1 2018-07-12 stsp stop_blame(blame);
4777 245d91c1 2018-07-12 stsp return err;
4780 245d91c1 2018-07-12 stsp static const struct got_error *
4781 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4782 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4784 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4785 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4787 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4789 c4843652 2019-08-12 stsp s->path = strdup(path);
4790 c4843652 2019-08-12 stsp if (s->path == NULL)
4791 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4793 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4794 c4843652 2019-08-12 stsp if (err) {
4795 c4843652 2019-08-12 stsp free(s->path);
4796 7cbe629d 2018-08-04 stsp return err;
4799 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4800 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4801 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4802 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4803 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4804 fb2756b9 2018-08-04 stsp s->repo = repo;
4805 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4806 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4808 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4809 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4810 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4811 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4813 11b20872 2019-11-08 stsp return err;
4816 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4817 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4818 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4819 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4820 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4822 a5388363 2020-12-01 naddy return run_blame(view);
4825 e5a0f69f 2018-08-18 stsp static const struct got_error *
4826 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4828 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4829 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4831 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4832 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4834 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4835 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4836 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4837 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4838 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4841 e5a0f69f 2018-08-18 stsp free(s->path);
4842 11b20872 2019-11-08 stsp free_colors(&s->colors);
4843 e5a0f69f 2018-08-18 stsp return err;
4846 7cbe629d 2018-08-04 stsp static const struct got_error *
4847 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4849 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4851 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4852 6c4c42e0 2019-06-24 stsp return NULL;
4855 6c4c42e0 2019-06-24 stsp static const struct got_error *
4856 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4858 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4859 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4860 6c4c42e0 2019-06-24 stsp int lineno;
4861 cb713507 2022-06-16 stsp char *line = NULL;
4862 826082fe 2020-12-10 stsp size_t linesize = 0;
4863 826082fe 2020-12-10 stsp ssize_t linelen;
4865 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4866 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4867 6c4c42e0 2019-06-24 stsp return NULL;
4870 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4871 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4872 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4874 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4876 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
4878 6c4c42e0 2019-06-24 stsp while (1) {
4879 6c4c42e0 2019-06-24 stsp off_t offset;
4881 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4882 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4883 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4887 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4888 6c4c42e0 2019-06-24 stsp lineno = 1;
4890 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4893 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4894 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4895 6c4c42e0 2019-06-24 stsp free(line);
4896 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4898 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4899 cb713507 2022-06-16 stsp if (linelen != -1) {
4900 cb713507 2022-06-16 stsp char *exstr;
4901 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4904 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4905 cb713507 2022-06-16 stsp &view->regmatch)) {
4906 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4907 cb713507 2022-06-16 stsp s->matched_line = lineno;
4908 cb713507 2022-06-16 stsp free(exstr);
4911 cb713507 2022-06-16 stsp free(exstr);
4913 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4918 826082fe 2020-12-10 stsp free(line);
4920 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4921 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4922 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4925 145b6838 2022-06-16 stsp return err;
4928 6c4c42e0 2019-06-24 stsp static const struct got_error *
4929 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4931 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4932 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4933 2b380cc8 2018-10-24 stsp int errcode;
4935 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4936 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4937 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4938 2b380cc8 2018-10-24 stsp if (errcode)
4939 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4941 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4944 51fe7530 2019-08-19 stsp if (s->blame_complete)
4945 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4947 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4949 669b5ffa 2018-10-07 stsp view_vborder(view);
4950 e5a0f69f 2018-08-18 stsp return err;
4953 e5a0f69f 2018-08-18 stsp static const struct got_error *
4954 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4956 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4957 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4958 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4959 83cc4199 2022-06-13 stsp int begin_x = 0, nscroll = view->nlines - 2;
4961 e5a0f69f 2018-08-18 stsp switch (ch) {
4963 145b6838 2022-06-16 stsp view->x = 0;
4966 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4968 145b6838 2022-06-16 stsp case KEY_RIGHT:
4970 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4971 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4973 145b6838 2022-06-16 stsp case KEY_LEFT:
4975 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4978 1e37a5c2 2019-05-12 jcs s->done = 1;
4980 4deef56f 2021-09-02 naddy case 'g':
4981 4deef56f 2021-09-02 naddy case KEY_HOME:
4982 4deef56f 2021-09-02 naddy s->selected_line = 1;
4983 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4985 4deef56f 2021-09-02 naddy case 'G':
4986 4deef56f 2021-09-02 naddy case KEY_END:
4987 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4988 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4989 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4991 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4992 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4993 4deef56f 2021-09-02 naddy (view->nlines - 3);
4997 1e37a5c2 2019-05-12 jcs case KEY_UP:
4998 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4999 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5000 1e37a5c2 2019-05-12 jcs s->selected_line--;
5001 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5002 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5003 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5005 83cc4199 2022-06-13 stsp case CTRL('u'):
5007 83cc4199 2022-06-13 stsp nscroll /= 2;
5008 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5009 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5010 ea025d1d 2020-02-22 naddy case CTRL('b'):
5012 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5013 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5016 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5017 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5019 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5022 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5023 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5024 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
5025 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
5026 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5027 1e37a5c2 2019-05-12 jcs s->selected_line++;
5028 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
5029 1e37a5c2 2019-05-12 jcs s->blame.nlines)
5030 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5033 1e37a5c2 2019-05-12 jcs case 'p': {
5034 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5035 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5036 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5037 1e37a5c2 2019-05-12 jcs if (id == NULL)
5039 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5040 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5041 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5042 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5043 1e37a5c2 2019-05-12 jcs int obj_type;
5044 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5045 1e37a5c2 2019-05-12 jcs s->repo, id);
5048 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5049 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5050 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5051 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5054 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5055 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5056 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5059 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5060 a44927cc 2022-04-07 stsp pcommit, s->path);
5061 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5062 e5a0f69f 2018-08-18 stsp if (err) {
5063 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5064 1e37a5c2 2019-05-12 jcs err = NULL;
5065 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5068 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5070 1e37a5c2 2019-05-12 jcs free(blob_id);
5071 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5072 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5073 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5076 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5077 d7b5a0e8 2022-04-20 stsp &pid->id);
5078 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5080 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5081 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5083 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5088 1e37a5c2 2019-05-12 jcs s->done = 1;
5089 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5090 1e37a5c2 2019-05-12 jcs s->done = 0;
5091 1e37a5c2 2019-05-12 jcs if (thread_err)
5093 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5094 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5095 a5388363 2020-12-01 naddy err = run_blame(view);
5100 61417565 2022-06-20 mark case 'C': {
5101 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5102 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5103 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5105 1e37a5c2 2019-05-12 jcs s->done = 1;
5106 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5107 1e37a5c2 2019-05-12 jcs s->done = 0;
5108 1e37a5c2 2019-05-12 jcs if (thread_err)
5110 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5111 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5112 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5113 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5114 a5388363 2020-12-01 naddy err = run_blame(view);
5119 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5120 1e37a5c2 2019-05-12 jcs case '\r': {
5121 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5122 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5123 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5124 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5125 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5126 1e37a5c2 2019-05-12 jcs if (id == NULL)
5128 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5131 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5132 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
5133 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5134 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5135 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5136 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5137 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5138 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5141 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5142 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5143 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5145 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5148 e78dc838 2020-12-04 stsp view->focussed = 0;
5149 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5150 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5151 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5154 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5157 e78dc838 2020-12-04 stsp view->focus_child = 1;
5159 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5164 83cc4199 2022-06-13 stsp case CTRL('d'):
5166 83cc4199 2022-06-13 stsp nscroll /= 2;
5167 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5168 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5169 ea025d1d 2020-02-22 naddy case CTRL('f'):
5172 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5173 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5174 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5177 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5178 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5179 83cc4199 2022-06-13 stsp s->selected_line +=
5180 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5181 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5183 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5184 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5186 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5187 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5189 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5190 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5191 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5192 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5198 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5201 a70480e0 2018-06-23 stsp static const struct got_error *
5202 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5204 a70480e0 2018-06-23 stsp const struct got_error *error;
5205 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5206 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5207 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5208 0587e10c 2020-07-23 stsp char *link_target = NULL;
5209 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5210 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5211 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5213 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5214 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5216 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5217 a70480e0 2018-06-23 stsp switch (ch) {
5219 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5222 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5223 69069811 2018-08-02 stsp if (repo_path == NULL)
5224 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5228 17020d27 2019-03-07 stsp usage_blame();
5229 a70480e0 2018-06-23 stsp /* NOTREACHED */
5233 a70480e0 2018-06-23 stsp argc -= optind;
5234 a70480e0 2018-06-23 stsp argv += optind;
5236 f135c941 2020-02-20 stsp if (argc != 1)
5237 a70480e0 2018-06-23 stsp usage_blame();
5239 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5240 0ae84acc 2022-06-15 tracey if (error != NULL)
5241 0ae84acc 2022-06-15 tracey goto done;
5243 69069811 2018-08-02 stsp if (repo_path == NULL) {
5244 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5245 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5246 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5247 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5248 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5249 c156c7a4 2020-12-18 stsp goto done;
5250 f135c941 2020-02-20 stsp if (worktree)
5251 eb41ed75 2019-02-05 stsp repo_path =
5252 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5254 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5255 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5256 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5257 c156c7a4 2020-12-18 stsp goto done;
5261 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5262 c02c541e 2019-03-29 stsp if (error != NULL)
5263 8e94dd5b 2019-01-04 stsp goto done;
5265 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5266 f135c941 2020-02-20 stsp worktree);
5267 c02c541e 2019-03-29 stsp if (error)
5268 92205607 2019-01-04 stsp goto done;
5270 f135c941 2020-02-20 stsp init_curses();
5272 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5273 51a10b52 2020-12-26 stsp if (error)
5274 51a10b52 2020-12-26 stsp goto done;
5276 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5277 eb41ed75 2019-02-05 stsp if (error)
5278 69069811 2018-08-02 stsp goto done;
5280 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5281 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5282 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5283 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5284 a70480e0 2018-06-23 stsp if (error != NULL)
5285 66b4983c 2018-06-23 stsp goto done;
5286 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5287 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5289 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5290 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5292 a19e88aa 2018-06-23 stsp if (error != NULL)
5293 8b473291 2019-02-21 stsp goto done;
5295 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5296 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5297 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5298 e1cd8fed 2018-08-01 stsp goto done;
5301 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5302 a44927cc 2022-04-07 stsp if (error)
5303 a44927cc 2022-04-07 stsp goto done;
5305 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5306 a44927cc 2022-04-07 stsp commit, repo);
5307 7cbe629d 2018-08-04 stsp if (error)
5308 7cbe629d 2018-08-04 stsp goto done;
5310 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5311 78756c87 2020-11-24 stsp commit_id, repo);
5312 0587e10c 2020-07-23 stsp if (error)
5313 0587e10c 2020-07-23 stsp goto done;
5314 12314ad4 2019-08-31 stsp if (worktree) {
5315 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5316 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5317 12314ad4 2019-08-31 stsp worktree = NULL;
5319 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5321 69069811 2018-08-02 stsp free(repo_path);
5322 f135c941 2020-02-20 stsp free(in_repo_path);
5323 0587e10c 2020-07-23 stsp free(link_target);
5324 69069811 2018-08-02 stsp free(cwd);
5325 a70480e0 2018-06-23 stsp free(commit_id);
5326 a44927cc 2022-04-07 stsp if (commit)
5327 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5328 eb41ed75 2019-02-05 stsp if (worktree)
5329 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5330 1d0f4054 2021-06-17 stsp if (repo) {
5331 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5332 1d0f4054 2021-06-17 stsp if (error == NULL)
5333 1d0f4054 2021-06-17 stsp error = close_err;
5335 0ae84acc 2022-06-15 tracey if (pack_fds) {
5336 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5337 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5338 0ae84acc 2022-06-15 tracey if (error == NULL)
5339 0ae84acc 2022-06-15 tracey error = pack_err;
5341 51a10b52 2020-12-26 stsp tog_free_refs();
5342 a70480e0 2018-06-23 stsp return error;
5345 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5346 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5348 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5349 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5350 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5351 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5352 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5353 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5354 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5356 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5358 f7d12f7e 2018-08-01 stsp werase(view->window);
5360 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5361 ffd1d5e5 2018-06-23 stsp return NULL;
5363 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5366 ffd1d5e5 2018-06-23 stsp return err;
5367 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5368 a3404814 2018-09-02 stsp wstandout(view->window);
5369 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5371 11b20872 2019-11-08 stsp wattr_on(view->window,
5372 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5373 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5375 11b20872 2019-11-08 stsp wattr_off(view->window,
5376 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5377 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5378 a3404814 2018-09-02 stsp wstandend(view->window);
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 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5388 ce52c690 2018-06-23 stsp return err;
5389 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5390 2550e4c3 2018-07-13 stsp free(wline);
5391 2550e4c3 2018-07-13 stsp wline = NULL;
5392 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5393 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5394 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5395 ffd1d5e5 2018-06-23 stsp return NULL;
5396 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5397 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5398 a1eca9bb 2018-06-23 stsp return NULL;
5400 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5401 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5402 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5403 0cf4efb1 2018-09-29 stsp if (view->focussed)
5404 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5405 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5407 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5408 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5409 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5410 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5411 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5412 ffd1d5e5 2018-06-23 stsp return NULL;
5416 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5419 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5420 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5421 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5422 848d6979 2019-08-12 stsp const char *modestr = "";
5423 56e0773d 2019-11-28 stsp mode_t mode;
5425 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5426 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5428 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5429 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5430 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5432 638f9024 2019-05-13 stsp return got_error_from_errno(
5433 230a42bd 2019-05-11 jcs "got_object_id_str");
5435 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5436 63c5ca5d 2019-08-24 stsp modestr = "$";
5437 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5440 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5441 d86d3b18 2020-12-01 naddy te, s->repo);
5442 0d6c6ee3 2020-05-20 stsp if (err) {
5443 0d6c6ee3 2020-05-20 stsp free(id_str);
5444 0d6c6ee3 2020-05-20 stsp return err;
5446 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5447 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5448 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5450 848d6979 2019-08-12 stsp modestr = "@";
5452 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5453 848d6979 2019-08-12 stsp modestr = "/";
5454 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5455 848d6979 2019-08-12 stsp modestr = "*";
5456 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5457 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5458 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5459 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5460 1d13200f 2018-07-12 stsp free(id_str);
5461 0d6c6ee3 2020-05-20 stsp free(link_target);
5462 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5464 1d13200f 2018-07-12 stsp free(id_str);
5465 0d6c6ee3 2020-05-20 stsp free(link_target);
5466 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5468 ffd1d5e5 2018-06-23 stsp if (err) {
5469 ffd1d5e5 2018-06-23 stsp free(line);
5472 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5473 0cf4efb1 2018-09-29 stsp if (view->focussed)
5474 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5475 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5477 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5479 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5480 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5481 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5483 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5484 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5485 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5486 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5487 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5488 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5489 ffd1d5e5 2018-06-23 stsp free(line);
5490 2550e4c3 2018-07-13 stsp free(wline);
5491 2550e4c3 2018-07-13 stsp wline = NULL;
5493 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5494 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5495 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5499 ffd1d5e5 2018-06-23 stsp return err;
5502 ffd1d5e5 2018-06-23 stsp static void
5503 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5505 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5506 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5507 fa86c4bf 2020-11-29 stsp int i = 0;
5509 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5512 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5513 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5514 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5515 fa86c4bf 2020-11-29 stsp if (!isroot)
5516 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5519 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5520 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5524 fa86c4bf 2020-11-29 stsp static void
5525 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5527 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5528 ffd1d5e5 2018-06-23 stsp int n = 0;
5530 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5531 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5532 694d3271 2020-12-01 naddy s->first_displayed_entry);
5534 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5536 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5537 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5538 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5539 768394f3 2019-01-24 stsp if (last) {
5540 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5541 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5546 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5547 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5548 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5550 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5551 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5552 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5554 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5555 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5556 56e0773d 2019-11-28 stsp + 1 /* slash */;
5558 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5560 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5561 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5562 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5564 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5565 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5566 d9765a41 2018-06-23 stsp while (pt) {
5567 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5568 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5569 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5570 cb2ebc8a 2018-06-23 stsp goto done;
5572 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5573 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5574 cb2ebc8a 2018-06-23 stsp goto done;
5576 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5579 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5580 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5581 ce52c690 2018-06-23 stsp goto done;
5585 ce52c690 2018-06-23 stsp if (err) {
5586 ce52c690 2018-06-23 stsp free(*path);
5587 ce52c690 2018-06-23 stsp *path = NULL;
5589 ce52c690 2018-06-23 stsp return err;
5592 ce52c690 2018-06-23 stsp static const struct got_error *
5593 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5594 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5595 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5597 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5598 ce52c690 2018-06-23 stsp char *path;
5599 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5601 a0de39f3 2019-08-09 stsp *new_view = NULL;
5603 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5605 ce52c690 2018-06-23 stsp return err;
5607 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5608 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5609 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5610 83ce39e3 2019-08-12 stsp goto done;
5613 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5614 e5a0f69f 2018-08-18 stsp if (err) {
5615 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5616 fc06ba56 2019-08-22 stsp err = NULL;
5617 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5619 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5621 83ce39e3 2019-08-12 stsp free(path);
5622 69efd4c4 2018-07-18 stsp return err;
5625 69efd4c4 2018-07-18 stsp static const struct got_error *
5626 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5627 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5629 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5630 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5631 69efd4c4 2018-07-18 stsp char *path;
5633 a0de39f3 2019-08-09 stsp *new_view = NULL;
5635 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5636 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5637 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5639 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5641 69efd4c4 2018-07-18 stsp return err;
5643 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5646 e5a0f69f 2018-08-18 stsp view_close(log_view);
5648 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5649 cb2ebc8a 2018-06-23 stsp free(path);
5650 cb2ebc8a 2018-06-23 stsp return err;
5653 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5654 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5655 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5657 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5658 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5659 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5660 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5662 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5663 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5665 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5666 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5667 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5669 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5671 bc573f3b 2021-07-10 stsp goto done;
5674 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5675 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5676 bc573f3b 2021-07-10 stsp * closed on demand.
5678 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5679 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5681 bc573f3b 2021-07-10 stsp goto done;
5682 bc573f3b 2021-07-10 stsp s->tree = s->root;
5684 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5685 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5686 ffd1d5e5 2018-06-23 stsp goto done;
5688 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5689 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5690 ffd1d5e5 2018-06-23 stsp goto done;
5693 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5694 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5695 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5696 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5697 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5698 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5699 9cd7cbd1 2020-12-07 stsp goto done;
5702 fb2756b9 2018-08-04 stsp s->repo = repo;
5704 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5705 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5706 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5707 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5709 c0b01bdb 2019-11-08 stsp goto done;
5710 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5711 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5713 c0b01bdb 2019-11-08 stsp goto done;
5714 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5715 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5716 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5718 c0b01bdb 2019-11-08 stsp goto done;
5720 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5721 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5722 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5724 11b20872 2019-11-08 stsp goto done;
5726 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5727 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5729 c0b01bdb 2019-11-08 stsp goto done;
5732 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5733 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5734 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5735 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5736 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5738 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5739 bc573f3b 2021-07-10 stsp if (commit)
5740 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5742 bc573f3b 2021-07-10 stsp close_tree_view(view);
5743 ad80ab7b 2018-08-04 stsp return err;
5746 e5a0f69f 2018-08-18 stsp static const struct got_error *
5747 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5749 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5751 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5752 fb2756b9 2018-08-04 stsp free(s->tree_label);
5753 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5754 6484ec90 2018-09-29 stsp free(s->commit_id);
5755 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5756 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5757 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5758 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5759 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5760 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5761 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5762 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5763 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5764 ad80ab7b 2018-08-04 stsp free(parent);
5767 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5768 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5769 bc573f3b 2021-07-10 stsp if (s->root)
5770 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5771 7c32bd05 2019-06-22 stsp return NULL;
5774 7c32bd05 2019-06-22 stsp static const struct got_error *
5775 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5777 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5779 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5780 7c32bd05 2019-06-22 stsp return NULL;
5783 7c32bd05 2019-06-22 stsp static int
5784 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5786 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5788 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5792 7c32bd05 2019-06-22 stsp static const struct got_error *
5793 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5795 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5796 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5798 7c32bd05 2019-06-22 stsp if (!view->searching) {
5799 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5800 7c32bd05 2019-06-22 stsp return NULL;
5803 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5804 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5805 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5806 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5807 56e0773d 2019-11-28 stsp s->selected_entry);
5809 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5811 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5812 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5814 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5815 56e0773d 2019-11-28 stsp s->selected_entry);
5818 487cd7d2 2021-12-17 stsp if (s->selected_entry)
5819 487cd7d2 2021-12-17 stsp te = s->selected_entry;
5820 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
5821 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5823 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5826 7c32bd05 2019-06-22 stsp while (1) {
5827 56e0773d 2019-11-28 stsp if (te == NULL) {
5828 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5829 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5830 ac66afb8 2019-06-24 stsp return NULL;
5832 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5833 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5835 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5838 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5839 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5840 56e0773d 2019-11-28 stsp s->matched_entry = te;
5844 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5845 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5847 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5850 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5851 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5852 7c32bd05 2019-06-22 stsp s->selected = 0;
5855 e5a0f69f 2018-08-18 stsp return NULL;
5858 ad80ab7b 2018-08-04 stsp static const struct got_error *
5859 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5861 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5862 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5863 e5a0f69f 2018-08-18 stsp char *parent_path;
5865 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5867 e5a0f69f 2018-08-18 stsp return err;
5869 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5870 e5a0f69f 2018-08-18 stsp free(parent_path);
5872 669b5ffa 2018-10-07 stsp view_vborder(view);
5873 e5a0f69f 2018-08-18 stsp return err;
5876 e5a0f69f 2018-08-18 stsp static const struct got_error *
5877 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5879 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5880 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5881 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5882 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5883 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
5885 e5a0f69f 2018-08-18 stsp switch (ch) {
5887 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5890 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5892 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5893 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5894 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5895 e78dc838 2020-12-04 stsp view->focussed = 0;
5896 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5897 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5898 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5900 1e37a5c2 2019-05-12 jcs return err;
5901 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
5904 e78dc838 2020-12-04 stsp view->focus_child = 1;
5906 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5909 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5910 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5911 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5912 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5913 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5914 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5915 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5916 152c1c93 2020-11-29 stsp if (err) {
5917 152c1c93 2020-11-29 stsp view_close(ref_view);
5918 152c1c93 2020-11-29 stsp return err;
5920 e78dc838 2020-12-04 stsp view->focussed = 0;
5921 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5922 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5923 152c1c93 2020-11-29 stsp err = view_close_child(view);
5925 152c1c93 2020-11-29 stsp return err;
5926 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
5929 e78dc838 2020-12-04 stsp view->focus_child = 1;
5931 152c1c93 2020-11-29 stsp *new_view = ref_view;
5933 e4526bf5 2021-09-03 naddy case 'g':
5934 e4526bf5 2021-09-03 naddy case KEY_HOME:
5935 e4526bf5 2021-09-03 naddy s->selected = 0;
5936 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5937 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5938 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5940 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5942 e4526bf5 2021-09-03 naddy case 'G':
5943 e4526bf5 2021-09-03 naddy case KEY_END:
5944 e4526bf5 2021-09-03 naddy s->selected = 0;
5945 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5946 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5947 e4526bf5 2021-09-03 naddy if (te == NULL) {
5948 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5949 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5954 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5955 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5957 e4526bf5 2021-09-03 naddy if (n > 0)
5958 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5961 1e37a5c2 2019-05-12 jcs case KEY_UP:
5962 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5963 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5964 1e37a5c2 2019-05-12 jcs s->selected--;
5967 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5969 83cc4199 2022-06-13 stsp case CTRL('u'):
5971 83cc4199 2022-06-13 stsp nscroll /= 2;
5972 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5973 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5974 ea025d1d 2020-02-22 naddy case CTRL('b'):
5976 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5977 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5978 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5979 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5981 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5982 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5984 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
5987 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5988 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5989 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5990 1e37a5c2 2019-05-12 jcs s->selected++;
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 */
5997 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5999 83cc4199 2022-06-13 stsp case CTRL('d'):
6001 83cc4199 2022-06-13 stsp nscroll /= 2;
6002 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6003 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6004 ea025d1d 2020-02-22 naddy case CTRL('f'):
6006 48bb96f0 2022-06-20 naddy case ' ':
6007 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6009 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6010 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6011 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6012 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6015 83cc4199 2022-06-13 stsp tree_scroll_down(s, nscroll);
6017 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6019 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6020 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6021 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6022 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6023 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
6025 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6026 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6028 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6029 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6030 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6031 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6032 1e37a5c2 2019-05-12 jcs s->selected_entry =
6033 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6034 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6035 1e37a5c2 2019-05-12 jcs free(parent);
6036 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6037 56e0773d 2019-11-28 stsp s->selected_entry))) {
6038 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6039 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6040 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6043 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6044 941e9f74 2019-05-21 stsp if (err) {
6045 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6048 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6049 56e0773d 2019-11-28 stsp s->selected_entry))) {
6050 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6051 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
6052 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
6054 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
6055 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6056 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6059 e78dc838 2020-12-04 stsp view->focussed = 0;
6060 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6061 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6062 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6064 669b5ffa 2018-10-07 stsp return err;
6065 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6068 e78dc838 2020-12-04 stsp view->focus_child = 1;
6070 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6073 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6074 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6075 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6081 ffd1d5e5 2018-06-23 stsp return err;
6084 ffd1d5e5 2018-06-23 stsp __dead static void
6085 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6088 87411fa9 2022-06-16 stsp fprintf(stderr,
6089 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6090 ffd1d5e5 2018-06-23 stsp getprogname());
6094 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6095 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6097 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6098 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6099 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6100 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6101 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6102 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6103 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6104 4e97c21c 2020-12-06 stsp char *label = NULL;
6105 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6106 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6108 5221c383 2018-08-01 stsp struct tog_view *view;
6109 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6111 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6112 ffd1d5e5 2018-06-23 stsp switch (ch) {
6114 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6117 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6118 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6119 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6123 e99e2d15 2020-11-24 naddy usage_tree();
6124 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6128 ffd1d5e5 2018-06-23 stsp argc -= optind;
6129 ffd1d5e5 2018-06-23 stsp argv += optind;
6131 55cccc34 2020-02-20 stsp if (argc > 1)
6132 e99e2d15 2020-11-24 naddy usage_tree();
6134 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6135 0ae84acc 2022-06-15 tracey if (error != NULL)
6136 0ae84acc 2022-06-15 tracey goto done;
6138 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6139 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6140 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6141 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6142 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6143 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6144 c156c7a4 2020-12-18 stsp goto done;
6145 55cccc34 2020-02-20 stsp if (worktree)
6146 52185f70 2019-02-05 stsp repo_path =
6147 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6149 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6150 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6151 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6152 c156c7a4 2020-12-18 stsp goto done;
6156 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6157 c02c541e 2019-03-29 stsp if (error != NULL)
6158 52185f70 2019-02-05 stsp goto done;
6160 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6161 55cccc34 2020-02-20 stsp repo, worktree);
6162 55cccc34 2020-02-20 stsp if (error)
6163 55cccc34 2020-02-20 stsp goto done;
6165 55cccc34 2020-02-20 stsp init_curses();
6167 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6168 c02c541e 2019-03-29 stsp if (error)
6169 52185f70 2019-02-05 stsp goto done;
6171 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6172 51a10b52 2020-12-26 stsp if (error)
6173 51a10b52 2020-12-26 stsp goto done;
6175 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6176 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6177 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6178 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6179 4e97c21c 2020-12-06 stsp if (error)
6180 4e97c21c 2020-12-06 stsp goto done;
6181 4e97c21c 2020-12-06 stsp head_ref_name = label;
6183 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6184 4e97c21c 2020-12-06 stsp if (error == NULL)
6185 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6186 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6187 4e97c21c 2020-12-06 stsp goto done;
6188 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6189 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6190 4e97c21c 2020-12-06 stsp if (error)
6191 4e97c21c 2020-12-06 stsp goto done;
6194 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6195 a44927cc 2022-04-07 stsp if (error)
6196 a44927cc 2022-04-07 stsp goto done;
6198 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6199 5221c383 2018-08-01 stsp if (view == NULL) {
6200 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6201 5221c383 2018-08-01 stsp goto done;
6203 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6204 ad80ab7b 2018-08-04 stsp if (error)
6205 ad80ab7b 2018-08-04 stsp goto done;
6206 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6207 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6208 d91faf3b 2020-12-01 naddy in_repo_path);
6209 55cccc34 2020-02-20 stsp if (error)
6210 55cccc34 2020-02-20 stsp goto done;
6213 55cccc34 2020-02-20 stsp if (worktree) {
6214 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6215 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6216 55cccc34 2020-02-20 stsp worktree = NULL;
6218 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6220 52185f70 2019-02-05 stsp free(repo_path);
6221 e4a0e26d 2020-02-20 stsp free(cwd);
6222 ffd1d5e5 2018-06-23 stsp free(commit_id);
6223 4e97c21c 2020-12-06 stsp free(label);
6225 486cd271 2020-12-06 stsp got_ref_close(ref);
6226 1d0f4054 2021-06-17 stsp if (repo) {
6227 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6228 1d0f4054 2021-06-17 stsp if (error == NULL)
6229 1d0f4054 2021-06-17 stsp error = close_err;
6231 0ae84acc 2022-06-15 tracey if (pack_fds) {
6232 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6233 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6234 0ae84acc 2022-06-15 tracey if (error == NULL)
6235 0ae84acc 2022-06-15 tracey error = pack_err;
6237 51a10b52 2020-12-26 stsp tog_free_refs();
6238 ffd1d5e5 2018-06-23 stsp return error;
6241 6458efa5 2020-11-24 stsp static const struct got_error *
6242 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6244 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6245 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6247 6458efa5 2020-11-24 stsp s->nrefs = 0;
6248 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6249 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6250 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6251 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6252 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6255 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6256 6458efa5 2020-11-24 stsp if (re == NULL)
6257 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6259 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6260 8924d611 2020-12-26 stsp if (re->ref == NULL)
6261 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6262 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6263 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6266 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6267 6458efa5 2020-11-24 stsp return NULL;
6271 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6273 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6275 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6276 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6277 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6278 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6283 6458efa5 2020-11-24 stsp static const struct got_error *
6284 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6286 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6287 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6289 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6290 6458efa5 2020-11-24 stsp s->repo = repo;
6292 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6293 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6295 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6297 6458efa5 2020-11-24 stsp return err;
6299 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6300 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6301 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6302 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6304 6458efa5 2020-11-24 stsp goto done;
6306 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6307 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6308 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6310 6458efa5 2020-11-24 stsp goto done;
6312 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6313 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6314 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6316 cc488aa7 2022-01-23 stsp goto done;
6318 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6319 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6320 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6322 6458efa5 2020-11-24 stsp goto done;
6325 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6326 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6327 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6328 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6329 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6332 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6333 6458efa5 2020-11-24 stsp return err;
6336 6458efa5 2020-11-24 stsp static const struct got_error *
6337 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6339 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6341 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6342 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6344 6458efa5 2020-11-24 stsp return NULL;
6347 6458efa5 2020-11-24 stsp static const struct got_error *
6348 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6349 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6351 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6352 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6353 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6354 6458efa5 2020-11-24 stsp int obj_type;
6356 c42c9805 2020-11-24 stsp *commit_id = NULL;
6358 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6360 6458efa5 2020-11-24 stsp return err;
6362 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6364 6458efa5 2020-11-24 stsp goto done;
6366 6458efa5 2020-11-24 stsp switch (obj_type) {
6367 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6368 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6370 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6371 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6373 6458efa5 2020-11-24 stsp goto done;
6374 c42c9805 2020-11-24 stsp free(obj_id);
6375 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6376 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6378 6458efa5 2020-11-24 stsp goto done;
6379 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6380 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6381 c42c9805 2020-11-24 stsp goto done;
6383 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6384 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6385 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6386 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6387 c42c9805 2020-11-24 stsp goto done;
6391 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6397 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6398 c42c9805 2020-11-24 stsp if (err) {
6399 c42c9805 2020-11-24 stsp free(*commit_id);
6400 c42c9805 2020-11-24 stsp *commit_id = NULL;
6402 c42c9805 2020-11-24 stsp return err;
6405 c42c9805 2020-11-24 stsp static const struct got_error *
6406 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6407 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6409 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6410 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6411 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6413 c42c9805 2020-11-24 stsp *new_view = NULL;
6415 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6416 c42c9805 2020-11-24 stsp if (err) {
6417 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6418 c42c9805 2020-11-24 stsp return err;
6420 c42c9805 2020-11-24 stsp return NULL;
6423 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6424 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6425 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6426 6458efa5 2020-11-24 stsp goto done;
6429 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6430 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6433 6458efa5 2020-11-24 stsp view_close(log_view);
6435 6458efa5 2020-11-24 stsp *new_view = log_view;
6436 c42c9805 2020-11-24 stsp free(commit_id);
6437 6458efa5 2020-11-24 stsp return err;
6440 ce5b7c56 2019-07-09 stsp static void
6441 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6443 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6444 34ba6917 2020-11-30 stsp int i = 0;
6446 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6449 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6450 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6451 34ba6917 2020-11-30 stsp if (re == NULL)
6453 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6454 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6458 34ba6917 2020-11-30 stsp static void
6459 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6461 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6462 6458efa5 2020-11-24 stsp int n = 0;
6464 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6465 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6467 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6469 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6470 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6471 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6472 6458efa5 2020-11-24 stsp if (last) {
6473 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6474 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6479 6458efa5 2020-11-24 stsp static const struct got_error *
6480 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6482 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6484 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6485 6458efa5 2020-11-24 stsp return NULL;
6488 6458efa5 2020-11-24 stsp static int
6489 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6491 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6493 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6497 6458efa5 2020-11-24 stsp static const struct got_error *
6498 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6500 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6501 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6503 6458efa5 2020-11-24 stsp if (!view->searching) {
6504 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6505 6458efa5 2020-11-24 stsp return NULL;
6508 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6509 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6510 6458efa5 2020-11-24 stsp if (s->selected_entry)
6511 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6513 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6514 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6516 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6517 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6519 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6520 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6523 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6524 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6525 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6526 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6528 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6531 6458efa5 2020-11-24 stsp while (1) {
6532 6458efa5 2020-11-24 stsp if (re == NULL) {
6533 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6534 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6535 6458efa5 2020-11-24 stsp return NULL;
6537 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6538 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6540 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6543 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6544 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6545 6458efa5 2020-11-24 stsp s->matched_entry = re;
6549 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6550 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6552 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6555 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6556 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6557 6458efa5 2020-11-24 stsp s->selected = 0;
6560 6458efa5 2020-11-24 stsp return NULL;
6563 6458efa5 2020-11-24 stsp static const struct got_error *
6564 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6566 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6567 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6568 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6569 6458efa5 2020-11-24 stsp char *line = NULL;
6570 6458efa5 2020-11-24 stsp wchar_t *wline;
6571 6458efa5 2020-11-24 stsp struct tog_color *tc;
6572 6458efa5 2020-11-24 stsp int width, n;
6573 6458efa5 2020-11-24 stsp int limit = view->nlines;
6575 6458efa5 2020-11-24 stsp werase(view->window);
6577 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6579 6458efa5 2020-11-24 stsp if (limit == 0)
6580 6458efa5 2020-11-24 stsp return NULL;
6582 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6584 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6585 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6586 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6588 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6589 6458efa5 2020-11-24 stsp if (err) {
6590 6458efa5 2020-11-24 stsp free(line);
6591 6458efa5 2020-11-24 stsp return err;
6593 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6594 6458efa5 2020-11-24 stsp wstandout(view->window);
6595 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6596 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6597 6458efa5 2020-11-24 stsp wstandend(view->window);
6598 6458efa5 2020-11-24 stsp free(wline);
6599 6458efa5 2020-11-24 stsp wline = NULL;
6600 6458efa5 2020-11-24 stsp free(line);
6601 6458efa5 2020-11-24 stsp line = NULL;
6602 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6603 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6604 6458efa5 2020-11-24 stsp if (--limit <= 0)
6605 6458efa5 2020-11-24 stsp return NULL;
6608 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6609 6458efa5 2020-11-24 stsp char *line = NULL;
6610 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6612 b4996bee 2022-06-16 stsp if (s->show_date) {
6613 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
6614 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
6615 b4996bee 2022-06-16 stsp struct got_object_id *id;
6616 b4996bee 2022-06-16 stsp struct tm tm;
6619 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6621 b4996bee 2022-06-16 stsp return err;
6622 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
6623 b4996bee 2022-06-16 stsp if (err) {
6624 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6626 b4996bee 2022-06-16 stsp return err;
6628 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
6630 b4996bee 2022-06-16 stsp if (err) {
6632 b4996bee 2022-06-16 stsp return err;
6634 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
6635 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
6637 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
6638 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
6641 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
6642 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
6643 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6644 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
6646 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6647 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
6648 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
6649 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6650 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6651 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6652 6458efa5 2020-11-24 stsp struct got_object_id *id;
6653 6458efa5 2020-11-24 stsp char *id_str;
6654 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6656 6458efa5 2020-11-24 stsp return err;
6657 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6658 6458efa5 2020-11-24 stsp if (err) {
6660 6458efa5 2020-11-24 stsp return err;
6662 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6663 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6664 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6666 6458efa5 2020-11-24 stsp free(id_str);
6667 6458efa5 2020-11-24 stsp return err;
6670 6458efa5 2020-11-24 stsp free(id_str);
6671 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6672 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
6673 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
6675 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6677 6458efa5 2020-11-24 stsp if (err) {
6678 6458efa5 2020-11-24 stsp free(line);
6679 6458efa5 2020-11-24 stsp return err;
6681 6458efa5 2020-11-24 stsp if (n == s->selected) {
6682 6458efa5 2020-11-24 stsp if (view->focussed)
6683 6458efa5 2020-11-24 stsp wstandout(view->window);
6684 6458efa5 2020-11-24 stsp s->selected_entry = re;
6686 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6688 6458efa5 2020-11-24 stsp wattr_on(view->window,
6689 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6690 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6692 6458efa5 2020-11-24 stsp wattr_off(view->window,
6693 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6694 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6695 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6696 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6697 6458efa5 2020-11-24 stsp wstandend(view->window);
6698 6458efa5 2020-11-24 stsp free(line);
6699 6458efa5 2020-11-24 stsp free(wline);
6700 6458efa5 2020-11-24 stsp wline = NULL;
6702 6458efa5 2020-11-24 stsp s->ndisplayed++;
6703 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6706 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6709 6458efa5 2020-11-24 stsp view_vborder(view);
6710 6458efa5 2020-11-24 stsp return err;
6713 6458efa5 2020-11-24 stsp static const struct got_error *
6714 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6715 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6717 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6718 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6719 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6721 c42c9805 2020-11-24 stsp *new_view = NULL;
6723 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6724 c42c9805 2020-11-24 stsp if (err) {
6725 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6726 c42c9805 2020-11-24 stsp return err;
6728 c42c9805 2020-11-24 stsp return NULL;
6732 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6733 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6734 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6735 c42c9805 2020-11-24 stsp goto done;
6738 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6739 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6741 c42c9805 2020-11-24 stsp goto done;
6743 c42c9805 2020-11-24 stsp *new_view = tree_view;
6745 c42c9805 2020-11-24 stsp free(commit_id);
6746 c42c9805 2020-11-24 stsp return err;
6748 c42c9805 2020-11-24 stsp static const struct got_error *
6749 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6751 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6752 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6753 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6754 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6755 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
6757 6458efa5 2020-11-24 stsp switch (ch) {
6759 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6762 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
6765 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
6766 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6767 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
6768 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
6771 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
6772 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
6773 50617b77 2021-11-20 stsp &tog_refs, s->repo);
6776 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
6777 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
6779 6458efa5 2020-11-24 stsp case KEY_ENTER:
6780 6458efa5 2020-11-24 stsp case '\r':
6781 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6783 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6784 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6785 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6787 e78dc838 2020-12-04 stsp view->focussed = 0;
6788 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6789 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6790 6458efa5 2020-11-24 stsp err = view_close_child(view);
6792 6458efa5 2020-11-24 stsp return err;
6793 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6796 e78dc838 2020-12-04 stsp view->focus_child = 1;
6798 6458efa5 2020-11-24 stsp *new_view = log_view;
6801 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6803 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6804 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6805 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6807 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6809 e78dc838 2020-12-04 stsp view->focussed = 0;
6810 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6811 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6812 c42c9805 2020-11-24 stsp err = view_close_child(view);
6814 c42c9805 2020-11-24 stsp return err;
6815 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
6818 e78dc838 2020-12-04 stsp view->focus_child = 1;
6820 c42c9805 2020-11-24 stsp *new_view = tree_view;
6822 e4526bf5 2021-09-03 naddy case 'g':
6823 e4526bf5 2021-09-03 naddy case KEY_HOME:
6824 e4526bf5 2021-09-03 naddy s->selected = 0;
6825 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6827 e4526bf5 2021-09-03 naddy case 'G':
6828 e4526bf5 2021-09-03 naddy case KEY_END:
6829 e4526bf5 2021-09-03 naddy s->selected = 0;
6830 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6831 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6832 e4526bf5 2021-09-03 naddy if (re == NULL)
6834 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6835 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6837 e4526bf5 2021-09-03 naddy if (n > 0)
6838 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6841 6458efa5 2020-11-24 stsp case KEY_UP:
6842 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6843 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6844 6458efa5 2020-11-24 stsp s->selected--;
6847 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6849 83cc4199 2022-06-13 stsp case CTRL('u'):
6851 83cc4199 2022-06-13 stsp nscroll /= 2;
6852 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6853 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6854 6458efa5 2020-11-24 stsp case CTRL('b'):
6856 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6857 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
6858 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
6861 6458efa5 2020-11-24 stsp case KEY_DOWN:
6862 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6863 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6864 6458efa5 2020-11-24 stsp s->selected++;
6867 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6868 6458efa5 2020-11-24 stsp /* can't scroll any further */
6870 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6872 83cc4199 2022-06-13 stsp case CTRL('d'):
6874 83cc4199 2022-06-13 stsp nscroll /= 2;
6875 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6876 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6877 6458efa5 2020-11-24 stsp case CTRL('f'):
6879 48bb96f0 2022-06-20 naddy case ' ':
6880 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6881 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6882 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6883 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6884 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6887 83cc4199 2022-06-13 stsp ref_scroll_down(s, nscroll);
6889 6458efa5 2020-11-24 stsp case CTRL('l'):
6890 8924d611 2020-12-26 stsp tog_free_refs();
6891 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
6894 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6895 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6897 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6898 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6899 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6905 6458efa5 2020-11-24 stsp return err;
6908 6458efa5 2020-11-24 stsp __dead static void
6909 6458efa5 2020-11-24 stsp usage_ref(void)
6912 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6913 6458efa5 2020-11-24 stsp getprogname());
6917 6458efa5 2020-11-24 stsp static const struct got_error *
6918 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6920 6458efa5 2020-11-24 stsp const struct got_error *error;
6921 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6922 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6923 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6925 6458efa5 2020-11-24 stsp struct tog_view *view;
6926 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6928 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6929 6458efa5 2020-11-24 stsp switch (ch) {
6931 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6932 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6933 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6937 e99e2d15 2020-11-24 naddy usage_ref();
6938 6458efa5 2020-11-24 stsp /* NOTREACHED */
6942 6458efa5 2020-11-24 stsp argc -= optind;
6943 6458efa5 2020-11-24 stsp argv += optind;
6945 6458efa5 2020-11-24 stsp if (argc > 1)
6946 e99e2d15 2020-11-24 naddy usage_ref();
6948 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6949 0ae84acc 2022-06-15 tracey if (error != NULL)
6950 0ae84acc 2022-06-15 tracey goto done;
6952 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6953 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6954 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6955 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6956 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6957 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6958 c156c7a4 2020-12-18 stsp goto done;
6959 6458efa5 2020-11-24 stsp if (worktree)
6960 6458efa5 2020-11-24 stsp repo_path =
6961 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6963 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6964 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6965 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6966 c156c7a4 2020-12-18 stsp goto done;
6970 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6971 6458efa5 2020-11-24 stsp if (error != NULL)
6972 6458efa5 2020-11-24 stsp goto done;
6974 6458efa5 2020-11-24 stsp init_curses();
6976 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6977 51a10b52 2020-12-26 stsp if (error)
6978 51a10b52 2020-12-26 stsp goto done;
6980 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6981 6458efa5 2020-11-24 stsp if (error)
6982 6458efa5 2020-11-24 stsp goto done;
6984 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6985 6458efa5 2020-11-24 stsp if (view == NULL) {
6986 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6987 6458efa5 2020-11-24 stsp goto done;
6990 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6991 6458efa5 2020-11-24 stsp if (error)
6992 6458efa5 2020-11-24 stsp goto done;
6994 6458efa5 2020-11-24 stsp if (worktree) {
6995 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6996 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6997 6458efa5 2020-11-24 stsp worktree = NULL;
6999 6458efa5 2020-11-24 stsp error = view_loop(view);
7001 6458efa5 2020-11-24 stsp free(repo_path);
7002 6458efa5 2020-11-24 stsp free(cwd);
7003 1d0f4054 2021-06-17 stsp if (repo) {
7004 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7005 1d0f4054 2021-06-17 stsp if (close_err)
7006 1d0f4054 2021-06-17 stsp error = close_err;
7008 0ae84acc 2022-06-15 tracey if (pack_fds) {
7009 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7010 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7011 0ae84acc 2022-06-15 tracey if (error == NULL)
7012 0ae84acc 2022-06-15 tracey error = pack_err;
7014 51a10b52 2020-12-26 stsp tog_free_refs();
7015 6458efa5 2020-11-24 stsp return error;
7018 6458efa5 2020-11-24 stsp static void
7019 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7023 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7024 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7025 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7026 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7028 6879ba42 2020-10-01 naddy fputc('\n', fp);
7031 4ed7e80c 2018-05-20 stsp __dead static void
7032 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7034 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7036 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7037 6879ba42 2020-10-01 naddy getprogname());
7038 6879ba42 2020-10-01 naddy if (hflag) {
7039 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7040 6879ba42 2020-10-01 naddy list_commands(fp);
7042 6879ba42 2020-10-01 naddy exit(status);
7045 c2301be8 2018-04-30 stsp static char **
7046 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7048 ee85c5e8 2020-02-29 stsp va_list ap;
7049 c2301be8 2018-04-30 stsp char **argv;
7052 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7054 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7055 c2301be8 2018-04-30 stsp if (argv == NULL)
7056 c2301be8 2018-04-30 stsp err(1, "calloc");
7057 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7058 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7059 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7060 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7063 ee85c5e8 2020-02-29 stsp va_end(ap);
7064 c2301be8 2018-04-30 stsp return argv;
7068 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7069 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7070 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7071 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7072 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7074 ee85c5e8 2020-02-29 stsp static const struct got_error *
7075 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7077 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7078 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7079 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7080 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7081 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7082 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7083 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7084 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7085 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7087 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7088 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7089 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7091 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7092 0ae84acc 2022-06-15 tracey if (error != NULL)
7093 0ae84acc 2022-06-15 tracey goto done;
7095 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7096 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7097 ee85c5e8 2020-02-29 stsp goto done;
7099 ee85c5e8 2020-02-29 stsp if (worktree)
7100 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7102 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7103 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7104 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7105 ee85c5e8 2020-02-29 stsp goto done;
7108 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7109 ee85c5e8 2020-02-29 stsp if (error != NULL)
7110 ee85c5e8 2020-02-29 stsp goto done;
7112 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7113 ee85c5e8 2020-02-29 stsp repo, worktree);
7114 ee85c5e8 2020-02-29 stsp if (error)
7115 ee85c5e8 2020-02-29 stsp goto done;
7117 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7118 84de9106 2020-12-26 stsp if (error)
7119 84de9106 2020-12-26 stsp goto done;
7120 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7121 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7122 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7123 ee85c5e8 2020-02-29 stsp if (error)
7124 ee85c5e8 2020-02-29 stsp goto done;
7126 ee85c5e8 2020-02-29 stsp if (worktree) {
7127 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7128 ee85c5e8 2020-02-29 stsp worktree = NULL;
7131 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7132 a44927cc 2022-04-07 stsp if (error)
7133 a44927cc 2022-04-07 stsp goto done;
7135 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7136 ee85c5e8 2020-02-29 stsp if (error) {
7137 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7138 ee85c5e8 2020-02-29 stsp goto done;
7139 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7140 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7141 6879ba42 2020-10-01 naddy usage(1, 1);
7142 ee85c5e8 2020-02-29 stsp /* not reached */
7145 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7146 1d0f4054 2021-06-17 stsp if (error == NULL)
7147 1d0f4054 2021-06-17 stsp error = close_err;
7148 ee85c5e8 2020-02-29 stsp repo = NULL;
7150 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7151 ee85c5e8 2020-02-29 stsp if (error)
7152 ee85c5e8 2020-02-29 stsp goto done;
7154 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7156 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7157 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7159 1d0f4054 2021-06-17 stsp if (repo) {
7160 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7161 1d0f4054 2021-06-17 stsp if (error == NULL)
7162 1d0f4054 2021-06-17 stsp error = close_err;
7164 a44927cc 2022-04-07 stsp if (commit)
7165 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7166 ee85c5e8 2020-02-29 stsp if (worktree)
7167 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7168 0ae84acc 2022-06-15 tracey if (pack_fds) {
7169 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7170 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7171 0ae84acc 2022-06-15 tracey if (error == NULL)
7172 0ae84acc 2022-06-15 tracey error = pack_err;
7175 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7176 ee85c5e8 2020-02-29 stsp free(commit_id);
7177 ee85c5e8 2020-02-29 stsp free(cwd);
7178 ee85c5e8 2020-02-29 stsp free(repo_path);
7179 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7180 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7182 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7183 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7184 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7186 87670572 2020-12-26 naddy tog_free_refs();
7187 ee85c5e8 2020-02-29 stsp return error;
7191 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7193 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7194 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7195 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7196 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7197 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7198 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7199 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7202 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7204 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7205 9f7d7167 2018-04-29 stsp switch (ch) {
7207 9f7d7167 2018-04-29 stsp hflag = 1;
7210 53ccebc2 2019-07-30 stsp Vflag = 1;
7213 6879ba42 2020-10-01 naddy usage(hflag, 1);
7214 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7218 9f7d7167 2018-04-29 stsp argc -= optind;
7219 9f7d7167 2018-04-29 stsp argv += optind;
7220 9814e6a3 2020-09-27 naddy optind = 1;
7221 c2301be8 2018-04-30 stsp optreset = 1;
7223 53ccebc2 2019-07-30 stsp if (Vflag) {
7224 53ccebc2 2019-07-30 stsp got_version_print_str();
7225 6879ba42 2020-10-01 naddy return 0;
7228 4010e238 2020-12-04 stsp #ifndef PROFILE
7229 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7230 4010e238 2020-12-04 stsp NULL) == -1)
7231 4010e238 2020-12-04 stsp err(1, "pledge");
7234 c2301be8 2018-04-30 stsp if (argc == 0) {
7235 f29d3e89 2018-06-23 stsp if (hflag)
7236 6879ba42 2020-10-01 naddy usage(hflag, 0);
7237 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7238 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7240 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7244 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7245 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7246 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7247 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7248 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7254 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7255 ee85c5e8 2020-02-29 stsp if (argc != 1)
7256 6879ba42 2020-10-01 naddy usage(0, 1);
7257 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7258 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7260 ee85c5e8 2020-02-29 stsp if (hflag)
7261 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7263 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7267 b46c1e04 2020-09-20 naddy putchar('\n');
7268 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7270 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7271 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7272 a2f4a359 2020-02-28 stsp free(cmd_argv);
7275 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7276 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);