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 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
18 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
23 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 9f7d7167 2018-04-29 stsp #include <curses.h>
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
44 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 53ccebc2 2019-07-30 stsp #include "got_version.h"
47 9f7d7167 2018-04-29 stsp #include "got_error.h"
48 80ddbec8 2018-04-29 stsp #include "got_object.h"
49 80ddbec8 2018-04-29 stsp #include "got_reference.h"
50 80ddbec8 2018-04-29 stsp #include "got_repository.h"
51 80ddbec8 2018-04-29 stsp #include "got_diff.h"
52 511a516b 2018-05-19 stsp #include "got_opentemp.h"
53 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
54 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
55 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
56 a70480e0 2018-06-23 stsp #include "got_blame.h"
57 c2db6724 2019-01-04 stsp #include "got_privsep.h"
58 1dd54920 2019-05-11 stsp #include "got_path.h"
59 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 dd038bc6 2021-09-21 thomas.ad //#define update_panels() (0)
62 dd038bc6 2021-09-21 thomas.ad //#define doupdate() (0)
65 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
72 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
73 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
76 9f7d7167 2018-04-29 stsp #ifndef nitems
77 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 9f7d7167 2018-04-29 stsp struct tog_cmd {
81 c2301be8 2018-04-30 stsp const char *name;
82 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
90 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
91 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
96 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
97 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
99 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
100 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
101 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
102 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
103 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
104 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
107 d6b05b5b 2018-08-04 stsp enum tog_view_type {
108 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
109 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
110 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
111 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
112 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
115 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
117 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
118 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
119 ba4f502b 2018-08-04 stsp struct got_object_id *id;
120 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
123 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 ba4f502b 2018-08-04 stsp struct commit_queue {
125 ba4f502b 2018-08-04 stsp int ncommits;
126 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
129 f26dddb7 2019-11-08 stsp struct tog_color {
130 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
131 6d17833f 2019-11-08 stsp regex_t regex;
132 6d17833f 2019-11-08 stsp short colorpair;
134 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
136 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 2183bbf6 2022-01-23 thomas static const struct got_error *
140 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 2183bbf6 2022-01-23 thomas struct got_reference* re2)
143 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
144 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
145 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
147 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
148 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
151 2183bbf6 2022-01-23 thomas *cmp = -1;
152 2183bbf6 2022-01-23 thomas return NULL;
153 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
154 2183bbf6 2022-01-23 thomas *cmp = 1;
155 2183bbf6 2022-01-23 thomas return NULL;
158 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 2183bbf6 2022-01-23 thomas return NULL;
162 11b20872 2019-11-08 stsp static const struct got_error *
163 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
165 51a10b52 2020-12-26 stsp const struct got_error *err;
167 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 51a10b52 2020-12-26 stsp return err;
173 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
177 51a10b52 2020-12-26 stsp static void
178 51a10b52 2020-12-26 stsp tog_free_refs(void)
180 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
181 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
182 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
184 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
187 51a10b52 2020-12-26 stsp static const struct got_error *
188 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
189 11b20872 2019-11-08 stsp int idx, short color)
191 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
192 11b20872 2019-11-08 stsp struct tog_color *tc;
193 11b20872 2019-11-08 stsp int regerr = 0;
195 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
196 11b20872 2019-11-08 stsp return NULL;
198 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
200 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
201 11b20872 2019-11-08 stsp if (tc == NULL)
202 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
203 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
204 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 11b20872 2019-11-08 stsp if (regerr) {
206 11b20872 2019-11-08 stsp static char regerr_msg[512];
207 11b20872 2019-11-08 stsp static char err_msg[512];
208 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
209 11b20872 2019-11-08 stsp sizeof(regerr_msg));
210 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 11b20872 2019-11-08 stsp regerr_msg);
212 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
214 11b20872 2019-11-08 stsp return err;
216 11b20872 2019-11-08 stsp tc->colorpair = idx;
217 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
218 11b20872 2019-11-08 stsp return NULL;
221 11b20872 2019-11-08 stsp static void
222 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
224 11b20872 2019-11-08 stsp struct tog_color *tc;
226 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
227 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
228 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
229 11b20872 2019-11-08 stsp regfree(&tc->regex);
234 11b20872 2019-11-08 stsp struct tog_color *
235 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
237 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
239 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
240 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
244 11b20872 2019-11-08 stsp return NULL;
248 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
250 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_CYAN;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 11b20872 2019-11-08 stsp return COLOR_YELLOW;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 11b20872 2019-11-08 stsp return COLOR_GREEN;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_CYAN;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 11b20872 2019-11-08 stsp return COLOR_GREEN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 6458efa5 2020-11-24 stsp return COLOR_GREEN;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
278 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
285 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
287 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
289 11b20872 2019-11-08 stsp if (val == NULL)
290 11b20872 2019-11-08 stsp return default_color_value(envvar);
292 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
293 11b20872 2019-11-08 stsp return COLOR_BLACK;
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
295 11b20872 2019-11-08 stsp return COLOR_RED;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
297 11b20872 2019-11-08 stsp return COLOR_GREEN;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
299 11b20872 2019-11-08 stsp return COLOR_YELLOW;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
301 11b20872 2019-11-08 stsp return COLOR_BLUE;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
303 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
305 11b20872 2019-11-08 stsp return COLOR_CYAN;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
307 11b20872 2019-11-08 stsp return COLOR_WHITE;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
311 11b20872 2019-11-08 stsp return default_color_value(envvar);
315 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
316 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
317 3dbaef42 2020-11-24 stsp const char *label1, *label2;
318 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
319 15a087fe 2019-02-21 stsp int first_displayed_line;
320 15a087fe 2019-02-21 stsp int last_displayed_line;
322 15a087fe 2019-02-21 stsp int diff_context;
323 3dbaef42 2020-11-24 stsp int ignore_whitespace;
324 64453f7e 2020-11-21 stsp int force_text_diff;
325 15a087fe 2019-02-21 stsp struct got_repository *repo;
326 bddb1296 2019-11-08 stsp struct tog_colors colors;
327 fe621944 2020-11-10 stsp size_t nlines;
328 f44b1f58 2020-02-02 tracey off_t *line_offsets;
329 f44b1f58 2020-02-02 tracey int matched_line;
330 f44b1f58 2020-02-02 tracey int selected_line;
332 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
333 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
336 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
339 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
340 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
341 1a76625f 2018-10-22 stsp int commits_needed;
342 fb280deb 2021-08-30 stsp int load_all;
343 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
344 1a76625f 2018-10-22 stsp struct commit_queue *commits;
345 1a76625f 2018-10-22 stsp const char *in_repo_path;
346 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
347 1a76625f 2018-10-22 stsp struct got_repository *repo;
348 1af5eddf 2022-06-23 thomas int *pack_fds;
349 1a76625f 2018-10-22 stsp int log_complete;
350 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
351 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
352 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
353 13add988 2019-10-15 stsp int *searching;
354 13add988 2019-10-15 stsp int *search_next_done;
355 13add988 2019-10-15 stsp regex_t *regex;
358 1a76625f 2018-10-22 stsp struct tog_log_view_state {
359 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
360 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
361 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
362 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
363 b01e7d3b 2018-08-04 stsp int selected;
364 b01e7d3b 2018-08-04 stsp char *in_repo_path;
365 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
366 b672a97a 2020-01-27 stsp int log_branches;
367 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
368 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
369 1a76625f 2018-10-22 stsp sig_atomic_t quit;
370 1a76625f 2018-10-22 stsp pthread_t thread;
371 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
372 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
373 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
374 11b20872 2019-11-08 stsp struct tog_colors colors;
377 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
378 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
381 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
382 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
385 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
386 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
387 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
388 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
389 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
390 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
391 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
393 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
394 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
395 e9424729 2018-08-04 stsp int nlines;
397 e9424729 2018-08-04 stsp struct tog_view *view;
398 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
402 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
403 e9424729 2018-08-04 stsp const char *path;
404 e9424729 2018-08-04 stsp struct got_repository *repo;
405 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
406 e9424729 2018-08-04 stsp int *complete;
407 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
408 fc06ba56 2019-08-22 stsp void *cancel_arg;
411 e9424729 2018-08-04 stsp struct tog_blame {
413 be659d10 2020-11-18 stsp off_t filesize;
414 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
415 6fcac457 2018-11-19 stsp int nlines;
416 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
417 e9424729 2018-08-04 stsp pthread_t thread;
418 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
419 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
420 e9424729 2018-08-04 stsp const char *path;
421 7cd52833 2022-06-23 thomas int *pack_fds;
424 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
425 7cbe629d 2018-08-04 stsp int first_displayed_line;
426 7cbe629d 2018-08-04 stsp int last_displayed_line;
427 7cbe629d 2018-08-04 stsp int selected_line;
428 7cbe629d 2018-08-04 stsp int blame_complete;
431 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
432 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
433 e5a0f69f 2018-08-18 stsp char *path;
434 7cbe629d 2018-08-04 stsp struct got_repository *repo;
435 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
436 e9424729 2018-08-04 stsp struct tog_blame blame;
437 6c4c42e0 2019-06-24 stsp int matched_line;
438 11b20872 2019-11-08 stsp struct tog_colors colors;
441 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
442 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
443 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
444 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
445 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
446 ad80ab7b 2018-08-04 stsp int selected;
449 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
451 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
452 ad80ab7b 2018-08-04 stsp char *tree_label;
453 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
454 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
455 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
457 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
458 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
459 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
460 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
461 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
462 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
463 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
464 6458efa5 2020-11-24 stsp struct tog_colors colors;
467 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
468 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
469 6458efa5 2020-11-24 stsp struct got_reference *ref;
473 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
475 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
476 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
479 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
480 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
481 6458efa5 2020-11-24 stsp struct got_repository *repo;
482 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
483 bddb1296 2019-11-08 stsp struct tog_colors colors;
487 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
489 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
490 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
491 669b5ffa 2018-10-07 stsp * there is enough screen estate.
493 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
494 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
496 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
497 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
498 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
500 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
501 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
503 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
505 cc3c9aac 2018-08-01 stsp struct tog_view {
506 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
507 26ed57b2 2018-05-19 stsp WINDOW *window;
508 26ed57b2 2018-05-19 stsp PANEL *panel;
509 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
510 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
511 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
512 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
514 669b5ffa 2018-10-07 stsp struct tog_view *parent;
515 669b5ffa 2018-10-07 stsp struct tog_view *child;
518 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
519 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
520 e78dc838 2020-12-04 stsp * between parent and child.
521 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
522 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
523 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
524 e78dc838 2020-12-04 stsp * situations.
526 e78dc838 2020-12-04 stsp int focus_child;
528 5dc9f4bc 2018-08-04 stsp /* type-specific state */
529 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
531 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
532 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
533 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
534 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
535 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
538 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
539 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
540 e78dc838 2020-12-04 stsp struct tog_view *, int);
541 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
543 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
544 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
545 c0c4acc8 2021-01-24 stsp int search_started;
546 60493ae3 2019-06-20 stsp int searching;
547 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
548 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
549 60493ae3 2019-06-20 stsp int search_next_done;
550 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
551 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
552 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
553 1803e47f 2019-06-22 stsp regex_t regex;
554 41605754 2020-11-12 stsp regmatch_t regmatch;
557 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
558 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
559 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
560 78756c87 2020-11-24 stsp struct got_repository *);
561 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
562 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_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_diff_view(struct tog_view *);
565 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
566 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
568 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
569 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
570 78756c87 2020-11-24 stsp const char *, const char *, int);
571 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
572 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
573 e78dc838 2020-12-04 stsp struct tog_view *, int);
574 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
575 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
576 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
579 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
580 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
582 e78dc838 2020-12-04 stsp struct tog_view *, int);
583 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
584 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
585 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
587 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
588 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
589 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
590 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
591 e78dc838 2020-12-04 stsp struct tog_view *, int);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
593 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
594 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
596 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
597 6458efa5 2020-11-24 stsp struct got_repository *);
598 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
599 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
600 e78dc838 2020-12-04 stsp struct tog_view *, int);
601 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
602 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
603 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
605 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
606 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
607 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
608 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
609 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
611 25791caa 2018-10-24 stsp static void
612 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
614 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
617 83baff54 2019-08-12 stsp static void
618 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
620 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
623 61266923 2020-01-14 stsp static void
624 61266923 2020-01-14 stsp tog_sigcont(int signo)
626 61266923 2020-01-14 stsp tog_sigcont_received = 1;
629 296152d1 2022-05-31 thomas static void
630 296152d1 2022-05-31 thomas tog_sigint(int signo)
632 296152d1 2022-05-31 thomas tog_sigint_received = 1;
635 296152d1 2022-05-31 thomas static void
636 296152d1 2022-05-31 thomas tog_sigterm(int signo)
638 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
641 296152d1 2022-05-31 thomas static int
642 296152d1 2022-05-31 thomas tog_fatal_signal_received()
644 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
645 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
649 e5a0f69f 2018-08-18 stsp static const struct got_error *
650 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
652 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
654 669b5ffa 2018-10-07 stsp if (view->child) {
655 669b5ffa 2018-10-07 stsp view_close(view->child);
656 669b5ffa 2018-10-07 stsp view->child = NULL;
658 e5a0f69f 2018-08-18 stsp if (view->close)
659 e5a0f69f 2018-08-18 stsp err = view->close(view);
660 ea5e7bb5 2018-08-01 stsp if (view->panel)
661 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
662 ea5e7bb5 2018-08-01 stsp if (view->window)
663 ea5e7bb5 2018-08-01 stsp delwin(view->window);
664 ea5e7bb5 2018-08-01 stsp free(view);
665 e5a0f69f 2018-08-18 stsp return err;
668 ea5e7bb5 2018-08-01 stsp static struct tog_view *
669 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
670 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
672 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
674 ea5e7bb5 2018-08-01 stsp if (view == NULL)
675 ea5e7bb5 2018-08-01 stsp return NULL;
677 d6b05b5b 2018-08-04 stsp view->type = type;
678 f7d12f7e 2018-08-01 stsp view->lines = LINES;
679 f7d12f7e 2018-08-01 stsp view->cols = COLS;
680 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
681 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
682 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
683 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
684 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
685 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
686 96a765a8 2018-08-04 stsp view_close(view);
687 ea5e7bb5 2018-08-01 stsp return NULL;
689 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
690 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
691 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
692 96a765a8 2018-08-04 stsp view_close(view);
693 ea5e7bb5 2018-08-01 stsp return NULL;
696 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
697 ea5e7bb5 2018-08-01 stsp return view;
701 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
703 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
705 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
708 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
710 5c60c32a 2018-10-18 stsp static const struct got_error *
711 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
713 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
715 5c60c32a 2018-10-18 stsp view->begin_y = 0;
716 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
717 5c60c32a 2018-10-18 stsp view->nlines = LINES;
718 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
719 5c60c32a 2018-10-18 stsp view->lines = LINES;
720 5c60c32a 2018-10-18 stsp view->cols = COLS;
721 5c60c32a 2018-10-18 stsp err = view_resize(view);
723 5c60c32a 2018-10-18 stsp return err;
725 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
726 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
728 5c60c32a 2018-10-18 stsp return NULL;
731 5c60c32a 2018-10-18 stsp static const struct got_error *
732 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
734 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
736 5c60c32a 2018-10-18 stsp view->begin_x = 0;
737 5c60c32a 2018-10-18 stsp view->begin_y = 0;
738 5c60c32a 2018-10-18 stsp view->nlines = LINES;
739 5c60c32a 2018-10-18 stsp view->ncols = COLS;
740 5c60c32a 2018-10-18 stsp view->lines = LINES;
741 5c60c32a 2018-10-18 stsp view->cols = COLS;
742 5c60c32a 2018-10-18 stsp err = view_resize(view);
744 5c60c32a 2018-10-18 stsp return err;
746 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
747 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
749 5c60c32a 2018-10-18 stsp return NULL;
753 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
755 5c60c32a 2018-10-18 stsp return view->parent == NULL;
758 4d8c2215 2018-08-19 stsp static const struct got_error *
759 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
761 f7d12f7e 2018-08-01 stsp int nlines, ncols;
763 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
764 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
766 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
768 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
769 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
771 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
773 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
774 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
775 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
776 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
777 25791caa 2018-10-24 stsp wclear(view->window);
779 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
780 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
781 0cf4efb1 2018-09-29 stsp view->lines = LINES;
782 0cf4efb1 2018-09-29 stsp view->cols = COLS;
784 6e3e5d9c 2018-10-18 stsp if (view->child) {
785 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
786 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
787 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
788 5c60c32a 2018-10-18 stsp if (view->child->focussed)
789 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
791 5c60c32a 2018-10-18 stsp show_panel(view->panel);
793 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
794 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
798 5c60c32a 2018-10-18 stsp return NULL;
801 669b5ffa 2018-10-07 stsp static const struct got_error *
802 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
804 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
806 669b5ffa 2018-10-07 stsp if (view->child == NULL)
807 669b5ffa 2018-10-07 stsp return NULL;
809 669b5ffa 2018-10-07 stsp err = view_close(view->child);
810 669b5ffa 2018-10-07 stsp view->child = NULL;
811 669b5ffa 2018-10-07 stsp return err;
814 72a9cb46 2020-12-03 stsp static void
815 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
817 669b5ffa 2018-10-07 stsp view->child = child;
818 669b5ffa 2018-10-07 stsp child->parent = view;
822 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
824 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
827 34bc9ec9 2019-02-22 stsp static void
828 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
830 25791caa 2018-10-24 stsp int cols, lines;
831 25791caa 2018-10-24 stsp struct winsize size;
833 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 25791caa 2018-10-24 stsp cols = 80; /* Default */
835 25791caa 2018-10-24 stsp lines = 24;
837 25791caa 2018-10-24 stsp cols = size.ws_col;
838 25791caa 2018-10-24 stsp lines = size.ws_row;
840 25791caa 2018-10-24 stsp resize_term(lines, cols);
843 2b49a8ae 2019-06-22 stsp static const struct got_error *
844 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
846 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
847 2b49a8ae 2019-06-22 stsp char pattern[1024];
850 c0c4acc8 2021-01-24 stsp if (view->search_started) {
851 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
852 c0c4acc8 2021-01-24 stsp view->searching = 0;
853 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
855 c0c4acc8 2021-01-24 stsp view->search_started = 0;
857 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
858 2b49a8ae 2019-06-22 stsp return NULL;
860 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
863 2b49a8ae 2019-06-22 stsp nocbreak();
865 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
868 2b49a8ae 2019-06-22 stsp if (ret == ERR)
869 2b49a8ae 2019-06-22 stsp return NULL;
871 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 7c32bd05 2019-06-22 stsp err = view->search_start(view);
874 7c32bd05 2019-06-22 stsp regfree(&view->regex);
875 7c32bd05 2019-06-22 stsp return err;
877 c0c4acc8 2021-01-24 stsp view->search_started = 1;
878 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
879 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
880 2b49a8ae 2019-06-22 stsp view->search_next(view);
883 2b49a8ae 2019-06-22 stsp return NULL;
886 0cf4efb1 2018-09-29 stsp static const struct got_error *
887 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
888 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
890 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
891 669b5ffa 2018-10-07 stsp struct tog_view *v;
892 1a76625f 2018-10-22 stsp int ch, errcode;
894 e5a0f69f 2018-08-18 stsp *new = NULL;
896 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
897 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
902 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
903 82954512 2020-02-03 stsp if (errcode)
904 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
905 82954512 2020-02-03 stsp "pthread_mutex_unlock");
906 3da8ef85 2021-09-21 stsp sched_yield();
907 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
908 82954512 2020-02-03 stsp if (errcode)
909 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
910 82954512 2020-02-03 stsp "pthread_mutex_lock");
911 60493ae3 2019-06-20 stsp view->search_next(view);
912 60493ae3 2019-06-20 stsp return NULL;
915 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
916 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
917 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
918 1a76625f 2018-10-22 stsp if (errcode)
919 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
921 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
922 1a76625f 2018-10-22 stsp if (errcode)
923 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
924 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
926 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
927 25791caa 2018-10-24 stsp tog_resizeterm();
928 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
929 61266923 2020-01-14 stsp tog_sigcont_received = 0;
930 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
931 25791caa 2018-10-24 stsp err = view_resize(v);
933 25791caa 2018-10-24 stsp return err;
934 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
936 25791caa 2018-10-24 stsp return err;
937 cdfcfb03 2020-12-06 stsp if (v->child) {
938 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
940 cdfcfb03 2020-12-06 stsp return err;
941 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
942 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
944 cdfcfb03 2020-12-06 stsp return err;
949 e5a0f69f 2018-08-18 stsp switch (ch) {
951 1e37a5c2 2019-05-12 jcs if (view->child) {
952 e78dc838 2020-12-04 stsp view->focussed = 0;
953 e78dc838 2020-12-04 stsp view->child->focussed = 1;
954 e78dc838 2020-12-04 stsp view->focus_child = 1;
955 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
956 e78dc838 2020-12-04 stsp view->focussed = 0;
957 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
958 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
962 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
963 9970f7fc 2020-12-03 stsp view->dying = 1;
969 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
970 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
972 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
973 e78dc838 2020-12-04 stsp view->focussed = 0;
974 e78dc838 2020-12-04 stsp view->child->focussed = 1;
975 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
977 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
980 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
981 9970f7fc 2020-12-03 stsp KEY_RESIZE);
983 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
984 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
985 e78dc838 2020-12-04 stsp view->focussed = 1;
986 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
988 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
992 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
995 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
998 60493ae3 2019-06-20 stsp if (view->search_start)
999 2b49a8ae 2019-06-22 stsp view_search_start(view);
1001 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1005 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1006 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1007 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1009 60493ae3 2019-06-20 stsp view->search_next(view);
1011 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1014 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1018 e5a0f69f 2018-08-18 stsp return err;
1022 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1024 0cf4efb1 2018-09-29 stsp PANEL *panel;
1025 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1027 669b5ffa 2018-10-07 stsp if (view->parent)
1028 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1030 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1031 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1034 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1035 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1040 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1042 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1043 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1045 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1047 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1050 669b5ffa 2018-10-07 stsp return view->focussed;
1053 bcbd79e2 2018-08-19 stsp static const struct got_error *
1054 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1056 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1057 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1058 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1059 fd823528 2018-10-22 stsp int fast_refresh = 10;
1060 1a76625f 2018-10-22 stsp int done = 0, errcode;
1062 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1063 1a76625f 2018-10-22 stsp if (errcode)
1064 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1066 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1067 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1069 1004088d 2018-09-29 stsp view->focussed = 1;
1070 878940b7 2018-09-29 stsp err = view->show(view);
1072 0cf4efb1 2018-09-29 stsp return err;
1073 0cf4efb1 2018-09-29 stsp update_panels();
1074 0cf4efb1 2018-09-29 stsp doupdate();
1075 296152d1 2022-05-31 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1077 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1078 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1080 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1083 9970f7fc 2020-12-03 stsp if (view->dying) {
1084 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1086 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1087 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1089 e78dc838 2020-12-04 stsp else if (view->parent)
1090 669b5ffa 2018-10-07 stsp prev = view->parent;
1092 e78dc838 2020-12-04 stsp if (view->parent) {
1093 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1094 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1096 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1098 9970f7fc 2020-12-03 stsp err = view_close(view);
1100 e5a0f69f 2018-08-18 stsp goto done;
1102 e78dc838 2020-12-04 stsp view = NULL;
1103 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1104 e78dc838 2020-12-04 stsp if (v->focussed)
1107 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1108 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1110 e78dc838 2020-12-04 stsp view = prev;
1111 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1112 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1113 e78dc838 2020-12-04 stsp tog_view_list_head);
1115 e78dc838 2020-12-04 stsp if (view) {
1116 e78dc838 2020-12-04 stsp if (view->focus_child) {
1117 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1118 e78dc838 2020-12-04 stsp view = view->child;
1120 e78dc838 2020-12-04 stsp view->focussed = 1;
1124 bcbd79e2 2018-08-19 stsp if (new_view) {
1125 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1126 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1127 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1128 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1130 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1131 86c66b02 2018-10-18 stsp err = view_close(v);
1133 86c66b02 2018-10-18 stsp goto done;
1136 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1137 fed7eaa8 2018-10-24 stsp view = new_view;
1139 669b5ffa 2018-10-07 stsp if (view) {
1140 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1141 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1142 e78dc838 2020-12-04 stsp view = view->child;
1144 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1145 e78dc838 2020-12-04 stsp view = view->parent;
1147 e78dc838 2020-12-04 stsp show_panel(view->panel);
1148 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1149 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1150 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1151 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1153 1a76625f 2018-10-22 stsp goto done;
1155 669b5ffa 2018-10-07 stsp err = view->show(view);
1157 1a76625f 2018-10-22 stsp goto done;
1158 669b5ffa 2018-10-07 stsp if (view->child) {
1159 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1161 1a76625f 2018-10-22 stsp goto done;
1163 1a76625f 2018-10-22 stsp update_panels();
1164 1a76625f 2018-10-22 stsp doupdate();
1168 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1169 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1170 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1171 e5a0f69f 2018-08-18 stsp view_close(view);
1174 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1175 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1176 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1178 e5a0f69f 2018-08-18 stsp return err;
1181 4ed7e80c 2018-05-20 stsp __dead static void
1182 9f7d7167 2018-04-29 stsp usage_log(void)
1185 c70c5802 2018-08-01 stsp fprintf(stderr,
1186 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1187 9f7d7167 2018-04-29 stsp getprogname());
1191 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1192 80ddbec8 2018-04-29 stsp static const struct got_error *
1193 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1195 00dfcb92 2018-06-11 stsp char *vis = NULL;
1196 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1198 963b370f 2018-05-20 stsp *ws = NULL;
1199 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1200 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1201 00dfcb92 2018-06-11 stsp int vislen;
1202 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1203 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1205 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1206 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1208 00dfcb92 2018-06-11 stsp return err;
1209 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1210 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1211 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1212 a7f50699 2018-06-11 stsp goto done;
1216 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1217 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1218 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1219 a7f50699 2018-06-11 stsp goto done;
1222 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1223 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1225 00dfcb92 2018-06-11 stsp free(vis);
1226 963b370f 2018-05-20 stsp if (err) {
1227 963b370f 2018-05-20 stsp free(*ws);
1228 963b370f 2018-05-20 stsp *ws = NULL;
1229 963b370f 2018-05-20 stsp *wlen = 0;
1231 963b370f 2018-05-20 stsp return err;
1234 05171be4 2022-06-23 thomas static const struct got_error *
1235 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1237 05171be4 2022-06-23 thomas char *dst;
1238 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1240 05171be4 2022-06-23 thomas *ptr = NULL;
1241 05171be4 2022-06-23 thomas n = len = strlen(src);
1242 83316834 2022-06-23 thomas dst = malloc(n + 1);
1243 05171be4 2022-06-23 thomas if (dst == NULL)
1244 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1246 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1247 05171be4 2022-06-23 thomas const char c = src[idx];
1249 05171be4 2022-06-23 thomas if (c == '\t') {
1250 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1251 b235ad5d 2022-06-23 thomas char *p;
1253 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1254 83316834 2022-06-23 thomas if (p == NULL) {
1255 83316834 2022-06-23 thomas free(dst);
1256 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1259 83316834 2022-06-23 thomas dst = p;
1260 05171be4 2022-06-23 thomas n += nb;
1261 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1262 05171be4 2022-06-23 thomas sz += nb;
1264 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1268 05171be4 2022-06-23 thomas dst[sz] = '\0';
1269 05171be4 2022-06-23 thomas *ptr = dst;
1270 05171be4 2022-06-23 thomas return NULL;
1274 f91a2b48 2022-06-23 thomas * Skip leading nscroll columns of a wide character string.
1275 f91a2b48 2022-06-23 thomas * Returns the index to the first character of the scrolled string.
1277 f91a2b48 2022-06-23 thomas static const struct got_error *
1278 f91a2b48 2022-06-23 thomas scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1279 f91a2b48 2022-06-23 thomas int col_tab_align)
1281 f91a2b48 2022-06-23 thomas int cols = 0;
1282 f91a2b48 2022-06-23 thomas size_t wlen = wcslen(wline);
1283 f91a2b48 2022-06-23 thomas int i = 0;
1285 f91a2b48 2022-06-23 thomas *scrollx = 0;
1287 f91a2b48 2022-06-23 thomas while (i < wlen && cols < nscroll) {
1288 f91a2b48 2022-06-23 thomas int width = wcwidth(wline[i]);
1290 f91a2b48 2022-06-23 thomas if (width == 0) {
1292 f91a2b48 2022-06-23 thomas continue;
1295 f91a2b48 2022-06-23 thomas if (width == 1 || width == 2) {
1296 f91a2b48 2022-06-23 thomas if (cols + width > nscroll)
1298 f91a2b48 2022-06-23 thomas cols += width;
1300 f91a2b48 2022-06-23 thomas } else if (width == -1) {
1301 f91a2b48 2022-06-23 thomas if (wline[i] == L'\t') {
1302 f91a2b48 2022-06-23 thomas width = TABSIZE -
1303 f91a2b48 2022-06-23 thomas ((cols + col_tab_align) % TABSIZE);
1304 f91a2b48 2022-06-23 thomas } else {
1305 f91a2b48 2022-06-23 thomas width = 1;
1306 f91a2b48 2022-06-23 thomas wline[i] = L'.';
1308 f91a2b48 2022-06-23 thomas if (cols + width > nscroll)
1310 f91a2b48 2022-06-23 thomas cols += width;
1313 f91a2b48 2022-06-23 thomas return got_error_from_errno("wcwidth");
1316 f91a2b48 2022-06-23 thomas *scrollx = i;
1317 f91a2b48 2022-06-23 thomas return NULL;
1321 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1322 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1323 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1325 f91a2b48 2022-06-23 thomas static const struct got_error *
1326 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1327 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1329 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1330 963b370f 2018-05-20 stsp int cols = 0;
1331 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1332 05171be4 2022-06-23 thomas char *exstr = NULL;
1333 963b370f 2018-05-20 stsp size_t wlen;
1334 f91a2b48 2022-06-23 thomas int i, scrollx = 0;
1336 963b370f 2018-05-20 stsp *wlinep = NULL;
1337 b700b5d6 2018-07-10 stsp *widthp = 0;
1339 05171be4 2022-06-23 thomas if (expand) {
1340 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1341 05171be4 2022-06-23 thomas if (err)
1342 05171be4 2022-06-23 thomas return err;
1345 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1346 05171be4 2022-06-23 thomas free(exstr);
1348 963b370f 2018-05-20 stsp return err;
1350 f91a2b48 2022-06-23 thomas err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1351 f91a2b48 2022-06-23 thomas if (err)
1352 f91a2b48 2022-06-23 thomas goto done;
1354 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1355 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1358 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1359 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1363 f91a2b48 2022-06-23 thomas i = scrollx;
1364 27a741e5 2019-09-11 stsp while (i < wlen) {
1365 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1367 27a741e5 2019-09-11 stsp if (width == 0) {
1372 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1373 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1375 27a741e5 2019-09-11 stsp cols += width;
1377 27a741e5 2019-09-11 stsp } else if (width == -1) {
1378 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1379 27a741e5 2019-09-11 stsp width = TABSIZE -
1380 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1382 748d5cab 2020-12-14 naddy width = 1;
1383 748d5cab 2020-12-14 naddy wline[i] = L'.';
1385 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1387 748d5cab 2020-12-14 naddy cols += width;
1390 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1391 963b370f 2018-05-20 stsp goto done;
1394 963b370f 2018-05-20 stsp wline[i] = L'\0';
1395 b700b5d6 2018-07-10 stsp if (widthp)
1396 b700b5d6 2018-07-10 stsp *widthp = cols;
1397 f91a2b48 2022-06-23 thomas if (scrollxp)
1398 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1401 963b370f 2018-05-20 stsp free(wline);
1403 963b370f 2018-05-20 stsp *wlinep = wline;
1404 963b370f 2018-05-20 stsp return err;
1407 8b473291 2019-02-21 stsp static const struct got_error*
1408 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1409 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1411 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1412 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1414 8b473291 2019-02-21 stsp const char *name;
1416 8b473291 2019-02-21 stsp *refs_str = NULL;
1418 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1419 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1420 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1423 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1424 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1426 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1427 8b473291 2019-02-21 stsp name += 5;
1428 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1429 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1431 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1432 8b473291 2019-02-21 stsp name += 6;
1433 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1434 8b473291 2019-02-21 stsp name += 8;
1435 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1436 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1439 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1442 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1443 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1444 5d844a1e 2019-08-13 stsp if (err) {
1445 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1446 48cae60d 2020-09-22 stsp free(ref_id);
1449 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1450 5d844a1e 2019-08-13 stsp err = NULL;
1451 5d844a1e 2019-08-13 stsp tag = NULL;
1454 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1455 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1456 48cae60d 2020-09-22 stsp free(ref_id);
1458 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1459 52b5abe1 2019-08-13 stsp if (cmp != 0)
1461 8b473291 2019-02-21 stsp s = *refs_str;
1462 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1463 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1464 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1466 8b473291 2019-02-21 stsp *refs_str = NULL;
1472 8b473291 2019-02-21 stsp return err;
1475 963b370f 2018-05-20 stsp static const struct got_error *
1476 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1477 27a741e5 2019-09-11 stsp int col_tab_align)
1479 e6b8b890 2020-12-29 naddy char *smallerthan;
1481 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1482 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1483 5813d178 2019-03-09 stsp author = smallerthan + 1;
1484 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1485 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1486 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1489 5813d178 2019-03-09 stsp static const struct got_error *
1490 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1491 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1492 8fdc79fe 2020-12-01 naddy int author_display_cols)
1494 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1495 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1496 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1497 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1498 5813d178 2019-03-09 stsp char *author = NULL;
1499 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1500 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1501 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1502 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1503 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1504 ccb26ccd 2018-11-05 stsp struct tm tm;
1505 45d799e2 2018-12-23 stsp time_t committer_time;
1506 11b20872 2019-11-08 stsp struct tog_color *tc;
1508 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1509 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1510 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1511 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1512 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1514 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1515 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1517 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1518 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1520 11b20872 2019-11-08 stsp wattr_on(view->window,
1521 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1522 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1524 11b20872 2019-11-08 stsp wattr_off(view->window,
1525 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1526 27a741e5 2019-09-11 stsp col = limit;
1527 b39d25c7 2018-07-10 stsp if (col > avail)
1528 b39d25c7 2018-07-10 stsp goto done;
1530 6570a66d 2019-11-08 stsp if (avail >= 120) {
1531 6570a66d 2019-11-08 stsp char *id_str;
1532 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1534 6570a66d 2019-11-08 stsp goto done;
1535 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1537 11b20872 2019-11-08 stsp wattr_on(view->window,
1538 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1539 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1541 11b20872 2019-11-08 stsp wattr_off(view->window,
1542 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1543 6570a66d 2019-11-08 stsp free(id_str);
1545 6570a66d 2019-11-08 stsp if (col > avail)
1546 6570a66d 2019-11-08 stsp goto done;
1549 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1550 5813d178 2019-03-09 stsp if (author == NULL) {
1551 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1552 80ddbec8 2018-04-29 stsp goto done;
1554 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1556 bb737323 2018-05-20 stsp goto done;
1557 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1559 11b20872 2019-11-08 stsp wattr_on(view->window,
1560 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1561 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1563 11b20872 2019-11-08 stsp wattr_off(view->window,
1564 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1565 bb737323 2018-05-20 stsp col += author_width;
1566 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1567 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1569 bb737323 2018-05-20 stsp author_width++;
1571 9c2eaf34 2018-05-20 stsp if (col > avail)
1572 9c2eaf34 2018-05-20 stsp goto done;
1574 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1576 6d9fbc00 2018-04-29 stsp goto done;
1577 bb737323 2018-05-20 stsp logmsg = logmsg0;
1578 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1580 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1581 bb737323 2018-05-20 stsp if (newline)
1582 bb737323 2018-05-20 stsp *newline = '\0';
1583 f91a2b48 2022-06-23 thomas limit = avail - col;
1584 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1585 f91a2b48 2022-06-23 thomas limit, col, 1);
1586 331b1a16 2022-06-23 thomas if (err)
1587 331b1a16 2022-06-23 thomas goto done;
1588 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
1589 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
1590 27a741e5 2019-09-11 stsp while (col < avail) {
1591 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1595 80ddbec8 2018-04-29 stsp free(logmsg0);
1596 bb737323 2018-05-20 stsp free(wlogmsg);
1597 5813d178 2019-03-09 stsp free(author);
1598 bb737323 2018-05-20 stsp free(wauthor);
1599 80ddbec8 2018-04-29 stsp free(line);
1600 80ddbec8 2018-04-29 stsp return err;
1603 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1604 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1605 899d86c2 2018-05-10 stsp struct got_object_id *id)
1607 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1609 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1610 80ddbec8 2018-04-29 stsp if (entry == NULL)
1611 899d86c2 2018-05-10 stsp return NULL;
1613 899d86c2 2018-05-10 stsp entry->id = id;
1614 99db9666 2018-05-07 stsp entry->commit = commit;
1615 899d86c2 2018-05-10 stsp return entry;
1618 99db9666 2018-05-07 stsp static void
1619 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1621 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1623 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1624 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1625 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1626 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1627 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1628 99db9666 2018-05-07 stsp free(entry);
1631 99db9666 2018-05-07 stsp static void
1632 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1634 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1635 99db9666 2018-05-07 stsp pop_commit(commits);
1638 c4972b91 2018-05-07 stsp static const struct got_error *
1639 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1640 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1642 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1643 13add988 2019-10-15 stsp regmatch_t regmatch;
1644 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1646 13add988 2019-10-15 stsp *have_match = 0;
1648 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1650 13add988 2019-10-15 stsp return err;
1652 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1654 13add988 2019-10-15 stsp goto done;
1656 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1657 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1658 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1659 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1660 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1661 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1662 13add988 2019-10-15 stsp *have_match = 1;
1664 13add988 2019-10-15 stsp free(id_str);
1665 13add988 2019-10-15 stsp free(logmsg);
1666 13add988 2019-10-15 stsp return err;
1669 13add988 2019-10-15 stsp static const struct got_error *
1670 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1672 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1675 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1676 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1677 1a76625f 2018-10-22 stsp * while updating the display.
1680 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1681 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1682 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1683 1a76625f 2018-10-22 stsp int errcode;
1685 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1686 4e0d2870 2020-12-07 naddy NULL, NULL);
1687 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1690 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1693 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1694 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1695 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1699 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1700 1a76625f 2018-10-22 stsp if (errcode) {
1701 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1702 13add988 2019-10-15 stsp "pthread_mutex_lock");
1706 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1707 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1708 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1710 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1711 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1712 7c1452c1 2020-03-26 stsp int have_match;
1713 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1716 7c1452c1 2020-03-26 stsp if (have_match)
1717 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1720 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1721 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1722 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1723 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1726 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1728 9ba79e04 2018-06-11 stsp return err;
1731 2b779855 2020-12-05 naddy static void
1732 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1734 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1735 2b779855 2020-12-05 naddy int ncommits = 0;
1737 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1738 2b779855 2020-12-05 naddy while (entry) {
1739 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1740 2b779855 2020-12-05 naddy s->selected_entry = entry;
1743 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1744 2b779855 2020-12-05 naddy ncommits++;
1748 0553a4e3 2018-04-30 stsp static const struct got_error *
1749 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1751 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1752 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1753 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1754 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1755 60493ae3 2019-06-20 stsp int width;
1756 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1757 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1758 8b473291 2019-02-21 stsp char *refs_str = NULL;
1759 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1760 11b20872 2019-11-08 stsp struct tog_color *tc;
1761 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1763 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1764 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1765 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1766 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1768 ecb28ae0 2018-07-16 stsp return err;
1769 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1770 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1771 d2075bf3 2020-12-25 stsp if (refs) {
1772 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1773 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1775 d2075bf3 2020-12-25 stsp goto done;
1779 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1780 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1782 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1783 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1784 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1785 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1786 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1787 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1788 8f4ed634 2020-03-26 stsp goto done;
1791 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1793 f9686aa5 2020-03-27 stsp if (view->searching) {
1794 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1795 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1796 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1797 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1798 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1799 f9686aa5 2020-03-27 stsp search_str = "searching...";
1802 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1803 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1804 f9686aa5 2020-03-27 stsp search_str ? search_str :
1805 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1806 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1807 8f4ed634 2020-03-26 stsp goto done;
1811 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1812 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1813 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1814 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1815 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1816 1a76625f 2018-10-22 stsp header = NULL;
1817 1a76625f 2018-10-22 stsp goto done;
1819 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1820 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1821 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1822 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1823 1a76625f 2018-10-22 stsp header = NULL;
1824 1a76625f 2018-10-22 stsp goto done;
1826 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1828 1a76625f 2018-10-22 stsp goto done;
1830 2814baeb 2018-08-01 stsp werase(view->window);
1832 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1833 a3404814 2018-09-02 stsp wstandout(view->window);
1834 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1836 11b20872 2019-11-08 stsp wattr_on(view->window,
1837 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1838 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1840 11b20872 2019-11-08 stsp wattr_off(view->window,
1841 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1842 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1843 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1846 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1847 a3404814 2018-09-02 stsp wstandend(view->window);
1848 ecb28ae0 2018-07-16 stsp free(wline);
1849 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1850 1a76625f 2018-10-22 stsp goto done;
1852 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
1853 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1854 5813d178 2019-03-09 stsp ncommits = 0;
1855 05171be4 2022-06-23 thomas view->maxx = 0;
1856 5813d178 2019-03-09 stsp while (entry) {
1857 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
1858 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
1859 5813d178 2019-03-09 stsp int width;
1860 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1862 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1863 5813d178 2019-03-09 stsp if (author == NULL) {
1864 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1865 5813d178 2019-03-09 stsp goto done;
1867 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1868 27a741e5 2019-09-11 stsp date_display_cols);
1869 5813d178 2019-03-09 stsp if (author_cols < width)
1870 5813d178 2019-03-09 stsp author_cols = width;
1871 5813d178 2019-03-09 stsp free(wauthor);
1872 5813d178 2019-03-09 stsp free(author);
1873 05171be4 2022-06-23 thomas err = got_object_commit_get_logmsg(&msg0, entry->commit);
1874 05171be4 2022-06-23 thomas if (err)
1875 05171be4 2022-06-23 thomas goto done;
1876 05171be4 2022-06-23 thomas msg = msg0;
1877 05171be4 2022-06-23 thomas while (*msg == '\n')
1879 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
1880 331b1a16 2022-06-23 thomas *eol = '\0';
1881 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1882 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
1883 331b1a16 2022-06-23 thomas if (err)
1884 331b1a16 2022-06-23 thomas goto done;
1885 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
1886 05171be4 2022-06-23 thomas free(msg0);
1887 331b1a16 2022-06-23 thomas free(wmsg);
1888 7ca04879 2019-10-19 stsp ncommits++;
1889 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1892 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1893 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1894 867c6645 2018-07-10 stsp ncommits = 0;
1895 899d86c2 2018-05-10 stsp while (entry) {
1896 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1898 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1899 2814baeb 2018-08-01 stsp wstandout(view->window);
1900 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1901 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1902 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1903 2814baeb 2018-08-01 stsp wstandend(view->window);
1905 60493ae3 2019-06-20 stsp goto done;
1906 0553a4e3 2018-04-30 stsp ncommits++;
1907 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1908 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1911 1a57306a 2018-09-02 stsp view_vborder(view);
1912 dd038bc6 2021-09-21 thomas.ad update_panels();
1913 dd038bc6 2021-09-21 thomas.ad doupdate();
1915 1a76625f 2018-10-22 stsp free(id_str);
1916 8b473291 2019-02-21 stsp free(refs_str);
1917 1a76625f 2018-10-22 stsp free(ncommits_str);
1918 1a76625f 2018-10-22 stsp free(header);
1919 80ddbec8 2018-04-29 stsp return err;
1922 07b55e75 2018-05-10 stsp static void
1923 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1925 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1926 07b55e75 2018-05-10 stsp int nscrolled = 0;
1928 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1929 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1932 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1933 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1934 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1935 07b55e75 2018-05-10 stsp if (entry) {
1936 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1937 07b55e75 2018-05-10 stsp nscrolled++;
1942 aa075928 2018-05-10 stsp static const struct got_error *
1943 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1945 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1946 5e224a3e 2019-02-22 stsp int errcode;
1948 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1950 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1951 ffe38506 2020-12-01 naddy if (ta->log_complete)
1954 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1955 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1956 7aafa0d1 2019-02-22 stsp if (errcode)
1957 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1958 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1961 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1962 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1964 7c1452c1 2020-03-26 stsp if (!wait)
1967 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1968 ffe38506 2020-12-01 naddy show_log_view(view);
1969 7c1452c1 2020-03-26 stsp update_panels();
1970 7c1452c1 2020-03-26 stsp doupdate();
1972 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1973 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1974 82954512 2020-02-03 stsp if (errcode)
1975 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1976 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1978 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1979 ffe38506 2020-12-01 naddy show_log_view(view);
1980 7c1452c1 2020-03-26 stsp update_panels();
1981 7c1452c1 2020-03-26 stsp doupdate();
1984 5e224a3e 2019-02-22 stsp return NULL;
1987 5e224a3e 2019-02-22 stsp static const struct got_error *
1988 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1990 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1991 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1992 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1993 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1995 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1996 5e224a3e 2019-02-22 stsp return NULL;
1998 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1999 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2000 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2002 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2004 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2005 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2007 5e224a3e 2019-02-22 stsp return err;
2011 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2012 00ba99a7 2019-05-12 jcs if (pentry == NULL)
2015 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
2017 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2018 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2020 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2021 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2023 dd0a52c1 2018-05-20 stsp return err;
2026 cd0acaa7 2018-05-20 stsp static const struct got_error *
2027 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2028 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2029 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2031 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2032 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2033 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2035 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2036 15a94983 2018-12-23 stsp if (diff_view == NULL)
2037 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2039 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2040 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2041 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2042 e5a0f69f 2018-08-18 stsp if (err == NULL)
2043 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2044 cd0acaa7 2018-05-20 stsp return err;
2047 80ddbec8 2018-04-29 stsp static const struct got_error *
2048 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2049 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2051 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2053 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2054 941e9f74 2019-05-21 stsp if (parent == NULL)
2055 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2057 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2058 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2059 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2060 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2061 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2062 941e9f74 2019-05-21 stsp s->tree = subtree;
2063 941e9f74 2019-05-21 stsp s->selected = 0;
2064 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2065 941e9f74 2019-05-21 stsp return NULL;
2068 941e9f74 2019-05-21 stsp static const struct got_error *
2069 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2070 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2072 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2073 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2074 941e9f74 2019-05-21 stsp const char *p;
2075 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2077 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2079 941e9f74 2019-05-21 stsp while (*p) {
2080 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2081 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2082 56e0773d 2019-11-28 stsp char *te_name;
2084 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2087 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2088 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2089 941e9f74 2019-05-21 stsp if (slash == NULL)
2090 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2092 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2093 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2094 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2097 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2098 56e0773d 2019-11-28 stsp if (te == NULL) {
2099 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2100 56e0773d 2019-11-28 stsp free(te_name);
2103 56e0773d 2019-11-28 stsp free(te_name);
2104 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2106 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2107 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2109 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2110 941e9f74 2019-05-21 stsp if (slash)
2111 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2113 941e9f74 2019-05-21 stsp subpath = strdup(path);
2114 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2115 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2119 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2124 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2125 941e9f74 2019-05-21 stsp free(tree_id);
2129 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2130 941e9f74 2019-05-21 stsp if (err) {
2131 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2134 941e9f74 2019-05-21 stsp if (slash == NULL)
2136 941e9f74 2019-05-21 stsp free(subpath);
2137 941e9f74 2019-05-21 stsp subpath = NULL;
2138 941e9f74 2019-05-21 stsp p = slash;
2141 941e9f74 2019-05-21 stsp free(subpath);
2142 1a76625f 2018-10-22 stsp return err;
2145 61266923 2020-01-14 stsp static const struct got_error *
2146 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2147 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2148 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2150 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2151 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2152 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2154 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2155 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2156 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2158 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2160 55cccc34 2020-02-20 stsp return err;
2161 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2163 55cccc34 2020-02-20 stsp *new_view = tree_view;
2165 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2166 55cccc34 2020-02-20 stsp return NULL;
2168 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2171 55cccc34 2020-02-20 stsp static const struct got_error *
2172 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2174 61266923 2020-01-14 stsp sigset_t sigset;
2175 61266923 2020-01-14 stsp int errcode;
2177 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2178 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2180 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2181 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2182 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2183 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2184 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2185 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2186 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2187 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2188 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2190 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2191 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2192 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2194 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2195 61266923 2020-01-14 stsp if (errcode)
2196 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2198 61266923 2020-01-14 stsp return NULL;
2201 1a76625f 2018-10-22 stsp static void *
2202 1a76625f 2018-10-22 stsp log_thread(void *arg)
2204 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2205 1a76625f 2018-10-22 stsp int errcode = 0;
2206 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2207 1a76625f 2018-10-22 stsp int done = 0;
2209 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2211 61266923 2020-01-14 stsp return (void *)err;
2213 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2214 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2215 1a76625f 2018-10-22 stsp if (err) {
2216 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2217 1a76625f 2018-10-22 stsp return (void *)err;
2218 1a76625f 2018-10-22 stsp err = NULL;
2220 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2221 1a76625f 2018-10-22 stsp a->commits_needed--;
2223 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2224 3abe8080 2019-04-10 stsp if (errcode) {
2225 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2226 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2228 3abe8080 2019-04-10 stsp } else if (*a->quit)
2230 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2231 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2232 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2233 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2236 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2237 7c1452c1 2020-03-26 stsp if (errcode) {
2238 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2239 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2240 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2245 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2247 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2248 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2249 7c1452c1 2020-03-26 stsp &tog_mutex);
2250 7c1452c1 2020-03-26 stsp if (errcode)
2251 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2252 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2253 21355643 2020-12-06 stsp if (*a->quit)
2258 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2259 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2260 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2261 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2263 3abe8080 2019-04-10 stsp a->log_complete = 1;
2264 1a76625f 2018-10-22 stsp return (void *)err;
2267 1a76625f 2018-10-22 stsp static const struct got_error *
2268 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2270 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2271 1a76625f 2018-10-22 stsp int errcode;
2273 1a76625f 2018-10-22 stsp if (s->thread) {
2274 1a76625f 2018-10-22 stsp s->quit = 1;
2275 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2276 1a76625f 2018-10-22 stsp if (errcode)
2277 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2278 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2279 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2280 1a76625f 2018-10-22 stsp if (errcode)
2281 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2282 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2283 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2284 1a76625f 2018-10-22 stsp if (errcode)
2285 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2286 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2287 1a76625f 2018-10-22 stsp if (errcode)
2288 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2289 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2290 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2293 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2294 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2295 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2298 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2299 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2300 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2301 1af5eddf 2022-06-23 thomas if (err == NULL)
2302 1af5eddf 2022-06-23 thomas err = pack_err;
2303 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2306 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2307 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2308 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2311 9343a5fb 2018-06-23 stsp return err;
2314 9343a5fb 2018-06-23 stsp static const struct got_error *
2315 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2317 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2318 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2319 276b94a1 2020-11-13 naddy int errcode;
2321 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2323 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2324 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2325 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2327 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2328 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2329 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2331 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2332 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2333 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2334 1a76625f 2018-10-22 stsp free(s->start_id);
2335 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2336 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2337 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2338 1a76625f 2018-10-22 stsp return err;
2341 1a76625f 2018-10-22 stsp static const struct got_error *
2342 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2344 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2346 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2347 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2348 60493ae3 2019-06-20 stsp return NULL;
2351 60493ae3 2019-06-20 stsp static const struct got_error *
2352 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2354 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2355 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2356 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2358 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2359 f9686aa5 2020-03-27 stsp show_log_view(view);
2360 f9686aa5 2020-03-27 stsp update_panels();
2361 f9686aa5 2020-03-27 stsp doupdate();
2363 96e2b566 2019-07-08 stsp if (s->search_entry) {
2364 13add988 2019-10-15 stsp int errcode, ch;
2365 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2366 13add988 2019-10-15 stsp if (errcode)
2367 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2368 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2369 13add988 2019-10-15 stsp ch = wgetch(view->window);
2370 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2371 13add988 2019-10-15 stsp if (errcode)
2372 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2373 13add988 2019-10-15 stsp "pthread_mutex_lock");
2374 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2375 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2376 678cbce5 2019-07-28 stsp return NULL;
2378 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2379 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2381 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2382 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2383 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2384 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2385 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2387 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2388 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2390 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2393 60493ae3 2019-06-20 stsp while (1) {
2394 13add988 2019-10-15 stsp int have_match = 0;
2396 60493ae3 2019-06-20 stsp if (entry == NULL) {
2397 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2398 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2399 f9967bca 2020-03-27 stsp view->search_next_done =
2400 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2401 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2402 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2403 f801134a 2019-06-25 stsp return NULL;
2406 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2407 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2408 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2410 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2411 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2414 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2415 13add988 2019-10-15 stsp &view->regex);
2418 13add988 2019-10-15 stsp if (have_match) {
2419 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2420 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2424 96e2b566 2019-07-08 stsp s->search_entry = entry;
2425 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2426 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2428 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2431 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2432 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2433 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2434 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2436 60493ae3 2019-06-20 stsp return err;
2439 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2440 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2442 60493ae3 2019-06-20 stsp return err;
2447 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2449 60493ae3 2019-06-20 stsp return NULL;
2452 60493ae3 2019-06-20 stsp static const struct got_error *
2453 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2454 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2455 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2457 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2458 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2459 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2460 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2461 1a76625f 2018-10-22 stsp int errcode;
2463 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2464 f135c941 2020-02-20 stsp free(s->in_repo_path);
2465 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2466 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2467 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2470 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2471 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2472 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2474 fb2756b9 2018-08-04 stsp s->repo = repo;
2475 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2476 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2477 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2478 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2479 9cd7cbd1 2020-12-07 stsp goto done;
2482 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2483 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2484 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2485 5036bf37 2018-09-24 stsp goto done;
2487 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2489 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2490 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2491 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2492 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2494 11b20872 2019-11-08 stsp goto done;
2495 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2496 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2497 11b20872 2019-11-08 stsp if (err) {
2498 11b20872 2019-11-08 stsp free_colors(&s->colors);
2499 11b20872 2019-11-08 stsp goto done;
2501 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2502 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2503 11b20872 2019-11-08 stsp if (err) {
2504 11b20872 2019-11-08 stsp free_colors(&s->colors);
2505 11b20872 2019-11-08 stsp goto done;
2509 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2510 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2511 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2512 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2513 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2515 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2516 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2517 1af5eddf 2022-06-23 thomas if (err)
2518 1af5eddf 2022-06-23 thomas goto done;
2520 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2521 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2522 7cd52833 2022-06-23 thomas if (err)
2523 7cd52833 2022-06-23 thomas goto done;
2524 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2525 b672a97a 2020-01-27 stsp !s->log_branches);
2527 1a76625f 2018-10-22 stsp goto done;
2528 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2529 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2531 c5b78334 2020-01-12 stsp goto done;
2533 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2534 1a76625f 2018-10-22 stsp if (errcode) {
2535 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2536 1a76625f 2018-10-22 stsp goto done;
2538 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2539 7c1452c1 2020-03-26 stsp if (errcode) {
2540 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2541 7c1452c1 2020-03-26 stsp goto done;
2544 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2545 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2546 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2547 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2548 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2549 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2550 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2551 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2552 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2553 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2554 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2555 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2556 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2559 1a76625f 2018-10-22 stsp close_log_view(view);
2560 ba4f502b 2018-08-04 stsp return err;
2563 e5a0f69f 2018-08-18 stsp static const struct got_error *
2564 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2566 f2f6d207 2020-11-24 stsp const struct got_error *err;
2567 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2569 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2570 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2571 2b380cc8 2018-10-24 stsp &s->thread_args);
2572 2b380cc8 2018-10-24 stsp if (errcode)
2573 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2574 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2575 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2577 f2f6d207 2020-11-24 stsp return err;
2581 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2584 e5a0f69f 2018-08-18 stsp static const struct got_error *
2585 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2587 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2588 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2589 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2590 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2591 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2592 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
2594 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2595 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2596 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2597 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2598 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2599 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2600 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2601 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2602 fb280deb 2021-08-30 stsp select_commit(s);
2604 528dedf3 2021-08-30 stsp return NULL;
2607 528dedf3 2021-08-30 stsp switch (ch) {
2609 1e37a5c2 2019-05-12 jcs s->quit = 1;
2611 05171be4 2022-06-23 thomas case '0':
2612 05171be4 2022-06-23 thomas view->x = 0;
2614 05171be4 2022-06-23 thomas case '$':
2615 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
2617 05171be4 2022-06-23 thomas case KEY_RIGHT:
2618 05171be4 2022-06-23 thomas case 'l':
2619 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
2620 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
2622 05171be4 2022-06-23 thomas case KEY_LEFT:
2623 05171be4 2022-06-23 thomas case 'h':
2624 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
2627 1e37a5c2 2019-05-12 jcs case KEY_UP:
2630 f7140bf5 2021-10-17 thomas case CTRL('p'):
2631 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2633 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2634 1e37a5c2 2019-05-12 jcs s->selected--;
2636 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2637 912a3f79 2021-08-30 j select_commit(s);
2640 912a3f79 2021-08-30 j case KEY_HOME:
2641 912a3f79 2021-08-30 j s->selected = 0;
2642 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2643 2b779855 2020-12-05 naddy select_commit(s);
2645 70f17a53 2022-06-13 thomas case CTRL('u'):
2646 23427b14 2022-06-23 thomas case 'u':
2647 70f17a53 2022-06-13 thomas nscroll /= 2;
2648 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2649 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2650 a4292ac5 2019-05-12 jcs case CTRL('b'):
2651 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2653 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2654 70f17a53 2022-06-13 thomas s->selected = MAX(0, s->selected - nscroll - 1);
2656 70f17a53 2022-06-13 thomas log_scroll_up(s, nscroll);
2657 2b779855 2020-12-05 naddy select_commit(s);
2660 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2663 f7140bf5 2021-10-17 thomas case CTRL('n'):
2664 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2666 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2667 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2668 1e37a5c2 2019-05-12 jcs s->selected++;
2670 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2674 912a3f79 2021-08-30 j select_commit(s);
2677 912a3f79 2021-08-30 j case KEY_END: {
2678 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2679 912a3f79 2021-08-30 j * traverse them all. */
2680 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2681 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2682 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2685 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2686 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2687 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2688 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2690 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2691 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2693 f3bc9f1d 2021-09-05 naddy if (n > 0)
2694 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2695 2b779855 2020-12-05 naddy select_commit(s);
2698 bccd1d5d 2022-06-13 thomas case CTRL('d'):
2699 23427b14 2022-06-23 thomas case 'd':
2700 70f17a53 2022-06-13 thomas nscroll /= 2;
2701 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2702 70f17a53 2022-06-13 thomas case KEY_NPAGE:
2703 70f17a53 2022-06-13 thomas case CTRL('f'): {
2704 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2705 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2706 1e37a5c2 2019-05-12 jcs if (first == NULL)
2708 70f17a53 2022-06-13 thomas err = log_scroll_down(view, nscroll);
2711 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2712 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2713 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2714 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2715 70f17a53 2022-06-13 thomas s->selected += MIN(s->last_displayed_entry->idx -
2716 70f17a53 2022-06-13 thomas s->selected_entry->idx, nscroll + 1);
2718 2b779855 2020-12-05 naddy select_commit(s);
2721 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2722 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2723 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2724 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2725 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2726 2b779855 2020-12-05 naddy select_commit(s);
2727 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2728 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2729 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2730 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2731 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2734 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2737 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2739 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2740 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2741 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2742 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2743 78756c87 2020-11-24 stsp view, s->repo);
2746 e78dc838 2020-12-04 stsp view->focussed = 0;
2747 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2748 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2749 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2751 1e37a5c2 2019-05-12 jcs return err;
2752 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2753 e78dc838 2020-12-04 stsp view->focus_child = 1;
2755 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2758 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2760 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2761 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2762 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2763 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2767 e78dc838 2020-12-04 stsp view->focussed = 0;
2768 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2769 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2770 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2772 1e37a5c2 2019-05-12 jcs return err;
2773 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2774 e78dc838 2020-12-04 stsp view->focus_child = 1;
2776 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2778 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2779 21355643 2020-12-06 stsp case CTRL('l'):
2781 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2782 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2784 21355643 2020-12-06 stsp err = stop_log_thread(s);
2786 74cfe85e 2020-10-20 stsp return err;
2787 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2788 21355643 2020-12-06 stsp char *parent_path;
2789 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2791 21355643 2020-12-06 stsp return err;
2792 21355643 2020-12-06 stsp free(s->in_repo_path);
2793 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2794 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2795 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2796 21355643 2020-12-06 stsp struct got_object_id *start_id;
2797 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2798 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2799 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2801 21355643 2020-12-06 stsp return err;
2802 21355643 2020-12-06 stsp free(s->start_id);
2803 21355643 2020-12-06 stsp s->start_id = start_id;
2804 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2805 21355643 2020-12-06 stsp } else /* 'B' */
2806 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2808 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2809 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2810 bf7e79b3 2022-06-23 thomas if (err)
2811 bf7e79b3 2022-06-23 thomas return err;
2813 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
2814 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
2815 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2817 21355643 2020-12-06 stsp return err;
2818 51a10b52 2020-12-26 stsp tog_free_refs();
2819 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2821 ca51c541 2020-12-07 stsp return err;
2822 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2823 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2825 d01904d4 2019-06-25 stsp return err;
2826 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2827 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2829 b672a97a 2020-01-27 stsp return err;
2830 21355643 2020-12-06 stsp free_commits(&s->commits);
2831 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2832 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2833 21355643 2020-12-06 stsp s->selected_entry = NULL;
2834 21355643 2020-12-06 stsp s->selected = 0;
2835 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2836 21355643 2020-12-06 stsp s->quit = 0;
2837 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2840 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2841 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2842 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2843 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2844 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2845 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2846 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2847 6458efa5 2020-11-24 stsp if (err) {
2848 6458efa5 2020-11-24 stsp view_close(ref_view);
2849 6458efa5 2020-11-24 stsp return err;
2851 e78dc838 2020-12-04 stsp view->focussed = 0;
2852 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2853 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2854 6458efa5 2020-11-24 stsp err = view_close_child(view);
2856 6458efa5 2020-11-24 stsp return err;
2857 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2858 e78dc838 2020-12-04 stsp view->focus_child = 1;
2860 6458efa5 2020-11-24 stsp *new_view = ref_view;
2866 80ddbec8 2018-04-29 stsp return err;
2869 4ed7e80c 2018-05-20 stsp static const struct got_error *
2870 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2872 c2db6724 2019-01-04 stsp const struct got_error *error;
2874 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2875 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2876 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2878 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2879 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2881 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2882 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2884 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2885 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2887 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2888 c2db6724 2019-01-04 stsp if (error != NULL)
2889 c2db6724 2019-01-04 stsp return error;
2891 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2892 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2894 c2db6724 2019-01-04 stsp return NULL;
2897 a915003a 2019-02-05 stsp static void
2898 a915003a 2019-02-05 stsp init_curses(void)
2901 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
2902 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
2903 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
2905 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
2906 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
2907 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
2908 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
2909 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
2911 a915003a 2019-02-05 stsp initscr();
2913 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2916 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2917 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2918 a915003a 2019-02-05 stsp curs_set(0);
2919 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2920 6d17833f 2019-11-08 stsp start_color();
2921 6d17833f 2019-11-08 stsp use_default_colors();
2925 c2db6724 2019-01-04 stsp static const struct got_error *
2926 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2927 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2929 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2931 f135c941 2020-02-20 stsp if (argc == 0) {
2932 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2933 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2934 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2935 f135c941 2020-02-20 stsp return NULL;
2938 f135c941 2020-02-20 stsp if (worktree) {
2939 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2942 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2944 f135c941 2020-02-20 stsp return err;
2945 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2946 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2947 bfd61697 2020-11-14 stsp p) == -1) {
2948 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2949 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2953 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2955 f135c941 2020-02-20 stsp return err;
2958 f135c941 2020-02-20 stsp static const struct got_error *
2959 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2961 80ddbec8 2018-04-29 stsp const struct got_error *error;
2962 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2963 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2964 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2965 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2966 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2967 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2968 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2969 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2970 04cc582a 2018-08-01 stsp struct tog_view *view;
2971 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
2973 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2974 80ddbec8 2018-04-29 stsp switch (ch) {
2976 b672a97a 2020-01-27 stsp log_branches = 1;
2979 80ddbec8 2018-04-29 stsp start_commit = optarg;
2982 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2983 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2984 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2988 17020d27 2019-03-07 stsp usage_log();
2989 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2993 80ddbec8 2018-04-29 stsp argc -= optind;
2994 80ddbec8 2018-04-29 stsp argv += optind;
2996 f135c941 2020-02-20 stsp if (argc > 1)
2997 f135c941 2020-02-20 stsp usage_log();
2999 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3000 7cd52833 2022-06-23 thomas if (error != NULL)
3001 7cd52833 2022-06-23 thomas goto done;
3003 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3004 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3005 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3006 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3007 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3008 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3009 c156c7a4 2020-12-18 stsp goto done;
3010 a1fbf39a 2019-08-11 stsp if (worktree)
3011 6962eb72 2020-02-20 stsp repo_path =
3012 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3014 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3015 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3016 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3017 c156c7a4 2020-12-18 stsp goto done;
3021 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3022 80ddbec8 2018-04-29 stsp if (error != NULL)
3023 ecb28ae0 2018-07-16 stsp goto done;
3025 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3026 f135c941 2020-02-20 stsp repo, worktree);
3027 f135c941 2020-02-20 stsp if (error)
3028 f135c941 2020-02-20 stsp goto done;
3030 f135c941 2020-02-20 stsp init_curses();
3032 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3033 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3034 c02c541e 2019-03-29 stsp if (error)
3035 c02c541e 2019-03-29 stsp goto done;
3037 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3038 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3039 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3040 87670572 2020-12-26 naddy if (error)
3041 87670572 2020-12-26 naddy goto done;
3044 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3045 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3046 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3047 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3048 d8f38dc4 2020-12-05 stsp if (error)
3049 d8f38dc4 2020-12-05 stsp goto done;
3050 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3052 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3053 d8f38dc4 2020-12-05 stsp if (error == NULL)
3054 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3055 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3056 d8f38dc4 2020-12-05 stsp goto done;
3057 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3058 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3059 d8f38dc4 2020-12-05 stsp if (error)
3060 d8f38dc4 2020-12-05 stsp goto done;
3063 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3064 04cc582a 2018-08-01 stsp if (view == NULL) {
3065 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3066 04cc582a 2018-08-01 stsp goto done;
3068 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3069 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3070 ba4f502b 2018-08-04 stsp if (error)
3071 ba4f502b 2018-08-04 stsp goto done;
3072 2fc00ff4 2019-08-31 stsp if (worktree) {
3073 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3074 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3075 2fc00ff4 2019-08-31 stsp worktree = NULL;
3077 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3079 f135c941 2020-02-20 stsp free(in_repo_path);
3080 ecb28ae0 2018-07-16 stsp free(repo_path);
3081 ecb28ae0 2018-07-16 stsp free(cwd);
3082 899d86c2 2018-05-10 stsp free(start_id);
3083 d8f38dc4 2020-12-05 stsp free(label);
3085 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3086 1d0f4054 2021-06-17 stsp if (repo) {
3087 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3088 1d0f4054 2021-06-17 stsp if (error == NULL)
3089 1d0f4054 2021-06-17 stsp error = close_err;
3091 ec142235 2019-03-07 stsp if (worktree)
3092 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3093 7cd52833 2022-06-23 thomas if (pack_fds) {
3094 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3095 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3096 7cd52833 2022-06-23 thomas if (error == NULL)
3097 7cd52833 2022-06-23 thomas error = pack_err;
3099 51a10b52 2020-12-26 stsp tog_free_refs();
3100 80ddbec8 2018-04-29 stsp return error;
3103 4ed7e80c 2018-05-20 stsp __dead static void
3104 9f7d7167 2018-04-29 stsp usage_diff(void)
3107 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3108 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3112 6d17833f 2019-11-08 stsp static int
3113 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3114 41605754 2020-11-12 stsp regmatch_t *regmatch)
3116 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3119 f26dddb7 2019-11-08 stsp struct tog_color *
3120 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3122 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3124 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3125 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3126 6d17833f 2019-11-08 stsp return tc;
3129 6d17833f 2019-11-08 stsp return NULL;
3132 4ed7e80c 2018-05-20 stsp static const struct got_error *
3133 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3134 05171be4 2022-06-23 thomas WINDOW *window, int skip, regmatch_t *regmatch)
3136 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3137 41605754 2020-11-12 stsp wchar_t *wline;
3138 05171be4 2022-06-23 thomas int rme, rms, n, width;
3140 41605754 2020-11-12 stsp *wtotal = 0;
3141 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3142 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3144 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3145 05171be4 2022-06-23 thomas col_tab_align, 1);
3146 05171be4 2022-06-23 thomas if (err)
3147 41605754 2020-11-12 stsp return err;
3149 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3150 05171be4 2022-06-23 thomas n = MAX(rms - skip, 0);
3151 05171be4 2022-06-23 thomas if (n) {
3152 05171be4 2022-06-23 thomas waddnwstr(window, wline + skip, n);
3153 05171be4 2022-06-23 thomas wlimit -= n;
3154 05171be4 2022-06-23 thomas *wtotal += n;
3157 41605754 2020-11-12 stsp if (wlimit > 0) {
3158 05171be4 2022-06-23 thomas int len = rme - rms;
3160 05171be4 2022-06-23 thomas if (skip > rms) {
3161 05171be4 2022-06-23 thomas n = skip - rms;
3162 05171be4 2022-06-23 thomas len = MAX(len - n, 0);
3164 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3165 05171be4 2022-06-23 thomas if (len) {
3166 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3167 05171be4 2022-06-23 thomas waddnwstr(window, wline + rms + n, len);
3168 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3169 05171be4 2022-06-23 thomas wlimit -= len;
3170 05171be4 2022-06-23 thomas *wtotal += len;
3174 05171be4 2022-06-23 thomas if (wlimit > 0 && skip < width) { /* draw rest of line */
3176 05171be4 2022-06-23 thomas if (skip > rme)
3177 05171be4 2022-06-23 thomas n = MIN(skip - rme, width - rme);
3178 05171be4 2022-06-23 thomas waddnwstr(window, wline + rme + n, wlimit);
3181 05171be4 2022-06-23 thomas *wtotal = width;
3182 05171be4 2022-06-23 thomas free(wline);
3183 41605754 2020-11-12 stsp return NULL;
3186 41605754 2020-11-12 stsp static const struct got_error *
3187 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3189 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3190 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3191 61e69b96 2018-05-20 stsp const struct got_error *err;
3192 fe621944 2020-11-10 stsp int nprinted = 0;
3193 b304db33 2018-05-20 stsp char *line;
3194 826082fe 2020-12-10 stsp size_t linesize = 0;
3195 826082fe 2020-12-10 stsp ssize_t linelen;
3196 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3197 61e69b96 2018-05-20 stsp wchar_t *wline;
3198 e0b650dd 2018-05-20 stsp int width;
3199 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3200 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3201 fe621944 2020-11-10 stsp off_t line_offset;
3203 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3204 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3205 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3207 f7d12f7e 2018-08-01 stsp werase(view->window);
3209 a3404814 2018-09-02 stsp if (header) {
3210 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3211 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3212 135a2da0 2020-11-11 stsp header) == -1)
3213 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3214 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3216 135a2da0 2020-11-11 stsp free(line);
3218 a3404814 2018-09-02 stsp return err;
3220 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3221 a3404814 2018-09-02 stsp wstandout(view->window);
3222 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3223 e54cc94a 2020-11-11 stsp free(wline);
3224 e54cc94a 2020-11-11 stsp wline = NULL;
3225 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3226 a3404814 2018-09-02 stsp wstandend(view->window);
3227 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3228 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3230 a3404814 2018-09-02 stsp if (max_lines <= 1)
3231 a3404814 2018-09-02 stsp return NULL;
3232 a3404814 2018-09-02 stsp max_lines--;
3235 89f1a395 2020-12-01 naddy s->eof = 0;
3236 05171be4 2022-06-23 thomas view->maxx = 0;
3237 826082fe 2020-12-10 stsp line = NULL;
3238 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3239 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3240 826082fe 2020-12-10 stsp if (linelen == -1) {
3241 826082fe 2020-12-10 stsp if (feof(s->f)) {
3242 826082fe 2020-12-10 stsp s->eof = 1;
3245 826082fe 2020-12-10 stsp free(line);
3246 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3249 05171be4 2022-06-23 thomas view->maxx = MAX(view->maxx, linelen);
3251 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3253 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3254 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3255 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3256 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3257 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3258 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3259 41605754 2020-11-12 stsp if (err) {
3260 41605754 2020-11-12 stsp free(line);
3261 41605754 2020-11-12 stsp return err;
3264 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0,
3265 05171be4 2022-06-23 thomas view->x + view->ncols, 0, view->x ? 1 : 0);
3266 41605754 2020-11-12 stsp if (err) {
3267 41605754 2020-11-12 stsp free(line);
3268 41605754 2020-11-12 stsp return err;
3270 4122562d 2022-06-23 thomas if (view->x < width)
3271 05171be4 2022-06-23 thomas waddwstr(view->window, wline + view->x);
3272 41605754 2020-11-12 stsp free(wline);
3273 41605754 2020-11-12 stsp wline = NULL;
3276 6d17833f 2019-11-08 stsp wattr_off(view->window,
3277 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3278 05171be4 2022-06-23 thomas if (width - view->x <= view->ncols - 1)
3279 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3280 fe621944 2020-11-10 stsp nprinted++;
3282 826082fe 2020-12-10 stsp free(line);
3283 fe621944 2020-11-10 stsp if (nprinted >= 1)
3284 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3285 89f1a395 2020-12-01 naddy (nprinted - 1);
3287 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3289 1a57306a 2018-09-02 stsp view_vborder(view);
3291 89f1a395 2020-12-01 naddy if (s->eof) {
3292 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3293 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3294 c3e9aa98 2019-05-13 jcs nprinted++;
3297 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3298 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3300 c3e9aa98 2019-05-13 jcs return err;
3303 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3304 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3305 e54cc94a 2020-11-11 stsp free(wline);
3306 e54cc94a 2020-11-11 stsp wline = NULL;
3307 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3310 26ed57b2 2018-05-19 stsp return NULL;
3313 abd2672a 2018-12-23 stsp static char *
3314 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3316 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3317 09867e48 2019-08-13 stsp char *p, *s;
3319 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3320 09867e48 2019-08-13 stsp if (tm == NULL)
3321 09867e48 2019-08-13 stsp return NULL;
3322 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3323 09867e48 2019-08-13 stsp if (s == NULL)
3324 09867e48 2019-08-13 stsp return NULL;
3325 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3327 abd2672a 2018-12-23 stsp *p = '\0';
3331 4ed7e80c 2018-05-20 stsp static const struct got_error *
3332 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3333 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3335 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3336 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3337 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3338 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3340 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3341 0208f208 2020-05-05 stsp if (qid != NULL) {
3342 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3343 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3344 ec242592 2022-04-22 thomas &qid->id);
3346 0208f208 2020-05-05 stsp return err;
3348 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3349 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3350 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3351 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3352 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3354 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3358 0208f208 2020-05-05 stsp if (tree_id1) {
3359 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3361 0208f208 2020-05-05 stsp goto done;
3364 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3365 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3367 0208f208 2020-05-05 stsp goto done;
3369 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3370 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3372 0208f208 2020-05-05 stsp if (tree1)
3373 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3374 0208f208 2020-05-05 stsp if (tree2)
3375 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3376 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3377 0208f208 2020-05-05 stsp return err;
3380 0208f208 2020-05-05 stsp static const struct got_error *
3381 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3385 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3386 fe621944 2020-11-10 stsp if (p == NULL)
3387 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3388 fe621944 2020-11-10 stsp *line_offsets = p;
3389 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3390 fe621944 2020-11-10 stsp (*nlines)++;
3391 fe621944 2020-11-10 stsp return NULL;
3394 fe621944 2020-11-10 stsp static const struct got_error *
3395 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3396 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3397 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3399 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3400 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3401 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3402 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3403 45d799e2 2018-12-23 stsp time_t committer_time;
3404 45d799e2 2018-12-23 stsp const char *author, *committer;
3405 8b473291 2019-02-21 stsp char *refs_str = NULL;
3406 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3407 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3408 fe621944 2020-11-10 stsp off_t outoff = 0;
3411 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3413 8b473291 2019-02-21 stsp if (refs) {
3414 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3416 8b473291 2019-02-21 stsp return err;
3419 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3421 abd2672a 2018-12-23 stsp return err;
3423 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3424 15a94983 2018-12-23 stsp if (err) {
3425 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3426 15a94983 2018-12-23 stsp goto done;
3429 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3431 fe621944 2020-11-10 stsp goto done;
3433 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3434 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3435 fe621944 2020-11-10 stsp if (n < 0) {
3436 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3437 abd2672a 2018-12-23 stsp goto done;
3439 fe621944 2020-11-10 stsp outoff += n;
3440 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3442 fe621944 2020-11-10 stsp goto done;
3444 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3445 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3446 fe621944 2020-11-10 stsp if (n < 0) {
3447 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3448 abd2672a 2018-12-23 stsp goto done;
3450 fe621944 2020-11-10 stsp outoff += n;
3451 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3453 fe621944 2020-11-10 stsp goto done;
3455 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3456 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3457 fe621944 2020-11-10 stsp if (datestr) {
3458 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3459 fe621944 2020-11-10 stsp if (n < 0) {
3460 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3461 fe621944 2020-11-10 stsp goto done;
3463 fe621944 2020-11-10 stsp outoff += n;
3464 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3466 fe621944 2020-11-10 stsp goto done;
3468 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3469 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3470 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3471 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3472 fe621944 2020-11-10 stsp if (n < 0) {
3473 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3474 fe621944 2020-11-10 stsp goto done;
3476 fe621944 2020-11-10 stsp outoff += n;
3477 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3479 fe621944 2020-11-10 stsp goto done;
3481 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3482 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3483 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3484 ac4dc263 2021-09-24 thomas int pn = 1;
3485 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3486 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3487 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3488 ac4dc263 2021-09-24 thomas if (err)
3489 ac4dc263 2021-09-24 thomas goto done;
3490 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3491 ac4dc263 2021-09-24 thomas if (n < 0) {
3492 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3493 ac4dc263 2021-09-24 thomas goto done;
3495 ac4dc263 2021-09-24 thomas outoff += n;
3496 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3497 ac4dc263 2021-09-24 thomas if (err)
3498 ac4dc263 2021-09-24 thomas goto done;
3499 ac4dc263 2021-09-24 thomas free(id_str);
3500 ac4dc263 2021-09-24 thomas id_str = NULL;
3504 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3506 5943eee2 2019-08-13 stsp goto done;
3507 fe621944 2020-11-10 stsp s = logmsg;
3508 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3509 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3510 fe621944 2020-11-10 stsp if (n < 0) {
3511 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3512 fe621944 2020-11-10 stsp goto done;
3514 fe621944 2020-11-10 stsp outoff += n;
3515 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3517 fe621944 2020-11-10 stsp goto done;
3520 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3522 0208f208 2020-05-05 stsp goto done;
3523 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3524 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3525 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3526 fe621944 2020-11-10 stsp if (n < 0) {
3527 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3528 fe621944 2020-11-10 stsp goto done;
3530 fe621944 2020-11-10 stsp outoff += n;
3531 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3533 fe621944 2020-11-10 stsp goto done;
3534 0208f208 2020-05-05 stsp free((char *)pe->path);
3535 0208f208 2020-05-05 stsp free(pe->data);
3538 0208f208 2020-05-05 stsp fputc('\n', outfile);
3540 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3542 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3543 abd2672a 2018-12-23 stsp free(id_str);
3544 5943eee2 2019-08-13 stsp free(logmsg);
3545 8b473291 2019-02-21 stsp free(refs_str);
3546 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3547 7510f233 2020-08-09 stsp if (err) {
3548 ae6a6978 2020-08-09 stsp free(*line_offsets);
3549 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3550 ae6a6978 2020-08-09 stsp *nlines = 0;
3552 fe621944 2020-11-10 stsp return err;
3555 abd2672a 2018-12-23 stsp static const struct got_error *
3556 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3558 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3559 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3560 15a94983 2018-12-23 stsp int obj_type;
3562 fe621944 2020-11-10 stsp free(s->line_offsets);
3563 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3564 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3565 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3566 fe621944 2020-11-10 stsp s->nlines = 0;
3568 511a516b 2018-05-19 stsp f = got_opentemp();
3569 48ae06ee 2018-10-18 stsp if (f == NULL) {
3570 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3571 48ae06ee 2018-10-18 stsp goto done;
3573 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3574 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3575 fb43ecf1 2019-02-11 stsp goto done;
3579 15a94983 2018-12-23 stsp if (s->id1)
3580 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3582 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3584 15a94983 2018-12-23 stsp goto done;
3586 15a94983 2018-12-23 stsp switch (obj_type) {
3587 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3588 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3589 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3590 a0f32f33 2022-06-13 thomas s->diff_context, s->ignore_whitespace, s->force_text_diff,
3591 a0f32f33 2022-06-13 thomas s->repo, s->f);
3593 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3594 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3595 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3596 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3598 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3599 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3600 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3601 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3602 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3604 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3606 3ffacbe1 2020-02-02 tracey goto done;
3607 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3608 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3609 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3610 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3611 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3612 f44b1f58 2020-02-02 tracey if (err)
3613 f44b1f58 2020-02-02 tracey goto done;
3614 f44b1f58 2020-02-02 tracey } else {
3615 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3616 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3617 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3618 fe621944 2020-11-10 stsp err = write_commit_info(
3619 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3620 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3621 f44b1f58 2020-02-02 tracey if (err)
3622 f44b1f58 2020-02-02 tracey goto done;
3627 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3629 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3630 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3631 a0f32f33 2022-06-13 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3635 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3638 f44b1f58 2020-02-02 tracey if (err)
3639 f44b1f58 2020-02-02 tracey goto done;
3641 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3642 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3643 48ae06ee 2018-10-18 stsp return err;
3646 f5215bb9 2019-02-22 stsp static void
3647 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3649 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3650 f5215bb9 2019-02-22 stsp update_panels();
3651 f5215bb9 2019-02-22 stsp doupdate();
3654 f44b1f58 2020-02-02 tracey static const struct got_error *
3655 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3657 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3659 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3660 f44b1f58 2020-02-02 tracey return NULL;
3663 f44b1f58 2020-02-02 tracey static const struct got_error *
3664 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3666 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3667 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
3668 f44b1f58 2020-02-02 tracey int lineno;
3669 05171be4 2022-06-23 thomas char *exstr = NULL, *line = NULL;
3670 826082fe 2020-12-10 stsp size_t linesize = 0;
3671 826082fe 2020-12-10 stsp ssize_t linelen;
3673 f44b1f58 2020-02-02 tracey if (!view->searching) {
3674 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3675 f44b1f58 2020-02-02 tracey return NULL;
3678 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3679 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3680 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3682 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3684 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3686 f44b1f58 2020-02-02 tracey while (1) {
3687 f44b1f58 2020-02-02 tracey off_t offset;
3689 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3690 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3691 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3695 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3696 f44b1f58 2020-02-02 tracey lineno = 1;
3698 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3701 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3702 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3703 f44b1f58 2020-02-02 tracey free(line);
3704 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3706 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3707 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
3708 05171be4 2022-06-23 thomas if (err)
3710 826082fe 2020-12-10 stsp if (linelen != -1 &&
3711 05171be4 2022-06-23 thomas match_line(exstr, &view->regex, 1, &view->regmatch)) {
3712 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3713 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3716 05171be4 2022-06-23 thomas free(exstr);
3717 05171be4 2022-06-23 thomas exstr = NULL;
3718 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3719 f44b1f58 2020-02-02 tracey lineno++;
3721 f44b1f58 2020-02-02 tracey lineno--;
3723 826082fe 2020-12-10 stsp free(line);
3724 05171be4 2022-06-23 thomas free(exstr);
3726 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3727 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3728 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3731 05171be4 2022-06-23 thomas return err;
3734 48ae06ee 2018-10-18 stsp static const struct got_error *
3735 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
3737 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
3738 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
3740 a0f32f33 2022-06-13 thomas free(s->id1);
3741 a0f32f33 2022-06-13 thomas s->id1 = NULL;
3742 a0f32f33 2022-06-13 thomas free(s->id2);
3743 a0f32f33 2022-06-13 thomas s->id2 = NULL;
3744 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
3745 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3746 a0f32f33 2022-06-13 thomas s->f = NULL;
3747 a0f32f33 2022-06-13 thomas if (s->f1 && fclose(s->f1) == EOF)
3748 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3749 a0f32f33 2022-06-13 thomas s->f1 = NULL;
3750 a0f32f33 2022-06-13 thomas if (s->f2 && fclose(s->f2) == EOF)
3751 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3752 a0f32f33 2022-06-13 thomas s->f2 = NULL;
3753 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
3754 a0f32f33 2022-06-13 thomas free(s->line_offsets);
3755 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
3756 a0f32f33 2022-06-13 thomas s->nlines = 0;
3757 a0f32f33 2022-06-13 thomas return err;
3760 a0f32f33 2022-06-13 thomas static const struct got_error *
3761 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3762 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3763 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3764 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3766 48ae06ee 2018-10-18 stsp const struct got_error *err;
3767 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3769 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
3771 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3772 15a94983 2018-12-23 stsp int type1, type2;
3773 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3775 15a94983 2018-12-23 stsp return err;
3776 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3778 15a94983 2018-12-23 stsp return err;
3780 15a94983 2018-12-23 stsp if (type1 != type2)
3781 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3783 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3784 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3785 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3786 f44b1f58 2020-02-02 tracey s->repo = repo;
3787 f44b1f58 2020-02-02 tracey s->id1 = id1;
3788 f44b1f58 2020-02-02 tracey s->id2 = id2;
3789 3dbaef42 2020-11-24 stsp s->label1 = label1;
3790 3dbaef42 2020-11-24 stsp s->label2 = label2;
3792 15a94983 2018-12-23 stsp if (id1) {
3793 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3794 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3795 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3796 a0f32f33 2022-06-13 thomas s->f1 = got_opentemp();
3797 a0f32f33 2022-06-13 thomas if (s->f1 == NULL) {
3798 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3799 a0f32f33 2022-06-13 thomas goto done;
3802 5465d566 2020-02-01 tracey s->id1 = NULL;
3804 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3805 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3806 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
3807 a0f32f33 2022-06-13 thomas goto done;
3810 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
3811 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
3812 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3813 a0f32f33 2022-06-13 thomas goto done;
3816 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3817 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3818 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3819 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3820 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3821 5465d566 2020-02-01 tracey s->log_view = log_view;
3822 5465d566 2020-02-01 tracey s->repo = repo;
3824 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3825 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3826 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3827 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3828 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3830 a0f32f33 2022-06-13 thomas goto done;
3831 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3832 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3833 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3834 a0f32f33 2022-06-13 thomas if (err)
3835 a0f32f33 2022-06-13 thomas goto done;
3836 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3837 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3838 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3839 a0f32f33 2022-06-13 thomas if (err)
3840 a0f32f33 2022-06-13 thomas goto done;
3842 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3843 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3844 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3845 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3846 a0f32f33 2022-06-13 thomas if (err)
3847 a0f32f33 2022-06-13 thomas goto done;
3849 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3850 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3851 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3852 a0f32f33 2022-06-13 thomas if (err)
3853 a0f32f33 2022-06-13 thomas goto done;
3855 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3856 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3857 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3858 a0f32f33 2022-06-13 thomas if (err)
3859 a0f32f33 2022-06-13 thomas goto done;
3862 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3863 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3864 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3866 5465d566 2020-02-01 tracey err = create_diff(s);
3868 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3869 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3870 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3871 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3872 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3874 a0f32f33 2022-06-13 thomas if (err)
3875 a0f32f33 2022-06-13 thomas close_diff_view(view);
3876 e5a0f69f 2018-08-18 stsp return err;
3879 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3880 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3882 a3404814 2018-09-02 stsp const struct got_error *err;
3883 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3884 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3885 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3887 a3404814 2018-09-02 stsp if (s->id1) {
3888 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3890 a3404814 2018-09-02 stsp return err;
3891 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3893 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3895 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3897 a3404814 2018-09-02 stsp return err;
3898 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3900 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3901 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3902 a3404814 2018-09-02 stsp free(id_str1);
3903 a3404814 2018-09-02 stsp free(id_str2);
3904 a3404814 2018-09-02 stsp return err;
3906 a3404814 2018-09-02 stsp free(id_str1);
3907 a3404814 2018-09-02 stsp free(id_str2);
3909 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3910 267bb3b8 2021-08-01 stsp free(header);
3911 267bb3b8 2021-08-01 stsp return err;
3914 15a087fe 2019-02-21 stsp static const struct got_error *
3915 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3916 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3918 d7a04538 2019-02-21 stsp const struct got_error *err;
3919 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3920 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3921 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3923 15a087fe 2019-02-21 stsp free(s->id2);
3924 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3925 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3926 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3928 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3930 d7a04538 2019-02-21 stsp return err;
3931 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3932 15a087fe 2019-02-21 stsp free(s->id1);
3933 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3934 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3935 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3936 15a087fe 2019-02-21 stsp return NULL;
3939 0cf4efb1 2018-09-29 stsp static const struct got_error *
3940 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3942 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3943 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3944 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3945 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3946 826082fe 2020-12-10 stsp char *line = NULL;
3947 826082fe 2020-12-10 stsp size_t linesize = 0;
3948 826082fe 2020-12-10 stsp ssize_t linelen;
3949 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
3951 e5a0f69f 2018-08-18 stsp switch (ch) {
3952 05171be4 2022-06-23 thomas case '0':
3953 05171be4 2022-06-23 thomas view->x = 0;
3955 05171be4 2022-06-23 thomas case '$':
3956 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
3958 05171be4 2022-06-23 thomas case KEY_RIGHT:
3959 05171be4 2022-06-23 thomas case 'l':
3960 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
3961 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3963 05171be4 2022-06-23 thomas case KEY_LEFT:
3964 05171be4 2022-06-23 thomas case 'h':
3965 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3969 3dbaef42 2020-11-24 stsp if (ch == 'a')
3970 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3971 3dbaef42 2020-11-24 stsp if (ch == 'w')
3972 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3973 64453f7e 2020-11-21 stsp wclear(view->window);
3974 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3975 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3976 aa61903a 2021-12-31 thomas s->matched_line = 0;
3977 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3978 64453f7e 2020-11-21 stsp err = create_diff(s);
3981 912a3f79 2021-08-30 j case KEY_HOME:
3982 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3985 912a3f79 2021-08-30 j case KEY_END:
3989 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3993 1e37a5c2 2019-05-12 jcs case KEY_UP:
3994 f7140bf5 2021-10-17 thomas case CTRL('p'):
3995 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3996 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3998 70f17a53 2022-06-13 thomas case CTRL('u'):
3999 23427b14 2022-06-23 thomas case 'u':
4000 70f17a53 2022-06-13 thomas nscroll /= 2;
4001 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4002 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4003 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4004 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
4007 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4008 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4011 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4012 f7140bf5 2021-10-17 thomas case CTRL('n'):
4013 1e37a5c2 2019-05-12 jcs if (!s->eof)
4014 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4016 70f17a53 2022-06-13 thomas case CTRL('d'):
4017 23427b14 2022-06-23 thomas case 'd':
4018 70f17a53 2022-06-13 thomas nscroll /= 2;
4019 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4020 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4021 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4023 00ba99a7 2019-05-12 jcs if (s->eof)
4026 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4027 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4028 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4029 826082fe 2020-12-10 stsp if (linelen == -1) {
4030 826082fe 2020-12-10 stsp if (feof(s->f)) {
4031 826082fe 2020-12-10 stsp s->eof = 1;
4033 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4037 826082fe 2020-12-10 stsp free(line);
4040 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4041 1e37a5c2 2019-05-12 jcs s->diff_context--;
4042 aa61903a 2021-12-31 thomas s->matched_line = 0;
4043 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4044 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4045 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4046 27829c9e 2020-11-21 stsp s->nlines) {
4047 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4048 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4053 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4054 1e37a5c2 2019-05-12 jcs s->diff_context++;
4055 aa61903a 2021-12-31 thomas s->matched_line = 0;
4056 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4057 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4062 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4064 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4065 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4067 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4071 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4074 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4078 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4079 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4080 aa61903a 2021-12-31 thomas s->matched_line = 0;
4081 05171be4 2022-06-23 thomas view->x = 0;
4083 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4084 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4088 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4090 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4091 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4093 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4097 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4100 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4104 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4105 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4106 aa61903a 2021-12-31 thomas s->matched_line = 0;
4107 05171be4 2022-06-23 thomas view->x = 0;
4109 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4110 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4116 bcbd79e2 2018-08-19 stsp return err;
4119 4ed7e80c 2018-05-20 stsp static const struct got_error *
4120 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4122 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4123 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4124 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4125 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4126 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4127 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4128 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4129 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4130 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4131 3dbaef42 2020-11-24 stsp const char *errstr;
4132 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4133 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4135 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4136 26ed57b2 2018-05-19 stsp switch (ch) {
4138 64453f7e 2020-11-21 stsp force_text_diff = 1;
4141 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4143 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4144 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4145 8f666e67 2022-02-12 thomas errstr, errstr);
4147 09b5bff8 2020-02-23 naddy case 'r':
4148 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4149 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4150 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4152 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4155 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4158 17020d27 2019-03-07 stsp usage_diff();
4159 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4163 26ed57b2 2018-05-19 stsp argc -= optind;
4164 26ed57b2 2018-05-19 stsp argv += optind;
4166 26ed57b2 2018-05-19 stsp if (argc == 0) {
4167 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4168 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4169 15a94983 2018-12-23 stsp id_str1 = argv[0];
4170 15a94983 2018-12-23 stsp id_str2 = argv[1];
4172 26ed57b2 2018-05-19 stsp usage_diff();
4174 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4175 7cd52833 2022-06-23 thomas if (error)
4176 7cd52833 2022-06-23 thomas goto done;
4178 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4179 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4180 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4181 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4182 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4183 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4184 c156c7a4 2020-12-18 stsp goto done;
4185 a273ac94 2020-02-23 naddy if (worktree)
4186 a273ac94 2020-02-23 naddy repo_path =
4187 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4189 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4190 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4191 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4192 c156c7a4 2020-12-18 stsp goto done;
4196 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4197 eb6600df 2019-01-04 stsp if (error)
4198 eb6600df 2019-01-04 stsp goto done;
4200 a273ac94 2020-02-23 naddy init_curses();
4202 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4203 51a10b52 2020-12-26 stsp if (error)
4204 51a10b52 2020-12-26 stsp goto done;
4206 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4207 26ed57b2 2018-05-19 stsp if (error)
4208 26ed57b2 2018-05-19 stsp goto done;
4210 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4211 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4212 26ed57b2 2018-05-19 stsp if (error)
4213 26ed57b2 2018-05-19 stsp goto done;
4215 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4216 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4217 26ed57b2 2018-05-19 stsp if (error)
4218 26ed57b2 2018-05-19 stsp goto done;
4220 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4221 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4222 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4223 ea5e7bb5 2018-08-01 stsp goto done;
4225 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4226 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4227 5dc9f4bc 2018-08-04 stsp if (error)
4228 5dc9f4bc 2018-08-04 stsp goto done;
4229 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4231 3dbaef42 2020-11-24 stsp free(label1);
4232 3dbaef42 2020-11-24 stsp free(label2);
4233 c02c541e 2019-03-29 stsp free(repo_path);
4234 a273ac94 2020-02-23 naddy free(cwd);
4235 1d0f4054 2021-06-17 stsp if (repo) {
4236 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4237 1d0f4054 2021-06-17 stsp if (error == NULL)
4238 1d0f4054 2021-06-17 stsp error = close_err;
4240 a273ac94 2020-02-23 naddy if (worktree)
4241 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4242 7cd52833 2022-06-23 thomas if (pack_fds) {
4243 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4244 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4245 7cd52833 2022-06-23 thomas if (error == NULL)
4246 7cd52833 2022-06-23 thomas error = pack_err;
4248 51a10b52 2020-12-26 stsp tog_free_refs();
4249 26ed57b2 2018-05-19 stsp return error;
4252 4ed7e80c 2018-05-20 stsp __dead static void
4253 9f7d7167 2018-04-29 stsp usage_blame(void)
4256 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4257 9f7d7167 2018-04-29 stsp getprogname());
4261 84451b3e 2018-07-10 stsp struct tog_blame_line {
4262 84451b3e 2018-07-10 stsp int annotated;
4263 84451b3e 2018-07-10 stsp struct got_object_id *id;
4266 4ed7e80c 2018-05-20 stsp static const struct got_error *
4267 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4269 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4270 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4271 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4272 84451b3e 2018-07-10 stsp const struct got_error *err;
4273 02bce7e2 2022-06-23 thomas int lineno = 0, nprinted = 0, i;
4274 826082fe 2020-12-10 stsp char *line = NULL;
4275 826082fe 2020-12-10 stsp size_t linesize = 0;
4276 826082fe 2020-12-10 stsp ssize_t linelen;
4277 84451b3e 2018-07-10 stsp wchar_t *wline;
4278 27a741e5 2019-09-11 stsp int width;
4279 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4280 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4281 ab089a2a 2018-07-12 stsp char *id_str;
4282 11b20872 2019-11-08 stsp struct tog_color *tc;
4284 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4286 ab089a2a 2018-07-12 stsp return err;
4288 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4289 f7d12f7e 2018-08-01 stsp werase(view->window);
4291 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4292 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4293 ab089a2a 2018-07-12 stsp free(id_str);
4294 ab089a2a 2018-07-12 stsp return err;
4297 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4298 ab089a2a 2018-07-12 stsp free(line);
4299 2550e4c3 2018-07-13 stsp line = NULL;
4301 1cae65b4 2019-09-22 stsp return err;
4302 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4303 a3404814 2018-09-02 stsp wstandout(view->window);
4304 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4306 11b20872 2019-11-08 stsp wattr_on(view->window,
4307 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4308 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4310 11b20872 2019-11-08 stsp wattr_off(view->window,
4311 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4312 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4313 a3404814 2018-09-02 stsp wstandend(view->window);
4314 2550e4c3 2018-07-13 stsp free(wline);
4315 2550e4c3 2018-07-13 stsp wline = NULL;
4316 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4317 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4319 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4320 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4321 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4322 ab089a2a 2018-07-12 stsp free(id_str);
4323 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4325 ab089a2a 2018-07-12 stsp free(id_str);
4326 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4327 3f60a8ef 2018-07-10 stsp free(line);
4328 2550e4c3 2018-07-13 stsp line = NULL;
4330 3f60a8ef 2018-07-10 stsp return err;
4331 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4332 2550e4c3 2018-07-13 stsp free(wline);
4333 2550e4c3 2018-07-13 stsp wline = NULL;
4334 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4335 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4337 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4338 05171be4 2022-06-23 thomas view->maxx = 0;
4339 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4340 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4341 826082fe 2020-12-10 stsp if (linelen == -1) {
4342 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4343 826082fe 2020-12-10 stsp s->eof = 1;
4346 84451b3e 2018-07-10 stsp free(line);
4347 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4349 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4352 05171be4 2022-06-23 thomas view->maxx = MAX(view->maxx, linelen);
4354 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4355 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4357 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4358 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4359 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4360 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4361 27a741e5 2019-09-11 stsp !(view->focussed &&
4362 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4363 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4364 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4365 8d0fe45a 2019-08-12 stsp char *id_str;
4366 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4367 8d0fe45a 2019-08-12 stsp if (err) {
4368 8d0fe45a 2019-08-12 stsp free(line);
4369 8d0fe45a 2019-08-12 stsp return err;
4371 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4373 11b20872 2019-11-08 stsp wattr_on(view->window,
4374 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4375 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4377 11b20872 2019-11-08 stsp wattr_off(view->window,
4378 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4379 8d0fe45a 2019-08-12 stsp free(id_str);
4380 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4382 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4383 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4386 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4387 ee41ec32 2018-07-10 stsp prev_id = NULL;
4390 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4391 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4392 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4394 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4395 41605754 2020-11-12 stsp width = 9;
4396 41605754 2020-11-12 stsp wline = wcsdup(L"");
4397 41605754 2020-11-12 stsp if (wline == NULL) {
4398 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4399 41605754 2020-11-12 stsp free(line);
4400 41605754 2020-11-12 stsp return err;
4402 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4403 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4404 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4405 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4406 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4407 41605754 2020-11-12 stsp if (err) {
4408 41605754 2020-11-12 stsp free(line);
4409 41605754 2020-11-12 stsp return err;
4411 41605754 2020-11-12 stsp width += 9;
4413 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0,
4414 05171be4 2022-06-23 thomas view->x + view->ncols - 9, 9, 1);
4415 02bce7e2 2022-06-23 thomas if (err) {
4416 02bce7e2 2022-06-23 thomas free(line);
4417 02bce7e2 2022-06-23 thomas return err;
4419 02bce7e2 2022-06-23 thomas if (view->x < width) {
4420 02bce7e2 2022-06-23 thomas waddwstr(view->window, wline + view->x);
4421 02bce7e2 2022-06-23 thomas for (i = 0; i < view->x; i++)
4422 02bce7e2 2022-06-23 thomas width -= wcwidth(wline[i]);
4424 02bce7e2 2022-06-23 thomas width += 9;
4425 41605754 2020-11-12 stsp free(wline);
4426 41605754 2020-11-12 stsp wline = NULL;
4429 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4430 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4431 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4432 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4434 826082fe 2020-12-10 stsp free(line);
4435 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4437 1a57306a 2018-09-02 stsp view_vborder(view);
4439 84451b3e 2018-07-10 stsp return NULL;
4442 84451b3e 2018-07-10 stsp static const struct got_error *
4443 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4444 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4446 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4447 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4448 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4449 1a76625f 2018-10-22 stsp int errcode;
4451 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4452 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4453 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4455 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4456 1a76625f 2018-10-22 stsp if (errcode)
4457 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4459 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4460 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4461 d68a0a7d 2018-07-10 stsp goto done;
4464 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4465 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4467 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4468 d68a0a7d 2018-07-10 stsp if (line->annotated)
4469 d68a0a7d 2018-07-10 stsp goto done;
4471 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4472 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4473 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4474 84451b3e 2018-07-10 stsp goto done;
4476 84451b3e 2018-07-10 stsp line->annotated = 1;
4478 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4479 1a76625f 2018-10-22 stsp if (errcode)
4480 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4481 84451b3e 2018-07-10 stsp return err;
4484 84451b3e 2018-07-10 stsp static void *
4485 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4487 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4488 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4489 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4490 1a76625f 2018-10-22 stsp int errcode;
4492 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4494 61266923 2020-01-14 stsp return (void *)err;
4496 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4497 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4498 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4499 fc06ba56 2019-08-22 stsp err = NULL;
4501 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4502 1a76625f 2018-10-22 stsp if (errcode)
4503 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4504 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4506 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4507 1d0f4054 2021-06-17 stsp if (err == NULL)
4508 1d0f4054 2021-06-17 stsp err = close_err;
4509 c9beca56 2018-07-22 stsp ta->repo = NULL;
4510 c9beca56 2018-07-22 stsp *ta->complete = 1;
4512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4513 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4514 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4516 18430de3 2018-07-10 stsp return (void *)err;
4519 245d91c1 2018-07-12 stsp static struct got_object_id *
4520 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4521 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4523 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4525 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4526 8d0fe45a 2019-08-12 stsp return NULL;
4528 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4529 245d91c1 2018-07-12 stsp if (!line->annotated)
4530 245d91c1 2018-07-12 stsp return NULL;
4532 245d91c1 2018-07-12 stsp return line->id;
4535 b880a918 2018-07-10 stsp static const struct got_error *
4536 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4538 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4541 245d91c1 2018-07-12 stsp if (blame->thread) {
4542 1a76625f 2018-10-22 stsp int errcode;
4543 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4544 1a76625f 2018-10-22 stsp if (errcode)
4545 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4546 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4547 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4548 1a76625f 2018-10-22 stsp if (errcode)
4549 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4550 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4551 1a76625f 2018-10-22 stsp if (errcode)
4552 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4553 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4554 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4555 245d91c1 2018-07-12 stsp err = NULL;
4556 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4558 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4559 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4560 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4561 1d0f4054 2021-06-17 stsp if (err == NULL)
4562 1d0f4054 2021-06-17 stsp err = close_err;
4563 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4565 245d91c1 2018-07-12 stsp if (blame->f) {
4566 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4567 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4568 245d91c1 2018-07-12 stsp blame->f = NULL;
4570 57670559 2018-12-23 stsp if (blame->lines) {
4571 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4572 57670559 2018-12-23 stsp free(blame->lines[i].id);
4573 57670559 2018-12-23 stsp free(blame->lines);
4574 57670559 2018-12-23 stsp blame->lines = NULL;
4576 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4577 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4578 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
4579 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4580 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
4581 7cd52833 2022-06-23 thomas if (err == NULL)
4582 7cd52833 2022-06-23 thomas err = pack_err;
4583 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
4585 245d91c1 2018-07-12 stsp return err;
4588 245d91c1 2018-07-12 stsp static const struct got_error *
4589 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4591 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4592 fc06ba56 2019-08-22 stsp int *done = arg;
4593 fc06ba56 2019-08-22 stsp int errcode;
4595 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4596 fc06ba56 2019-08-22 stsp if (errcode)
4597 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4598 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4600 fc06ba56 2019-08-22 stsp if (*done)
4601 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4603 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4604 fc06ba56 2019-08-22 stsp if (errcode)
4605 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4606 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4608 fc06ba56 2019-08-22 stsp return err;
4611 fc06ba56 2019-08-22 stsp static const struct got_error *
4612 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4614 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4615 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4616 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4617 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4618 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4619 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4620 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4621 15a94983 2018-12-23 stsp int obj_type;
4622 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4624 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4625 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4627 15a94983 2018-12-23 stsp return err;
4629 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4630 945f9229 2022-04-16 thomas if (err)
4631 945f9229 2022-04-16 thomas goto done;
4633 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4635 84451b3e 2018-07-10 stsp goto done;
4637 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4638 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4639 84451b3e 2018-07-10 stsp goto done;
4642 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4644 a70480e0 2018-06-23 stsp goto done;
4645 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4646 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4647 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4648 84451b3e 2018-07-10 stsp goto done;
4650 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4651 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4653 1fddf795 2021-01-20 stsp goto done;
4654 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4655 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4656 84451b3e 2018-07-10 stsp goto done;
4659 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4660 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4661 b02560ec 2019-08-19 stsp blame->nlines--;
4663 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4664 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4665 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4666 84451b3e 2018-07-10 stsp goto done;
4669 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
4671 bd24772e 2018-07-11 stsp goto done;
4672 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4673 7cd52833 2022-06-23 thomas pack_fds);
4674 7cd52833 2022-06-23 thomas if (err)
4675 7cd52833 2022-06-23 thomas goto done;
4677 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
4678 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4679 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4680 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4681 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4682 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4683 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4684 245d91c1 2018-07-12 stsp goto done;
4686 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4688 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4689 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4690 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4691 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4692 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4693 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4694 a5388363 2020-12-01 naddy s->blame_complete = 0;
4696 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4697 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4698 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4699 f5a09613 2020-12-13 naddy s->selected_line = 1;
4701 aa61903a 2021-12-31 thomas s->matched_line = 0;
4704 945f9229 2022-04-16 thomas if (commit)
4705 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4707 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4708 27d434c2 2018-09-15 stsp free(obj_id);
4710 245d91c1 2018-07-12 stsp stop_blame(blame);
4711 245d91c1 2018-07-12 stsp return err;
4714 245d91c1 2018-07-12 stsp static const struct got_error *
4715 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4716 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4718 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4719 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4721 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4723 c4843652 2019-08-12 stsp s->path = strdup(path);
4724 c4843652 2019-08-12 stsp if (s->path == NULL)
4725 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4727 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4728 c4843652 2019-08-12 stsp if (err) {
4729 c4843652 2019-08-12 stsp free(s->path);
4730 7cbe629d 2018-08-04 stsp return err;
4733 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4734 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4735 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4736 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4737 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4738 fb2756b9 2018-08-04 stsp s->repo = repo;
4739 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4740 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4742 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4743 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4744 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4745 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4747 11b20872 2019-11-08 stsp return err;
4750 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4751 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4752 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4753 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4754 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4756 a5388363 2020-12-01 naddy return run_blame(view);
4759 e5a0f69f 2018-08-18 stsp static const struct got_error *
4760 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4762 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4763 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4765 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4766 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4768 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4769 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4770 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4771 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4772 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4775 e5a0f69f 2018-08-18 stsp free(s->path);
4776 11b20872 2019-11-08 stsp free_colors(&s->colors);
4777 e5a0f69f 2018-08-18 stsp return err;
4780 7cbe629d 2018-08-04 stsp static const struct got_error *
4781 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4783 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4785 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4786 6c4c42e0 2019-06-24 stsp return NULL;
4789 6c4c42e0 2019-06-24 stsp static const struct got_error *
4790 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4792 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4793 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4794 6c4c42e0 2019-06-24 stsp int lineno;
4795 05171be4 2022-06-23 thomas char *exstr = NULL, *line = NULL;
4796 826082fe 2020-12-10 stsp size_t linesize = 0;
4797 826082fe 2020-12-10 stsp ssize_t linelen;
4799 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4800 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4801 6c4c42e0 2019-06-24 stsp return NULL;
4804 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4805 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4806 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4808 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4810 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4812 6c4c42e0 2019-06-24 stsp while (1) {
4813 6c4c42e0 2019-06-24 stsp off_t offset;
4815 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4816 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4817 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4821 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4822 6c4c42e0 2019-06-24 stsp lineno = 1;
4824 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4827 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4828 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4829 6c4c42e0 2019-06-24 stsp free(line);
4830 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4832 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4833 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
4834 05171be4 2022-06-23 thomas if (err)
4836 826082fe 2020-12-10 stsp if (linelen != -1 &&
4837 05171be4 2022-06-23 thomas match_line(exstr, &view->regex, 1, &view->regmatch)) {
4838 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4839 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4842 05171be4 2022-06-23 thomas free(exstr);
4843 05171be4 2022-06-23 thomas exstr = NULL;
4844 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4849 826082fe 2020-12-10 stsp free(line);
4850 05171be4 2022-06-23 thomas free(exstr);
4852 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4853 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4854 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4857 05171be4 2022-06-23 thomas return err;
4860 6c4c42e0 2019-06-24 stsp static const struct got_error *
4861 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4863 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4864 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4865 2b380cc8 2018-10-24 stsp int errcode;
4867 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4868 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4869 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4870 2b380cc8 2018-10-24 stsp if (errcode)
4871 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4873 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4876 51fe7530 2019-08-19 stsp if (s->blame_complete)
4877 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4879 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4881 669b5ffa 2018-10-07 stsp view_vborder(view);
4882 e5a0f69f 2018-08-18 stsp return err;
4885 e5a0f69f 2018-08-18 stsp static const struct got_error *
4886 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4888 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4889 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4890 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4891 70f17a53 2022-06-13 thomas int begin_x = 0, nscroll = view->nlines - 2;
4893 e5a0f69f 2018-08-18 stsp switch (ch) {
4894 05171be4 2022-06-23 thomas case '0':
4895 05171be4 2022-06-23 thomas view->x = 0;
4897 05171be4 2022-06-23 thomas case '$':
4898 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4900 05171be4 2022-06-23 thomas case KEY_RIGHT:
4901 05171be4 2022-06-23 thomas case 'l':
4902 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4903 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4905 05171be4 2022-06-23 thomas case KEY_LEFT:
4906 05171be4 2022-06-23 thomas case 'h':
4907 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4910 1e37a5c2 2019-05-12 jcs s->done = 1;
4912 4deef56f 2021-09-02 naddy case 'g':
4913 4deef56f 2021-09-02 naddy case KEY_HOME:
4914 4deef56f 2021-09-02 naddy s->selected_line = 1;
4915 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4917 4deef56f 2021-09-02 naddy case 'G':
4918 4deef56f 2021-09-02 naddy case KEY_END:
4919 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4920 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4921 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4923 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4924 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4925 4deef56f 2021-09-02 naddy (view->nlines - 3);
4929 1e37a5c2 2019-05-12 jcs case KEY_UP:
4930 f7140bf5 2021-10-17 thomas case CTRL('p'):
4931 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4932 1e37a5c2 2019-05-12 jcs s->selected_line--;
4933 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4934 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4935 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4937 70f17a53 2022-06-13 thomas case CTRL('u'):
4938 23427b14 2022-06-23 thomas case 'u':
4939 70f17a53 2022-06-13 thomas nscroll /= 2;
4940 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4941 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4942 ea025d1d 2020-02-22 naddy case CTRL('b'):
4943 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4944 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
4947 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
4948 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
4950 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4953 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4954 f7140bf5 2021-10-17 thomas case CTRL('n'):
4955 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4956 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4957 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4958 1e37a5c2 2019-05-12 jcs s->selected_line++;
4959 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4960 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4961 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4964 1e37a5c2 2019-05-12 jcs case 'p': {
4965 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4966 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4967 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4968 1e37a5c2 2019-05-12 jcs if (id == NULL)
4970 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4971 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4972 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4973 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4974 1e37a5c2 2019-05-12 jcs int obj_type;
4975 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4976 1e37a5c2 2019-05-12 jcs s->repo, id);
4979 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4980 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4981 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4982 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4985 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4986 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4987 ec242592 2022-04-22 thomas s->repo, &pid->id);
4988 945f9229 2022-04-16 thomas if (err)
4990 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4991 945f9229 2022-04-16 thomas pcommit, s->path);
4992 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4993 e5a0f69f 2018-08-18 stsp if (err) {
4994 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4995 1e37a5c2 2019-05-12 jcs err = NULL;
4996 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4999 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5001 1e37a5c2 2019-05-12 jcs free(blob_id);
5002 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5003 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5004 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5007 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5008 ec242592 2022-04-22 thomas &pid->id);
5009 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5011 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5012 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5014 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5019 1e37a5c2 2019-05-12 jcs s->done = 1;
5020 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5021 1e37a5c2 2019-05-12 jcs s->done = 0;
5022 1e37a5c2 2019-05-12 jcs if (thread_err)
5024 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5025 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5026 a5388363 2020-12-01 naddy err = run_blame(view);
5031 1e37a5c2 2019-05-12 jcs case 'B': {
5032 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5033 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5034 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5036 1e37a5c2 2019-05-12 jcs s->done = 1;
5037 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5038 1e37a5c2 2019-05-12 jcs s->done = 0;
5039 1e37a5c2 2019-05-12 jcs if (thread_err)
5041 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5042 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5043 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5044 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5045 a5388363 2020-12-01 naddy err = run_blame(view);
5050 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5051 1e37a5c2 2019-05-12 jcs case '\r': {
5052 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5053 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5054 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5055 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5056 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5057 1e37a5c2 2019-05-12 jcs if (id == NULL)
5059 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5062 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5063 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
5064 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5065 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5066 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5067 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5068 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5069 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5072 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5073 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5074 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5076 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5079 e78dc838 2020-12-04 stsp view->focussed = 0;
5080 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5081 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5082 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5085 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
5086 e78dc838 2020-12-04 stsp view->focus_child = 1;
5088 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5093 70f17a53 2022-06-13 thomas case CTRL('d'):
5094 23427b14 2022-06-23 thomas case 'd':
5095 70f17a53 2022-06-13 thomas nscroll /= 2;
5096 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5097 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5098 ea025d1d 2020-02-22 naddy case CTRL('f'):
5100 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5101 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5102 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5105 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5106 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5107 70f17a53 2022-06-13 thomas s->selected_line +=
5108 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5109 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5111 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5112 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5114 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5115 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5117 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5118 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5119 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5120 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5126 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5129 a70480e0 2018-06-23 stsp static const struct got_error *
5130 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5132 a70480e0 2018-06-23 stsp const struct got_error *error;
5133 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5134 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5135 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5136 0587e10c 2020-07-23 stsp char *link_target = NULL;
5137 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5138 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5139 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5141 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5142 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5144 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5145 a70480e0 2018-06-23 stsp switch (ch) {
5147 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5150 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5151 69069811 2018-08-02 stsp if (repo_path == NULL)
5152 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5156 17020d27 2019-03-07 stsp usage_blame();
5157 a70480e0 2018-06-23 stsp /* NOTREACHED */
5161 a70480e0 2018-06-23 stsp argc -= optind;
5162 a70480e0 2018-06-23 stsp argv += optind;
5164 f135c941 2020-02-20 stsp if (argc != 1)
5165 a70480e0 2018-06-23 stsp usage_blame();
5167 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5168 7cd52833 2022-06-23 thomas if (error != NULL)
5169 7cd52833 2022-06-23 thomas goto done;
5171 69069811 2018-08-02 stsp if (repo_path == NULL) {
5172 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5173 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5174 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5175 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5176 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5177 c156c7a4 2020-12-18 stsp goto done;
5178 f135c941 2020-02-20 stsp if (worktree)
5179 eb41ed75 2019-02-05 stsp repo_path =
5180 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5182 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5183 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5184 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5185 c156c7a4 2020-12-18 stsp goto done;
5189 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5190 c02c541e 2019-03-29 stsp if (error != NULL)
5191 8e94dd5b 2019-01-04 stsp goto done;
5193 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5194 f135c941 2020-02-20 stsp worktree);
5195 c02c541e 2019-03-29 stsp if (error)
5196 92205607 2019-01-04 stsp goto done;
5198 f135c941 2020-02-20 stsp init_curses();
5200 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5201 51a10b52 2020-12-26 stsp if (error)
5202 51a10b52 2020-12-26 stsp goto done;
5204 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5205 eb41ed75 2019-02-05 stsp if (error)
5206 69069811 2018-08-02 stsp goto done;
5208 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5209 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5210 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5211 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5212 a70480e0 2018-06-23 stsp if (error != NULL)
5213 66b4983c 2018-06-23 stsp goto done;
5214 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5215 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5217 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5218 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5220 a19e88aa 2018-06-23 stsp if (error != NULL)
5221 8b473291 2019-02-21 stsp goto done;
5223 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5224 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5225 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5226 e1cd8fed 2018-08-01 stsp goto done;
5229 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5230 945f9229 2022-04-16 thomas if (error)
5231 945f9229 2022-04-16 thomas goto done;
5233 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5234 945f9229 2022-04-16 thomas commit, repo);
5235 7cbe629d 2018-08-04 stsp if (error)
5236 7cbe629d 2018-08-04 stsp goto done;
5238 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5239 78756c87 2020-11-24 stsp commit_id, repo);
5240 0587e10c 2020-07-23 stsp if (error)
5241 0587e10c 2020-07-23 stsp goto done;
5242 12314ad4 2019-08-31 stsp if (worktree) {
5243 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5244 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5245 12314ad4 2019-08-31 stsp worktree = NULL;
5247 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5249 69069811 2018-08-02 stsp free(repo_path);
5250 f135c941 2020-02-20 stsp free(in_repo_path);
5251 0587e10c 2020-07-23 stsp free(link_target);
5252 69069811 2018-08-02 stsp free(cwd);
5253 a70480e0 2018-06-23 stsp free(commit_id);
5254 945f9229 2022-04-16 thomas if (commit)
5255 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5256 eb41ed75 2019-02-05 stsp if (worktree)
5257 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5258 1d0f4054 2021-06-17 stsp if (repo) {
5259 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5260 1d0f4054 2021-06-17 stsp if (error == NULL)
5261 1d0f4054 2021-06-17 stsp error = close_err;
5263 7cd52833 2022-06-23 thomas if (pack_fds) {
5264 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5265 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5266 7cd52833 2022-06-23 thomas if (error == NULL)
5267 7cd52833 2022-06-23 thomas error = pack_err;
5269 51a10b52 2020-12-26 stsp tog_free_refs();
5270 a70480e0 2018-06-23 stsp return error;
5273 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5274 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5276 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5277 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5278 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5279 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5280 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5281 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5282 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5284 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5286 f7d12f7e 2018-08-01 stsp werase(view->window);
5288 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5289 ffd1d5e5 2018-06-23 stsp return NULL;
5291 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5294 ffd1d5e5 2018-06-23 stsp return err;
5295 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5296 a3404814 2018-09-02 stsp wstandout(view->window);
5297 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5299 11b20872 2019-11-08 stsp wattr_on(view->window,
5300 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5301 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5303 11b20872 2019-11-08 stsp wattr_off(view->window,
5304 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5305 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5306 a3404814 2018-09-02 stsp wstandend(view->window);
5307 2550e4c3 2018-07-13 stsp free(wline);
5308 2550e4c3 2018-07-13 stsp wline = NULL;
5309 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5310 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5311 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5312 ffd1d5e5 2018-06-23 stsp return NULL;
5313 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5316 ce52c690 2018-06-23 stsp return err;
5317 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5318 2550e4c3 2018-07-13 stsp free(wline);
5319 2550e4c3 2018-07-13 stsp wline = NULL;
5320 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5321 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5322 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5323 ffd1d5e5 2018-06-23 stsp return NULL;
5324 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5325 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5326 a1eca9bb 2018-06-23 stsp return NULL;
5328 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5329 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5330 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5331 0cf4efb1 2018-09-29 stsp if (view->focussed)
5332 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5333 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5335 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5336 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5337 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5338 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5339 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5340 ffd1d5e5 2018-06-23 stsp return NULL;
5344 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5347 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5348 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5349 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5350 848d6979 2019-08-12 stsp const char *modestr = "";
5351 56e0773d 2019-11-28 stsp mode_t mode;
5353 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5354 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5356 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5357 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5358 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5360 638f9024 2019-05-13 stsp return got_error_from_errno(
5361 230a42bd 2019-05-11 jcs "got_object_id_str");
5363 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5364 63c5ca5d 2019-08-24 stsp modestr = "$";
5365 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5368 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5369 d86d3b18 2020-12-01 naddy te, s->repo);
5370 0d6c6ee3 2020-05-20 stsp if (err) {
5371 0d6c6ee3 2020-05-20 stsp free(id_str);
5372 0d6c6ee3 2020-05-20 stsp return err;
5374 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5375 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5376 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5378 848d6979 2019-08-12 stsp modestr = "@";
5380 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5381 848d6979 2019-08-12 stsp modestr = "/";
5382 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5383 848d6979 2019-08-12 stsp modestr = "*";
5384 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5385 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5386 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5387 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5388 1d13200f 2018-07-12 stsp free(id_str);
5389 0d6c6ee3 2020-05-20 stsp free(link_target);
5390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5392 1d13200f 2018-07-12 stsp free(id_str);
5393 0d6c6ee3 2020-05-20 stsp free(link_target);
5394 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5396 ffd1d5e5 2018-06-23 stsp if (err) {
5397 ffd1d5e5 2018-06-23 stsp free(line);
5400 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5401 0cf4efb1 2018-09-29 stsp if (view->focussed)
5402 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5403 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5405 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5407 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5408 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5409 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5411 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5412 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5413 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5414 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5415 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5416 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5417 ffd1d5e5 2018-06-23 stsp free(line);
5418 2550e4c3 2018-07-13 stsp free(wline);
5419 2550e4c3 2018-07-13 stsp wline = NULL;
5421 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5422 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5423 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5427 ffd1d5e5 2018-06-23 stsp return err;
5430 ffd1d5e5 2018-06-23 stsp static void
5431 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5433 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5434 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5435 fa86c4bf 2020-11-29 stsp int i = 0;
5437 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5440 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5441 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5442 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5443 fa86c4bf 2020-11-29 stsp if (!isroot)
5444 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5447 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5448 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5452 fa86c4bf 2020-11-29 stsp static void
5453 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5455 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5456 ffd1d5e5 2018-06-23 stsp int n = 0;
5458 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5459 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5460 694d3271 2020-12-01 naddy s->first_displayed_entry);
5462 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5464 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5465 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5466 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5467 768394f3 2019-01-24 stsp if (last) {
5468 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5469 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5474 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5475 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5476 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5478 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5479 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5480 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5482 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5483 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5484 56e0773d 2019-11-28 stsp + 1 /* slash */;
5486 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5488 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5489 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5490 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5492 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5493 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5494 d9765a41 2018-06-23 stsp while (pt) {
5495 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5496 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5497 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5498 cb2ebc8a 2018-06-23 stsp goto done;
5500 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5501 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5502 cb2ebc8a 2018-06-23 stsp goto done;
5504 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5507 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5508 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5509 ce52c690 2018-06-23 stsp goto done;
5513 ce52c690 2018-06-23 stsp if (err) {
5514 ce52c690 2018-06-23 stsp free(*path);
5515 ce52c690 2018-06-23 stsp *path = NULL;
5517 ce52c690 2018-06-23 stsp return err;
5520 ce52c690 2018-06-23 stsp static const struct got_error *
5521 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5522 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5523 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5525 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5526 ce52c690 2018-06-23 stsp char *path;
5527 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5529 a0de39f3 2019-08-09 stsp *new_view = NULL;
5531 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5533 ce52c690 2018-06-23 stsp return err;
5535 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5536 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5537 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5538 83ce39e3 2019-08-12 stsp goto done;
5541 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5542 e5a0f69f 2018-08-18 stsp if (err) {
5543 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5544 fc06ba56 2019-08-22 stsp err = NULL;
5545 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5547 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5549 83ce39e3 2019-08-12 stsp free(path);
5550 69efd4c4 2018-07-18 stsp return err;
5553 69efd4c4 2018-07-18 stsp static const struct got_error *
5554 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5555 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5557 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5558 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5559 69efd4c4 2018-07-18 stsp char *path;
5561 a0de39f3 2019-08-09 stsp *new_view = NULL;
5563 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5564 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5565 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5567 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5569 69efd4c4 2018-07-18 stsp return err;
5571 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5574 e5a0f69f 2018-08-18 stsp view_close(log_view);
5576 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5577 cb2ebc8a 2018-06-23 stsp free(path);
5578 cb2ebc8a 2018-06-23 stsp return err;
5581 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5582 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5583 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5585 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5586 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5587 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5588 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5590 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5591 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5593 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5594 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5595 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5597 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5599 bc573f3b 2021-07-10 stsp goto done;
5602 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5603 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5604 bc573f3b 2021-07-10 stsp * closed on demand.
5606 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5607 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5609 bc573f3b 2021-07-10 stsp goto done;
5610 bc573f3b 2021-07-10 stsp s->tree = s->root;
5612 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5613 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5614 ffd1d5e5 2018-06-23 stsp goto done;
5616 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5617 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5618 ffd1d5e5 2018-06-23 stsp goto done;
5621 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5622 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5623 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5624 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5625 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5626 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5627 9cd7cbd1 2020-12-07 stsp goto done;
5630 fb2756b9 2018-08-04 stsp s->repo = repo;
5632 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5633 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5634 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5635 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5637 c0b01bdb 2019-11-08 stsp goto done;
5638 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5639 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5641 c0b01bdb 2019-11-08 stsp goto done;
5642 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5643 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5644 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5646 c0b01bdb 2019-11-08 stsp goto done;
5648 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5649 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5650 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5652 11b20872 2019-11-08 stsp goto done;
5654 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5655 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5657 c0b01bdb 2019-11-08 stsp goto done;
5660 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5661 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5662 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5663 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5664 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5666 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5667 bc573f3b 2021-07-10 stsp if (commit)
5668 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5670 bc573f3b 2021-07-10 stsp close_tree_view(view);
5671 ad80ab7b 2018-08-04 stsp return err;
5674 e5a0f69f 2018-08-18 stsp static const struct got_error *
5675 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5677 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5679 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5680 fb2756b9 2018-08-04 stsp free(s->tree_label);
5681 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5682 6484ec90 2018-09-29 stsp free(s->commit_id);
5683 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5684 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5685 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5686 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5687 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5688 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5689 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5690 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5691 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5692 ad80ab7b 2018-08-04 stsp free(parent);
5695 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5696 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5697 bc573f3b 2021-07-10 stsp if (s->root)
5698 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5699 7c32bd05 2019-06-22 stsp return NULL;
5702 7c32bd05 2019-06-22 stsp static const struct got_error *
5703 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5705 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5707 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5708 7c32bd05 2019-06-22 stsp return NULL;
5711 7c32bd05 2019-06-22 stsp static int
5712 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5714 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5716 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5720 7c32bd05 2019-06-22 stsp static const struct got_error *
5721 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5723 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5724 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5726 7c32bd05 2019-06-22 stsp if (!view->searching) {
5727 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5728 7c32bd05 2019-06-22 stsp return NULL;
5731 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5732 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5733 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5734 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5735 56e0773d 2019-11-28 stsp s->selected_entry);
5737 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5739 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5740 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5742 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5743 56e0773d 2019-11-28 stsp s->selected_entry);
5746 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5747 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5748 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5749 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5751 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5754 7c32bd05 2019-06-22 stsp while (1) {
5755 56e0773d 2019-11-28 stsp if (te == NULL) {
5756 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5757 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5758 ac66afb8 2019-06-24 stsp return NULL;
5760 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5761 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5763 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5766 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5767 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5768 56e0773d 2019-11-28 stsp s->matched_entry = te;
5772 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5773 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5775 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5778 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5779 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5780 7c32bd05 2019-06-22 stsp s->selected = 0;
5783 e5a0f69f 2018-08-18 stsp return NULL;
5786 ad80ab7b 2018-08-04 stsp static const struct got_error *
5787 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5789 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5790 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5791 e5a0f69f 2018-08-18 stsp char *parent_path;
5793 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5795 e5a0f69f 2018-08-18 stsp return err;
5797 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5798 e5a0f69f 2018-08-18 stsp free(parent_path);
5800 669b5ffa 2018-10-07 stsp view_vborder(view);
5801 e5a0f69f 2018-08-18 stsp return err;
5804 e5a0f69f 2018-08-18 stsp static const struct got_error *
5805 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5807 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5808 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5809 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5810 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5811 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 3;
5813 e5a0f69f 2018-08-18 stsp switch (ch) {
5815 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5818 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5820 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5821 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5822 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5823 e78dc838 2020-12-04 stsp view->focussed = 0;
5824 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5825 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5826 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5828 1e37a5c2 2019-05-12 jcs return err;
5829 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5830 e78dc838 2020-12-04 stsp view->focus_child = 1;
5832 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5835 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5836 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5837 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5838 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5839 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5840 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5841 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5842 152c1c93 2020-11-29 stsp if (err) {
5843 152c1c93 2020-11-29 stsp view_close(ref_view);
5844 152c1c93 2020-11-29 stsp return err;
5846 e78dc838 2020-12-04 stsp view->focussed = 0;
5847 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5848 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5849 152c1c93 2020-11-29 stsp err = view_close_child(view);
5851 152c1c93 2020-11-29 stsp return err;
5852 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5853 e78dc838 2020-12-04 stsp view->focus_child = 1;
5855 152c1c93 2020-11-29 stsp *new_view = ref_view;
5857 e4526bf5 2021-09-03 naddy case 'g':
5858 e4526bf5 2021-09-03 naddy case KEY_HOME:
5859 e4526bf5 2021-09-03 naddy s->selected = 0;
5860 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5861 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5862 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5864 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5866 e4526bf5 2021-09-03 naddy case 'G':
5867 e4526bf5 2021-09-03 naddy case KEY_END:
5868 e4526bf5 2021-09-03 naddy s->selected = 0;
5869 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5870 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5871 e4526bf5 2021-09-03 naddy if (te == NULL) {
5872 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5873 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5878 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5879 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5881 e4526bf5 2021-09-03 naddy if (n > 0)
5882 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5885 1e37a5c2 2019-05-12 jcs case KEY_UP:
5886 f7140bf5 2021-10-17 thomas case CTRL('p'):
5887 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5888 1e37a5c2 2019-05-12 jcs s->selected--;
5891 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5893 70f17a53 2022-06-13 thomas case CTRL('u'):
5894 23427b14 2022-06-23 thomas case 'u':
5895 70f17a53 2022-06-13 thomas nscroll /= 2;
5896 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5897 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5898 ea025d1d 2020-02-22 naddy case CTRL('b'):
5899 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5900 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5901 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5902 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5904 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5905 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5907 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
5910 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5911 f7140bf5 2021-10-17 thomas case CTRL('n'):
5912 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5913 1e37a5c2 2019-05-12 jcs s->selected++;
5916 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5918 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5920 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5922 70f17a53 2022-06-13 thomas case CTRL('d'):
5923 23427b14 2022-06-23 thomas case 'd':
5924 70f17a53 2022-06-13 thomas nscroll /= 2;
5925 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5926 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5927 ea025d1d 2020-02-22 naddy case CTRL('f'):
5928 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5930 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5931 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5932 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
5933 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
5936 70f17a53 2022-06-13 thomas tree_scroll_down(s, nscroll);
5938 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5940 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5941 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5942 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5943 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5944 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5946 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5947 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5949 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5950 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5951 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5952 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5953 1e37a5c2 2019-05-12 jcs s->selected_entry =
5954 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5955 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5956 1e37a5c2 2019-05-12 jcs free(parent);
5957 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5958 56e0773d 2019-11-28 stsp s->selected_entry))) {
5959 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5960 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5961 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5964 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5965 941e9f74 2019-05-21 stsp if (err) {
5966 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5969 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5970 56e0773d 2019-11-28 stsp s->selected_entry))) {
5971 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5972 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5973 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5975 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5976 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5977 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5980 e78dc838 2020-12-04 stsp view->focussed = 0;
5981 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5982 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5983 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5985 669b5ffa 2018-10-07 stsp return err;
5986 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5987 e78dc838 2020-12-04 stsp view->focus_child = 1;
5989 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5992 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5993 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5994 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6000 ffd1d5e5 2018-06-23 stsp return err;
6003 ffd1d5e5 2018-06-23 stsp __dead static void
6004 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6007 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6008 ffd1d5e5 2018-06-23 stsp getprogname());
6012 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6013 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6015 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6016 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6017 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6018 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6019 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6020 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6021 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6022 4e97c21c 2020-12-06 stsp char *label = NULL;
6023 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6024 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6026 5221c383 2018-08-01 stsp struct tog_view *view;
6027 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6029 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6030 ffd1d5e5 2018-06-23 stsp switch (ch) {
6032 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6035 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6036 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6037 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6041 e99e2d15 2020-11-24 naddy usage_tree();
6042 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6046 ffd1d5e5 2018-06-23 stsp argc -= optind;
6047 ffd1d5e5 2018-06-23 stsp argv += optind;
6049 55cccc34 2020-02-20 stsp if (argc > 1)
6050 e99e2d15 2020-11-24 naddy usage_tree();
6052 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6053 7cd52833 2022-06-23 thomas if (error != NULL)
6054 7cd52833 2022-06-23 thomas goto done;
6056 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6057 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6058 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6059 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6060 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6061 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6062 c156c7a4 2020-12-18 stsp goto done;
6063 55cccc34 2020-02-20 stsp if (worktree)
6064 52185f70 2019-02-05 stsp repo_path =
6065 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6067 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6068 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6069 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6070 c156c7a4 2020-12-18 stsp goto done;
6074 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6075 c02c541e 2019-03-29 stsp if (error != NULL)
6076 52185f70 2019-02-05 stsp goto done;
6078 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6079 55cccc34 2020-02-20 stsp repo, worktree);
6080 55cccc34 2020-02-20 stsp if (error)
6081 55cccc34 2020-02-20 stsp goto done;
6083 55cccc34 2020-02-20 stsp init_curses();
6085 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6086 c02c541e 2019-03-29 stsp if (error)
6087 52185f70 2019-02-05 stsp goto done;
6089 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6090 51a10b52 2020-12-26 stsp if (error)
6091 51a10b52 2020-12-26 stsp goto done;
6093 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6094 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6095 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6096 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6097 4e97c21c 2020-12-06 stsp if (error)
6098 4e97c21c 2020-12-06 stsp goto done;
6099 4e97c21c 2020-12-06 stsp head_ref_name = label;
6101 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6102 4e97c21c 2020-12-06 stsp if (error == NULL)
6103 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6104 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6105 4e97c21c 2020-12-06 stsp goto done;
6106 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6107 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6108 4e97c21c 2020-12-06 stsp if (error)
6109 4e97c21c 2020-12-06 stsp goto done;
6112 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6113 945f9229 2022-04-16 thomas if (error)
6114 945f9229 2022-04-16 thomas goto done;
6116 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6117 5221c383 2018-08-01 stsp if (view == NULL) {
6118 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6119 5221c383 2018-08-01 stsp goto done;
6121 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6122 ad80ab7b 2018-08-04 stsp if (error)
6123 ad80ab7b 2018-08-04 stsp goto done;
6124 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6125 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
6126 d91faf3b 2020-12-01 naddy in_repo_path);
6127 55cccc34 2020-02-20 stsp if (error)
6128 55cccc34 2020-02-20 stsp goto done;
6131 55cccc34 2020-02-20 stsp if (worktree) {
6132 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6133 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6134 55cccc34 2020-02-20 stsp worktree = NULL;
6136 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6138 52185f70 2019-02-05 stsp free(repo_path);
6139 e4a0e26d 2020-02-20 stsp free(cwd);
6140 ffd1d5e5 2018-06-23 stsp free(commit_id);
6141 4e97c21c 2020-12-06 stsp free(label);
6143 486cd271 2020-12-06 stsp got_ref_close(ref);
6144 1d0f4054 2021-06-17 stsp if (repo) {
6145 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6146 1d0f4054 2021-06-17 stsp if (error == NULL)
6147 1d0f4054 2021-06-17 stsp error = close_err;
6149 7cd52833 2022-06-23 thomas if (pack_fds) {
6150 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6151 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6152 7cd52833 2022-06-23 thomas if (error == NULL)
6153 7cd52833 2022-06-23 thomas error = pack_err;
6155 51a10b52 2020-12-26 stsp tog_free_refs();
6156 ffd1d5e5 2018-06-23 stsp return error;
6159 6458efa5 2020-11-24 stsp static const struct got_error *
6160 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6162 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6163 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6165 6458efa5 2020-11-24 stsp s->nrefs = 0;
6166 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6167 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
6168 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
6169 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
6170 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
6173 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6174 6458efa5 2020-11-24 stsp if (re == NULL)
6175 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6177 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6178 8924d611 2020-12-26 stsp if (re->ref == NULL)
6179 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6180 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6181 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6184 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6185 6458efa5 2020-11-24 stsp return NULL;
6189 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6191 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6193 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6194 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6195 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6196 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6201 6458efa5 2020-11-24 stsp static const struct got_error *
6202 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6204 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6205 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6207 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6208 6458efa5 2020-11-24 stsp s->repo = repo;
6210 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6211 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6213 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6215 6458efa5 2020-11-24 stsp return err;
6217 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6218 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6219 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6220 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6222 6458efa5 2020-11-24 stsp goto done;
6224 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6225 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6226 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6228 6458efa5 2020-11-24 stsp goto done;
6230 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6231 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6232 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6233 2183bbf6 2022-01-23 thomas if (err)
6234 2183bbf6 2022-01-23 thomas goto done;
6236 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
6237 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
6238 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
6240 6458efa5 2020-11-24 stsp goto done;
6243 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6244 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6245 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6246 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6247 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6250 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6251 6458efa5 2020-11-24 stsp return err;
6254 6458efa5 2020-11-24 stsp static const struct got_error *
6255 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6257 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6259 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6260 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6262 6458efa5 2020-11-24 stsp return NULL;
6265 6458efa5 2020-11-24 stsp static const struct got_error *
6266 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6267 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6269 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6270 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6271 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6272 6458efa5 2020-11-24 stsp int obj_type;
6274 c42c9805 2020-11-24 stsp *commit_id = NULL;
6276 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6278 6458efa5 2020-11-24 stsp return err;
6280 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6282 6458efa5 2020-11-24 stsp goto done;
6284 6458efa5 2020-11-24 stsp switch (obj_type) {
6285 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6286 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6288 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6289 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6291 6458efa5 2020-11-24 stsp goto done;
6292 c42c9805 2020-11-24 stsp free(obj_id);
6293 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6294 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6296 6458efa5 2020-11-24 stsp goto done;
6297 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6298 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6299 c42c9805 2020-11-24 stsp goto done;
6301 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6302 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6303 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6304 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6305 c42c9805 2020-11-24 stsp goto done;
6309 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6315 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6316 c42c9805 2020-11-24 stsp if (err) {
6317 c42c9805 2020-11-24 stsp free(*commit_id);
6318 c42c9805 2020-11-24 stsp *commit_id = NULL;
6320 c42c9805 2020-11-24 stsp return err;
6323 c42c9805 2020-11-24 stsp static const struct got_error *
6324 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6325 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6327 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6328 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6329 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6331 c42c9805 2020-11-24 stsp *new_view = NULL;
6333 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6334 c42c9805 2020-11-24 stsp if (err) {
6335 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6336 c42c9805 2020-11-24 stsp return err;
6338 c42c9805 2020-11-24 stsp return NULL;
6341 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6342 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6343 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6344 6458efa5 2020-11-24 stsp goto done;
6347 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6348 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6351 6458efa5 2020-11-24 stsp view_close(log_view);
6353 6458efa5 2020-11-24 stsp *new_view = log_view;
6354 c42c9805 2020-11-24 stsp free(commit_id);
6355 6458efa5 2020-11-24 stsp return err;
6358 ce5b7c56 2019-07-09 stsp static void
6359 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6361 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6362 34ba6917 2020-11-30 stsp int i = 0;
6364 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6367 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6368 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6369 34ba6917 2020-11-30 stsp if (re == NULL)
6371 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6372 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6376 34ba6917 2020-11-30 stsp static void
6377 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6379 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6380 6458efa5 2020-11-24 stsp int n = 0;
6382 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6383 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6385 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6387 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6388 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6389 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6390 6458efa5 2020-11-24 stsp if (last) {
6391 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6392 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6397 6458efa5 2020-11-24 stsp static const struct got_error *
6398 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6400 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6402 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6403 6458efa5 2020-11-24 stsp return NULL;
6406 6458efa5 2020-11-24 stsp static int
6407 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6409 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6411 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6415 6458efa5 2020-11-24 stsp static const struct got_error *
6416 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6418 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6419 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6421 6458efa5 2020-11-24 stsp if (!view->searching) {
6422 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6423 6458efa5 2020-11-24 stsp return NULL;
6426 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6427 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6428 6458efa5 2020-11-24 stsp if (s->selected_entry)
6429 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6431 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6432 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6434 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6435 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6437 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6438 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6441 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6442 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6443 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6444 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6446 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6449 6458efa5 2020-11-24 stsp while (1) {
6450 6458efa5 2020-11-24 stsp if (re == NULL) {
6451 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6452 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6453 6458efa5 2020-11-24 stsp return NULL;
6455 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6456 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6458 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6461 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6462 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6463 6458efa5 2020-11-24 stsp s->matched_entry = re;
6467 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6468 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6470 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6473 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6474 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6475 6458efa5 2020-11-24 stsp s->selected = 0;
6478 6458efa5 2020-11-24 stsp return NULL;
6481 6458efa5 2020-11-24 stsp static const struct got_error *
6482 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6484 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6485 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6486 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6487 6458efa5 2020-11-24 stsp char *line = NULL;
6488 6458efa5 2020-11-24 stsp wchar_t *wline;
6489 6458efa5 2020-11-24 stsp struct tog_color *tc;
6490 6458efa5 2020-11-24 stsp int width, n;
6491 6458efa5 2020-11-24 stsp int limit = view->nlines;
6493 6458efa5 2020-11-24 stsp werase(view->window);
6495 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6497 6458efa5 2020-11-24 stsp if (limit == 0)
6498 6458efa5 2020-11-24 stsp return NULL;
6500 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6502 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6503 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6504 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6506 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6507 6458efa5 2020-11-24 stsp if (err) {
6508 6458efa5 2020-11-24 stsp free(line);
6509 6458efa5 2020-11-24 stsp return err;
6511 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6512 6458efa5 2020-11-24 stsp wstandout(view->window);
6513 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6514 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6515 6458efa5 2020-11-24 stsp wstandend(view->window);
6516 6458efa5 2020-11-24 stsp free(wline);
6517 6458efa5 2020-11-24 stsp wline = NULL;
6518 6458efa5 2020-11-24 stsp free(line);
6519 6458efa5 2020-11-24 stsp line = NULL;
6520 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6521 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6522 6458efa5 2020-11-24 stsp if (--limit <= 0)
6523 6458efa5 2020-11-24 stsp return NULL;
6526 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6527 6458efa5 2020-11-24 stsp char *line = NULL;
6528 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6530 84227eb1 2022-06-23 thomas if (s->show_date) {
6531 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
6532 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
6533 84227eb1 2022-06-23 thomas struct got_object_id *id;
6534 84227eb1 2022-06-23 thomas struct tm tm;
6535 84227eb1 2022-06-23 thomas time_t t;
6537 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
6538 84227eb1 2022-06-23 thomas if (err)
6539 84227eb1 2022-06-23 thomas return err;
6540 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
6541 84227eb1 2022-06-23 thomas if (err) {
6542 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
6543 84227eb1 2022-06-23 thomas free(id);
6544 84227eb1 2022-06-23 thomas return err;
6546 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
6548 84227eb1 2022-06-23 thomas if (err) {
6549 84227eb1 2022-06-23 thomas free(id);
6550 84227eb1 2022-06-23 thomas return err;
6552 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
6553 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
6554 84227eb1 2022-06-23 thomas } else {
6555 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
6556 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
6558 84227eb1 2022-06-23 thomas free(id);
6559 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
6560 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
6561 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6562 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
6564 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6565 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
6566 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
6567 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6568 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6569 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6570 6458efa5 2020-11-24 stsp struct got_object_id *id;
6571 6458efa5 2020-11-24 stsp char *id_str;
6572 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6574 6458efa5 2020-11-24 stsp return err;
6575 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6576 6458efa5 2020-11-24 stsp if (err) {
6578 6458efa5 2020-11-24 stsp return err;
6580 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6581 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6582 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6584 6458efa5 2020-11-24 stsp free(id_str);
6585 6458efa5 2020-11-24 stsp return err;
6588 6458efa5 2020-11-24 stsp free(id_str);
6589 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6590 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
6591 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
6593 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6595 6458efa5 2020-11-24 stsp if (err) {
6596 6458efa5 2020-11-24 stsp free(line);
6597 6458efa5 2020-11-24 stsp return err;
6599 6458efa5 2020-11-24 stsp if (n == s->selected) {
6600 6458efa5 2020-11-24 stsp if (view->focussed)
6601 6458efa5 2020-11-24 stsp wstandout(view->window);
6602 6458efa5 2020-11-24 stsp s->selected_entry = re;
6604 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6606 6458efa5 2020-11-24 stsp wattr_on(view->window,
6607 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6608 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6610 6458efa5 2020-11-24 stsp wattr_off(view->window,
6611 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6612 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6613 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6614 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6615 6458efa5 2020-11-24 stsp wstandend(view->window);
6616 6458efa5 2020-11-24 stsp free(line);
6617 6458efa5 2020-11-24 stsp free(wline);
6618 6458efa5 2020-11-24 stsp wline = NULL;
6620 6458efa5 2020-11-24 stsp s->ndisplayed++;
6621 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6624 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6627 6458efa5 2020-11-24 stsp view_vborder(view);
6628 6458efa5 2020-11-24 stsp return err;
6631 6458efa5 2020-11-24 stsp static const struct got_error *
6632 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6633 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6635 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6636 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6637 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6639 c42c9805 2020-11-24 stsp *new_view = NULL;
6641 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6642 c42c9805 2020-11-24 stsp if (err) {
6643 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6644 c42c9805 2020-11-24 stsp return err;
6646 c42c9805 2020-11-24 stsp return NULL;
6650 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6651 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6652 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6653 c42c9805 2020-11-24 stsp goto done;
6656 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6657 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6659 c42c9805 2020-11-24 stsp goto done;
6661 c42c9805 2020-11-24 stsp *new_view = tree_view;
6663 c42c9805 2020-11-24 stsp free(commit_id);
6664 c42c9805 2020-11-24 stsp return err;
6666 c42c9805 2020-11-24 stsp static const struct got_error *
6667 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6669 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6670 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6671 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6672 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6673 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
6675 6458efa5 2020-11-24 stsp switch (ch) {
6677 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6679 84227eb1 2022-06-23 thomas case 'm':
6680 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
6682 98182bd0 2021-11-20 thomas case 'o':
6683 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6684 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6685 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6686 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6687 c591440f 2021-11-20 thomas if (err)
6689 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6690 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6691 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6692 3bfadbd4 2021-11-20 thomas if (err)
6694 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6695 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6697 6458efa5 2020-11-24 stsp case KEY_ENTER:
6698 6458efa5 2020-11-24 stsp case '\r':
6699 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6701 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6702 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6703 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6705 e78dc838 2020-12-04 stsp view->focussed = 0;
6706 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6707 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6708 6458efa5 2020-11-24 stsp err = view_close_child(view);
6710 6458efa5 2020-11-24 stsp return err;
6711 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6712 e78dc838 2020-12-04 stsp view->focus_child = 1;
6714 6458efa5 2020-11-24 stsp *new_view = log_view;
6717 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6719 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6720 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6721 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6723 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6725 e78dc838 2020-12-04 stsp view->focussed = 0;
6726 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6727 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6728 c42c9805 2020-11-24 stsp err = view_close_child(view);
6730 c42c9805 2020-11-24 stsp return err;
6731 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6732 e78dc838 2020-12-04 stsp view->focus_child = 1;
6734 c42c9805 2020-11-24 stsp *new_view = tree_view;
6736 e4526bf5 2021-09-03 naddy case 'g':
6737 e4526bf5 2021-09-03 naddy case KEY_HOME:
6738 e4526bf5 2021-09-03 naddy s->selected = 0;
6739 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6741 e4526bf5 2021-09-03 naddy case 'G':
6742 e4526bf5 2021-09-03 naddy case KEY_END:
6743 e4526bf5 2021-09-03 naddy s->selected = 0;
6744 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6745 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6746 e4526bf5 2021-09-03 naddy if (re == NULL)
6748 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6749 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6751 e4526bf5 2021-09-03 naddy if (n > 0)
6752 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6755 6458efa5 2020-11-24 stsp case KEY_UP:
6756 f7140bf5 2021-10-17 thomas case CTRL('p'):
6757 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6758 6458efa5 2020-11-24 stsp s->selected--;
6761 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6763 70f17a53 2022-06-13 thomas case CTRL('u'):
6764 23427b14 2022-06-23 thomas case 'u':
6765 70f17a53 2022-06-13 thomas nscroll /= 2;
6766 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6767 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6768 6458efa5 2020-11-24 stsp case CTRL('b'):
6769 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6770 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
6771 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
6774 6458efa5 2020-11-24 stsp case KEY_DOWN:
6775 f7140bf5 2021-10-17 thomas case CTRL('n'):
6776 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6777 6458efa5 2020-11-24 stsp s->selected++;
6780 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6781 6458efa5 2020-11-24 stsp /* can't scroll any further */
6783 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6785 70f17a53 2022-06-13 thomas case CTRL('d'):
6786 23427b14 2022-06-23 thomas case 'd':
6787 70f17a53 2022-06-13 thomas nscroll /= 2;
6788 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6789 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6790 6458efa5 2020-11-24 stsp case CTRL('f'):
6791 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6792 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6793 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6794 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6795 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6798 70f17a53 2022-06-13 thomas ref_scroll_down(s, nscroll);
6800 6458efa5 2020-11-24 stsp case CTRL('l'):
6801 8924d611 2020-12-26 stsp tog_free_refs();
6802 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6805 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6806 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6808 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6809 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6810 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6816 6458efa5 2020-11-24 stsp return err;
6819 6458efa5 2020-11-24 stsp __dead static void
6820 6458efa5 2020-11-24 stsp usage_ref(void)
6823 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6824 6458efa5 2020-11-24 stsp getprogname());
6828 6458efa5 2020-11-24 stsp static const struct got_error *
6829 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6831 6458efa5 2020-11-24 stsp const struct got_error *error;
6832 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6833 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6834 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6836 6458efa5 2020-11-24 stsp struct tog_view *view;
6837 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6839 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6840 6458efa5 2020-11-24 stsp switch (ch) {
6842 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6843 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6844 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6848 e99e2d15 2020-11-24 naddy usage_ref();
6849 6458efa5 2020-11-24 stsp /* NOTREACHED */
6853 6458efa5 2020-11-24 stsp argc -= optind;
6854 6458efa5 2020-11-24 stsp argv += optind;
6856 6458efa5 2020-11-24 stsp if (argc > 1)
6857 e99e2d15 2020-11-24 naddy usage_ref();
6859 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6860 7cd52833 2022-06-23 thomas if (error != NULL)
6861 7cd52833 2022-06-23 thomas goto done;
6863 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6864 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6865 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6866 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6867 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6868 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6869 c156c7a4 2020-12-18 stsp goto done;
6870 6458efa5 2020-11-24 stsp if (worktree)
6871 6458efa5 2020-11-24 stsp repo_path =
6872 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6874 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6875 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6876 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6877 c156c7a4 2020-12-18 stsp goto done;
6881 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6882 6458efa5 2020-11-24 stsp if (error != NULL)
6883 6458efa5 2020-11-24 stsp goto done;
6885 6458efa5 2020-11-24 stsp init_curses();
6887 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6888 51a10b52 2020-12-26 stsp if (error)
6889 51a10b52 2020-12-26 stsp goto done;
6891 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6892 6458efa5 2020-11-24 stsp if (error)
6893 6458efa5 2020-11-24 stsp goto done;
6895 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6896 6458efa5 2020-11-24 stsp if (view == NULL) {
6897 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6898 6458efa5 2020-11-24 stsp goto done;
6901 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6902 6458efa5 2020-11-24 stsp if (error)
6903 6458efa5 2020-11-24 stsp goto done;
6905 6458efa5 2020-11-24 stsp if (worktree) {
6906 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6907 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6908 6458efa5 2020-11-24 stsp worktree = NULL;
6910 6458efa5 2020-11-24 stsp error = view_loop(view);
6912 6458efa5 2020-11-24 stsp free(repo_path);
6913 6458efa5 2020-11-24 stsp free(cwd);
6914 1d0f4054 2021-06-17 stsp if (repo) {
6915 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6916 1d0f4054 2021-06-17 stsp if (close_err)
6917 1d0f4054 2021-06-17 stsp error = close_err;
6919 7cd52833 2022-06-23 thomas if (pack_fds) {
6920 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6921 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6922 7cd52833 2022-06-23 thomas if (error == NULL)
6923 7cd52833 2022-06-23 thomas error = pack_err;
6925 51a10b52 2020-12-26 stsp tog_free_refs();
6926 6458efa5 2020-11-24 stsp return error;
6929 6458efa5 2020-11-24 stsp static void
6930 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6934 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6935 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6936 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6937 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6939 6879ba42 2020-10-01 naddy fputc('\n', fp);
6942 4ed7e80c 2018-05-20 stsp __dead static void
6943 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6945 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6947 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6948 6879ba42 2020-10-01 naddy getprogname());
6949 6879ba42 2020-10-01 naddy if (hflag) {
6950 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6951 6879ba42 2020-10-01 naddy list_commands(fp);
6953 6879ba42 2020-10-01 naddy exit(status);
6956 c2301be8 2018-04-30 stsp static char **
6957 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6959 ee85c5e8 2020-02-29 stsp va_list ap;
6960 c2301be8 2018-04-30 stsp char **argv;
6963 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6965 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6966 c2301be8 2018-04-30 stsp if (argv == NULL)
6967 c2301be8 2018-04-30 stsp err(1, "calloc");
6968 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6969 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6970 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6971 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6974 ee85c5e8 2020-02-29 stsp va_end(ap);
6975 c2301be8 2018-04-30 stsp return argv;
6979 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6980 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6981 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6982 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6983 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6985 ee85c5e8 2020-02-29 stsp static const struct got_error *
6986 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6988 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6989 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6990 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6991 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6992 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6993 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6994 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6995 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6996 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6998 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6999 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7000 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7002 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7003 7cd52833 2022-06-23 thomas if (error != NULL)
7004 7cd52833 2022-06-23 thomas goto done;
7006 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7007 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7008 ee85c5e8 2020-02-29 stsp goto done;
7010 ee85c5e8 2020-02-29 stsp if (worktree)
7011 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7013 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7014 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7015 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7016 ee85c5e8 2020-02-29 stsp goto done;
7019 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7020 ee85c5e8 2020-02-29 stsp if (error != NULL)
7021 ee85c5e8 2020-02-29 stsp goto done;
7023 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7024 ee85c5e8 2020-02-29 stsp repo, worktree);
7025 ee85c5e8 2020-02-29 stsp if (error)
7026 ee85c5e8 2020-02-29 stsp goto done;
7028 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7029 84de9106 2020-12-26 stsp if (error)
7030 84de9106 2020-12-26 stsp goto done;
7031 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7032 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7033 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7034 ee85c5e8 2020-02-29 stsp if (error)
7035 ee85c5e8 2020-02-29 stsp goto done;
7037 ee85c5e8 2020-02-29 stsp if (worktree) {
7038 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7039 ee85c5e8 2020-02-29 stsp worktree = NULL;
7042 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7043 945f9229 2022-04-16 thomas if (error)
7044 945f9229 2022-04-16 thomas goto done;
7046 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7047 ee85c5e8 2020-02-29 stsp if (error) {
7048 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7049 ee85c5e8 2020-02-29 stsp goto done;
7050 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7051 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7052 6879ba42 2020-10-01 naddy usage(1, 1);
7053 ee85c5e8 2020-02-29 stsp /* not reached */
7056 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7057 1d0f4054 2021-06-17 stsp if (error == NULL)
7058 1d0f4054 2021-06-17 stsp error = close_err;
7059 ee85c5e8 2020-02-29 stsp repo = NULL;
7061 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7062 ee85c5e8 2020-02-29 stsp if (error)
7063 ee85c5e8 2020-02-29 stsp goto done;
7065 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7067 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7068 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7070 1d0f4054 2021-06-17 stsp if (repo) {
7071 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7072 1d0f4054 2021-06-17 stsp if (error == NULL)
7073 1d0f4054 2021-06-17 stsp error = close_err;
7075 945f9229 2022-04-16 thomas if (commit)
7076 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7077 ee85c5e8 2020-02-29 stsp if (worktree)
7078 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7079 7cd52833 2022-06-23 thomas if (pack_fds) {
7080 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7081 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7082 7cd52833 2022-06-23 thomas if (error == NULL)
7083 7cd52833 2022-06-23 thomas error = pack_err;
7086 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7087 ee85c5e8 2020-02-29 stsp free(commit_id);
7088 ee85c5e8 2020-02-29 stsp free(cwd);
7089 ee85c5e8 2020-02-29 stsp free(repo_path);
7090 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7091 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7093 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7094 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7095 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7097 87670572 2020-12-26 naddy tog_free_refs();
7098 ee85c5e8 2020-02-29 stsp return error;
7102 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7104 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7105 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
7106 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7107 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7108 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
7109 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7110 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7113 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7115 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7116 9f7d7167 2018-04-29 stsp switch (ch) {
7118 9f7d7167 2018-04-29 stsp hflag = 1;
7121 53ccebc2 2019-07-30 stsp Vflag = 1;
7124 6879ba42 2020-10-01 naddy usage(hflag, 1);
7125 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7129 9f7d7167 2018-04-29 stsp argc -= optind;
7130 9f7d7167 2018-04-29 stsp argv += optind;
7131 9814e6a3 2020-09-27 naddy optind = 1;
7132 c2301be8 2018-04-30 stsp optreset = 1;
7134 53ccebc2 2019-07-30 stsp if (Vflag) {
7135 53ccebc2 2019-07-30 stsp got_version_print_str();
7136 6879ba42 2020-10-01 naddy return 0;
7139 4010e238 2020-12-04 stsp #ifndef PROFILE
7140 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7141 4010e238 2020-12-04 stsp NULL) == -1)
7142 4010e238 2020-12-04 stsp err(1, "pledge");
7145 c2301be8 2018-04-30 stsp if (argc == 0) {
7146 f29d3e89 2018-06-23 stsp if (hflag)
7147 6879ba42 2020-10-01 naddy usage(hflag, 0);
7148 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7149 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7151 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7155 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7156 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7157 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7158 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7159 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7165 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7166 ee85c5e8 2020-02-29 stsp if (argc != 1)
7167 6879ba42 2020-10-01 naddy usage(0, 1);
7168 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7169 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7171 ee85c5e8 2020-02-29 stsp if (hflag)
7172 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7174 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7178 b46c1e04 2020-09-20 naddy putchar('\n');
7179 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7181 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7182 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7183 a2f4a359 2020-02-28 stsp free(cmd_argv);
7186 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7187 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);