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 1a76625f 2018-10-22 stsp int log_complete;
342 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
343 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
344 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
345 13add988 2019-10-15 stsp int *searching;
346 13add988 2019-10-15 stsp int *search_next_done;
347 13add988 2019-10-15 stsp regex_t *regex;
350 1a76625f 2018-10-22 stsp struct tog_log_view_state {
351 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
352 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
353 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
354 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
355 b01e7d3b 2018-08-04 stsp int selected;
356 b01e7d3b 2018-08-04 stsp char *in_repo_path;
357 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
358 b672a97a 2020-01-27 stsp int log_branches;
359 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
360 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
361 1a76625f 2018-10-22 stsp sig_atomic_t quit;
362 1a76625f 2018-10-22 stsp pthread_t thread;
363 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
364 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
365 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
366 11b20872 2019-11-08 stsp struct tog_colors colors;
369 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
370 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
371 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
372 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
373 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
374 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
375 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
376 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
377 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
378 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
379 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
380 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
381 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
382 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
383 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
385 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
386 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
387 e9424729 2018-08-04 stsp int nlines;
389 e9424729 2018-08-04 stsp struct tog_view *view;
390 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
394 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
395 e9424729 2018-08-04 stsp const char *path;
396 e9424729 2018-08-04 stsp struct got_repository *repo;
397 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
398 e9424729 2018-08-04 stsp int *complete;
399 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
400 fc06ba56 2019-08-22 stsp void *cancel_arg;
403 e9424729 2018-08-04 stsp struct tog_blame {
405 be659d10 2020-11-18 stsp off_t filesize;
406 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
407 6fcac457 2018-11-19 stsp int nlines;
408 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
409 e9424729 2018-08-04 stsp pthread_t thread;
410 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
411 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
412 e9424729 2018-08-04 stsp const char *path;
415 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
416 7cbe629d 2018-08-04 stsp int first_displayed_line;
417 7cbe629d 2018-08-04 stsp int last_displayed_line;
418 7cbe629d 2018-08-04 stsp int selected_line;
419 7cbe629d 2018-08-04 stsp int blame_complete;
422 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
423 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
424 e5a0f69f 2018-08-18 stsp char *path;
425 7cbe629d 2018-08-04 stsp struct got_repository *repo;
426 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
427 e9424729 2018-08-04 stsp struct tog_blame blame;
428 6c4c42e0 2019-06-24 stsp int matched_line;
429 11b20872 2019-11-08 stsp struct tog_colors colors;
432 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
433 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
434 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
435 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
436 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
437 ad80ab7b 2018-08-04 stsp int selected;
440 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
442 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
443 ad80ab7b 2018-08-04 stsp char *tree_label;
444 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
445 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
446 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
447 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
448 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
449 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
450 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
451 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
452 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
453 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
454 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
455 6458efa5 2020-11-24 stsp struct tog_colors colors;
458 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
459 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
460 6458efa5 2020-11-24 stsp struct got_reference *ref;
464 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
466 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
467 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
468 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
469 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
471 7f66531d 2021-11-16 stsp int nrefs, ndisplayed, selected, show_ids, sort_by_date;
472 6458efa5 2020-11-24 stsp struct got_repository *repo;
473 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
474 bddb1296 2019-11-08 stsp struct tog_colors colors;
478 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
480 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
481 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
482 669b5ffa 2018-10-07 stsp * there is enough screen estate.
484 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
485 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
487 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
488 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
489 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
491 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
492 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
494 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
496 cc3c9aac 2018-08-01 stsp struct tog_view {
497 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
498 26ed57b2 2018-05-19 stsp WINDOW *window;
499 26ed57b2 2018-05-19 stsp PANEL *panel;
500 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
501 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
502 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
504 669b5ffa 2018-10-07 stsp struct tog_view *parent;
505 669b5ffa 2018-10-07 stsp struct tog_view *child;
508 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
509 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
510 e78dc838 2020-12-04 stsp * between parent and child.
511 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
512 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
513 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
514 e78dc838 2020-12-04 stsp * situations.
516 e78dc838 2020-12-04 stsp int focus_child;
518 5dc9f4bc 2018-08-04 stsp /* type-specific state */
519 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
521 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
522 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
523 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
524 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
525 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
528 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
529 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
530 e78dc838 2020-12-04 stsp struct tog_view *, int);
531 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
533 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
534 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
535 c0c4acc8 2021-01-24 stsp int search_started;
536 60493ae3 2019-06-20 stsp int searching;
537 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
538 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
539 60493ae3 2019-06-20 stsp int search_next_done;
540 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
541 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
542 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
543 1803e47f 2019-06-22 stsp regex_t regex;
544 41605754 2020-11-12 stsp regmatch_t regmatch;
547 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
548 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
549 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
550 78756c87 2020-11-24 stsp struct got_repository *);
551 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
552 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
553 e78dc838 2020-12-04 stsp struct tog_view *, int);
554 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
555 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
556 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
558 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
559 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
560 78756c87 2020-11-24 stsp const char *, const char *, int);
561 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
562 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
563 e78dc838 2020-12-04 stsp struct tog_view *, int);
564 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
565 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
566 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
568 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
569 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
570 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
572 e78dc838 2020-12-04 stsp struct tog_view *, int);
573 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
574 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
575 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
577 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
578 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
579 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
583 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
584 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
586 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
587 6458efa5 2020-11-24 stsp struct got_repository *);
588 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
589 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
592 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
593 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
595 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
596 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
597 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
598 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
599 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
601 25791caa 2018-10-24 stsp static void
602 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
604 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
607 83baff54 2019-08-12 stsp static void
608 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
610 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
613 61266923 2020-01-14 stsp static void
614 61266923 2020-01-14 stsp tog_sigcont(int signo)
616 61266923 2020-01-14 stsp tog_sigcont_received = 1;
619 2497f032 2022-05-31 stsp static void
620 2497f032 2022-05-31 stsp tog_sigint(int signo)
622 2497f032 2022-05-31 stsp tog_sigint_received = 1;
625 2497f032 2022-05-31 stsp static void
626 2497f032 2022-05-31 stsp tog_sigterm(int signo)
628 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
632 2497f032 2022-05-31 stsp tog_fatal_signal_received()
634 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
635 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
639 e5a0f69f 2018-08-18 stsp static const struct got_error *
640 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
642 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
644 669b5ffa 2018-10-07 stsp if (view->child) {
645 669b5ffa 2018-10-07 stsp view_close(view->child);
646 669b5ffa 2018-10-07 stsp view->child = NULL;
648 e5a0f69f 2018-08-18 stsp if (view->close)
649 e5a0f69f 2018-08-18 stsp err = view->close(view);
650 ea5e7bb5 2018-08-01 stsp if (view->panel)
651 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
652 ea5e7bb5 2018-08-01 stsp if (view->window)
653 ea5e7bb5 2018-08-01 stsp delwin(view->window);
654 ea5e7bb5 2018-08-01 stsp free(view);
655 e5a0f69f 2018-08-18 stsp return err;
658 ea5e7bb5 2018-08-01 stsp static struct tog_view *
659 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
660 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
662 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
664 ea5e7bb5 2018-08-01 stsp if (view == NULL)
665 ea5e7bb5 2018-08-01 stsp return NULL;
667 d6b05b5b 2018-08-04 stsp view->type = type;
668 f7d12f7e 2018-08-01 stsp view->lines = LINES;
669 f7d12f7e 2018-08-01 stsp view->cols = COLS;
670 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
671 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
672 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
673 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
674 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
675 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
676 96a765a8 2018-08-04 stsp view_close(view);
677 ea5e7bb5 2018-08-01 stsp return NULL;
679 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
680 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
681 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
682 96a765a8 2018-08-04 stsp view_close(view);
683 ea5e7bb5 2018-08-01 stsp return NULL;
686 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
687 ea5e7bb5 2018-08-01 stsp return view;
691 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
693 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
695 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
698 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
700 5c60c32a 2018-10-18 stsp static const struct got_error *
701 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
703 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
705 5c60c32a 2018-10-18 stsp view->begin_y = 0;
706 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
707 5c60c32a 2018-10-18 stsp view->nlines = LINES;
708 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
709 5c60c32a 2018-10-18 stsp view->lines = LINES;
710 5c60c32a 2018-10-18 stsp view->cols = COLS;
711 5c60c32a 2018-10-18 stsp err = view_resize(view);
713 5c60c32a 2018-10-18 stsp return err;
715 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
716 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
718 5c60c32a 2018-10-18 stsp return NULL;
721 5c60c32a 2018-10-18 stsp static const struct got_error *
722 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
724 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
726 5c60c32a 2018-10-18 stsp view->begin_x = 0;
727 5c60c32a 2018-10-18 stsp view->begin_y = 0;
728 5c60c32a 2018-10-18 stsp view->nlines = LINES;
729 5c60c32a 2018-10-18 stsp view->ncols = COLS;
730 5c60c32a 2018-10-18 stsp view->lines = LINES;
731 5c60c32a 2018-10-18 stsp view->cols = COLS;
732 5c60c32a 2018-10-18 stsp err = view_resize(view);
734 5c60c32a 2018-10-18 stsp return err;
736 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
737 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
739 5c60c32a 2018-10-18 stsp return NULL;
743 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
745 5c60c32a 2018-10-18 stsp return view->parent == NULL;
748 4d8c2215 2018-08-19 stsp static const struct got_error *
749 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
751 f7d12f7e 2018-08-01 stsp int nlines, ncols;
753 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
754 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
756 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
758 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
759 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
761 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
763 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
764 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
765 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
766 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
767 25791caa 2018-10-24 stsp wclear(view->window);
769 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
770 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
771 0cf4efb1 2018-09-29 stsp view->lines = LINES;
772 0cf4efb1 2018-09-29 stsp view->cols = COLS;
774 6e3e5d9c 2018-10-18 stsp if (view->child) {
775 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
776 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
777 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
778 5c60c32a 2018-10-18 stsp if (view->child->focussed)
779 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
781 5c60c32a 2018-10-18 stsp show_panel(view->panel);
783 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
784 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
788 5c60c32a 2018-10-18 stsp return NULL;
791 669b5ffa 2018-10-07 stsp static const struct got_error *
792 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
794 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
796 669b5ffa 2018-10-07 stsp if (view->child == NULL)
797 669b5ffa 2018-10-07 stsp return NULL;
799 669b5ffa 2018-10-07 stsp err = view_close(view->child);
800 669b5ffa 2018-10-07 stsp view->child = NULL;
801 669b5ffa 2018-10-07 stsp return err;
804 72a9cb46 2020-12-03 stsp static void
805 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
807 669b5ffa 2018-10-07 stsp view->child = child;
808 669b5ffa 2018-10-07 stsp child->parent = view;
812 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
814 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
817 34bc9ec9 2019-02-22 stsp static void
818 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
820 25791caa 2018-10-24 stsp int cols, lines;
821 25791caa 2018-10-24 stsp struct winsize size;
823 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
824 25791caa 2018-10-24 stsp cols = 80; /* Default */
825 25791caa 2018-10-24 stsp lines = 24;
827 25791caa 2018-10-24 stsp cols = size.ws_col;
828 25791caa 2018-10-24 stsp lines = size.ws_row;
830 25791caa 2018-10-24 stsp resize_term(lines, cols);
833 2b49a8ae 2019-06-22 stsp static const struct got_error *
834 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
836 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
837 2b49a8ae 2019-06-22 stsp char pattern[1024];
840 c0c4acc8 2021-01-24 stsp if (view->search_started) {
841 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
842 c0c4acc8 2021-01-24 stsp view->searching = 0;
843 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
845 c0c4acc8 2021-01-24 stsp view->search_started = 0;
847 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
848 2b49a8ae 2019-06-22 stsp return NULL;
850 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
851 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
853 2b49a8ae 2019-06-22 stsp nocbreak();
855 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
858 2b49a8ae 2019-06-22 stsp if (ret == ERR)
859 2b49a8ae 2019-06-22 stsp return NULL;
861 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
862 7c32bd05 2019-06-22 stsp err = view->search_start(view);
864 7c32bd05 2019-06-22 stsp regfree(&view->regex);
865 7c32bd05 2019-06-22 stsp return err;
867 c0c4acc8 2021-01-24 stsp view->search_started = 1;
868 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
869 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
870 2b49a8ae 2019-06-22 stsp view->search_next(view);
873 2b49a8ae 2019-06-22 stsp return NULL;
876 0cf4efb1 2018-09-29 stsp static const struct got_error *
877 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
878 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
880 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
881 669b5ffa 2018-10-07 stsp struct tog_view *v;
882 1a76625f 2018-10-22 stsp int ch, errcode;
884 e5a0f69f 2018-08-18 stsp *new = NULL;
886 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
887 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
888 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
889 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
891 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
892 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
893 82954512 2020-02-03 stsp if (errcode)
894 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
895 82954512 2020-02-03 stsp "pthread_mutex_unlock");
896 3da8ef85 2021-09-21 stsp sched_yield();
897 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
898 82954512 2020-02-03 stsp if (errcode)
899 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
900 82954512 2020-02-03 stsp "pthread_mutex_lock");
901 60493ae3 2019-06-20 stsp view->search_next(view);
902 60493ae3 2019-06-20 stsp return NULL;
905 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
906 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
907 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
908 1a76625f 2018-10-22 stsp if (errcode)
909 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
910 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
911 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
912 1a76625f 2018-10-22 stsp if (errcode)
913 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
914 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
916 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
917 25791caa 2018-10-24 stsp tog_resizeterm();
918 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
919 61266923 2020-01-14 stsp tog_sigcont_received = 0;
920 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
921 25791caa 2018-10-24 stsp err = view_resize(v);
923 25791caa 2018-10-24 stsp return err;
924 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
926 25791caa 2018-10-24 stsp return err;
927 cdfcfb03 2020-12-06 stsp if (v->child) {
928 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
930 cdfcfb03 2020-12-06 stsp return err;
931 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
932 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
934 cdfcfb03 2020-12-06 stsp return err;
939 e5a0f69f 2018-08-18 stsp switch (ch) {
941 1e37a5c2 2019-05-12 jcs if (view->child) {
942 e78dc838 2020-12-04 stsp view->focussed = 0;
943 e78dc838 2020-12-04 stsp view->child->focussed = 1;
944 e78dc838 2020-12-04 stsp view->focus_child = 1;
945 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
946 e78dc838 2020-12-04 stsp view->focussed = 0;
947 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
948 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
952 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
953 9970f7fc 2020-12-03 stsp view->dying = 1;
959 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
960 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
962 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
963 e78dc838 2020-12-04 stsp view->focussed = 0;
964 e78dc838 2020-12-04 stsp view->child->focussed = 1;
965 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
967 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
970 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
971 9970f7fc 2020-12-03 stsp KEY_RESIZE);
973 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
974 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
975 e78dc838 2020-12-04 stsp view->focussed = 1;
976 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
978 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
982 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
985 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
988 60493ae3 2019-06-20 stsp if (view->search_start)
989 2b49a8ae 2019-06-22 stsp view_search_start(view);
991 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
995 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
996 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
997 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
998 60493ae3 2019-06-20 stsp view->search_next_done = 0;
999 60493ae3 2019-06-20 stsp view->search_next(view);
1001 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1004 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1008 e5a0f69f 2018-08-18 stsp return err;
1012 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1014 0cf4efb1 2018-09-29 stsp PANEL *panel;
1015 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1017 669b5ffa 2018-10-07 stsp if (view->parent)
1018 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1020 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1021 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1024 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1025 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1026 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1030 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1032 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1033 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1035 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1037 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1040 669b5ffa 2018-10-07 stsp return view->focussed;
1043 bcbd79e2 2018-08-19 stsp static const struct got_error *
1044 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1046 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1047 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1048 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1049 fd823528 2018-10-22 stsp int fast_refresh = 10;
1050 1a76625f 2018-10-22 stsp int done = 0, errcode;
1052 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1053 1a76625f 2018-10-22 stsp if (errcode)
1054 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1056 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1057 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1059 1004088d 2018-09-29 stsp view->focussed = 1;
1060 878940b7 2018-09-29 stsp err = view->show(view);
1062 0cf4efb1 2018-09-29 stsp return err;
1063 0cf4efb1 2018-09-29 stsp update_panels();
1064 0cf4efb1 2018-09-29 stsp doupdate();
1065 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1066 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1067 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1068 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1070 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1073 9970f7fc 2020-12-03 stsp if (view->dying) {
1074 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1076 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1077 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1079 e78dc838 2020-12-04 stsp else if (view->parent)
1080 669b5ffa 2018-10-07 stsp prev = view->parent;
1082 e78dc838 2020-12-04 stsp if (view->parent) {
1083 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1084 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1086 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1088 9970f7fc 2020-12-03 stsp err = view_close(view);
1090 e5a0f69f 2018-08-18 stsp goto done;
1092 e78dc838 2020-12-04 stsp view = NULL;
1093 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1094 e78dc838 2020-12-04 stsp if (v->focussed)
1097 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1098 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1100 e78dc838 2020-12-04 stsp view = prev;
1101 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1102 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1103 e78dc838 2020-12-04 stsp tog_view_list_head);
1105 e78dc838 2020-12-04 stsp if (view) {
1106 e78dc838 2020-12-04 stsp if (view->focus_child) {
1107 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1108 e78dc838 2020-12-04 stsp view = view->child;
1110 e78dc838 2020-12-04 stsp view->focussed = 1;
1114 bcbd79e2 2018-08-19 stsp if (new_view) {
1115 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1116 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1117 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1118 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1120 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1121 86c66b02 2018-10-18 stsp err = view_close(v);
1123 86c66b02 2018-10-18 stsp goto done;
1126 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1127 fed7eaa8 2018-10-24 stsp view = new_view;
1129 669b5ffa 2018-10-07 stsp if (view) {
1130 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1131 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1132 e78dc838 2020-12-04 stsp view = view->child;
1134 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1135 e78dc838 2020-12-04 stsp view = view->parent;
1137 e78dc838 2020-12-04 stsp show_panel(view->panel);
1138 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1139 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1140 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1141 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1143 1a76625f 2018-10-22 stsp goto done;
1145 669b5ffa 2018-10-07 stsp err = view->show(view);
1147 1a76625f 2018-10-22 stsp goto done;
1148 669b5ffa 2018-10-07 stsp if (view->child) {
1149 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1151 1a76625f 2018-10-22 stsp goto done;
1153 1a76625f 2018-10-22 stsp update_panels();
1154 1a76625f 2018-10-22 stsp doupdate();
1158 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1159 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1160 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1161 e5a0f69f 2018-08-18 stsp view_close(view);
1164 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1165 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1166 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1168 e5a0f69f 2018-08-18 stsp return err;
1171 4ed7e80c 2018-05-20 stsp __dead static void
1172 9f7d7167 2018-04-29 stsp usage_log(void)
1175 c70c5802 2018-08-01 stsp fprintf(stderr,
1176 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1177 9f7d7167 2018-04-29 stsp getprogname());
1181 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1182 80ddbec8 2018-04-29 stsp static const struct got_error *
1183 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1185 00dfcb92 2018-06-11 stsp char *vis = NULL;
1186 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1188 963b370f 2018-05-20 stsp *ws = NULL;
1189 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1190 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1191 00dfcb92 2018-06-11 stsp int vislen;
1192 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1193 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1195 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1196 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1198 00dfcb92 2018-06-11 stsp return err;
1199 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1200 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1201 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1202 a7f50699 2018-06-11 stsp goto done;
1206 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1207 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1208 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1209 a7f50699 2018-06-11 stsp goto done;
1212 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1213 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1215 00dfcb92 2018-06-11 stsp free(vis);
1216 963b370f 2018-05-20 stsp if (err) {
1217 963b370f 2018-05-20 stsp free(*ws);
1218 963b370f 2018-05-20 stsp *ws = NULL;
1219 963b370f 2018-05-20 stsp *wlen = 0;
1221 963b370f 2018-05-20 stsp return err;
1224 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1225 963b370f 2018-05-20 stsp static const struct got_error *
1226 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1227 27a741e5 2019-09-11 stsp int col_tab_align)
1229 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1230 963b370f 2018-05-20 stsp int cols = 0;
1231 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1232 963b370f 2018-05-20 stsp size_t wlen;
1235 963b370f 2018-05-20 stsp *wlinep = NULL;
1236 b700b5d6 2018-07-10 stsp *widthp = 0;
1238 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1240 963b370f 2018-05-20 stsp return err;
1242 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1243 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1246 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1247 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1252 27a741e5 2019-09-11 stsp while (i < wlen) {
1253 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1255 27a741e5 2019-09-11 stsp if (width == 0) {
1260 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1261 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1263 27a741e5 2019-09-11 stsp cols += width;
1265 27a741e5 2019-09-11 stsp } else if (width == -1) {
1266 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1267 27a741e5 2019-09-11 stsp width = TABSIZE -
1268 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1270 748d5cab 2020-12-14 naddy width = 1;
1271 748d5cab 2020-12-14 naddy wline[i] = L'.';
1273 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1275 748d5cab 2020-12-14 naddy cols += width;
1278 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1279 963b370f 2018-05-20 stsp goto done;
1282 963b370f 2018-05-20 stsp wline[i] = L'\0';
1283 b700b5d6 2018-07-10 stsp if (widthp)
1284 b700b5d6 2018-07-10 stsp *widthp = cols;
1287 963b370f 2018-05-20 stsp free(wline);
1289 963b370f 2018-05-20 stsp *wlinep = wline;
1290 963b370f 2018-05-20 stsp return err;
1293 8b473291 2019-02-21 stsp static const struct got_error*
1294 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1295 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1297 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1298 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1300 8b473291 2019-02-21 stsp const char *name;
1302 8b473291 2019-02-21 stsp *refs_str = NULL;
1304 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1305 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1306 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1309 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1310 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1312 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1313 8b473291 2019-02-21 stsp name += 5;
1314 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1315 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1317 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1318 8b473291 2019-02-21 stsp name += 6;
1319 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1320 8b473291 2019-02-21 stsp name += 8;
1321 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1322 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1325 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1328 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1329 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1330 5d844a1e 2019-08-13 stsp if (err) {
1331 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1332 48cae60d 2020-09-22 stsp free(ref_id);
1335 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1336 5d844a1e 2019-08-13 stsp err = NULL;
1337 5d844a1e 2019-08-13 stsp tag = NULL;
1340 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1341 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1342 48cae60d 2020-09-22 stsp free(ref_id);
1344 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1345 52b5abe1 2019-08-13 stsp if (cmp != 0)
1347 8b473291 2019-02-21 stsp s = *refs_str;
1348 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1349 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1350 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1352 8b473291 2019-02-21 stsp *refs_str = NULL;
1358 8b473291 2019-02-21 stsp return err;
1361 963b370f 2018-05-20 stsp static const struct got_error *
1362 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1363 27a741e5 2019-09-11 stsp int col_tab_align)
1365 e6b8b890 2020-12-29 naddy char *smallerthan;
1367 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1368 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1369 5813d178 2019-03-09 stsp author = smallerthan + 1;
1370 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1371 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1374 5813d178 2019-03-09 stsp static const struct got_error *
1375 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1376 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1377 8fdc79fe 2020-12-01 naddy int author_display_cols)
1379 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1380 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1381 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1382 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1383 5813d178 2019-03-09 stsp char *author = NULL;
1384 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1385 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1386 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1387 bb737323 2018-05-20 stsp int col, limit;
1388 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1389 ccb26ccd 2018-11-05 stsp struct tm tm;
1390 45d799e2 2018-12-23 stsp time_t committer_time;
1391 11b20872 2019-11-08 stsp struct tog_color *tc;
1393 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1394 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1395 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1396 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1397 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1399 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1400 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1402 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1403 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1405 11b20872 2019-11-08 stsp wattr_on(view->window,
1406 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1407 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1409 11b20872 2019-11-08 stsp wattr_off(view->window,
1410 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1411 27a741e5 2019-09-11 stsp col = limit;
1412 b39d25c7 2018-07-10 stsp if (col > avail)
1413 b39d25c7 2018-07-10 stsp goto done;
1415 6570a66d 2019-11-08 stsp if (avail >= 120) {
1416 6570a66d 2019-11-08 stsp char *id_str;
1417 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1419 6570a66d 2019-11-08 stsp goto done;
1420 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1422 11b20872 2019-11-08 stsp wattr_on(view->window,
1423 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1424 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1426 11b20872 2019-11-08 stsp wattr_off(view->window,
1427 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1428 6570a66d 2019-11-08 stsp free(id_str);
1430 6570a66d 2019-11-08 stsp if (col > avail)
1431 6570a66d 2019-11-08 stsp goto done;
1434 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1435 5813d178 2019-03-09 stsp if (author == NULL) {
1436 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1437 80ddbec8 2018-04-29 stsp goto done;
1439 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1441 bb737323 2018-05-20 stsp goto done;
1442 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1444 11b20872 2019-11-08 stsp wattr_on(view->window,
1445 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1446 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1448 11b20872 2019-11-08 stsp wattr_off(view->window,
1449 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1450 bb737323 2018-05-20 stsp col += author_width;
1451 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1452 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1454 bb737323 2018-05-20 stsp author_width++;
1456 9c2eaf34 2018-05-20 stsp if (col > avail)
1457 9c2eaf34 2018-05-20 stsp goto done;
1459 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1461 6d9fbc00 2018-04-29 stsp goto done;
1462 bb737323 2018-05-20 stsp logmsg = logmsg0;
1463 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1465 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1466 bb737323 2018-05-20 stsp if (newline)
1467 bb737323 2018-05-20 stsp *newline = '\0';
1468 bb737323 2018-05-20 stsp limit = avail - col;
1469 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1471 bb737323 2018-05-20 stsp goto done;
1472 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1473 bb737323 2018-05-20 stsp col += logmsg_width;
1474 27a741e5 2019-09-11 stsp while (col < avail) {
1475 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1479 80ddbec8 2018-04-29 stsp free(logmsg0);
1480 bb737323 2018-05-20 stsp free(wlogmsg);
1481 5813d178 2019-03-09 stsp free(author);
1482 bb737323 2018-05-20 stsp free(wauthor);
1483 80ddbec8 2018-04-29 stsp free(line);
1484 80ddbec8 2018-04-29 stsp return err;
1487 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1488 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1489 899d86c2 2018-05-10 stsp struct got_object_id *id)
1491 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1493 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1494 80ddbec8 2018-04-29 stsp if (entry == NULL)
1495 899d86c2 2018-05-10 stsp return NULL;
1497 899d86c2 2018-05-10 stsp entry->id = id;
1498 99db9666 2018-05-07 stsp entry->commit = commit;
1499 899d86c2 2018-05-10 stsp return entry;
1502 99db9666 2018-05-07 stsp static void
1503 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1505 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1507 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1508 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1509 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1510 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1511 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1512 99db9666 2018-05-07 stsp free(entry);
1515 99db9666 2018-05-07 stsp static void
1516 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1518 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1519 99db9666 2018-05-07 stsp pop_commit(commits);
1522 c4972b91 2018-05-07 stsp static const struct got_error *
1523 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1524 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1526 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1527 13add988 2019-10-15 stsp regmatch_t regmatch;
1528 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1530 13add988 2019-10-15 stsp *have_match = 0;
1532 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1534 13add988 2019-10-15 stsp return err;
1536 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1538 13add988 2019-10-15 stsp goto done;
1540 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1541 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1542 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1543 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1544 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1545 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1546 13add988 2019-10-15 stsp *have_match = 1;
1548 13add988 2019-10-15 stsp free(id_str);
1549 13add988 2019-10-15 stsp free(logmsg);
1550 13add988 2019-10-15 stsp return err;
1553 13add988 2019-10-15 stsp static const struct got_error *
1554 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1556 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1559 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1560 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1561 1a76625f 2018-10-22 stsp * while updating the display.
1564 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1565 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1566 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1567 1a76625f 2018-10-22 stsp int errcode;
1569 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1570 4e0d2870 2020-12-07 naddy NULL, NULL);
1571 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1574 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1577 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1578 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1579 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1583 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1584 1a76625f 2018-10-22 stsp if (errcode) {
1585 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1586 13add988 2019-10-15 stsp "pthread_mutex_lock");
1590 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1591 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1592 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1594 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1595 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1596 7c1452c1 2020-03-26 stsp int have_match;
1597 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1600 7c1452c1 2020-03-26 stsp if (have_match)
1601 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1604 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1605 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1606 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1607 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1610 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1612 9ba79e04 2018-06-11 stsp return err;
1615 2b779855 2020-12-05 naddy static void
1616 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1618 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1619 2b779855 2020-12-05 naddy int ncommits = 0;
1621 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1622 2b779855 2020-12-05 naddy while (entry) {
1623 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1624 2b779855 2020-12-05 naddy s->selected_entry = entry;
1627 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1628 2b779855 2020-12-05 naddy ncommits++;
1632 0553a4e3 2018-04-30 stsp static const struct got_error *
1633 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1635 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1636 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1637 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1638 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1639 60493ae3 2019-06-20 stsp int width;
1640 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1641 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1642 8b473291 2019-02-21 stsp char *refs_str = NULL;
1643 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1644 11b20872 2019-11-08 stsp struct tog_color *tc;
1645 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1647 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1648 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1649 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1650 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1652 ecb28ae0 2018-07-16 stsp return err;
1653 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1654 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1655 d2075bf3 2020-12-25 stsp if (refs) {
1656 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1657 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1659 d2075bf3 2020-12-25 stsp goto done;
1663 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1664 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1666 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1667 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1668 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1669 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1670 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1671 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1672 8f4ed634 2020-03-26 stsp goto done;
1675 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1677 f9686aa5 2020-03-27 stsp if (view->searching) {
1678 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1679 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1680 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1681 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1682 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1683 f9686aa5 2020-03-27 stsp search_str = "searching...";
1686 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1687 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1688 f9686aa5 2020-03-27 stsp search_str ? search_str :
1689 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1690 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1691 8f4ed634 2020-03-26 stsp goto done;
1695 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1696 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1697 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1698 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1699 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1700 1a76625f 2018-10-22 stsp header = NULL;
1701 1a76625f 2018-10-22 stsp goto done;
1703 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1704 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1705 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1706 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1707 1a76625f 2018-10-22 stsp header = NULL;
1708 1a76625f 2018-10-22 stsp goto done;
1710 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1712 1a76625f 2018-10-22 stsp goto done;
1714 2814baeb 2018-08-01 stsp werase(view->window);
1716 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1717 a3404814 2018-09-02 stsp wstandout(view->window);
1718 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1720 11b20872 2019-11-08 stsp wattr_on(view->window,
1721 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1722 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1724 11b20872 2019-11-08 stsp wattr_off(view->window,
1725 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1726 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1727 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1730 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1731 a3404814 2018-09-02 stsp wstandend(view->window);
1732 ecb28ae0 2018-07-16 stsp free(wline);
1733 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1734 1a76625f 2018-10-22 stsp goto done;
1736 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1737 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1738 5813d178 2019-03-09 stsp ncommits = 0;
1739 5813d178 2019-03-09 stsp while (entry) {
1740 5813d178 2019-03-09 stsp char *author;
1741 5813d178 2019-03-09 stsp wchar_t *wauthor;
1742 5813d178 2019-03-09 stsp int width;
1743 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1745 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1746 5813d178 2019-03-09 stsp if (author == NULL) {
1747 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1748 5813d178 2019-03-09 stsp goto done;
1750 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1751 27a741e5 2019-09-11 stsp date_display_cols);
1752 5813d178 2019-03-09 stsp if (author_cols < width)
1753 5813d178 2019-03-09 stsp author_cols = width;
1754 5813d178 2019-03-09 stsp free(wauthor);
1755 5813d178 2019-03-09 stsp free(author);
1756 7ca04879 2019-10-19 stsp ncommits++;
1757 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1760 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1761 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1762 867c6645 2018-07-10 stsp ncommits = 0;
1763 899d86c2 2018-05-10 stsp while (entry) {
1764 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1766 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1767 2814baeb 2018-08-01 stsp wstandout(view->window);
1768 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1769 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1770 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1771 2814baeb 2018-08-01 stsp wstandend(view->window);
1773 60493ae3 2019-06-20 stsp goto done;
1774 0553a4e3 2018-04-30 stsp ncommits++;
1775 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1776 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1779 1a57306a 2018-09-02 stsp view_vborder(view);
1781 1a76625f 2018-10-22 stsp free(id_str);
1782 8b473291 2019-02-21 stsp free(refs_str);
1783 1a76625f 2018-10-22 stsp free(ncommits_str);
1784 1a76625f 2018-10-22 stsp free(header);
1785 80ddbec8 2018-04-29 stsp return err;
1788 07b55e75 2018-05-10 stsp static void
1789 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1791 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1792 07b55e75 2018-05-10 stsp int nscrolled = 0;
1794 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1795 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1798 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1799 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1800 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1801 07b55e75 2018-05-10 stsp if (entry) {
1802 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1803 07b55e75 2018-05-10 stsp nscrolled++;
1808 aa075928 2018-05-10 stsp static const struct got_error *
1809 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1811 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1812 5e224a3e 2019-02-22 stsp int errcode;
1814 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1816 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1817 ffe38506 2020-12-01 naddy if (ta->log_complete)
1820 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1821 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1822 7aafa0d1 2019-02-22 stsp if (errcode)
1823 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1824 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1827 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1828 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1830 7c1452c1 2020-03-26 stsp if (!wait)
1833 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1834 ffe38506 2020-12-01 naddy show_log_view(view);
1835 7c1452c1 2020-03-26 stsp update_panels();
1836 7c1452c1 2020-03-26 stsp doupdate();
1838 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1839 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1840 82954512 2020-02-03 stsp if (errcode)
1841 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1842 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1844 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1845 ffe38506 2020-12-01 naddy show_log_view(view);
1846 7c1452c1 2020-03-26 stsp update_panels();
1847 7c1452c1 2020-03-26 stsp doupdate();
1850 5e224a3e 2019-02-22 stsp return NULL;
1853 5e224a3e 2019-02-22 stsp static const struct got_error *
1854 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1856 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1857 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1858 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1859 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1861 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1862 5e224a3e 2019-02-22 stsp return NULL;
1864 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1865 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1866 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1868 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1870 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1871 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1873 5e224a3e 2019-02-22 stsp return err;
1877 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1878 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1881 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1883 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1884 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1886 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1887 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1889 dd0a52c1 2018-05-20 stsp return err;
1892 cd0acaa7 2018-05-20 stsp static const struct got_error *
1893 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1894 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1895 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1897 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1898 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1899 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1901 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1902 15a94983 2018-12-23 stsp if (diff_view == NULL)
1903 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1905 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1906 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1907 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1908 e5a0f69f 2018-08-18 stsp if (err == NULL)
1909 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1910 cd0acaa7 2018-05-20 stsp return err;
1913 80ddbec8 2018-04-29 stsp static const struct got_error *
1914 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1915 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1917 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1919 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1920 941e9f74 2019-05-21 stsp if (parent == NULL)
1921 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1923 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1924 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1925 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1926 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1927 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1928 941e9f74 2019-05-21 stsp s->tree = subtree;
1929 941e9f74 2019-05-21 stsp s->selected = 0;
1930 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1931 941e9f74 2019-05-21 stsp return NULL;
1934 941e9f74 2019-05-21 stsp static const struct got_error *
1935 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1936 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
1938 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1939 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1940 941e9f74 2019-05-21 stsp const char *p;
1941 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1943 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1945 941e9f74 2019-05-21 stsp while (*p) {
1946 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1947 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1948 56e0773d 2019-11-28 stsp char *te_name;
1950 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1953 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1954 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1955 941e9f74 2019-05-21 stsp if (slash == NULL)
1956 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1958 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1959 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1960 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1963 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1964 56e0773d 2019-11-28 stsp if (te == NULL) {
1965 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1966 56e0773d 2019-11-28 stsp free(te_name);
1969 56e0773d 2019-11-28 stsp free(te_name);
1970 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1972 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1973 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1975 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1976 941e9f74 2019-05-21 stsp if (slash)
1977 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1979 941e9f74 2019-05-21 stsp subpath = strdup(path);
1980 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1981 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1985 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
1990 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1991 941e9f74 2019-05-21 stsp free(tree_id);
1995 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1996 941e9f74 2019-05-21 stsp if (err) {
1997 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2000 941e9f74 2019-05-21 stsp if (slash == NULL)
2002 941e9f74 2019-05-21 stsp free(subpath);
2003 941e9f74 2019-05-21 stsp subpath = NULL;
2004 941e9f74 2019-05-21 stsp p = slash;
2007 941e9f74 2019-05-21 stsp free(subpath);
2008 1a76625f 2018-10-22 stsp return err;
2011 61266923 2020-01-14 stsp static const struct got_error *
2012 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2013 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2014 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2016 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2017 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2018 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2020 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2021 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2022 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2024 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2026 55cccc34 2020-02-20 stsp return err;
2027 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2029 55cccc34 2020-02-20 stsp *new_view = tree_view;
2031 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2032 55cccc34 2020-02-20 stsp return NULL;
2034 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2037 55cccc34 2020-02-20 stsp static const struct got_error *
2038 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2040 61266923 2020-01-14 stsp sigset_t sigset;
2041 61266923 2020-01-14 stsp int errcode;
2043 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2044 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2046 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2047 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2048 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2049 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2050 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2051 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2052 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2053 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2054 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2056 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2057 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2058 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2060 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2061 61266923 2020-01-14 stsp if (errcode)
2062 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2064 61266923 2020-01-14 stsp return NULL;
2067 1a76625f 2018-10-22 stsp static void *
2068 1a76625f 2018-10-22 stsp log_thread(void *arg)
2070 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2071 1a76625f 2018-10-22 stsp int errcode = 0;
2072 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2073 1a76625f 2018-10-22 stsp int done = 0;
2075 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2077 61266923 2020-01-14 stsp return (void *)err;
2079 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2080 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2081 1a76625f 2018-10-22 stsp if (err) {
2082 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2083 1a76625f 2018-10-22 stsp return (void *)err;
2084 1a76625f 2018-10-22 stsp err = NULL;
2086 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2087 1a76625f 2018-10-22 stsp a->commits_needed--;
2089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2090 3abe8080 2019-04-10 stsp if (errcode) {
2091 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2092 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2094 3abe8080 2019-04-10 stsp } else if (*a->quit)
2096 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2097 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2098 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2099 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2102 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2103 7c1452c1 2020-03-26 stsp if (errcode) {
2104 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2105 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2106 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2111 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2113 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2114 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2115 7c1452c1 2020-03-26 stsp &tog_mutex);
2116 7c1452c1 2020-03-26 stsp if (errcode)
2117 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2118 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2119 21355643 2020-12-06 stsp if (*a->quit)
2124 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2125 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2126 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2127 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2129 3abe8080 2019-04-10 stsp a->log_complete = 1;
2130 1a76625f 2018-10-22 stsp return (void *)err;
2133 1a76625f 2018-10-22 stsp static const struct got_error *
2134 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2136 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2137 1a76625f 2018-10-22 stsp int errcode;
2139 1a76625f 2018-10-22 stsp if (s->thread) {
2140 1a76625f 2018-10-22 stsp s->quit = 1;
2141 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2142 1a76625f 2018-10-22 stsp if (errcode)
2143 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2144 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2145 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2146 1a76625f 2018-10-22 stsp if (errcode)
2147 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2148 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2149 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2150 1a76625f 2018-10-22 stsp if (errcode)
2151 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2152 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2153 1a76625f 2018-10-22 stsp if (errcode)
2154 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2155 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2156 1a76625f 2018-10-22 stsp s->thread = NULL;
2159 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2160 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2161 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2164 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2165 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2166 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2169 9343a5fb 2018-06-23 stsp return err;
2172 9343a5fb 2018-06-23 stsp static const struct got_error *
2173 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2175 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2176 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2177 276b94a1 2020-11-13 naddy int errcode;
2179 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2181 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2182 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2183 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2185 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2186 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2187 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2189 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2190 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2191 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2192 1a76625f 2018-10-22 stsp free(s->start_id);
2193 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2194 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2195 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2196 1a76625f 2018-10-22 stsp return err;
2199 1a76625f 2018-10-22 stsp static const struct got_error *
2200 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2202 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2204 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2205 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2206 60493ae3 2019-06-20 stsp return NULL;
2209 60493ae3 2019-06-20 stsp static const struct got_error *
2210 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2212 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2213 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2214 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2216 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2217 f9686aa5 2020-03-27 stsp show_log_view(view);
2218 f9686aa5 2020-03-27 stsp update_panels();
2219 f9686aa5 2020-03-27 stsp doupdate();
2221 96e2b566 2019-07-08 stsp if (s->search_entry) {
2222 13add988 2019-10-15 stsp int errcode, ch;
2223 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2224 13add988 2019-10-15 stsp if (errcode)
2225 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2226 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2227 13add988 2019-10-15 stsp ch = wgetch(view->window);
2228 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2229 13add988 2019-10-15 stsp if (errcode)
2230 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2231 13add988 2019-10-15 stsp "pthread_mutex_lock");
2232 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2233 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2234 678cbce5 2019-07-28 stsp return NULL;
2236 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2237 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2239 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2240 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2241 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2242 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2243 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2245 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2246 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2248 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2251 60493ae3 2019-06-20 stsp while (1) {
2252 13add988 2019-10-15 stsp int have_match = 0;
2254 60493ae3 2019-06-20 stsp if (entry == NULL) {
2255 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2256 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2257 f9967bca 2020-03-27 stsp view->search_next_done =
2258 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2259 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2260 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2261 f801134a 2019-06-25 stsp return NULL;
2264 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2265 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2266 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2268 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2269 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2272 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2273 13add988 2019-10-15 stsp &view->regex);
2276 13add988 2019-10-15 stsp if (have_match) {
2277 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2278 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2282 96e2b566 2019-07-08 stsp s->search_entry = entry;
2283 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2284 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2286 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2289 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2290 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2291 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2292 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2294 60493ae3 2019-06-20 stsp return err;
2297 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2298 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2300 60493ae3 2019-06-20 stsp return err;
2305 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2307 60493ae3 2019-06-20 stsp return NULL;
2310 60493ae3 2019-06-20 stsp static const struct got_error *
2311 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2312 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2313 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2315 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2316 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2317 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2318 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2319 1a76625f 2018-10-22 stsp int errcode;
2321 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2322 f135c941 2020-02-20 stsp free(s->in_repo_path);
2323 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2324 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2325 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2328 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2329 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2330 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2332 fb2756b9 2018-08-04 stsp s->repo = repo;
2333 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2334 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2335 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2336 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2337 9cd7cbd1 2020-12-07 stsp goto done;
2340 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2341 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2342 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2343 5036bf37 2018-09-24 stsp goto done;
2345 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2347 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2348 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2349 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2350 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2352 11b20872 2019-11-08 stsp goto done;
2353 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2354 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2355 11b20872 2019-11-08 stsp if (err) {
2356 11b20872 2019-11-08 stsp free_colors(&s->colors);
2357 11b20872 2019-11-08 stsp goto done;
2359 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2360 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2361 11b20872 2019-11-08 stsp if (err) {
2362 11b20872 2019-11-08 stsp free_colors(&s->colors);
2363 11b20872 2019-11-08 stsp goto done;
2367 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2368 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2369 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2370 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2371 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2373 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2375 1a76625f 2018-10-22 stsp goto done;
2376 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2377 b672a97a 2020-01-27 stsp !s->log_branches);
2379 1a76625f 2018-10-22 stsp goto done;
2380 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2381 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2383 c5b78334 2020-01-12 stsp goto done;
2385 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2386 1a76625f 2018-10-22 stsp if (errcode) {
2387 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2388 1a76625f 2018-10-22 stsp goto done;
2390 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2391 7c1452c1 2020-03-26 stsp if (errcode) {
2392 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2393 7c1452c1 2020-03-26 stsp goto done;
2396 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2397 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2398 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2399 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2400 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2401 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2402 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2403 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2404 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2405 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2406 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2407 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2408 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2411 1a76625f 2018-10-22 stsp close_log_view(view);
2412 ba4f502b 2018-08-04 stsp return err;
2415 e5a0f69f 2018-08-18 stsp static const struct got_error *
2416 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2418 f2f6d207 2020-11-24 stsp const struct got_error *err;
2419 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2421 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2422 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2423 2b380cc8 2018-10-24 stsp &s->thread_args);
2424 2b380cc8 2018-10-24 stsp if (errcode)
2425 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2426 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2427 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2429 f2f6d207 2020-11-24 stsp return err;
2433 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2436 e5a0f69f 2018-08-18 stsp static const struct got_error *
2437 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2439 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2440 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2441 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2442 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2443 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2444 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
2446 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2447 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2448 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2449 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2450 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2451 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2452 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2453 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2454 fb280deb 2021-08-30 stsp select_commit(s);
2456 528dedf3 2021-08-30 stsp return NULL;
2459 528dedf3 2021-08-30 stsp switch (ch) {
2461 1e37a5c2 2019-05-12 jcs s->quit = 1;
2464 1e37a5c2 2019-05-12 jcs case KEY_UP:
2467 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2468 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2470 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2471 1e37a5c2 2019-05-12 jcs s->selected--;
2473 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2474 912a3f79 2021-08-30 j select_commit(s);
2477 912a3f79 2021-08-30 j case KEY_HOME:
2478 912a3f79 2021-08-30 j s->selected = 0;
2479 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2480 2b779855 2020-12-05 naddy select_commit(s);
2482 83cc4199 2022-06-13 stsp case CTRL('u'):
2483 83cc4199 2022-06-13 stsp nscroll /= 2;
2484 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2485 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2486 a4292ac5 2019-05-12 jcs case CTRL('b'):
2487 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2489 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2490 83cc4199 2022-06-13 stsp s->selected = MAX(0, s->selected - nscroll - 1);
2492 83cc4199 2022-06-13 stsp log_scroll_up(s, nscroll);
2493 2b779855 2020-12-05 naddy select_commit(s);
2496 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2499 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2500 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2502 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2503 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2504 1e37a5c2 2019-05-12 jcs s->selected++;
2506 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2510 912a3f79 2021-08-30 j select_commit(s);
2513 912a3f79 2021-08-30 j case KEY_END: {
2514 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2515 912a3f79 2021-08-30 j * traverse them all. */
2516 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2517 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2518 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2521 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2522 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2523 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2524 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2526 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2527 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2529 f3bc9f1d 2021-09-05 naddy if (n > 0)
2530 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2531 2b779855 2020-12-05 naddy select_commit(s);
2534 80b7e8da 2022-06-11 stsp case CTRL('d'):
2535 83cc4199 2022-06-13 stsp nscroll /= 2;
2536 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2537 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2538 83cc4199 2022-06-13 stsp case CTRL('f'): {
2539 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2540 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2541 1e37a5c2 2019-05-12 jcs if (first == NULL)
2543 83cc4199 2022-06-13 stsp err = log_scroll_down(view, nscroll);
2546 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2547 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2548 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2549 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2550 83cc4199 2022-06-13 stsp s->selected += MIN(s->last_displayed_entry->idx -
2551 83cc4199 2022-06-13 stsp s->selected_entry->idx, nscroll + 1);
2553 2b779855 2020-12-05 naddy select_commit(s);
2556 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2557 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2558 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2559 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2560 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2561 2b779855 2020-12-05 naddy select_commit(s);
2562 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2563 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2564 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2565 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2566 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2569 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2572 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2574 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2575 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2576 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2577 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2578 78756c87 2020-11-24 stsp view, s->repo);
2581 e78dc838 2020-12-04 stsp view->focussed = 0;
2582 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2583 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2584 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2586 1e37a5c2 2019-05-12 jcs return err;
2587 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2588 e78dc838 2020-12-04 stsp view->focus_child = 1;
2590 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2593 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2595 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2596 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2597 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2598 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2602 e78dc838 2020-12-04 stsp view->focussed = 0;
2603 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2604 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2605 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2607 1e37a5c2 2019-05-12 jcs return err;
2608 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2609 e78dc838 2020-12-04 stsp view->focus_child = 1;
2611 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2613 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2614 21355643 2020-12-06 stsp case CTRL('l'):
2616 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2617 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2619 21355643 2020-12-06 stsp err = stop_log_thread(s);
2621 74cfe85e 2020-10-20 stsp return err;
2622 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2623 21355643 2020-12-06 stsp char *parent_path;
2624 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2626 21355643 2020-12-06 stsp return err;
2627 21355643 2020-12-06 stsp free(s->in_repo_path);
2628 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2629 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2630 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2631 21355643 2020-12-06 stsp struct got_object_id *start_id;
2632 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2633 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2634 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2636 21355643 2020-12-06 stsp return err;
2637 21355643 2020-12-06 stsp free(s->start_id);
2638 21355643 2020-12-06 stsp s->start_id = start_id;
2639 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2640 21355643 2020-12-06 stsp } else /* 'B' */
2641 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2643 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2644 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2646 21355643 2020-12-06 stsp return err;
2647 51a10b52 2020-12-26 stsp tog_free_refs();
2648 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
2650 ca51c541 2020-12-07 stsp return err;
2651 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2652 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2654 d01904d4 2019-06-25 stsp return err;
2655 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2656 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2658 b672a97a 2020-01-27 stsp return err;
2659 21355643 2020-12-06 stsp free_commits(&s->commits);
2660 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2661 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2662 21355643 2020-12-06 stsp s->selected_entry = NULL;
2663 21355643 2020-12-06 stsp s->selected = 0;
2664 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2665 21355643 2020-12-06 stsp s->quit = 0;
2666 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2669 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2670 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2671 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2672 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2673 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2674 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2675 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2676 6458efa5 2020-11-24 stsp if (err) {
2677 6458efa5 2020-11-24 stsp view_close(ref_view);
2678 6458efa5 2020-11-24 stsp return err;
2680 e78dc838 2020-12-04 stsp view->focussed = 0;
2681 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2682 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2683 6458efa5 2020-11-24 stsp err = view_close_child(view);
2685 6458efa5 2020-11-24 stsp return err;
2686 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2687 e78dc838 2020-12-04 stsp view->focus_child = 1;
2689 6458efa5 2020-11-24 stsp *new_view = ref_view;
2695 80ddbec8 2018-04-29 stsp return err;
2698 4ed7e80c 2018-05-20 stsp static const struct got_error *
2699 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2701 c2db6724 2019-01-04 stsp const struct got_error *error;
2703 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2704 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2705 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2707 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2708 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2710 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2711 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2713 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2714 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2716 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2717 c2db6724 2019-01-04 stsp if (error != NULL)
2718 c2db6724 2019-01-04 stsp return error;
2720 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2721 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2723 c2db6724 2019-01-04 stsp return NULL;
2726 a915003a 2019-02-05 stsp static void
2727 a915003a 2019-02-05 stsp init_curses(void)
2730 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
2731 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
2732 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
2734 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
2735 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
2736 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
2737 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
2738 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
2740 a915003a 2019-02-05 stsp initscr();
2742 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2745 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2746 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2747 a915003a 2019-02-05 stsp curs_set(0);
2748 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2749 6d17833f 2019-11-08 stsp start_color();
2750 6d17833f 2019-11-08 stsp use_default_colors();
2754 c2db6724 2019-01-04 stsp static const struct got_error *
2755 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2756 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2758 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2760 f135c941 2020-02-20 stsp if (argc == 0) {
2761 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2762 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2763 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2764 f135c941 2020-02-20 stsp return NULL;
2767 f135c941 2020-02-20 stsp if (worktree) {
2768 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2771 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2773 f135c941 2020-02-20 stsp return err;
2774 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2775 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2776 bfd61697 2020-11-14 stsp p) == -1) {
2777 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2778 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2782 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2784 f135c941 2020-02-20 stsp return err;
2787 f135c941 2020-02-20 stsp static const struct got_error *
2788 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2790 80ddbec8 2018-04-29 stsp const struct got_error *error;
2791 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2792 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2793 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2794 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2795 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2796 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2797 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2798 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2799 04cc582a 2018-08-01 stsp struct tog_view *view;
2801 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2802 80ddbec8 2018-04-29 stsp switch (ch) {
2804 b672a97a 2020-01-27 stsp log_branches = 1;
2807 80ddbec8 2018-04-29 stsp start_commit = optarg;
2810 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2811 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2812 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2816 17020d27 2019-03-07 stsp usage_log();
2817 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2821 80ddbec8 2018-04-29 stsp argc -= optind;
2822 80ddbec8 2018-04-29 stsp argv += optind;
2824 f135c941 2020-02-20 stsp if (argc > 1)
2825 f135c941 2020-02-20 stsp usage_log();
2827 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2828 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2829 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2830 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2831 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2832 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2833 c156c7a4 2020-12-18 stsp goto done;
2834 a1fbf39a 2019-08-11 stsp if (worktree)
2835 6962eb72 2020-02-20 stsp repo_path =
2836 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2838 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2839 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2840 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2841 c156c7a4 2020-12-18 stsp goto done;
2845 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2846 80ddbec8 2018-04-29 stsp if (error != NULL)
2847 ecb28ae0 2018-07-16 stsp goto done;
2849 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2850 f135c941 2020-02-20 stsp repo, worktree);
2851 f135c941 2020-02-20 stsp if (error)
2852 f135c941 2020-02-20 stsp goto done;
2854 f135c941 2020-02-20 stsp init_curses();
2856 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2857 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2858 c02c541e 2019-03-29 stsp if (error)
2859 c02c541e 2019-03-29 stsp goto done;
2861 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2862 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2863 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
2864 87670572 2020-12-26 naddy if (error)
2865 87670572 2020-12-26 naddy goto done;
2868 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2869 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2870 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2871 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2872 d8f38dc4 2020-12-05 stsp if (error)
2873 d8f38dc4 2020-12-05 stsp goto done;
2874 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2876 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2877 d8f38dc4 2020-12-05 stsp if (error == NULL)
2878 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2879 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2880 d8f38dc4 2020-12-05 stsp goto done;
2881 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2882 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2883 d8f38dc4 2020-12-05 stsp if (error)
2884 d8f38dc4 2020-12-05 stsp goto done;
2887 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2888 04cc582a 2018-08-01 stsp if (view == NULL) {
2889 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2890 04cc582a 2018-08-01 stsp goto done;
2892 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2893 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2894 ba4f502b 2018-08-04 stsp if (error)
2895 ba4f502b 2018-08-04 stsp goto done;
2896 2fc00ff4 2019-08-31 stsp if (worktree) {
2897 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2898 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2899 2fc00ff4 2019-08-31 stsp worktree = NULL;
2901 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2903 f135c941 2020-02-20 stsp free(in_repo_path);
2904 ecb28ae0 2018-07-16 stsp free(repo_path);
2905 ecb28ae0 2018-07-16 stsp free(cwd);
2906 899d86c2 2018-05-10 stsp free(start_id);
2907 d8f38dc4 2020-12-05 stsp free(label);
2909 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2910 1d0f4054 2021-06-17 stsp if (repo) {
2911 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2912 1d0f4054 2021-06-17 stsp if (error == NULL)
2913 1d0f4054 2021-06-17 stsp error = close_err;
2915 ec142235 2019-03-07 stsp if (worktree)
2916 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2917 51a10b52 2020-12-26 stsp tog_free_refs();
2918 80ddbec8 2018-04-29 stsp return error;
2921 4ed7e80c 2018-05-20 stsp __dead static void
2922 9f7d7167 2018-04-29 stsp usage_diff(void)
2925 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2926 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2930 6d17833f 2019-11-08 stsp static int
2931 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2932 41605754 2020-11-12 stsp regmatch_t *regmatch)
2934 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2937 f26dddb7 2019-11-08 stsp struct tog_color *
2938 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2940 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2942 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2943 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2944 6d17833f 2019-11-08 stsp return tc;
2947 6d17833f 2019-11-08 stsp return NULL;
2950 4ed7e80c 2018-05-20 stsp static const struct got_error *
2951 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2952 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2954 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2955 41605754 2020-11-12 stsp wchar_t *wline;
2956 41605754 2020-11-12 stsp int width;
2959 41605754 2020-11-12 stsp *wtotal = 0;
2961 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2962 41605754 2020-11-12 stsp if (s == NULL)
2963 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2965 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2966 41605754 2020-11-12 stsp if (err) {
2968 41605754 2020-11-12 stsp return err;
2970 41605754 2020-11-12 stsp waddwstr(window, wline);
2971 41605754 2020-11-12 stsp free(wline);
2973 41605754 2020-11-12 stsp wlimit -= width;
2974 41605754 2020-11-12 stsp *wtotal += width;
2976 41605754 2020-11-12 stsp if (wlimit > 0) {
2977 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2978 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2979 41605754 2020-11-12 stsp if (s == NULL) {
2980 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2982 41605754 2020-11-12 stsp return err;
2984 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2985 41605754 2020-11-12 stsp if (err) {
2987 41605754 2020-11-12 stsp return err;
2989 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2990 41605754 2020-11-12 stsp waddwstr(window, wline);
2991 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2992 41605754 2020-11-12 stsp free(wline);
2994 41605754 2020-11-12 stsp wlimit -= width;
2995 41605754 2020-11-12 stsp *wtotal += width;
2998 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2999 41605754 2020-11-12 stsp err = format_line(&wline, &width,
3000 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
3002 41605754 2020-11-12 stsp return err;
3003 41605754 2020-11-12 stsp waddwstr(window, wline);
3004 41605754 2020-11-12 stsp free(wline);
3005 41605754 2020-11-12 stsp *wtotal += width;
3008 41605754 2020-11-12 stsp return NULL;
3011 41605754 2020-11-12 stsp static const struct got_error *
3012 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3014 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3015 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3016 61e69b96 2018-05-20 stsp const struct got_error *err;
3017 fe621944 2020-11-10 stsp int nprinted = 0;
3018 b304db33 2018-05-20 stsp char *line;
3019 826082fe 2020-12-10 stsp size_t linesize = 0;
3020 826082fe 2020-12-10 stsp ssize_t linelen;
3021 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3022 61e69b96 2018-05-20 stsp wchar_t *wline;
3023 e0b650dd 2018-05-20 stsp int width;
3024 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3025 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3026 fe621944 2020-11-10 stsp off_t line_offset;
3028 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3029 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3030 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3032 f7d12f7e 2018-08-01 stsp werase(view->window);
3034 a3404814 2018-09-02 stsp if (header) {
3035 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3036 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3037 135a2da0 2020-11-11 stsp header) == -1)
3038 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3039 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3040 135a2da0 2020-11-11 stsp free(line);
3042 a3404814 2018-09-02 stsp return err;
3044 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3045 a3404814 2018-09-02 stsp wstandout(view->window);
3046 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3047 e54cc94a 2020-11-11 stsp free(wline);
3048 e54cc94a 2020-11-11 stsp wline = NULL;
3049 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3050 a3404814 2018-09-02 stsp wstandend(view->window);
3051 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3052 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3054 a3404814 2018-09-02 stsp if (max_lines <= 1)
3055 a3404814 2018-09-02 stsp return NULL;
3056 a3404814 2018-09-02 stsp max_lines--;
3059 89f1a395 2020-12-01 naddy s->eof = 0;
3060 826082fe 2020-12-10 stsp line = NULL;
3061 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3062 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3063 826082fe 2020-12-10 stsp if (linelen == -1) {
3064 826082fe 2020-12-10 stsp if (feof(s->f)) {
3065 826082fe 2020-12-10 stsp s->eof = 1;
3068 826082fe 2020-12-10 stsp free(line);
3069 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3072 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3074 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3075 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3076 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3077 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3078 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3079 41605754 2020-11-12 stsp view->window, regmatch);
3080 41605754 2020-11-12 stsp if (err) {
3081 41605754 2020-11-12 stsp free(line);
3082 41605754 2020-11-12 stsp return err;
3085 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3086 41605754 2020-11-12 stsp if (err) {
3087 41605754 2020-11-12 stsp free(line);
3088 41605754 2020-11-12 stsp return err;
3090 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3091 41605754 2020-11-12 stsp free(wline);
3092 41605754 2020-11-12 stsp wline = NULL;
3095 6d17833f 2019-11-08 stsp wattr_off(view->window,
3096 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3097 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3098 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3099 fe621944 2020-11-10 stsp nprinted++;
3101 826082fe 2020-12-10 stsp free(line);
3102 fe621944 2020-11-10 stsp if (nprinted >= 1)
3103 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3104 89f1a395 2020-12-01 naddy (nprinted - 1);
3106 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3108 1a57306a 2018-09-02 stsp view_vborder(view);
3110 89f1a395 2020-12-01 naddy if (s->eof) {
3111 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3112 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3113 c3e9aa98 2019-05-13 jcs nprinted++;
3116 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3118 c3e9aa98 2019-05-13 jcs return err;
3121 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3122 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3123 e54cc94a 2020-11-11 stsp free(wline);
3124 e54cc94a 2020-11-11 stsp wline = NULL;
3125 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3128 26ed57b2 2018-05-19 stsp return NULL;
3131 abd2672a 2018-12-23 stsp static char *
3132 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3134 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3135 09867e48 2019-08-13 stsp char *p, *s;
3137 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3138 09867e48 2019-08-13 stsp if (tm == NULL)
3139 09867e48 2019-08-13 stsp return NULL;
3140 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3141 09867e48 2019-08-13 stsp if (s == NULL)
3142 09867e48 2019-08-13 stsp return NULL;
3143 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3145 abd2672a 2018-12-23 stsp *p = '\0';
3149 4ed7e80c 2018-05-20 stsp static const struct got_error *
3150 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3151 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3153 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3154 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3155 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3156 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3158 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3159 0208f208 2020-05-05 stsp if (qid != NULL) {
3160 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3161 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3162 d7b5a0e8 2022-04-20 stsp &qid->id);
3164 0208f208 2020-05-05 stsp return err;
3166 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3167 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3168 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3169 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3170 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3172 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3176 0208f208 2020-05-05 stsp if (tree_id1) {
3177 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3179 0208f208 2020-05-05 stsp goto done;
3182 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3183 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3185 0208f208 2020-05-05 stsp goto done;
3187 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3188 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3190 0208f208 2020-05-05 stsp if (tree1)
3191 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3192 0208f208 2020-05-05 stsp if (tree2)
3193 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3194 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3195 0208f208 2020-05-05 stsp return err;
3198 0208f208 2020-05-05 stsp static const struct got_error *
3199 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3203 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3204 fe621944 2020-11-10 stsp if (p == NULL)
3205 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3206 fe621944 2020-11-10 stsp *line_offsets = p;
3207 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3208 fe621944 2020-11-10 stsp (*nlines)++;
3209 fe621944 2020-11-10 stsp return NULL;
3212 fe621944 2020-11-10 stsp static const struct got_error *
3213 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3214 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3215 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3217 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3218 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3219 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3220 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3221 45d799e2 2018-12-23 stsp time_t committer_time;
3222 45d799e2 2018-12-23 stsp const char *author, *committer;
3223 8b473291 2019-02-21 stsp char *refs_str = NULL;
3224 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3225 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3226 fe621944 2020-11-10 stsp off_t outoff = 0;
3229 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3231 8b473291 2019-02-21 stsp if (refs) {
3232 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3234 8b473291 2019-02-21 stsp return err;
3237 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3239 abd2672a 2018-12-23 stsp return err;
3241 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3242 15a94983 2018-12-23 stsp if (err) {
3243 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3244 15a94983 2018-12-23 stsp goto done;
3247 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3249 fe621944 2020-11-10 stsp goto done;
3251 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3252 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3253 fe621944 2020-11-10 stsp if (n < 0) {
3254 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3255 abd2672a 2018-12-23 stsp goto done;
3257 fe621944 2020-11-10 stsp outoff += n;
3258 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3260 fe621944 2020-11-10 stsp goto done;
3262 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3263 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3264 fe621944 2020-11-10 stsp if (n < 0) {
3265 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3266 abd2672a 2018-12-23 stsp goto done;
3268 fe621944 2020-11-10 stsp outoff += n;
3269 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3271 fe621944 2020-11-10 stsp goto done;
3273 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3274 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3275 fe621944 2020-11-10 stsp if (datestr) {
3276 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3277 fe621944 2020-11-10 stsp if (n < 0) {
3278 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3279 fe621944 2020-11-10 stsp goto done;
3281 fe621944 2020-11-10 stsp outoff += n;
3282 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3284 fe621944 2020-11-10 stsp goto done;
3286 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3287 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3288 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3289 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3290 fe621944 2020-11-10 stsp if (n < 0) {
3291 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3292 fe621944 2020-11-10 stsp goto done;
3294 fe621944 2020-11-10 stsp outoff += n;
3295 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3297 fe621944 2020-11-10 stsp goto done;
3299 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3300 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3301 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3302 9f98ca05 2021-09-24 stsp int pn = 1;
3303 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3304 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3305 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3307 9f98ca05 2021-09-24 stsp goto done;
3308 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3309 9f98ca05 2021-09-24 stsp if (n < 0) {
3310 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3311 9f98ca05 2021-09-24 stsp goto done;
3313 9f98ca05 2021-09-24 stsp outoff += n;
3314 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3316 9f98ca05 2021-09-24 stsp goto done;
3317 9f98ca05 2021-09-24 stsp free(id_str);
3318 9f98ca05 2021-09-24 stsp id_str = NULL;
3322 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3324 5943eee2 2019-08-13 stsp goto done;
3325 fe621944 2020-11-10 stsp s = logmsg;
3326 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3327 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3328 fe621944 2020-11-10 stsp if (n < 0) {
3329 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3330 fe621944 2020-11-10 stsp goto done;
3332 fe621944 2020-11-10 stsp outoff += n;
3333 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3335 fe621944 2020-11-10 stsp goto done;
3338 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3340 0208f208 2020-05-05 stsp goto done;
3341 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3342 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3343 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3344 fe621944 2020-11-10 stsp if (n < 0) {
3345 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3346 fe621944 2020-11-10 stsp goto done;
3348 fe621944 2020-11-10 stsp outoff += n;
3349 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3351 fe621944 2020-11-10 stsp goto done;
3352 0208f208 2020-05-05 stsp free((char *)pe->path);
3353 0208f208 2020-05-05 stsp free(pe->data);
3356 0208f208 2020-05-05 stsp fputc('\n', outfile);
3358 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3360 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3361 abd2672a 2018-12-23 stsp free(id_str);
3362 5943eee2 2019-08-13 stsp free(logmsg);
3363 8b473291 2019-02-21 stsp free(refs_str);
3364 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3365 7510f233 2020-08-09 stsp if (err) {
3366 ae6a6978 2020-08-09 stsp free(*line_offsets);
3367 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3368 ae6a6978 2020-08-09 stsp *nlines = 0;
3370 fe621944 2020-11-10 stsp return err;
3373 abd2672a 2018-12-23 stsp static const struct got_error *
3374 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3376 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3377 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3378 15a94983 2018-12-23 stsp int obj_type;
3380 fe621944 2020-11-10 stsp free(s->line_offsets);
3381 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3382 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3383 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3384 fe621944 2020-11-10 stsp s->nlines = 0;
3386 511a516b 2018-05-19 stsp f = got_opentemp();
3387 48ae06ee 2018-10-18 stsp if (f == NULL) {
3388 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3389 48ae06ee 2018-10-18 stsp goto done;
3391 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3392 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3393 fb43ecf1 2019-02-11 stsp goto done;
3397 15a94983 2018-12-23 stsp if (s->id1)
3398 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3400 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3402 15a94983 2018-12-23 stsp goto done;
3404 15a94983 2018-12-23 stsp switch (obj_type) {
3405 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3406 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3407 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3408 b72706c3 2022-06-01 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3409 b72706c3 2022-06-01 stsp s->repo, s->f);
3411 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3412 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3413 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3414 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3416 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3417 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3418 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3419 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3420 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3422 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3424 3ffacbe1 2020-02-02 tracey goto done;
3425 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3426 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3427 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3428 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3429 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3430 f44b1f58 2020-02-02 tracey if (err)
3431 f44b1f58 2020-02-02 tracey goto done;
3432 f44b1f58 2020-02-02 tracey } else {
3433 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3434 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3435 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3436 fe621944 2020-11-10 stsp err = write_commit_info(
3437 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3438 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3439 f44b1f58 2020-02-02 tracey if (err)
3440 f44b1f58 2020-02-02 tracey goto done;
3445 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3447 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3448 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3449 b72706c3 2022-06-01 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3453 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3456 f44b1f58 2020-02-02 tracey if (err)
3457 f44b1f58 2020-02-02 tracey goto done;
3459 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3460 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3461 48ae06ee 2018-10-18 stsp return err;
3464 f5215bb9 2019-02-22 stsp static void
3465 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3467 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3468 f5215bb9 2019-02-22 stsp update_panels();
3469 f5215bb9 2019-02-22 stsp doupdate();
3472 f44b1f58 2020-02-02 tracey static const struct got_error *
3473 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3475 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3477 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3478 f44b1f58 2020-02-02 tracey return NULL;
3481 f44b1f58 2020-02-02 tracey static const struct got_error *
3482 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3484 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3485 f44b1f58 2020-02-02 tracey int lineno;
3486 826082fe 2020-12-10 stsp char *line = NULL;
3487 826082fe 2020-12-10 stsp size_t linesize = 0;
3488 826082fe 2020-12-10 stsp ssize_t linelen;
3490 f44b1f58 2020-02-02 tracey if (!view->searching) {
3491 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3492 f44b1f58 2020-02-02 tracey return NULL;
3495 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3496 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3497 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3499 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3501 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
3503 f44b1f58 2020-02-02 tracey while (1) {
3504 f44b1f58 2020-02-02 tracey off_t offset;
3506 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3507 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3508 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3512 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3513 f44b1f58 2020-02-02 tracey lineno = 1;
3515 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3518 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3519 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3520 f44b1f58 2020-02-02 tracey free(line);
3521 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3523 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3524 826082fe 2020-12-10 stsp if (linelen != -1 &&
3525 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3526 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3527 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3530 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3531 f44b1f58 2020-02-02 tracey lineno++;
3533 f44b1f58 2020-02-02 tracey lineno--;
3535 826082fe 2020-12-10 stsp free(line);
3537 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3538 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3539 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3542 f44b1f58 2020-02-02 tracey return NULL;
3545 48ae06ee 2018-10-18 stsp static const struct got_error *
3546 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
3548 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
3549 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
3551 b72706c3 2022-06-01 stsp free(s->id1);
3552 b72706c3 2022-06-01 stsp s->id1 = NULL;
3553 b72706c3 2022-06-01 stsp free(s->id2);
3554 b72706c3 2022-06-01 stsp s->id2 = NULL;
3555 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
3556 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3557 b72706c3 2022-06-01 stsp s->f = NULL;
3558 b72706c3 2022-06-01 stsp if (s->f1 && fclose(s->f1) == EOF)
3559 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3560 b72706c3 2022-06-01 stsp s->f1 = NULL;
3561 b72706c3 2022-06-01 stsp if (s->f2 && fclose(s->f2) == EOF)
3562 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3563 b72706c3 2022-06-01 stsp s->f2 = NULL;
3564 b72706c3 2022-06-01 stsp free_colors(&s->colors);
3565 b72706c3 2022-06-01 stsp free(s->line_offsets);
3566 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
3567 b72706c3 2022-06-01 stsp s->nlines = 0;
3568 b72706c3 2022-06-01 stsp return err;
3571 b72706c3 2022-06-01 stsp static const struct got_error *
3572 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3573 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3574 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3575 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3577 48ae06ee 2018-10-18 stsp const struct got_error *err;
3578 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3580 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
3582 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3583 15a94983 2018-12-23 stsp int type1, type2;
3584 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3586 15a94983 2018-12-23 stsp return err;
3587 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3589 15a94983 2018-12-23 stsp return err;
3591 15a94983 2018-12-23 stsp if (type1 != type2)
3592 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3594 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3595 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3596 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3597 f44b1f58 2020-02-02 tracey s->repo = repo;
3598 f44b1f58 2020-02-02 tracey s->id1 = id1;
3599 f44b1f58 2020-02-02 tracey s->id2 = id2;
3600 3dbaef42 2020-11-24 stsp s->label1 = label1;
3601 3dbaef42 2020-11-24 stsp s->label2 = label2;
3603 15a94983 2018-12-23 stsp if (id1) {
3604 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3605 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3606 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3607 b72706c3 2022-06-01 stsp s->f1 = got_opentemp();
3608 b72706c3 2022-06-01 stsp if (s->f1 == NULL) {
3609 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3610 b72706c3 2022-06-01 stsp goto done;
3613 5465d566 2020-02-01 tracey s->id1 = NULL;
3615 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3616 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3617 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
3618 b72706c3 2022-06-01 stsp goto done;
3621 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
3622 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
3623 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3624 b72706c3 2022-06-01 stsp goto done;
3627 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3628 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3629 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3630 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3631 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3632 5465d566 2020-02-01 tracey s->log_view = log_view;
3633 5465d566 2020-02-01 tracey s->repo = repo;
3635 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3636 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3637 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3638 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3639 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3641 b72706c3 2022-06-01 stsp goto done;
3642 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3643 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3644 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3646 b72706c3 2022-06-01 stsp goto done;
3647 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3648 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3649 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3651 b72706c3 2022-06-01 stsp goto done;
3653 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3654 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3655 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3656 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3658 b72706c3 2022-06-01 stsp goto done;
3660 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3661 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3662 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3664 b72706c3 2022-06-01 stsp goto done;
3666 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3667 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3668 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3670 b72706c3 2022-06-01 stsp goto done;
3673 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3674 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3675 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3677 5465d566 2020-02-01 tracey err = create_diff(s);
3679 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3680 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3681 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3682 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3683 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3686 b72706c3 2022-06-01 stsp close_diff_view(view);
3687 e5a0f69f 2018-08-18 stsp return err;
3690 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3691 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3693 a3404814 2018-09-02 stsp const struct got_error *err;
3694 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3695 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3696 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3698 a3404814 2018-09-02 stsp if (s->id1) {
3699 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3701 a3404814 2018-09-02 stsp return err;
3702 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3704 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3706 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3708 a3404814 2018-09-02 stsp return err;
3709 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3711 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3712 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3713 a3404814 2018-09-02 stsp free(id_str1);
3714 a3404814 2018-09-02 stsp free(id_str2);
3715 a3404814 2018-09-02 stsp return err;
3717 a3404814 2018-09-02 stsp free(id_str1);
3718 a3404814 2018-09-02 stsp free(id_str2);
3720 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3721 267bb3b8 2021-08-01 stsp free(header);
3722 267bb3b8 2021-08-01 stsp return err;
3725 15a087fe 2019-02-21 stsp static const struct got_error *
3726 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3727 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3729 d7a04538 2019-02-21 stsp const struct got_error *err;
3730 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3731 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3732 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3734 15a087fe 2019-02-21 stsp free(s->id2);
3735 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3736 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3737 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3739 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3741 d7a04538 2019-02-21 stsp return err;
3742 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3743 15a087fe 2019-02-21 stsp free(s->id1);
3744 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3745 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3746 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3747 15a087fe 2019-02-21 stsp return NULL;
3750 0cf4efb1 2018-09-29 stsp static const struct got_error *
3751 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3753 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3754 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3755 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3756 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3757 826082fe 2020-12-10 stsp char *line = NULL;
3758 826082fe 2020-12-10 stsp size_t linesize = 0;
3759 826082fe 2020-12-10 stsp ssize_t linelen;
3760 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
3762 e5a0f69f 2018-08-18 stsp switch (ch) {
3765 3dbaef42 2020-11-24 stsp if (ch == 'a')
3766 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3767 3dbaef42 2020-11-24 stsp if (ch == 'w')
3768 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3769 64453f7e 2020-11-21 stsp wclear(view->window);
3770 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3771 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3772 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3773 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3774 64453f7e 2020-11-21 stsp err = create_diff(s);
3777 912a3f79 2021-08-30 j case KEY_HOME:
3778 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3781 912a3f79 2021-08-30 j case KEY_END:
3785 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3789 1e37a5c2 2019-05-12 jcs case KEY_UP:
3790 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3791 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3792 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3794 83cc4199 2022-06-13 stsp case CTRL('u'):
3795 83cc4199 2022-06-13 stsp nscroll /= 2;
3796 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3797 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3798 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3799 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3802 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
3803 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3806 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3807 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3808 1e37a5c2 2019-05-12 jcs if (!s->eof)
3809 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3811 83cc4199 2022-06-13 stsp case CTRL('d'):
3812 83cc4199 2022-06-13 stsp nscroll /= 2;
3813 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3814 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3815 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3817 00ba99a7 2019-05-12 jcs if (s->eof)
3820 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
3821 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3822 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3823 826082fe 2020-12-10 stsp if (linelen == -1) {
3824 826082fe 2020-12-10 stsp if (feof(s->f)) {
3825 826082fe 2020-12-10 stsp s->eof = 1;
3827 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3831 826082fe 2020-12-10 stsp free(line);
3834 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3835 1e37a5c2 2019-05-12 jcs s->diff_context--;
3836 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3837 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3838 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3839 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3840 27829c9e 2020-11-21 stsp s->nlines) {
3841 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3842 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3847 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3848 1e37a5c2 2019-05-12 jcs s->diff_context++;
3849 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3850 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3851 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3856 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3858 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3859 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3861 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3865 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3868 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3872 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3873 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3874 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3876 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3877 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3881 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3883 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3884 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3886 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3890 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3893 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3897 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3898 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3899 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3901 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3902 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3908 bcbd79e2 2018-08-19 stsp return err;
3911 4ed7e80c 2018-05-20 stsp static const struct got_error *
3912 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3914 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3915 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3916 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3917 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3918 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3919 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3920 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3921 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3922 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3923 3dbaef42 2020-11-24 stsp const char *errstr;
3924 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3926 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3927 26ed57b2 2018-05-19 stsp switch (ch) {
3929 64453f7e 2020-11-21 stsp force_text_diff = 1;
3932 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3934 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3935 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
3936 5a20d08d 2022-02-09 op errstr, errstr);
3938 09b5bff8 2020-02-23 naddy case 'r':
3939 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3940 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3941 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3943 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3946 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3949 17020d27 2019-03-07 stsp usage_diff();
3950 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3954 26ed57b2 2018-05-19 stsp argc -= optind;
3955 26ed57b2 2018-05-19 stsp argv += optind;
3957 26ed57b2 2018-05-19 stsp if (argc == 0) {
3958 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3959 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3960 15a94983 2018-12-23 stsp id_str1 = argv[0];
3961 15a94983 2018-12-23 stsp id_str2 = argv[1];
3963 26ed57b2 2018-05-19 stsp usage_diff();
3965 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3966 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3967 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3968 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3969 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3970 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3971 c156c7a4 2020-12-18 stsp goto done;
3972 a273ac94 2020-02-23 naddy if (worktree)
3973 a273ac94 2020-02-23 naddy repo_path =
3974 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3976 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3977 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3978 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3979 c156c7a4 2020-12-18 stsp goto done;
3983 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3984 eb6600df 2019-01-04 stsp if (error)
3985 eb6600df 2019-01-04 stsp goto done;
3987 a273ac94 2020-02-23 naddy init_curses();
3989 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3990 51a10b52 2020-12-26 stsp if (error)
3991 51a10b52 2020-12-26 stsp goto done;
3993 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3994 26ed57b2 2018-05-19 stsp if (error)
3995 26ed57b2 2018-05-19 stsp goto done;
3997 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3998 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3999 26ed57b2 2018-05-19 stsp if (error)
4000 26ed57b2 2018-05-19 stsp goto done;
4002 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4003 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4004 26ed57b2 2018-05-19 stsp if (error)
4005 26ed57b2 2018-05-19 stsp goto done;
4007 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4008 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4009 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4010 ea5e7bb5 2018-08-01 stsp goto done;
4012 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4013 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4014 5dc9f4bc 2018-08-04 stsp if (error)
4015 5dc9f4bc 2018-08-04 stsp goto done;
4016 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4018 3dbaef42 2020-11-24 stsp free(label1);
4019 3dbaef42 2020-11-24 stsp free(label2);
4020 c02c541e 2019-03-29 stsp free(repo_path);
4021 a273ac94 2020-02-23 naddy free(cwd);
4022 1d0f4054 2021-06-17 stsp if (repo) {
4023 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4024 1d0f4054 2021-06-17 stsp if (error == NULL)
4025 1d0f4054 2021-06-17 stsp error = close_err;
4027 a273ac94 2020-02-23 naddy if (worktree)
4028 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4029 51a10b52 2020-12-26 stsp tog_free_refs();
4030 26ed57b2 2018-05-19 stsp return error;
4033 4ed7e80c 2018-05-20 stsp __dead static void
4034 9f7d7167 2018-04-29 stsp usage_blame(void)
4037 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4038 9f7d7167 2018-04-29 stsp getprogname());
4042 84451b3e 2018-07-10 stsp struct tog_blame_line {
4043 84451b3e 2018-07-10 stsp int annotated;
4044 84451b3e 2018-07-10 stsp struct got_object_id *id;
4047 4ed7e80c 2018-05-20 stsp static const struct got_error *
4048 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4050 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4051 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4052 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4053 84451b3e 2018-07-10 stsp const struct got_error *err;
4054 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4055 826082fe 2020-12-10 stsp char *line = NULL;
4056 826082fe 2020-12-10 stsp size_t linesize = 0;
4057 826082fe 2020-12-10 stsp ssize_t linelen;
4058 84451b3e 2018-07-10 stsp wchar_t *wline;
4059 27a741e5 2019-09-11 stsp int width;
4060 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4061 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4062 ab089a2a 2018-07-12 stsp char *id_str;
4063 11b20872 2019-11-08 stsp struct tog_color *tc;
4065 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4067 ab089a2a 2018-07-12 stsp return err;
4069 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4070 f7d12f7e 2018-08-01 stsp werase(view->window);
4072 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4073 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4074 ab089a2a 2018-07-12 stsp free(id_str);
4075 ab089a2a 2018-07-12 stsp return err;
4078 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4079 ab089a2a 2018-07-12 stsp free(line);
4080 2550e4c3 2018-07-13 stsp line = NULL;
4082 1cae65b4 2019-09-22 stsp return err;
4083 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4084 a3404814 2018-09-02 stsp wstandout(view->window);
4085 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4087 11b20872 2019-11-08 stsp wattr_on(view->window,
4088 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4089 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4091 11b20872 2019-11-08 stsp wattr_off(view->window,
4092 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4093 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4094 a3404814 2018-09-02 stsp wstandend(view->window);
4095 2550e4c3 2018-07-13 stsp free(wline);
4096 2550e4c3 2018-07-13 stsp wline = NULL;
4097 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4098 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4100 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4101 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4102 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4103 ab089a2a 2018-07-12 stsp free(id_str);
4104 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4106 ab089a2a 2018-07-12 stsp free(id_str);
4107 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4108 3f60a8ef 2018-07-10 stsp free(line);
4109 2550e4c3 2018-07-13 stsp line = NULL;
4111 3f60a8ef 2018-07-10 stsp return err;
4112 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4113 2550e4c3 2018-07-13 stsp free(wline);
4114 2550e4c3 2018-07-13 stsp wline = NULL;
4115 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4116 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4118 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4119 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4120 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4121 826082fe 2020-12-10 stsp if (linelen == -1) {
4122 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4123 826082fe 2020-12-10 stsp s->eof = 1;
4126 84451b3e 2018-07-10 stsp free(line);
4127 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4129 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4132 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4133 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4135 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4136 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4137 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4138 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4139 27a741e5 2019-09-11 stsp !(view->focussed &&
4140 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4141 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4142 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4143 8d0fe45a 2019-08-12 stsp char *id_str;
4144 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4145 8d0fe45a 2019-08-12 stsp if (err) {
4146 8d0fe45a 2019-08-12 stsp free(line);
4147 8d0fe45a 2019-08-12 stsp return err;
4149 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4151 11b20872 2019-11-08 stsp wattr_on(view->window,
4152 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4153 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4155 11b20872 2019-11-08 stsp wattr_off(view->window,
4156 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4157 8d0fe45a 2019-08-12 stsp free(id_str);
4158 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4160 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4161 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4164 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4165 ee41ec32 2018-07-10 stsp prev_id = NULL;
4168 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4169 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4170 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4172 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4173 41605754 2020-11-12 stsp width = 9;
4174 41605754 2020-11-12 stsp wline = wcsdup(L"");
4175 41605754 2020-11-12 stsp if (wline == NULL) {
4176 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4177 41605754 2020-11-12 stsp free(line);
4178 41605754 2020-11-12 stsp return err;
4180 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4181 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4182 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4183 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4184 41605754 2020-11-12 stsp view->window, regmatch);
4185 41605754 2020-11-12 stsp if (err) {
4186 41605754 2020-11-12 stsp free(line);
4187 41605754 2020-11-12 stsp return err;
4189 41605754 2020-11-12 stsp width += 9;
4191 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4192 41605754 2020-11-12 stsp view->ncols - 9, 9);
4193 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4194 41605754 2020-11-12 stsp free(wline);
4195 41605754 2020-11-12 stsp wline = NULL;
4196 41605754 2020-11-12 stsp width += 9;
4199 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4200 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4201 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4202 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4204 826082fe 2020-12-10 stsp free(line);
4205 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4207 1a57306a 2018-09-02 stsp view_vborder(view);
4209 84451b3e 2018-07-10 stsp return NULL;
4212 84451b3e 2018-07-10 stsp static const struct got_error *
4213 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4214 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4216 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4217 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4218 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4219 1a76625f 2018-10-22 stsp int errcode;
4221 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4222 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4223 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4225 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4226 1a76625f 2018-10-22 stsp if (errcode)
4227 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4229 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4230 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4231 d68a0a7d 2018-07-10 stsp goto done;
4234 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4235 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4237 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4238 d68a0a7d 2018-07-10 stsp if (line->annotated)
4239 d68a0a7d 2018-07-10 stsp goto done;
4241 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4242 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4243 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4244 84451b3e 2018-07-10 stsp goto done;
4246 84451b3e 2018-07-10 stsp line->annotated = 1;
4248 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4249 1a76625f 2018-10-22 stsp if (errcode)
4250 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4251 84451b3e 2018-07-10 stsp return err;
4254 84451b3e 2018-07-10 stsp static void *
4255 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4257 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4258 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4259 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4260 1a76625f 2018-10-22 stsp int errcode;
4262 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4264 61266923 2020-01-14 stsp return (void *)err;
4266 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4267 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4268 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4269 fc06ba56 2019-08-22 stsp err = NULL;
4271 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4272 1a76625f 2018-10-22 stsp if (errcode)
4273 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4274 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4276 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4277 1d0f4054 2021-06-17 stsp if (err == NULL)
4278 1d0f4054 2021-06-17 stsp err = close_err;
4279 c9beca56 2018-07-22 stsp ta->repo = NULL;
4280 c9beca56 2018-07-22 stsp *ta->complete = 1;
4282 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4283 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4284 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4286 18430de3 2018-07-10 stsp return (void *)err;
4289 245d91c1 2018-07-12 stsp static struct got_object_id *
4290 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4291 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4293 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4295 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4296 8d0fe45a 2019-08-12 stsp return NULL;
4298 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4299 245d91c1 2018-07-12 stsp if (!line->annotated)
4300 245d91c1 2018-07-12 stsp return NULL;
4302 245d91c1 2018-07-12 stsp return line->id;
4305 b880a918 2018-07-10 stsp static const struct got_error *
4306 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4308 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4311 245d91c1 2018-07-12 stsp if (blame->thread) {
4312 1a76625f 2018-10-22 stsp int errcode;
4313 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4314 1a76625f 2018-10-22 stsp if (errcode)
4315 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4316 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4317 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4318 1a76625f 2018-10-22 stsp if (errcode)
4319 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4320 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4321 1a76625f 2018-10-22 stsp if (errcode)
4322 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4323 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4324 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4325 245d91c1 2018-07-12 stsp err = NULL;
4326 245d91c1 2018-07-12 stsp blame->thread = NULL;
4328 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4329 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4330 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4331 1d0f4054 2021-06-17 stsp if (err == NULL)
4332 1d0f4054 2021-06-17 stsp err = close_err;
4333 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4335 245d91c1 2018-07-12 stsp if (blame->f) {
4336 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4337 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4338 245d91c1 2018-07-12 stsp blame->f = NULL;
4340 57670559 2018-12-23 stsp if (blame->lines) {
4341 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4342 57670559 2018-12-23 stsp free(blame->lines[i].id);
4343 57670559 2018-12-23 stsp free(blame->lines);
4344 57670559 2018-12-23 stsp blame->lines = NULL;
4346 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4347 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4349 245d91c1 2018-07-12 stsp return err;
4352 245d91c1 2018-07-12 stsp static const struct got_error *
4353 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4355 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4356 fc06ba56 2019-08-22 stsp int *done = arg;
4357 fc06ba56 2019-08-22 stsp int errcode;
4359 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4360 fc06ba56 2019-08-22 stsp if (errcode)
4361 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4362 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4364 fc06ba56 2019-08-22 stsp if (*done)
4365 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4367 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4368 fc06ba56 2019-08-22 stsp if (errcode)
4369 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4370 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4372 fc06ba56 2019-08-22 stsp return err;
4375 fc06ba56 2019-08-22 stsp static const struct got_error *
4376 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4378 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4379 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4380 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4381 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4382 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4383 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4384 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4385 15a94983 2018-12-23 stsp int obj_type;
4387 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
4388 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
4390 15a94983 2018-12-23 stsp return err;
4392 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4394 a44927cc 2022-04-07 stsp goto done;
4396 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4398 84451b3e 2018-07-10 stsp goto done;
4400 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4401 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4402 84451b3e 2018-07-10 stsp goto done;
4405 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4407 a70480e0 2018-06-23 stsp goto done;
4408 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4409 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4410 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4411 84451b3e 2018-07-10 stsp goto done;
4413 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4414 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4416 1fddf795 2021-01-20 stsp goto done;
4417 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4418 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4419 84451b3e 2018-07-10 stsp goto done;
4422 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4423 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4424 b02560ec 2019-08-19 stsp blame->nlines--;
4426 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4427 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4428 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4429 84451b3e 2018-07-10 stsp goto done;
4432 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4434 bd24772e 2018-07-11 stsp goto done;
4436 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4437 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4438 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4439 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4440 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4441 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4442 245d91c1 2018-07-12 stsp goto done;
4444 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4446 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4447 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4448 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4449 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4450 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4451 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4452 a5388363 2020-12-01 naddy s->blame_complete = 0;
4454 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4455 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4456 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4457 f5a09613 2020-12-13 naddy s->selected_line = 1;
4459 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4462 a44927cc 2022-04-07 stsp if (commit)
4463 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4465 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4466 27d434c2 2018-09-15 stsp free(obj_id);
4468 245d91c1 2018-07-12 stsp stop_blame(blame);
4469 245d91c1 2018-07-12 stsp return err;
4472 245d91c1 2018-07-12 stsp static const struct got_error *
4473 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4474 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4476 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4477 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4479 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4481 c4843652 2019-08-12 stsp s->path = strdup(path);
4482 c4843652 2019-08-12 stsp if (s->path == NULL)
4483 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4485 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4486 c4843652 2019-08-12 stsp if (err) {
4487 c4843652 2019-08-12 stsp free(s->path);
4488 7cbe629d 2018-08-04 stsp return err;
4491 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4492 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4493 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4494 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4495 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4496 fb2756b9 2018-08-04 stsp s->repo = repo;
4497 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4498 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4500 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4501 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4502 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4503 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4505 11b20872 2019-11-08 stsp return err;
4508 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4509 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4510 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4511 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4512 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4514 a5388363 2020-12-01 naddy return run_blame(view);
4517 e5a0f69f 2018-08-18 stsp static const struct got_error *
4518 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4520 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4521 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4523 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4524 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4526 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4527 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4528 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4529 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4530 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4533 e5a0f69f 2018-08-18 stsp free(s->path);
4534 11b20872 2019-11-08 stsp free_colors(&s->colors);
4536 e5a0f69f 2018-08-18 stsp return err;
4539 7cbe629d 2018-08-04 stsp static const struct got_error *
4540 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4542 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4544 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4545 6c4c42e0 2019-06-24 stsp return NULL;
4548 6c4c42e0 2019-06-24 stsp static const struct got_error *
4549 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4551 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4552 6c4c42e0 2019-06-24 stsp int lineno;
4553 826082fe 2020-12-10 stsp char *line = NULL;
4554 826082fe 2020-12-10 stsp size_t linesize = 0;
4555 826082fe 2020-12-10 stsp ssize_t linelen;
4557 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4558 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4559 6c4c42e0 2019-06-24 stsp return NULL;
4562 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4563 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4564 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4566 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4568 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
4570 6c4c42e0 2019-06-24 stsp while (1) {
4571 6c4c42e0 2019-06-24 stsp off_t offset;
4573 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4574 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4575 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4579 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4580 6c4c42e0 2019-06-24 stsp lineno = 1;
4582 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4585 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4586 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4587 6c4c42e0 2019-06-24 stsp free(line);
4588 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4590 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4591 826082fe 2020-12-10 stsp if (linelen != -1 &&
4592 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4593 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4594 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4597 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4602 826082fe 2020-12-10 stsp free(line);
4604 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4605 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4606 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4609 6c4c42e0 2019-06-24 stsp return NULL;
4612 6c4c42e0 2019-06-24 stsp static const struct got_error *
4613 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4615 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4616 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4617 2b380cc8 2018-10-24 stsp int errcode;
4619 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4620 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4621 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4622 2b380cc8 2018-10-24 stsp if (errcode)
4623 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4625 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4628 51fe7530 2019-08-19 stsp if (s->blame_complete)
4629 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4631 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4633 669b5ffa 2018-10-07 stsp view_vborder(view);
4634 e5a0f69f 2018-08-18 stsp return err;
4637 e5a0f69f 2018-08-18 stsp static const struct got_error *
4638 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4640 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4641 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4642 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4643 83cc4199 2022-06-13 stsp int begin_x = 0, nscroll = view->nlines - 2;
4645 e5a0f69f 2018-08-18 stsp switch (ch) {
4647 1e37a5c2 2019-05-12 jcs s->done = 1;
4649 4deef56f 2021-09-02 naddy case 'g':
4650 4deef56f 2021-09-02 naddy case KEY_HOME:
4651 4deef56f 2021-09-02 naddy s->selected_line = 1;
4652 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4654 4deef56f 2021-09-02 naddy case 'G':
4655 4deef56f 2021-09-02 naddy case KEY_END:
4656 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4657 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4658 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4660 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4661 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4662 4deef56f 2021-09-02 naddy (view->nlines - 3);
4666 1e37a5c2 2019-05-12 jcs case KEY_UP:
4667 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4668 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4669 1e37a5c2 2019-05-12 jcs s->selected_line--;
4670 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4671 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4672 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4674 83cc4199 2022-06-13 stsp case CTRL('u'):
4675 83cc4199 2022-06-13 stsp nscroll /= 2;
4676 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4677 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4678 ea025d1d 2020-02-22 naddy case CTRL('b'):
4679 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4680 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
4683 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
4684 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
4686 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4689 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4690 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4691 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4692 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4693 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4694 1e37a5c2 2019-05-12 jcs s->selected_line++;
4695 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4696 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4697 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4700 1e37a5c2 2019-05-12 jcs case 'p': {
4701 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4702 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4703 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4704 1e37a5c2 2019-05-12 jcs if (id == NULL)
4706 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4707 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
4708 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4709 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4710 1e37a5c2 2019-05-12 jcs int obj_type;
4711 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4712 1e37a5c2 2019-05-12 jcs s->repo, id);
4715 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4716 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4717 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4718 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4721 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4722 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
4723 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
4726 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4727 a44927cc 2022-04-07 stsp pcommit, s->path);
4728 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
4729 e5a0f69f 2018-08-18 stsp if (err) {
4730 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4731 1e37a5c2 2019-05-12 jcs err = NULL;
4732 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4735 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4737 1e37a5c2 2019-05-12 jcs free(blob_id);
4738 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4739 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4740 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4743 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4744 d7b5a0e8 2022-04-20 stsp &pid->id);
4745 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4747 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4748 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
4750 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4755 1e37a5c2 2019-05-12 jcs s->done = 1;
4756 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4757 1e37a5c2 2019-05-12 jcs s->done = 0;
4758 1e37a5c2 2019-05-12 jcs if (thread_err)
4760 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4761 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4762 a5388363 2020-12-01 naddy err = run_blame(view);
4767 1e37a5c2 2019-05-12 jcs case 'B': {
4768 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4769 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4770 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
4772 1e37a5c2 2019-05-12 jcs s->done = 1;
4773 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4774 1e37a5c2 2019-05-12 jcs s->done = 0;
4775 1e37a5c2 2019-05-12 jcs if (thread_err)
4777 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4778 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4779 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4780 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4781 a5388363 2020-12-01 naddy err = run_blame(view);
4786 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4787 1e37a5c2 2019-05-12 jcs case '\r': {
4788 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4789 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4790 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4791 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4792 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4793 1e37a5c2 2019-05-12 jcs if (id == NULL)
4795 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4798 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4799 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4800 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4801 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4802 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4803 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4804 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4805 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4808 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4809 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4810 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4812 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4815 e78dc838 2020-12-04 stsp view->focussed = 0;
4816 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4817 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4818 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4821 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4822 e78dc838 2020-12-04 stsp view->focus_child = 1;
4824 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4829 83cc4199 2022-06-13 stsp case CTRL('d'):
4830 83cc4199 2022-06-13 stsp nscroll /= 2;
4831 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4832 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4833 ea025d1d 2020-02-22 naddy case CTRL('f'):
4835 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4836 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4837 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4840 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4841 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4842 83cc4199 2022-06-13 stsp s->selected_line +=
4843 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
4844 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
4846 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
4847 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
4849 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4850 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
4852 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4853 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4854 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4855 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4861 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4864 a70480e0 2018-06-23 stsp static const struct got_error *
4865 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4867 a70480e0 2018-06-23 stsp const struct got_error *error;
4868 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4869 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4870 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4871 0587e10c 2020-07-23 stsp char *link_target = NULL;
4872 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4873 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4874 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4876 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4878 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4879 a70480e0 2018-06-23 stsp switch (ch) {
4881 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4884 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4885 69069811 2018-08-02 stsp if (repo_path == NULL)
4886 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4890 17020d27 2019-03-07 stsp usage_blame();
4891 a70480e0 2018-06-23 stsp /* NOTREACHED */
4895 a70480e0 2018-06-23 stsp argc -= optind;
4896 a70480e0 2018-06-23 stsp argv += optind;
4898 f135c941 2020-02-20 stsp if (argc != 1)
4899 a70480e0 2018-06-23 stsp usage_blame();
4901 69069811 2018-08-02 stsp if (repo_path == NULL) {
4902 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4903 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4904 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4905 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4906 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4907 c156c7a4 2020-12-18 stsp goto done;
4908 f135c941 2020-02-20 stsp if (worktree)
4909 eb41ed75 2019-02-05 stsp repo_path =
4910 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4912 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4913 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4914 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4915 c156c7a4 2020-12-18 stsp goto done;
4919 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4920 c02c541e 2019-03-29 stsp if (error != NULL)
4921 8e94dd5b 2019-01-04 stsp goto done;
4923 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4924 f135c941 2020-02-20 stsp worktree);
4925 c02c541e 2019-03-29 stsp if (error)
4926 92205607 2019-01-04 stsp goto done;
4928 f135c941 2020-02-20 stsp init_curses();
4930 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4931 51a10b52 2020-12-26 stsp if (error)
4932 51a10b52 2020-12-26 stsp goto done;
4934 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4935 eb41ed75 2019-02-05 stsp if (error)
4936 69069811 2018-08-02 stsp goto done;
4938 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4939 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4940 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4941 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4942 a70480e0 2018-06-23 stsp if (error != NULL)
4943 66b4983c 2018-06-23 stsp goto done;
4944 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4945 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4947 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4948 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4950 a19e88aa 2018-06-23 stsp if (error != NULL)
4951 8b473291 2019-02-21 stsp goto done;
4953 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4954 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4955 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4956 e1cd8fed 2018-08-01 stsp goto done;
4959 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4960 a44927cc 2022-04-07 stsp if (error)
4961 a44927cc 2022-04-07 stsp goto done;
4963 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4964 a44927cc 2022-04-07 stsp commit, repo);
4965 7cbe629d 2018-08-04 stsp if (error)
4966 7cbe629d 2018-08-04 stsp goto done;
4968 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4969 78756c87 2020-11-24 stsp commit_id, repo);
4970 0587e10c 2020-07-23 stsp if (error)
4971 0587e10c 2020-07-23 stsp goto done;
4972 12314ad4 2019-08-31 stsp if (worktree) {
4973 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4974 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4975 12314ad4 2019-08-31 stsp worktree = NULL;
4977 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4979 69069811 2018-08-02 stsp free(repo_path);
4980 f135c941 2020-02-20 stsp free(in_repo_path);
4981 0587e10c 2020-07-23 stsp free(link_target);
4982 69069811 2018-08-02 stsp free(cwd);
4983 a70480e0 2018-06-23 stsp free(commit_id);
4984 a44927cc 2022-04-07 stsp if (commit)
4985 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4986 eb41ed75 2019-02-05 stsp if (worktree)
4987 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4988 1d0f4054 2021-06-17 stsp if (repo) {
4989 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4990 1d0f4054 2021-06-17 stsp if (error == NULL)
4991 1d0f4054 2021-06-17 stsp error = close_err;
4993 51a10b52 2020-12-26 stsp tog_free_refs();
4994 a70480e0 2018-06-23 stsp return error;
4997 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4998 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5000 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5001 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5002 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5003 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5004 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5005 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5006 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5008 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5010 f7d12f7e 2018-08-01 stsp werase(view->window);
5012 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5013 ffd1d5e5 2018-06-23 stsp return NULL;
5015 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5017 ffd1d5e5 2018-06-23 stsp return err;
5018 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5019 a3404814 2018-09-02 stsp wstandout(view->window);
5020 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5022 11b20872 2019-11-08 stsp wattr_on(view->window,
5023 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5024 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5026 11b20872 2019-11-08 stsp wattr_off(view->window,
5027 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5028 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5029 a3404814 2018-09-02 stsp wstandend(view->window);
5030 2550e4c3 2018-07-13 stsp free(wline);
5031 2550e4c3 2018-07-13 stsp wline = NULL;
5032 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5033 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5034 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5035 ffd1d5e5 2018-06-23 stsp return NULL;
5036 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
5038 ce52c690 2018-06-23 stsp return err;
5039 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5040 2550e4c3 2018-07-13 stsp free(wline);
5041 2550e4c3 2018-07-13 stsp wline = NULL;
5042 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5043 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5044 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5045 ffd1d5e5 2018-06-23 stsp return NULL;
5046 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5047 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5048 a1eca9bb 2018-06-23 stsp return NULL;
5050 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5051 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5052 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5053 0cf4efb1 2018-09-29 stsp if (view->focussed)
5054 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5055 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5057 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5058 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5059 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5060 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5061 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5062 ffd1d5e5 2018-06-23 stsp return NULL;
5066 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5069 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5070 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5071 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5072 848d6979 2019-08-12 stsp const char *modestr = "";
5073 56e0773d 2019-11-28 stsp mode_t mode;
5075 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5076 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5078 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5079 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5080 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5082 638f9024 2019-05-13 stsp return got_error_from_errno(
5083 230a42bd 2019-05-11 jcs "got_object_id_str");
5085 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5086 63c5ca5d 2019-08-24 stsp modestr = "$";
5087 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5090 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5091 d86d3b18 2020-12-01 naddy te, s->repo);
5092 0d6c6ee3 2020-05-20 stsp if (err) {
5093 0d6c6ee3 2020-05-20 stsp free(id_str);
5094 0d6c6ee3 2020-05-20 stsp return err;
5096 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5097 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5098 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5100 848d6979 2019-08-12 stsp modestr = "@";
5102 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5103 848d6979 2019-08-12 stsp modestr = "/";
5104 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5105 848d6979 2019-08-12 stsp modestr = "*";
5106 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5107 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5108 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5109 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5110 1d13200f 2018-07-12 stsp free(id_str);
5111 0d6c6ee3 2020-05-20 stsp free(link_target);
5112 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5114 1d13200f 2018-07-12 stsp free(id_str);
5115 0d6c6ee3 2020-05-20 stsp free(link_target);
5116 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5117 ffd1d5e5 2018-06-23 stsp if (err) {
5118 ffd1d5e5 2018-06-23 stsp free(line);
5121 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5122 0cf4efb1 2018-09-29 stsp if (view->focussed)
5123 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5124 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5126 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5128 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5129 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5130 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5132 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5133 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5134 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5135 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5136 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5137 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5138 ffd1d5e5 2018-06-23 stsp free(line);
5139 2550e4c3 2018-07-13 stsp free(wline);
5140 2550e4c3 2018-07-13 stsp wline = NULL;
5142 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5143 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5144 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5148 ffd1d5e5 2018-06-23 stsp return err;
5151 ffd1d5e5 2018-06-23 stsp static void
5152 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5154 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5155 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5156 fa86c4bf 2020-11-29 stsp int i = 0;
5158 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5161 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5162 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5163 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5164 fa86c4bf 2020-11-29 stsp if (!isroot)
5165 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5168 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5169 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5173 fa86c4bf 2020-11-29 stsp static void
5174 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5176 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5177 ffd1d5e5 2018-06-23 stsp int n = 0;
5179 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5180 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5181 694d3271 2020-12-01 naddy s->first_displayed_entry);
5183 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5185 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5186 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5187 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5188 768394f3 2019-01-24 stsp if (last) {
5189 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5190 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5195 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5196 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5197 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5199 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5200 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5201 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5203 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5204 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5205 56e0773d 2019-11-28 stsp + 1 /* slash */;
5207 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5209 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5210 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5211 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5213 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5214 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5215 d9765a41 2018-06-23 stsp while (pt) {
5216 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5217 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5218 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5219 cb2ebc8a 2018-06-23 stsp goto done;
5221 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5222 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5223 cb2ebc8a 2018-06-23 stsp goto done;
5225 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5228 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5229 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5230 ce52c690 2018-06-23 stsp goto done;
5234 ce52c690 2018-06-23 stsp if (err) {
5235 ce52c690 2018-06-23 stsp free(*path);
5236 ce52c690 2018-06-23 stsp *path = NULL;
5238 ce52c690 2018-06-23 stsp return err;
5241 ce52c690 2018-06-23 stsp static const struct got_error *
5242 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5243 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5244 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5246 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5247 ce52c690 2018-06-23 stsp char *path;
5248 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5250 a0de39f3 2019-08-09 stsp *new_view = NULL;
5252 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5254 ce52c690 2018-06-23 stsp return err;
5256 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5257 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5258 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5259 83ce39e3 2019-08-12 stsp goto done;
5262 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5263 e5a0f69f 2018-08-18 stsp if (err) {
5264 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5265 fc06ba56 2019-08-22 stsp err = NULL;
5266 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5268 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5270 83ce39e3 2019-08-12 stsp free(path);
5271 69efd4c4 2018-07-18 stsp return err;
5274 69efd4c4 2018-07-18 stsp static const struct got_error *
5275 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5276 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5278 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5279 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5280 69efd4c4 2018-07-18 stsp char *path;
5282 a0de39f3 2019-08-09 stsp *new_view = NULL;
5284 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5285 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5286 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5288 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5290 69efd4c4 2018-07-18 stsp return err;
5292 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5295 e5a0f69f 2018-08-18 stsp view_close(log_view);
5297 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5298 cb2ebc8a 2018-06-23 stsp free(path);
5299 cb2ebc8a 2018-06-23 stsp return err;
5302 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5303 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5304 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5306 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5307 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5308 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5309 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5311 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5312 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5314 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5315 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5316 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5318 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5320 bc573f3b 2021-07-10 stsp goto done;
5323 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5324 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5325 bc573f3b 2021-07-10 stsp * closed on demand.
5327 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5328 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5330 bc573f3b 2021-07-10 stsp goto done;
5331 bc573f3b 2021-07-10 stsp s->tree = s->root;
5333 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5334 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5335 ffd1d5e5 2018-06-23 stsp goto done;
5337 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5338 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5339 ffd1d5e5 2018-06-23 stsp goto done;
5342 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5343 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5344 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5345 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5346 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5347 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5348 9cd7cbd1 2020-12-07 stsp goto done;
5351 fb2756b9 2018-08-04 stsp s->repo = repo;
5353 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5354 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5355 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5356 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5358 c0b01bdb 2019-11-08 stsp goto done;
5359 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5360 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5362 c0b01bdb 2019-11-08 stsp goto done;
5363 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5364 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5365 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5367 c0b01bdb 2019-11-08 stsp goto done;
5369 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5370 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5371 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5373 11b20872 2019-11-08 stsp goto done;
5375 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5376 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5378 c0b01bdb 2019-11-08 stsp goto done;
5381 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5382 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5383 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5384 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5385 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5387 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5388 bc573f3b 2021-07-10 stsp if (commit)
5389 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5391 bc573f3b 2021-07-10 stsp close_tree_view(view);
5392 ad80ab7b 2018-08-04 stsp return err;
5395 e5a0f69f 2018-08-18 stsp static const struct got_error *
5396 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5398 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5400 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5401 fb2756b9 2018-08-04 stsp free(s->tree_label);
5402 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5403 6484ec90 2018-09-29 stsp free(s->commit_id);
5404 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5405 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5406 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5407 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5408 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5409 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5410 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5411 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5412 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5413 ad80ab7b 2018-08-04 stsp free(parent);
5416 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5417 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5418 bc573f3b 2021-07-10 stsp if (s->root)
5419 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5420 7c32bd05 2019-06-22 stsp return NULL;
5423 7c32bd05 2019-06-22 stsp static const struct got_error *
5424 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5426 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5428 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5429 7c32bd05 2019-06-22 stsp return NULL;
5432 7c32bd05 2019-06-22 stsp static int
5433 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5435 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5437 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5441 7c32bd05 2019-06-22 stsp static const struct got_error *
5442 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5444 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5445 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5447 7c32bd05 2019-06-22 stsp if (!view->searching) {
5448 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5449 7c32bd05 2019-06-22 stsp return NULL;
5452 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5453 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5454 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5455 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5456 56e0773d 2019-11-28 stsp s->selected_entry);
5458 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5460 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5461 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5463 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5464 56e0773d 2019-11-28 stsp s->selected_entry);
5467 487cd7d2 2021-12-17 stsp if (s->selected_entry)
5468 487cd7d2 2021-12-17 stsp te = s->selected_entry;
5469 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
5470 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5472 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5475 7c32bd05 2019-06-22 stsp while (1) {
5476 56e0773d 2019-11-28 stsp if (te == NULL) {
5477 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5478 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5479 ac66afb8 2019-06-24 stsp return NULL;
5481 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5482 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5484 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5487 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5488 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5489 56e0773d 2019-11-28 stsp s->matched_entry = te;
5493 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5494 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5496 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5499 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5500 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5501 7c32bd05 2019-06-22 stsp s->selected = 0;
5504 e5a0f69f 2018-08-18 stsp return NULL;
5507 ad80ab7b 2018-08-04 stsp static const struct got_error *
5508 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5510 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5511 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5512 e5a0f69f 2018-08-18 stsp char *parent_path;
5514 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5516 e5a0f69f 2018-08-18 stsp return err;
5518 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5519 e5a0f69f 2018-08-18 stsp free(parent_path);
5521 669b5ffa 2018-10-07 stsp view_vborder(view);
5522 e5a0f69f 2018-08-18 stsp return err;
5525 e5a0f69f 2018-08-18 stsp static const struct got_error *
5526 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5528 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5529 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5530 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5531 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5532 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
5534 e5a0f69f 2018-08-18 stsp switch (ch) {
5536 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5539 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5541 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5542 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5543 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5544 e78dc838 2020-12-04 stsp view->focussed = 0;
5545 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5546 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5547 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5549 1e37a5c2 2019-05-12 jcs return err;
5550 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5551 e78dc838 2020-12-04 stsp view->focus_child = 1;
5553 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5556 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5557 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5558 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5559 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5560 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5561 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5562 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5563 152c1c93 2020-11-29 stsp if (err) {
5564 152c1c93 2020-11-29 stsp view_close(ref_view);
5565 152c1c93 2020-11-29 stsp return err;
5567 e78dc838 2020-12-04 stsp view->focussed = 0;
5568 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5569 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5570 152c1c93 2020-11-29 stsp err = view_close_child(view);
5572 152c1c93 2020-11-29 stsp return err;
5573 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5574 e78dc838 2020-12-04 stsp view->focus_child = 1;
5576 152c1c93 2020-11-29 stsp *new_view = ref_view;
5578 e4526bf5 2021-09-03 naddy case 'g':
5579 e4526bf5 2021-09-03 naddy case KEY_HOME:
5580 e4526bf5 2021-09-03 naddy s->selected = 0;
5581 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5582 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5583 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5585 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5587 e4526bf5 2021-09-03 naddy case 'G':
5588 e4526bf5 2021-09-03 naddy case KEY_END:
5589 e4526bf5 2021-09-03 naddy s->selected = 0;
5590 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5591 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5592 e4526bf5 2021-09-03 naddy if (te == NULL) {
5593 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5594 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5599 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5600 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5602 e4526bf5 2021-09-03 naddy if (n > 0)
5603 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5606 1e37a5c2 2019-05-12 jcs case KEY_UP:
5607 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5608 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5609 1e37a5c2 2019-05-12 jcs s->selected--;
5612 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5614 83cc4199 2022-06-13 stsp case CTRL('u'):
5615 83cc4199 2022-06-13 stsp nscroll /= 2;
5616 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5617 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5618 ea025d1d 2020-02-22 naddy case CTRL('b'):
5619 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5620 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5621 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5622 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5624 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5625 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5627 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
5630 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5631 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5632 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5633 1e37a5c2 2019-05-12 jcs s->selected++;
5636 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5638 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5640 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5642 83cc4199 2022-06-13 stsp case CTRL('d'):
5643 83cc4199 2022-06-13 stsp nscroll /= 2;
5644 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5645 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5646 ea025d1d 2020-02-22 naddy case CTRL('f'):
5647 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5649 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5650 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5651 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
5652 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
5655 83cc4199 2022-06-13 stsp tree_scroll_down(s, nscroll);
5657 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5659 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5660 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5661 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5662 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5663 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5665 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5666 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5668 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5669 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5670 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5671 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5672 1e37a5c2 2019-05-12 jcs s->selected_entry =
5673 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5674 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5675 1e37a5c2 2019-05-12 jcs free(parent);
5676 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5677 56e0773d 2019-11-28 stsp s->selected_entry))) {
5678 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5679 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5680 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5683 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5684 941e9f74 2019-05-21 stsp if (err) {
5685 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5688 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5689 56e0773d 2019-11-28 stsp s->selected_entry))) {
5690 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5691 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5692 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5694 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5695 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5696 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5699 e78dc838 2020-12-04 stsp view->focussed = 0;
5700 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5701 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5702 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5704 669b5ffa 2018-10-07 stsp return err;
5705 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5706 e78dc838 2020-12-04 stsp view->focus_child = 1;
5708 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5711 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5712 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5713 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5719 ffd1d5e5 2018-06-23 stsp return err;
5722 ffd1d5e5 2018-06-23 stsp __dead static void
5723 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5726 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5727 ffd1d5e5 2018-06-23 stsp getprogname());
5731 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5732 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5734 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5735 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5736 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5737 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5738 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5739 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5740 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5741 4e97c21c 2020-12-06 stsp char *label = NULL;
5742 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5743 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5745 5221c383 2018-08-01 stsp struct tog_view *view;
5747 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5748 ffd1d5e5 2018-06-23 stsp switch (ch) {
5750 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5753 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5754 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5755 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5759 e99e2d15 2020-11-24 naddy usage_tree();
5760 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5764 ffd1d5e5 2018-06-23 stsp argc -= optind;
5765 ffd1d5e5 2018-06-23 stsp argv += optind;
5767 55cccc34 2020-02-20 stsp if (argc > 1)
5768 e99e2d15 2020-11-24 naddy usage_tree();
5770 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5771 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5772 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5773 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5774 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5775 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5776 c156c7a4 2020-12-18 stsp goto done;
5777 55cccc34 2020-02-20 stsp if (worktree)
5778 52185f70 2019-02-05 stsp repo_path =
5779 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5781 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5782 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5783 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5784 c156c7a4 2020-12-18 stsp goto done;
5788 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5789 c02c541e 2019-03-29 stsp if (error != NULL)
5790 52185f70 2019-02-05 stsp goto done;
5792 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5793 55cccc34 2020-02-20 stsp repo, worktree);
5794 55cccc34 2020-02-20 stsp if (error)
5795 55cccc34 2020-02-20 stsp goto done;
5797 55cccc34 2020-02-20 stsp init_curses();
5799 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5800 c02c541e 2019-03-29 stsp if (error)
5801 52185f70 2019-02-05 stsp goto done;
5803 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5804 51a10b52 2020-12-26 stsp if (error)
5805 51a10b52 2020-12-26 stsp goto done;
5807 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5808 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5809 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5810 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5811 4e97c21c 2020-12-06 stsp if (error)
5812 4e97c21c 2020-12-06 stsp goto done;
5813 4e97c21c 2020-12-06 stsp head_ref_name = label;
5815 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5816 4e97c21c 2020-12-06 stsp if (error == NULL)
5817 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5818 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5819 4e97c21c 2020-12-06 stsp goto done;
5820 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5821 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5822 4e97c21c 2020-12-06 stsp if (error)
5823 4e97c21c 2020-12-06 stsp goto done;
5826 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5827 a44927cc 2022-04-07 stsp if (error)
5828 a44927cc 2022-04-07 stsp goto done;
5830 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5831 5221c383 2018-08-01 stsp if (view == NULL) {
5832 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5833 5221c383 2018-08-01 stsp goto done;
5835 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5836 ad80ab7b 2018-08-04 stsp if (error)
5837 ad80ab7b 2018-08-04 stsp goto done;
5838 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5839 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
5840 d91faf3b 2020-12-01 naddy in_repo_path);
5841 55cccc34 2020-02-20 stsp if (error)
5842 55cccc34 2020-02-20 stsp goto done;
5845 55cccc34 2020-02-20 stsp if (worktree) {
5846 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5847 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5848 55cccc34 2020-02-20 stsp worktree = NULL;
5850 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5852 52185f70 2019-02-05 stsp free(repo_path);
5853 e4a0e26d 2020-02-20 stsp free(cwd);
5854 ffd1d5e5 2018-06-23 stsp free(commit_id);
5855 4e97c21c 2020-12-06 stsp free(label);
5857 486cd271 2020-12-06 stsp got_ref_close(ref);
5858 1d0f4054 2021-06-17 stsp if (repo) {
5859 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5860 1d0f4054 2021-06-17 stsp if (error == NULL)
5861 1d0f4054 2021-06-17 stsp error = close_err;
5863 51a10b52 2020-12-26 stsp tog_free_refs();
5864 ffd1d5e5 2018-06-23 stsp return error;
5867 6458efa5 2020-11-24 stsp static const struct got_error *
5868 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5870 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5871 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5873 6458efa5 2020-11-24 stsp s->nrefs = 0;
5874 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5875 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
5876 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
5877 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
5878 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
5881 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5882 6458efa5 2020-11-24 stsp if (re == NULL)
5883 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5885 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5886 8924d611 2020-12-26 stsp if (re->ref == NULL)
5887 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5888 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5889 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5892 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5893 6458efa5 2020-11-24 stsp return NULL;
5897 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5899 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5901 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5902 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5903 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5904 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5909 6458efa5 2020-11-24 stsp static const struct got_error *
5910 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5912 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5913 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5915 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5916 6458efa5 2020-11-24 stsp s->repo = repo;
5918 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5919 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5921 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5923 6458efa5 2020-11-24 stsp return err;
5925 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5926 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5927 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5928 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5930 6458efa5 2020-11-24 stsp goto done;
5932 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5933 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5934 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5936 6458efa5 2020-11-24 stsp goto done;
5938 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5939 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5940 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5942 cc488aa7 2022-01-23 stsp goto done;
5944 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
5945 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
5946 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
5948 6458efa5 2020-11-24 stsp goto done;
5951 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5952 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5953 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5954 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5955 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5958 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5959 6458efa5 2020-11-24 stsp return err;
5962 6458efa5 2020-11-24 stsp static const struct got_error *
5963 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5965 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5967 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5968 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5970 6458efa5 2020-11-24 stsp return NULL;
5973 6458efa5 2020-11-24 stsp static const struct got_error *
5974 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5975 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5977 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5978 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5979 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5980 6458efa5 2020-11-24 stsp int obj_type;
5982 c42c9805 2020-11-24 stsp *commit_id = NULL;
5984 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5986 6458efa5 2020-11-24 stsp return err;
5988 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5990 6458efa5 2020-11-24 stsp goto done;
5992 6458efa5 2020-11-24 stsp switch (obj_type) {
5993 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5994 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5996 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5997 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5999 6458efa5 2020-11-24 stsp goto done;
6000 c42c9805 2020-11-24 stsp free(obj_id);
6001 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6002 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6004 6458efa5 2020-11-24 stsp goto done;
6005 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6006 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6007 c42c9805 2020-11-24 stsp goto done;
6009 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6010 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6011 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6012 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6013 c42c9805 2020-11-24 stsp goto done;
6017 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6023 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6024 c42c9805 2020-11-24 stsp if (err) {
6025 c42c9805 2020-11-24 stsp free(*commit_id);
6026 c42c9805 2020-11-24 stsp *commit_id = NULL;
6028 c42c9805 2020-11-24 stsp return err;
6031 c42c9805 2020-11-24 stsp static const struct got_error *
6032 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6033 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6035 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6036 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6037 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6039 c42c9805 2020-11-24 stsp *new_view = NULL;
6041 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6042 c42c9805 2020-11-24 stsp if (err) {
6043 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6044 c42c9805 2020-11-24 stsp return err;
6046 c42c9805 2020-11-24 stsp return NULL;
6049 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6050 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6051 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6052 6458efa5 2020-11-24 stsp goto done;
6055 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6056 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6059 6458efa5 2020-11-24 stsp view_close(log_view);
6061 6458efa5 2020-11-24 stsp *new_view = log_view;
6062 c42c9805 2020-11-24 stsp free(commit_id);
6063 6458efa5 2020-11-24 stsp return err;
6066 ce5b7c56 2019-07-09 stsp static void
6067 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6069 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6070 34ba6917 2020-11-30 stsp int i = 0;
6072 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6075 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6076 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6077 34ba6917 2020-11-30 stsp if (re == NULL)
6079 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6080 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6084 34ba6917 2020-11-30 stsp static void
6085 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6087 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6088 6458efa5 2020-11-24 stsp int n = 0;
6090 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6091 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6093 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6095 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6096 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6097 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6098 6458efa5 2020-11-24 stsp if (last) {
6099 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6100 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6105 6458efa5 2020-11-24 stsp static const struct got_error *
6106 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6108 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6110 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6111 6458efa5 2020-11-24 stsp return NULL;
6114 6458efa5 2020-11-24 stsp static int
6115 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6117 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6119 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6123 6458efa5 2020-11-24 stsp static const struct got_error *
6124 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6126 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6127 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6129 6458efa5 2020-11-24 stsp if (!view->searching) {
6130 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6131 6458efa5 2020-11-24 stsp return NULL;
6134 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6135 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6136 6458efa5 2020-11-24 stsp if (s->selected_entry)
6137 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6139 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6140 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6142 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6143 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6145 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6146 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6149 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6150 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6151 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6152 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6154 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6157 6458efa5 2020-11-24 stsp while (1) {
6158 6458efa5 2020-11-24 stsp if (re == NULL) {
6159 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6160 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6161 6458efa5 2020-11-24 stsp return NULL;
6163 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6164 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6166 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6169 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6170 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6171 6458efa5 2020-11-24 stsp s->matched_entry = re;
6175 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6176 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6178 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6181 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6182 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6183 6458efa5 2020-11-24 stsp s->selected = 0;
6186 6458efa5 2020-11-24 stsp return NULL;
6189 6458efa5 2020-11-24 stsp static const struct got_error *
6190 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6192 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6193 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6194 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6195 6458efa5 2020-11-24 stsp char *line = NULL;
6196 6458efa5 2020-11-24 stsp wchar_t *wline;
6197 6458efa5 2020-11-24 stsp struct tog_color *tc;
6198 6458efa5 2020-11-24 stsp int width, n;
6199 6458efa5 2020-11-24 stsp int limit = view->nlines;
6201 6458efa5 2020-11-24 stsp werase(view->window);
6203 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6205 6458efa5 2020-11-24 stsp if (limit == 0)
6206 6458efa5 2020-11-24 stsp return NULL;
6208 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6210 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6211 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6212 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6214 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6215 6458efa5 2020-11-24 stsp if (err) {
6216 6458efa5 2020-11-24 stsp free(line);
6217 6458efa5 2020-11-24 stsp return err;
6219 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6220 6458efa5 2020-11-24 stsp wstandout(view->window);
6221 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6222 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6223 6458efa5 2020-11-24 stsp wstandend(view->window);
6224 6458efa5 2020-11-24 stsp free(wline);
6225 6458efa5 2020-11-24 stsp wline = NULL;
6226 6458efa5 2020-11-24 stsp free(line);
6227 6458efa5 2020-11-24 stsp line = NULL;
6228 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6229 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6230 6458efa5 2020-11-24 stsp if (--limit <= 0)
6231 6458efa5 2020-11-24 stsp return NULL;
6234 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6235 6458efa5 2020-11-24 stsp char *line = NULL;
6237 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6238 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6239 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6240 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6241 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6242 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6243 6458efa5 2020-11-24 stsp struct got_object_id *id;
6244 6458efa5 2020-11-24 stsp char *id_str;
6245 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6247 6458efa5 2020-11-24 stsp return err;
6248 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6249 6458efa5 2020-11-24 stsp if (err) {
6251 6458efa5 2020-11-24 stsp return err;
6253 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6254 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6255 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6257 6458efa5 2020-11-24 stsp free(id_str);
6258 6458efa5 2020-11-24 stsp return err;
6261 6458efa5 2020-11-24 stsp free(id_str);
6263 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6264 6458efa5 2020-11-24 stsp if (line == NULL)
6265 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6268 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6269 6458efa5 2020-11-24 stsp if (err) {
6270 6458efa5 2020-11-24 stsp free(line);
6271 6458efa5 2020-11-24 stsp return err;
6273 6458efa5 2020-11-24 stsp if (n == s->selected) {
6274 6458efa5 2020-11-24 stsp if (view->focussed)
6275 6458efa5 2020-11-24 stsp wstandout(view->window);
6276 6458efa5 2020-11-24 stsp s->selected_entry = re;
6278 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6280 6458efa5 2020-11-24 stsp wattr_on(view->window,
6281 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6282 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6284 6458efa5 2020-11-24 stsp wattr_off(view->window,
6285 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6286 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6287 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6288 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6289 6458efa5 2020-11-24 stsp wstandend(view->window);
6290 6458efa5 2020-11-24 stsp free(line);
6291 6458efa5 2020-11-24 stsp free(wline);
6292 6458efa5 2020-11-24 stsp wline = NULL;
6294 6458efa5 2020-11-24 stsp s->ndisplayed++;
6295 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6298 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6301 6458efa5 2020-11-24 stsp view_vborder(view);
6302 6458efa5 2020-11-24 stsp return err;
6305 6458efa5 2020-11-24 stsp static const struct got_error *
6306 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6307 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6309 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6310 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6311 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6313 c42c9805 2020-11-24 stsp *new_view = NULL;
6315 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6316 c42c9805 2020-11-24 stsp if (err) {
6317 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6318 c42c9805 2020-11-24 stsp return err;
6320 c42c9805 2020-11-24 stsp return NULL;
6324 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6325 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6326 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6327 c42c9805 2020-11-24 stsp goto done;
6330 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6331 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6333 c42c9805 2020-11-24 stsp goto done;
6335 c42c9805 2020-11-24 stsp *new_view = tree_view;
6337 c42c9805 2020-11-24 stsp free(commit_id);
6338 c42c9805 2020-11-24 stsp return err;
6340 c42c9805 2020-11-24 stsp static const struct got_error *
6341 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6343 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6344 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6345 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6346 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6347 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
6349 6458efa5 2020-11-24 stsp switch (ch) {
6351 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6354 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
6355 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6356 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
6357 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
6360 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
6361 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
6362 50617b77 2021-11-20 stsp &tog_refs, s->repo);
6365 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
6366 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
6368 6458efa5 2020-11-24 stsp case KEY_ENTER:
6369 6458efa5 2020-11-24 stsp case '\r':
6370 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6372 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6373 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6374 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6376 e78dc838 2020-12-04 stsp view->focussed = 0;
6377 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6378 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6379 6458efa5 2020-11-24 stsp err = view_close_child(view);
6381 6458efa5 2020-11-24 stsp return err;
6382 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6383 e78dc838 2020-12-04 stsp view->focus_child = 1;
6385 6458efa5 2020-11-24 stsp *new_view = log_view;
6388 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6390 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6391 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6392 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6394 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6396 e78dc838 2020-12-04 stsp view->focussed = 0;
6397 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6398 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6399 c42c9805 2020-11-24 stsp err = view_close_child(view);
6401 c42c9805 2020-11-24 stsp return err;
6402 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6403 e78dc838 2020-12-04 stsp view->focus_child = 1;
6405 c42c9805 2020-11-24 stsp *new_view = tree_view;
6407 e4526bf5 2021-09-03 naddy case 'g':
6408 e4526bf5 2021-09-03 naddy case KEY_HOME:
6409 e4526bf5 2021-09-03 naddy s->selected = 0;
6410 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6412 e4526bf5 2021-09-03 naddy case 'G':
6413 e4526bf5 2021-09-03 naddy case KEY_END:
6414 e4526bf5 2021-09-03 naddy s->selected = 0;
6415 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6416 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6417 e4526bf5 2021-09-03 naddy if (re == NULL)
6419 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6420 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6422 e4526bf5 2021-09-03 naddy if (n > 0)
6423 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6426 6458efa5 2020-11-24 stsp case KEY_UP:
6427 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6428 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6429 6458efa5 2020-11-24 stsp s->selected--;
6432 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6434 83cc4199 2022-06-13 stsp case CTRL('u'):
6435 83cc4199 2022-06-13 stsp nscroll /= 2;
6436 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6437 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6438 6458efa5 2020-11-24 stsp case CTRL('b'):
6439 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6440 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
6441 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
6444 6458efa5 2020-11-24 stsp case KEY_DOWN:
6445 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6446 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6447 6458efa5 2020-11-24 stsp s->selected++;
6450 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6451 6458efa5 2020-11-24 stsp /* can't scroll any further */
6453 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6455 83cc4199 2022-06-13 stsp case CTRL('d'):
6456 83cc4199 2022-06-13 stsp nscroll /= 2;
6457 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6458 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6459 6458efa5 2020-11-24 stsp case CTRL('f'):
6460 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6461 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6462 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6463 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6464 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6467 83cc4199 2022-06-13 stsp ref_scroll_down(s, nscroll);
6469 6458efa5 2020-11-24 stsp case CTRL('l'):
6470 8924d611 2020-12-26 stsp tog_free_refs();
6471 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
6474 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6475 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6477 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6478 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6479 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6485 6458efa5 2020-11-24 stsp return err;
6488 6458efa5 2020-11-24 stsp __dead static void
6489 6458efa5 2020-11-24 stsp usage_ref(void)
6492 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6493 6458efa5 2020-11-24 stsp getprogname());
6497 6458efa5 2020-11-24 stsp static const struct got_error *
6498 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6500 6458efa5 2020-11-24 stsp const struct got_error *error;
6501 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6502 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6503 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6505 6458efa5 2020-11-24 stsp struct tog_view *view;
6507 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6508 6458efa5 2020-11-24 stsp switch (ch) {
6510 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6511 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6512 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6516 e99e2d15 2020-11-24 naddy usage_ref();
6517 6458efa5 2020-11-24 stsp /* NOTREACHED */
6521 6458efa5 2020-11-24 stsp argc -= optind;
6522 6458efa5 2020-11-24 stsp argv += optind;
6524 6458efa5 2020-11-24 stsp if (argc > 1)
6525 e99e2d15 2020-11-24 naddy usage_ref();
6527 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6528 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6529 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6530 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6531 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6532 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6533 c156c7a4 2020-12-18 stsp goto done;
6534 6458efa5 2020-11-24 stsp if (worktree)
6535 6458efa5 2020-11-24 stsp repo_path =
6536 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6538 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6539 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6540 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6541 c156c7a4 2020-12-18 stsp goto done;
6545 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6546 6458efa5 2020-11-24 stsp if (error != NULL)
6547 6458efa5 2020-11-24 stsp goto done;
6549 6458efa5 2020-11-24 stsp init_curses();
6551 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6552 51a10b52 2020-12-26 stsp if (error)
6553 51a10b52 2020-12-26 stsp goto done;
6555 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6556 6458efa5 2020-11-24 stsp if (error)
6557 6458efa5 2020-11-24 stsp goto done;
6559 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6560 6458efa5 2020-11-24 stsp if (view == NULL) {
6561 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6562 6458efa5 2020-11-24 stsp goto done;
6565 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6566 6458efa5 2020-11-24 stsp if (error)
6567 6458efa5 2020-11-24 stsp goto done;
6569 6458efa5 2020-11-24 stsp if (worktree) {
6570 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6571 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6572 6458efa5 2020-11-24 stsp worktree = NULL;
6574 6458efa5 2020-11-24 stsp error = view_loop(view);
6576 6458efa5 2020-11-24 stsp free(repo_path);
6577 6458efa5 2020-11-24 stsp free(cwd);
6578 1d0f4054 2021-06-17 stsp if (repo) {
6579 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6580 1d0f4054 2021-06-17 stsp if (close_err)
6581 1d0f4054 2021-06-17 stsp error = close_err;
6583 51a10b52 2020-12-26 stsp tog_free_refs();
6584 6458efa5 2020-11-24 stsp return error;
6587 6458efa5 2020-11-24 stsp static void
6588 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6592 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6593 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6594 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
6595 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6597 6879ba42 2020-10-01 naddy fputc('\n', fp);
6600 4ed7e80c 2018-05-20 stsp __dead static void
6601 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6603 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6605 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6606 6879ba42 2020-10-01 naddy getprogname());
6607 6879ba42 2020-10-01 naddy if (hflag) {
6608 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6609 6879ba42 2020-10-01 naddy list_commands(fp);
6611 6879ba42 2020-10-01 naddy exit(status);
6614 c2301be8 2018-04-30 stsp static char **
6615 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6617 ee85c5e8 2020-02-29 stsp va_list ap;
6618 c2301be8 2018-04-30 stsp char **argv;
6621 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6623 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6624 c2301be8 2018-04-30 stsp if (argv == NULL)
6625 c2301be8 2018-04-30 stsp err(1, "calloc");
6626 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6627 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6628 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6629 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6632 ee85c5e8 2020-02-29 stsp va_end(ap);
6633 c2301be8 2018-04-30 stsp return argv;
6637 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6638 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6639 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6640 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6641 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6643 ee85c5e8 2020-02-29 stsp static const struct got_error *
6644 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6646 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6647 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6648 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6649 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6650 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6651 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6652 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6653 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6655 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6656 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6657 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6659 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6660 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6661 ee85c5e8 2020-02-29 stsp goto done;
6663 ee85c5e8 2020-02-29 stsp if (worktree)
6664 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6666 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6667 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6668 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6669 ee85c5e8 2020-02-29 stsp goto done;
6672 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6673 ee85c5e8 2020-02-29 stsp if (error != NULL)
6674 ee85c5e8 2020-02-29 stsp goto done;
6676 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6677 ee85c5e8 2020-02-29 stsp repo, worktree);
6678 ee85c5e8 2020-02-29 stsp if (error)
6679 ee85c5e8 2020-02-29 stsp goto done;
6681 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6682 84de9106 2020-12-26 stsp if (error)
6683 84de9106 2020-12-26 stsp goto done;
6684 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6685 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6686 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6687 ee85c5e8 2020-02-29 stsp if (error)
6688 ee85c5e8 2020-02-29 stsp goto done;
6690 ee85c5e8 2020-02-29 stsp if (worktree) {
6691 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6692 ee85c5e8 2020-02-29 stsp worktree = NULL;
6695 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6696 a44927cc 2022-04-07 stsp if (error)
6697 a44927cc 2022-04-07 stsp goto done;
6699 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6700 ee85c5e8 2020-02-29 stsp if (error) {
6701 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6702 ee85c5e8 2020-02-29 stsp goto done;
6703 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6704 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6705 6879ba42 2020-10-01 naddy usage(1, 1);
6706 ee85c5e8 2020-02-29 stsp /* not reached */
6709 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6710 1d0f4054 2021-06-17 stsp if (error == NULL)
6711 1d0f4054 2021-06-17 stsp error = close_err;
6712 ee85c5e8 2020-02-29 stsp repo = NULL;
6714 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6715 ee85c5e8 2020-02-29 stsp if (error)
6716 ee85c5e8 2020-02-29 stsp goto done;
6718 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6720 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6721 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6723 1d0f4054 2021-06-17 stsp if (repo) {
6724 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6725 1d0f4054 2021-06-17 stsp if (error == NULL)
6726 1d0f4054 2021-06-17 stsp error = close_err;
6728 a44927cc 2022-04-07 stsp if (commit)
6729 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6730 ee85c5e8 2020-02-29 stsp if (worktree)
6731 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6733 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6734 ee85c5e8 2020-02-29 stsp free(commit_id);
6735 ee85c5e8 2020-02-29 stsp free(cwd);
6736 ee85c5e8 2020-02-29 stsp free(repo_path);
6737 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6738 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6740 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6741 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6742 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6744 87670572 2020-12-26 naddy tog_free_refs();
6745 ee85c5e8 2020-02-29 stsp return error;
6749 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6751 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6752 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6753 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6754 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6755 3e166534 2022-02-16 naddy static const struct option longopts[] = {
6756 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6757 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6760 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6762 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6763 9f7d7167 2018-04-29 stsp switch (ch) {
6765 9f7d7167 2018-04-29 stsp hflag = 1;
6768 53ccebc2 2019-07-30 stsp Vflag = 1;
6771 6879ba42 2020-10-01 naddy usage(hflag, 1);
6772 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6776 9f7d7167 2018-04-29 stsp argc -= optind;
6777 9f7d7167 2018-04-29 stsp argv += optind;
6778 9814e6a3 2020-09-27 naddy optind = 1;
6779 c2301be8 2018-04-30 stsp optreset = 1;
6781 53ccebc2 2019-07-30 stsp if (Vflag) {
6782 53ccebc2 2019-07-30 stsp got_version_print_str();
6783 6879ba42 2020-10-01 naddy return 0;
6786 4010e238 2020-12-04 stsp #ifndef PROFILE
6787 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6788 4010e238 2020-12-04 stsp NULL) == -1)
6789 4010e238 2020-12-04 stsp err(1, "pledge");
6792 c2301be8 2018-04-30 stsp if (argc == 0) {
6793 f29d3e89 2018-06-23 stsp if (hflag)
6794 6879ba42 2020-10-01 naddy usage(hflag, 0);
6795 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6796 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6798 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6802 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6803 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6804 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6805 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6806 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6812 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6813 ee85c5e8 2020-02-29 stsp if (argc != 1)
6814 6879ba42 2020-10-01 naddy usage(0, 1);
6815 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6816 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6818 ee85c5e8 2020-02-29 stsp if (hflag)
6819 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6821 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6825 b46c1e04 2020-09-20 naddy putchar('\n');
6826 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6828 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6829 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6830 a2f4a359 2020-02-28 stsp free(cmd_argv);
6833 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6834 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);