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 05171be4 2022-06-23 thomas dst = malloc((n + 1) * sizeof(char));
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 05171be4 2022-06-23 thomas n += nb;
1252 05171be4 2022-06-23 thomas dst = reallocarray(dst, n, sizeof(char));
1253 05171be4 2022-06-23 thomas if (dst == NULL)
1254 05171be4 2022-06-23 thomas return got_error_from_errno("reallocarray");
1255 05171be4 2022-06-23 thomas memcpy(dst + sz, " ", nb);
1256 05171be4 2022-06-23 thomas sz += nb;
1258 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1262 05171be4 2022-06-23 thomas dst[sz] = '\0';
1263 05171be4 2022-06-23 thomas *ptr = dst;
1264 05171be4 2022-06-23 thomas return NULL;
1267 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1268 963b370f 2018-05-20 stsp static const struct got_error *
1269 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1270 05171be4 2022-06-23 thomas int col_tab_align, int expand)
1272 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1273 963b370f 2018-05-20 stsp int cols = 0;
1274 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1275 05171be4 2022-06-23 thomas char *exstr = NULL;
1276 963b370f 2018-05-20 stsp size_t wlen;
1279 963b370f 2018-05-20 stsp *wlinep = NULL;
1280 b700b5d6 2018-07-10 stsp *widthp = 0;
1282 05171be4 2022-06-23 thomas if (expand) {
1283 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1284 05171be4 2022-06-23 thomas if (err)
1285 05171be4 2022-06-23 thomas return err;
1288 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1289 05171be4 2022-06-23 thomas free(exstr);
1291 963b370f 2018-05-20 stsp return err;
1293 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1294 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1297 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1298 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1303 27a741e5 2019-09-11 stsp while (i < wlen) {
1304 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1306 27a741e5 2019-09-11 stsp if (width == 0) {
1311 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1312 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1314 27a741e5 2019-09-11 stsp cols += width;
1316 27a741e5 2019-09-11 stsp } else if (width == -1) {
1317 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1318 27a741e5 2019-09-11 stsp width = TABSIZE -
1319 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1321 748d5cab 2020-12-14 naddy width = 1;
1322 748d5cab 2020-12-14 naddy wline[i] = L'.';
1324 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1326 748d5cab 2020-12-14 naddy cols += width;
1329 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1330 963b370f 2018-05-20 stsp goto done;
1333 963b370f 2018-05-20 stsp wline[i] = L'\0';
1334 b700b5d6 2018-07-10 stsp if (widthp)
1335 b700b5d6 2018-07-10 stsp *widthp = cols;
1338 963b370f 2018-05-20 stsp free(wline);
1340 963b370f 2018-05-20 stsp *wlinep = wline;
1341 963b370f 2018-05-20 stsp return err;
1344 331b1a16 2022-06-23 thomas /* Skip the leading nscroll columns of a wide character string. */
1345 331b1a16 2022-06-23 thomas const struct got_error *
1346 331b1a16 2022-06-23 thomas scroll_wline(wchar_t **wlinep, wchar_t *wline, int nscroll,
1347 331b1a16 2022-06-23 thomas int col_tab_align)
1349 331b1a16 2022-06-23 thomas int cols = 0;
1350 331b1a16 2022-06-23 thomas size_t wlen = wcslen(wline);
1351 331b1a16 2022-06-23 thomas int i = 0, j = 0;
1353 331b1a16 2022-06-23 thomas *wlinep = wline;
1355 331b1a16 2022-06-23 thomas while (i < wlen && cols < nscroll) {
1356 331b1a16 2022-06-23 thomas int width = wcwidth(wline[i]);
1358 331b1a16 2022-06-23 thomas if (width == 0) {
1360 331b1a16 2022-06-23 thomas continue;
1363 331b1a16 2022-06-23 thomas if (width == 1 || width == 2) {
1364 331b1a16 2022-06-23 thomas if (cols + width > nscroll)
1366 331b1a16 2022-06-23 thomas cols += width;
1368 331b1a16 2022-06-23 thomas } else if (width == -1) {
1369 331b1a16 2022-06-23 thomas if (wline[i] == L'\t') {
1370 331b1a16 2022-06-23 thomas width = TABSIZE -
1371 331b1a16 2022-06-23 thomas ((cols + col_tab_align) % TABSIZE);
1372 331b1a16 2022-06-23 thomas } else {
1373 331b1a16 2022-06-23 thomas width = 1;
1374 331b1a16 2022-06-23 thomas wline[i] = L'.';
1376 331b1a16 2022-06-23 thomas if (cols + width > nscroll)
1378 331b1a16 2022-06-23 thomas cols += width;
1381 331b1a16 2022-06-23 thomas return got_error_from_errno("wcwidth");
1385 331b1a16 2022-06-23 thomas *wlinep = &wline[j];
1386 331b1a16 2022-06-23 thomas return NULL;
1389 8b473291 2019-02-21 stsp static const struct got_error*
1390 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1391 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1393 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1394 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1396 8b473291 2019-02-21 stsp const char *name;
1398 8b473291 2019-02-21 stsp *refs_str = NULL;
1400 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1401 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1402 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1405 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1406 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1408 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1409 8b473291 2019-02-21 stsp name += 5;
1410 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1411 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1413 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1414 8b473291 2019-02-21 stsp name += 6;
1415 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1416 8b473291 2019-02-21 stsp name += 8;
1417 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1418 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1421 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1424 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1425 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1426 5d844a1e 2019-08-13 stsp if (err) {
1427 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1428 48cae60d 2020-09-22 stsp free(ref_id);
1431 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1432 5d844a1e 2019-08-13 stsp err = NULL;
1433 5d844a1e 2019-08-13 stsp tag = NULL;
1436 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1437 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1438 48cae60d 2020-09-22 stsp free(ref_id);
1440 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1441 52b5abe1 2019-08-13 stsp if (cmp != 0)
1443 8b473291 2019-02-21 stsp s = *refs_str;
1444 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1445 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1446 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1448 8b473291 2019-02-21 stsp *refs_str = NULL;
1454 8b473291 2019-02-21 stsp return err;
1457 963b370f 2018-05-20 stsp static const struct got_error *
1458 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1459 27a741e5 2019-09-11 stsp int col_tab_align)
1461 e6b8b890 2020-12-29 naddy char *smallerthan;
1463 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1464 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1465 5813d178 2019-03-09 stsp author = smallerthan + 1;
1466 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1467 05171be4 2022-06-23 thomas return format_line(wauthor, author_width, author, limit, col_tab_align,
1471 5813d178 2019-03-09 stsp static const struct got_error *
1472 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1473 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1474 8fdc79fe 2020-12-01 naddy int author_display_cols)
1476 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1477 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1478 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1479 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1480 5813d178 2019-03-09 stsp char *author = NULL;
1481 331b1a16 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL, *scrolled_wline;
1482 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1483 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1484 bb737323 2018-05-20 stsp int col, limit;
1485 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1486 ccb26ccd 2018-11-05 stsp struct tm tm;
1487 45d799e2 2018-12-23 stsp time_t committer_time;
1488 11b20872 2019-11-08 stsp struct tog_color *tc;
1490 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1491 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1492 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1493 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1494 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1496 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1497 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1499 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1500 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1502 11b20872 2019-11-08 stsp wattr_on(view->window,
1503 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1504 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1506 11b20872 2019-11-08 stsp wattr_off(view->window,
1507 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1508 27a741e5 2019-09-11 stsp col = limit;
1509 b39d25c7 2018-07-10 stsp if (col > avail)
1510 b39d25c7 2018-07-10 stsp goto done;
1512 6570a66d 2019-11-08 stsp if (avail >= 120) {
1513 6570a66d 2019-11-08 stsp char *id_str;
1514 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1516 6570a66d 2019-11-08 stsp goto done;
1517 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1519 11b20872 2019-11-08 stsp wattr_on(view->window,
1520 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1521 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1523 11b20872 2019-11-08 stsp wattr_off(view->window,
1524 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1525 6570a66d 2019-11-08 stsp free(id_str);
1527 6570a66d 2019-11-08 stsp if (col > avail)
1528 6570a66d 2019-11-08 stsp goto done;
1531 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1532 5813d178 2019-03-09 stsp if (author == NULL) {
1533 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1534 80ddbec8 2018-04-29 stsp goto done;
1536 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1538 bb737323 2018-05-20 stsp goto done;
1539 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1541 11b20872 2019-11-08 stsp wattr_on(view->window,
1542 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1543 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1545 11b20872 2019-11-08 stsp wattr_off(view->window,
1546 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1547 bb737323 2018-05-20 stsp col += author_width;
1548 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1549 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1551 bb737323 2018-05-20 stsp author_width++;
1553 9c2eaf34 2018-05-20 stsp if (col > avail)
1554 9c2eaf34 2018-05-20 stsp goto done;
1556 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1558 6d9fbc00 2018-04-29 stsp goto done;
1559 bb737323 2018-05-20 stsp logmsg = logmsg0;
1560 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1562 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1563 bb737323 2018-05-20 stsp if (newline)
1564 bb737323 2018-05-20 stsp *newline = '\0';
1565 05171be4 2022-06-23 thomas limit = view->x + avail - col;
1566 05171be4 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col, 1);
1568 bb737323 2018-05-20 stsp goto done;
1569 331b1a16 2022-06-23 thomas err = scroll_wline(&scrolled_wline, wlogmsg, view->x, col);
1570 331b1a16 2022-06-23 thomas if (err)
1571 331b1a16 2022-06-23 thomas goto done;
1572 331b1a16 2022-06-23 thomas waddwstr(view->window, scrolled_wline);
1573 331b1a16 2022-06-23 thomas logmsg_width = wcswidth(scrolled_wline, wcslen(scrolled_wline));
1574 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
1575 27a741e5 2019-09-11 stsp while (col < avail) {
1576 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1580 80ddbec8 2018-04-29 stsp free(logmsg0);
1581 bb737323 2018-05-20 stsp free(wlogmsg);
1582 5813d178 2019-03-09 stsp free(author);
1583 bb737323 2018-05-20 stsp free(wauthor);
1584 80ddbec8 2018-04-29 stsp free(line);
1585 80ddbec8 2018-04-29 stsp return err;
1588 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1589 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1590 899d86c2 2018-05-10 stsp struct got_object_id *id)
1592 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1594 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1595 80ddbec8 2018-04-29 stsp if (entry == NULL)
1596 899d86c2 2018-05-10 stsp return NULL;
1598 899d86c2 2018-05-10 stsp entry->id = id;
1599 99db9666 2018-05-07 stsp entry->commit = commit;
1600 899d86c2 2018-05-10 stsp return entry;
1603 99db9666 2018-05-07 stsp static void
1604 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1606 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1608 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1609 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1610 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1611 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1612 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1613 99db9666 2018-05-07 stsp free(entry);
1616 99db9666 2018-05-07 stsp static void
1617 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1619 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1620 99db9666 2018-05-07 stsp pop_commit(commits);
1623 c4972b91 2018-05-07 stsp static const struct got_error *
1624 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1625 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1627 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1628 13add988 2019-10-15 stsp regmatch_t regmatch;
1629 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1631 13add988 2019-10-15 stsp *have_match = 0;
1633 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1635 13add988 2019-10-15 stsp return err;
1637 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1639 13add988 2019-10-15 stsp goto done;
1641 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1642 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1643 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1644 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1645 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1646 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1647 13add988 2019-10-15 stsp *have_match = 1;
1649 13add988 2019-10-15 stsp free(id_str);
1650 13add988 2019-10-15 stsp free(logmsg);
1651 13add988 2019-10-15 stsp return err;
1654 13add988 2019-10-15 stsp static const struct got_error *
1655 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1657 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1660 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1661 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1662 1a76625f 2018-10-22 stsp * while updating the display.
1665 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1666 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1667 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1668 1a76625f 2018-10-22 stsp int errcode;
1670 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1671 4e0d2870 2020-12-07 naddy NULL, NULL);
1672 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1675 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1678 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1679 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1680 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1684 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1685 1a76625f 2018-10-22 stsp if (errcode) {
1686 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1687 13add988 2019-10-15 stsp "pthread_mutex_lock");
1691 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1692 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1693 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1695 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1696 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1697 7c1452c1 2020-03-26 stsp int have_match;
1698 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1701 7c1452c1 2020-03-26 stsp if (have_match)
1702 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1705 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1706 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1707 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1708 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1711 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1713 9ba79e04 2018-06-11 stsp return err;
1716 2b779855 2020-12-05 naddy static void
1717 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1719 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1720 2b779855 2020-12-05 naddy int ncommits = 0;
1722 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1723 2b779855 2020-12-05 naddy while (entry) {
1724 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1725 2b779855 2020-12-05 naddy s->selected_entry = entry;
1728 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1729 2b779855 2020-12-05 naddy ncommits++;
1733 0553a4e3 2018-04-30 stsp static const struct got_error *
1734 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1736 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1737 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1738 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1739 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1740 60493ae3 2019-06-20 stsp int width;
1741 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1742 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1743 8b473291 2019-02-21 stsp char *refs_str = NULL;
1744 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1745 11b20872 2019-11-08 stsp struct tog_color *tc;
1746 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1748 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1749 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1750 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1751 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1753 ecb28ae0 2018-07-16 stsp return err;
1754 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1755 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1756 d2075bf3 2020-12-25 stsp if (refs) {
1757 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1758 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1760 d2075bf3 2020-12-25 stsp goto done;
1764 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1765 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1767 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1768 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1769 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1770 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1771 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1772 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1773 8f4ed634 2020-03-26 stsp goto done;
1776 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1778 f9686aa5 2020-03-27 stsp if (view->searching) {
1779 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1780 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1781 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1782 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1783 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1784 f9686aa5 2020-03-27 stsp search_str = "searching...";
1787 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1788 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1789 f9686aa5 2020-03-27 stsp search_str ? search_str :
1790 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1791 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1792 8f4ed634 2020-03-26 stsp goto done;
1796 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1797 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1798 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1799 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1800 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1801 1a76625f 2018-10-22 stsp header = NULL;
1802 1a76625f 2018-10-22 stsp goto done;
1804 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1805 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1806 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1807 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1808 1a76625f 2018-10-22 stsp header = NULL;
1809 1a76625f 2018-10-22 stsp goto done;
1811 05171be4 2022-06-23 thomas err = format_line(&wline, &width, header, view->ncols, 0, 0);
1813 1a76625f 2018-10-22 stsp goto done;
1815 2814baeb 2018-08-01 stsp werase(view->window);
1817 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1818 a3404814 2018-09-02 stsp wstandout(view->window);
1819 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1821 11b20872 2019-11-08 stsp wattr_on(view->window,
1822 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1823 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1825 11b20872 2019-11-08 stsp wattr_off(view->window,
1826 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1827 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1828 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1831 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1832 a3404814 2018-09-02 stsp wstandend(view->window);
1833 ecb28ae0 2018-07-16 stsp free(wline);
1834 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1835 1a76625f 2018-10-22 stsp goto done;
1837 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
1838 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1839 5813d178 2019-03-09 stsp ncommits = 0;
1840 05171be4 2022-06-23 thomas view->maxx = 0;
1841 5813d178 2019-03-09 stsp while (entry) {
1842 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
1843 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
1844 5813d178 2019-03-09 stsp int width;
1845 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1847 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1848 5813d178 2019-03-09 stsp if (author == NULL) {
1849 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1850 5813d178 2019-03-09 stsp goto done;
1852 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1853 27a741e5 2019-09-11 stsp date_display_cols);
1854 5813d178 2019-03-09 stsp if (author_cols < width)
1855 5813d178 2019-03-09 stsp author_cols = width;
1856 5813d178 2019-03-09 stsp free(wauthor);
1857 5813d178 2019-03-09 stsp free(author);
1858 05171be4 2022-06-23 thomas err = got_object_commit_get_logmsg(&msg0, entry->commit);
1859 05171be4 2022-06-23 thomas if (err)
1860 05171be4 2022-06-23 thomas goto done;
1861 05171be4 2022-06-23 thomas msg = msg0;
1862 05171be4 2022-06-23 thomas while (*msg == '\n')
1864 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
1865 331b1a16 2022-06-23 thomas *eol = '\0';
1866 331b1a16 2022-06-23 thomas err = format_line(&wmsg, &width, msg, INT_MAX,
1867 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
1868 331b1a16 2022-06-23 thomas if (err)
1869 331b1a16 2022-06-23 thomas goto done;
1870 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
1871 05171be4 2022-06-23 thomas free(msg0);
1872 331b1a16 2022-06-23 thomas free(wmsg);
1873 7ca04879 2019-10-19 stsp ncommits++;
1874 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1877 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1878 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1879 867c6645 2018-07-10 stsp ncommits = 0;
1880 899d86c2 2018-05-10 stsp while (entry) {
1881 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1883 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1884 2814baeb 2018-08-01 stsp wstandout(view->window);
1885 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1886 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1887 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1888 2814baeb 2018-08-01 stsp wstandend(view->window);
1890 60493ae3 2019-06-20 stsp goto done;
1891 0553a4e3 2018-04-30 stsp ncommits++;
1892 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1893 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1896 1a57306a 2018-09-02 stsp view_vborder(view);
1897 dd038bc6 2021-09-21 thomas.ad update_panels();
1898 dd038bc6 2021-09-21 thomas.ad doupdate();
1900 1a76625f 2018-10-22 stsp free(id_str);
1901 8b473291 2019-02-21 stsp free(refs_str);
1902 1a76625f 2018-10-22 stsp free(ncommits_str);
1903 1a76625f 2018-10-22 stsp free(header);
1904 80ddbec8 2018-04-29 stsp return err;
1907 07b55e75 2018-05-10 stsp static void
1908 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1910 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1911 07b55e75 2018-05-10 stsp int nscrolled = 0;
1913 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1914 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1917 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1918 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1919 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1920 07b55e75 2018-05-10 stsp if (entry) {
1921 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1922 07b55e75 2018-05-10 stsp nscrolled++;
1927 aa075928 2018-05-10 stsp static const struct got_error *
1928 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1930 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1931 5e224a3e 2019-02-22 stsp int errcode;
1933 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1935 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1936 ffe38506 2020-12-01 naddy if (ta->log_complete)
1939 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1940 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1941 7aafa0d1 2019-02-22 stsp if (errcode)
1942 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1943 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1946 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1947 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1949 7c1452c1 2020-03-26 stsp if (!wait)
1952 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1953 ffe38506 2020-12-01 naddy show_log_view(view);
1954 7c1452c1 2020-03-26 stsp update_panels();
1955 7c1452c1 2020-03-26 stsp doupdate();
1957 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1958 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1959 82954512 2020-02-03 stsp if (errcode)
1960 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1961 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1963 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1964 ffe38506 2020-12-01 naddy show_log_view(view);
1965 7c1452c1 2020-03-26 stsp update_panels();
1966 7c1452c1 2020-03-26 stsp doupdate();
1969 5e224a3e 2019-02-22 stsp return NULL;
1972 5e224a3e 2019-02-22 stsp static const struct got_error *
1973 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1975 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1976 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1977 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1978 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1980 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1981 5e224a3e 2019-02-22 stsp return NULL;
1983 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1984 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1985 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1987 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1989 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1990 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1992 5e224a3e 2019-02-22 stsp return err;
1996 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1997 00ba99a7 2019-05-12 jcs if (pentry == NULL)
2000 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
2002 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2003 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2005 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2006 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2008 dd0a52c1 2018-05-20 stsp return err;
2011 cd0acaa7 2018-05-20 stsp static const struct got_error *
2012 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2013 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2014 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2016 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2017 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2018 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2020 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2021 15a94983 2018-12-23 stsp if (diff_view == NULL)
2022 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2024 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2025 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2026 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2027 e5a0f69f 2018-08-18 stsp if (err == NULL)
2028 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2029 cd0acaa7 2018-05-20 stsp return err;
2032 80ddbec8 2018-04-29 stsp static const struct got_error *
2033 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2034 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2036 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2038 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2039 941e9f74 2019-05-21 stsp if (parent == NULL)
2040 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2042 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2043 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2044 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2045 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2046 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2047 941e9f74 2019-05-21 stsp s->tree = subtree;
2048 941e9f74 2019-05-21 stsp s->selected = 0;
2049 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2050 941e9f74 2019-05-21 stsp return NULL;
2053 941e9f74 2019-05-21 stsp static const struct got_error *
2054 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2055 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2057 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2058 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2059 941e9f74 2019-05-21 stsp const char *p;
2060 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2062 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2064 941e9f74 2019-05-21 stsp while (*p) {
2065 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2066 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2067 56e0773d 2019-11-28 stsp char *te_name;
2069 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2072 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2073 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2074 941e9f74 2019-05-21 stsp if (slash == NULL)
2075 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2077 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2078 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2079 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2082 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2083 56e0773d 2019-11-28 stsp if (te == NULL) {
2084 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2085 56e0773d 2019-11-28 stsp free(te_name);
2088 56e0773d 2019-11-28 stsp free(te_name);
2089 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2091 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2092 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2094 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2095 941e9f74 2019-05-21 stsp if (slash)
2096 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2098 941e9f74 2019-05-21 stsp subpath = strdup(path);
2099 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2100 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2104 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2109 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2110 941e9f74 2019-05-21 stsp free(tree_id);
2114 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2115 941e9f74 2019-05-21 stsp if (err) {
2116 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2119 941e9f74 2019-05-21 stsp if (slash == NULL)
2121 941e9f74 2019-05-21 stsp free(subpath);
2122 941e9f74 2019-05-21 stsp subpath = NULL;
2123 941e9f74 2019-05-21 stsp p = slash;
2126 941e9f74 2019-05-21 stsp free(subpath);
2127 1a76625f 2018-10-22 stsp return err;
2130 61266923 2020-01-14 stsp static const struct got_error *
2131 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2132 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2133 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2135 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2136 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2137 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2139 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2140 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2141 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2143 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2145 55cccc34 2020-02-20 stsp return err;
2146 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2148 55cccc34 2020-02-20 stsp *new_view = tree_view;
2150 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2151 55cccc34 2020-02-20 stsp return NULL;
2153 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2156 55cccc34 2020-02-20 stsp static const struct got_error *
2157 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2159 61266923 2020-01-14 stsp sigset_t sigset;
2160 61266923 2020-01-14 stsp int errcode;
2162 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2163 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2165 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2166 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2167 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2168 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2169 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2170 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2171 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2172 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2173 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2175 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2176 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2177 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2179 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2180 61266923 2020-01-14 stsp if (errcode)
2181 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2183 61266923 2020-01-14 stsp return NULL;
2186 1a76625f 2018-10-22 stsp static void *
2187 1a76625f 2018-10-22 stsp log_thread(void *arg)
2189 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2190 1a76625f 2018-10-22 stsp int errcode = 0;
2191 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2192 1a76625f 2018-10-22 stsp int done = 0;
2194 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2196 61266923 2020-01-14 stsp return (void *)err;
2198 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2199 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2200 1a76625f 2018-10-22 stsp if (err) {
2201 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2202 1a76625f 2018-10-22 stsp return (void *)err;
2203 1a76625f 2018-10-22 stsp err = NULL;
2205 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2206 1a76625f 2018-10-22 stsp a->commits_needed--;
2208 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2209 3abe8080 2019-04-10 stsp if (errcode) {
2210 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2211 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2213 3abe8080 2019-04-10 stsp } else if (*a->quit)
2215 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2216 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2217 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2218 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2221 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2222 7c1452c1 2020-03-26 stsp if (errcode) {
2223 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2224 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2225 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2230 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2232 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2233 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2234 7c1452c1 2020-03-26 stsp &tog_mutex);
2235 7c1452c1 2020-03-26 stsp if (errcode)
2236 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2237 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2238 21355643 2020-12-06 stsp if (*a->quit)
2243 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2244 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2245 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2246 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2248 3abe8080 2019-04-10 stsp a->log_complete = 1;
2249 1a76625f 2018-10-22 stsp return (void *)err;
2252 1a76625f 2018-10-22 stsp static const struct got_error *
2253 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2255 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2256 1a76625f 2018-10-22 stsp int errcode;
2258 1a76625f 2018-10-22 stsp if (s->thread) {
2259 1a76625f 2018-10-22 stsp s->quit = 1;
2260 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2261 1a76625f 2018-10-22 stsp if (errcode)
2262 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2263 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2264 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2265 1a76625f 2018-10-22 stsp if (errcode)
2266 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2267 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2268 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2269 1a76625f 2018-10-22 stsp if (errcode)
2270 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2271 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2272 1a76625f 2018-10-22 stsp if (errcode)
2273 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2274 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2275 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2278 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2279 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2280 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2283 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2284 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2285 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2286 1af5eddf 2022-06-23 thomas if (err == NULL)
2287 1af5eddf 2022-06-23 thomas err = pack_err;
2288 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2291 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2292 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2293 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2296 9343a5fb 2018-06-23 stsp return err;
2299 9343a5fb 2018-06-23 stsp static const struct got_error *
2300 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2302 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2303 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2304 276b94a1 2020-11-13 naddy int errcode;
2306 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2308 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2309 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2310 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2312 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2313 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2314 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2316 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2317 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2318 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2319 1a76625f 2018-10-22 stsp free(s->start_id);
2320 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2321 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2322 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2323 1a76625f 2018-10-22 stsp return err;
2326 1a76625f 2018-10-22 stsp static const struct got_error *
2327 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2329 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2331 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2332 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2333 60493ae3 2019-06-20 stsp return NULL;
2336 60493ae3 2019-06-20 stsp static const struct got_error *
2337 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2339 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2340 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2341 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2343 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2344 f9686aa5 2020-03-27 stsp show_log_view(view);
2345 f9686aa5 2020-03-27 stsp update_panels();
2346 f9686aa5 2020-03-27 stsp doupdate();
2348 96e2b566 2019-07-08 stsp if (s->search_entry) {
2349 13add988 2019-10-15 stsp int errcode, ch;
2350 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2351 13add988 2019-10-15 stsp if (errcode)
2352 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2353 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2354 13add988 2019-10-15 stsp ch = wgetch(view->window);
2355 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2356 13add988 2019-10-15 stsp if (errcode)
2357 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2358 13add988 2019-10-15 stsp "pthread_mutex_lock");
2359 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2360 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2361 678cbce5 2019-07-28 stsp return NULL;
2363 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2364 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2366 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2367 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2368 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2369 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2370 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2372 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2373 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2375 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2378 60493ae3 2019-06-20 stsp while (1) {
2379 13add988 2019-10-15 stsp int have_match = 0;
2381 60493ae3 2019-06-20 stsp if (entry == NULL) {
2382 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2383 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2384 f9967bca 2020-03-27 stsp view->search_next_done =
2385 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2386 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2387 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2388 f801134a 2019-06-25 stsp return NULL;
2391 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2392 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2393 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2395 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2396 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2399 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2400 13add988 2019-10-15 stsp &view->regex);
2403 13add988 2019-10-15 stsp if (have_match) {
2404 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2405 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2409 96e2b566 2019-07-08 stsp s->search_entry = entry;
2410 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2411 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2413 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2416 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2417 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2418 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2419 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2421 60493ae3 2019-06-20 stsp return err;
2424 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2425 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2427 60493ae3 2019-06-20 stsp return err;
2432 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2434 60493ae3 2019-06-20 stsp return NULL;
2437 60493ae3 2019-06-20 stsp static const struct got_error *
2438 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2439 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2440 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2442 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2443 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2444 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2445 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2446 1a76625f 2018-10-22 stsp int errcode;
2448 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2449 f135c941 2020-02-20 stsp free(s->in_repo_path);
2450 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2451 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2452 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2455 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2456 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2457 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2459 fb2756b9 2018-08-04 stsp s->repo = repo;
2460 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2461 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2462 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2463 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2464 9cd7cbd1 2020-12-07 stsp goto done;
2467 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2468 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2469 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2470 5036bf37 2018-09-24 stsp goto done;
2472 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2474 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2475 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2476 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2477 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2479 11b20872 2019-11-08 stsp goto done;
2480 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2481 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2482 11b20872 2019-11-08 stsp if (err) {
2483 11b20872 2019-11-08 stsp free_colors(&s->colors);
2484 11b20872 2019-11-08 stsp goto done;
2486 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2487 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2488 11b20872 2019-11-08 stsp if (err) {
2489 11b20872 2019-11-08 stsp free_colors(&s->colors);
2490 11b20872 2019-11-08 stsp goto done;
2494 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2495 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2496 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2497 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2498 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2500 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2501 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2502 1af5eddf 2022-06-23 thomas if (err)
2503 1af5eddf 2022-06-23 thomas goto done;
2505 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2506 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2507 7cd52833 2022-06-23 thomas if (err)
2508 7cd52833 2022-06-23 thomas goto done;
2509 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2510 b672a97a 2020-01-27 stsp !s->log_branches);
2512 1a76625f 2018-10-22 stsp goto done;
2513 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2514 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2516 c5b78334 2020-01-12 stsp goto done;
2518 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2519 1a76625f 2018-10-22 stsp if (errcode) {
2520 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2521 1a76625f 2018-10-22 stsp goto done;
2523 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2524 7c1452c1 2020-03-26 stsp if (errcode) {
2525 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2526 7c1452c1 2020-03-26 stsp goto done;
2529 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2530 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2531 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2532 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2533 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2534 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2535 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2536 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2537 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2538 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2539 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2540 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2541 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2544 1a76625f 2018-10-22 stsp close_log_view(view);
2545 ba4f502b 2018-08-04 stsp return err;
2548 e5a0f69f 2018-08-18 stsp static const struct got_error *
2549 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2551 f2f6d207 2020-11-24 stsp const struct got_error *err;
2552 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2554 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2555 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2556 2b380cc8 2018-10-24 stsp &s->thread_args);
2557 2b380cc8 2018-10-24 stsp if (errcode)
2558 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2559 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2560 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2562 f2f6d207 2020-11-24 stsp return err;
2566 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2569 e5a0f69f 2018-08-18 stsp static const struct got_error *
2570 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2572 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2573 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2574 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2575 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2576 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2577 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
2579 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2580 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2581 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2582 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2583 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2584 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2585 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2586 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2587 fb280deb 2021-08-30 stsp select_commit(s);
2589 528dedf3 2021-08-30 stsp return NULL;
2592 528dedf3 2021-08-30 stsp switch (ch) {
2594 1e37a5c2 2019-05-12 jcs s->quit = 1;
2596 05171be4 2022-06-23 thomas case '0':
2597 05171be4 2022-06-23 thomas view->x = 0;
2599 05171be4 2022-06-23 thomas case '$':
2600 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
2602 05171be4 2022-06-23 thomas case KEY_RIGHT:
2603 05171be4 2022-06-23 thomas case 'l':
2604 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
2605 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
2607 05171be4 2022-06-23 thomas case KEY_LEFT:
2608 05171be4 2022-06-23 thomas case 'h':
2609 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
2612 1e37a5c2 2019-05-12 jcs case KEY_UP:
2615 f7140bf5 2021-10-17 thomas case CTRL('p'):
2616 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2618 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2619 1e37a5c2 2019-05-12 jcs s->selected--;
2621 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2622 912a3f79 2021-08-30 j select_commit(s);
2625 912a3f79 2021-08-30 j case KEY_HOME:
2626 912a3f79 2021-08-30 j s->selected = 0;
2627 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2628 2b779855 2020-12-05 naddy select_commit(s);
2630 70f17a53 2022-06-13 thomas case CTRL('u'):
2631 23427b14 2022-06-23 thomas case 'u':
2632 70f17a53 2022-06-13 thomas nscroll /= 2;
2633 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2634 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2635 a4292ac5 2019-05-12 jcs case CTRL('b'):
2636 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2638 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2639 70f17a53 2022-06-13 thomas s->selected = MAX(0, s->selected - nscroll - 1);
2641 70f17a53 2022-06-13 thomas log_scroll_up(s, nscroll);
2642 2b779855 2020-12-05 naddy select_commit(s);
2645 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2648 f7140bf5 2021-10-17 thomas case CTRL('n'):
2649 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2651 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2652 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2653 1e37a5c2 2019-05-12 jcs s->selected++;
2655 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2659 912a3f79 2021-08-30 j select_commit(s);
2662 912a3f79 2021-08-30 j case KEY_END: {
2663 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2664 912a3f79 2021-08-30 j * traverse them all. */
2665 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2666 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2667 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2670 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2671 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2672 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2673 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2675 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2676 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2678 f3bc9f1d 2021-09-05 naddy if (n > 0)
2679 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2680 2b779855 2020-12-05 naddy select_commit(s);
2683 bccd1d5d 2022-06-13 thomas case CTRL('d'):
2684 23427b14 2022-06-23 thomas case 'd':
2685 70f17a53 2022-06-13 thomas nscroll /= 2;
2686 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2687 70f17a53 2022-06-13 thomas case KEY_NPAGE:
2688 70f17a53 2022-06-13 thomas case CTRL('f'): {
2689 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2690 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2691 1e37a5c2 2019-05-12 jcs if (first == NULL)
2693 70f17a53 2022-06-13 thomas err = log_scroll_down(view, nscroll);
2696 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2697 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2698 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2699 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2700 70f17a53 2022-06-13 thomas s->selected += MIN(s->last_displayed_entry->idx -
2701 70f17a53 2022-06-13 thomas s->selected_entry->idx, nscroll + 1);
2703 2b779855 2020-12-05 naddy select_commit(s);
2706 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2707 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2708 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2709 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2710 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2711 2b779855 2020-12-05 naddy select_commit(s);
2712 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2713 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2714 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2715 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2716 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2719 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2722 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2724 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2725 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2726 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2727 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2728 78756c87 2020-11-24 stsp view, s->repo);
2731 e78dc838 2020-12-04 stsp view->focussed = 0;
2732 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2733 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2734 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2736 1e37a5c2 2019-05-12 jcs return err;
2737 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2738 e78dc838 2020-12-04 stsp view->focus_child = 1;
2740 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2743 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2745 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2746 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2747 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2748 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2752 e78dc838 2020-12-04 stsp view->focussed = 0;
2753 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2754 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2755 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2757 1e37a5c2 2019-05-12 jcs return err;
2758 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2759 e78dc838 2020-12-04 stsp view->focus_child = 1;
2761 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2763 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2764 21355643 2020-12-06 stsp case CTRL('l'):
2766 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2767 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2769 21355643 2020-12-06 stsp err = stop_log_thread(s);
2771 74cfe85e 2020-10-20 stsp return err;
2772 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2773 21355643 2020-12-06 stsp char *parent_path;
2774 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2776 21355643 2020-12-06 stsp return err;
2777 21355643 2020-12-06 stsp free(s->in_repo_path);
2778 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2779 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2780 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2781 21355643 2020-12-06 stsp struct got_object_id *start_id;
2782 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2783 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2784 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2786 21355643 2020-12-06 stsp return err;
2787 21355643 2020-12-06 stsp free(s->start_id);
2788 21355643 2020-12-06 stsp s->start_id = start_id;
2789 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2790 21355643 2020-12-06 stsp } else /* 'B' */
2791 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2793 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
2794 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
2795 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2797 21355643 2020-12-06 stsp return err;
2798 51a10b52 2020-12-26 stsp tog_free_refs();
2799 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2801 ca51c541 2020-12-07 stsp return err;
2802 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2803 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2805 d01904d4 2019-06-25 stsp return err;
2806 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2807 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2809 b672a97a 2020-01-27 stsp return err;
2810 21355643 2020-12-06 stsp free_commits(&s->commits);
2811 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2812 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2813 21355643 2020-12-06 stsp s->selected_entry = NULL;
2814 21355643 2020-12-06 stsp s->selected = 0;
2815 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2816 21355643 2020-12-06 stsp s->quit = 0;
2817 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2820 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2821 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2822 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2823 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2824 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2825 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2826 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2827 6458efa5 2020-11-24 stsp if (err) {
2828 6458efa5 2020-11-24 stsp view_close(ref_view);
2829 6458efa5 2020-11-24 stsp return err;
2831 e78dc838 2020-12-04 stsp view->focussed = 0;
2832 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2833 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2834 6458efa5 2020-11-24 stsp err = view_close_child(view);
2836 6458efa5 2020-11-24 stsp return err;
2837 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2838 e78dc838 2020-12-04 stsp view->focus_child = 1;
2840 6458efa5 2020-11-24 stsp *new_view = ref_view;
2846 80ddbec8 2018-04-29 stsp return err;
2849 4ed7e80c 2018-05-20 stsp static const struct got_error *
2850 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2852 c2db6724 2019-01-04 stsp const struct got_error *error;
2854 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2855 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2856 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2858 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2859 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2861 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2862 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2864 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2865 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2867 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2868 c2db6724 2019-01-04 stsp if (error != NULL)
2869 c2db6724 2019-01-04 stsp return error;
2871 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2872 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2874 c2db6724 2019-01-04 stsp return NULL;
2877 a915003a 2019-02-05 stsp static void
2878 a915003a 2019-02-05 stsp init_curses(void)
2881 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
2882 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
2883 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
2885 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
2886 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
2887 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
2888 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
2889 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
2891 a915003a 2019-02-05 stsp initscr();
2893 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2896 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2897 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2898 a915003a 2019-02-05 stsp curs_set(0);
2899 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2900 6d17833f 2019-11-08 stsp start_color();
2901 6d17833f 2019-11-08 stsp use_default_colors();
2905 c2db6724 2019-01-04 stsp static const struct got_error *
2906 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2907 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2909 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2911 f135c941 2020-02-20 stsp if (argc == 0) {
2912 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2913 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2914 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2915 f135c941 2020-02-20 stsp return NULL;
2918 f135c941 2020-02-20 stsp if (worktree) {
2919 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2922 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2924 f135c941 2020-02-20 stsp return err;
2925 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2926 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2927 bfd61697 2020-11-14 stsp p) == -1) {
2928 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2929 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2933 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2935 f135c941 2020-02-20 stsp return err;
2938 f135c941 2020-02-20 stsp static const struct got_error *
2939 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2941 80ddbec8 2018-04-29 stsp const struct got_error *error;
2942 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2943 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2944 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2945 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2946 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2947 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2948 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2949 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2950 04cc582a 2018-08-01 stsp struct tog_view *view;
2951 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
2953 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2954 80ddbec8 2018-04-29 stsp switch (ch) {
2956 b672a97a 2020-01-27 stsp log_branches = 1;
2959 80ddbec8 2018-04-29 stsp start_commit = optarg;
2962 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2963 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2964 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2968 17020d27 2019-03-07 stsp usage_log();
2969 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2973 80ddbec8 2018-04-29 stsp argc -= optind;
2974 80ddbec8 2018-04-29 stsp argv += optind;
2976 f135c941 2020-02-20 stsp if (argc > 1)
2977 f135c941 2020-02-20 stsp usage_log();
2979 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
2980 7cd52833 2022-06-23 thomas if (error != NULL)
2981 7cd52833 2022-06-23 thomas goto done;
2983 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2984 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2985 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2986 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2987 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2988 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2989 c156c7a4 2020-12-18 stsp goto done;
2990 a1fbf39a 2019-08-11 stsp if (worktree)
2991 6962eb72 2020-02-20 stsp repo_path =
2992 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2994 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2995 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2996 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2997 c156c7a4 2020-12-18 stsp goto done;
3001 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3002 80ddbec8 2018-04-29 stsp if (error != NULL)
3003 ecb28ae0 2018-07-16 stsp goto done;
3005 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3006 f135c941 2020-02-20 stsp repo, worktree);
3007 f135c941 2020-02-20 stsp if (error)
3008 f135c941 2020-02-20 stsp goto done;
3010 f135c941 2020-02-20 stsp init_curses();
3012 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3013 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3014 c02c541e 2019-03-29 stsp if (error)
3015 c02c541e 2019-03-29 stsp goto done;
3017 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3018 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3019 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3020 87670572 2020-12-26 naddy if (error)
3021 87670572 2020-12-26 naddy goto done;
3024 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3025 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3026 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3027 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3028 d8f38dc4 2020-12-05 stsp if (error)
3029 d8f38dc4 2020-12-05 stsp goto done;
3030 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3032 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3033 d8f38dc4 2020-12-05 stsp if (error == NULL)
3034 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3035 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3036 d8f38dc4 2020-12-05 stsp goto done;
3037 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3038 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3039 d8f38dc4 2020-12-05 stsp if (error)
3040 d8f38dc4 2020-12-05 stsp goto done;
3043 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3044 04cc582a 2018-08-01 stsp if (view == NULL) {
3045 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3046 04cc582a 2018-08-01 stsp goto done;
3048 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3049 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3050 ba4f502b 2018-08-04 stsp if (error)
3051 ba4f502b 2018-08-04 stsp goto done;
3052 2fc00ff4 2019-08-31 stsp if (worktree) {
3053 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3054 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3055 2fc00ff4 2019-08-31 stsp worktree = NULL;
3057 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3059 f135c941 2020-02-20 stsp free(in_repo_path);
3060 ecb28ae0 2018-07-16 stsp free(repo_path);
3061 ecb28ae0 2018-07-16 stsp free(cwd);
3062 899d86c2 2018-05-10 stsp free(start_id);
3063 d8f38dc4 2020-12-05 stsp free(label);
3065 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3066 1d0f4054 2021-06-17 stsp if (repo) {
3067 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3068 1d0f4054 2021-06-17 stsp if (error == NULL)
3069 1d0f4054 2021-06-17 stsp error = close_err;
3071 ec142235 2019-03-07 stsp if (worktree)
3072 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3073 7cd52833 2022-06-23 thomas if (pack_fds) {
3074 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3075 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3076 7cd52833 2022-06-23 thomas if (error == NULL)
3077 7cd52833 2022-06-23 thomas error = pack_err;
3079 51a10b52 2020-12-26 stsp tog_free_refs();
3080 80ddbec8 2018-04-29 stsp return error;
3083 4ed7e80c 2018-05-20 stsp __dead static void
3084 9f7d7167 2018-04-29 stsp usage_diff(void)
3087 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3088 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3092 6d17833f 2019-11-08 stsp static int
3093 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3094 41605754 2020-11-12 stsp regmatch_t *regmatch)
3096 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3099 f26dddb7 2019-11-08 stsp struct tog_color *
3100 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3102 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3104 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3105 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3106 6d17833f 2019-11-08 stsp return tc;
3109 6d17833f 2019-11-08 stsp return NULL;
3112 4ed7e80c 2018-05-20 stsp static const struct got_error *
3113 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3114 05171be4 2022-06-23 thomas WINDOW *window, int skip, regmatch_t *regmatch)
3116 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3117 41605754 2020-11-12 stsp wchar_t *wline;
3118 05171be4 2022-06-23 thomas int rme, rms, n, width;
3120 41605754 2020-11-12 stsp *wtotal = 0;
3121 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3122 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3124 05171be4 2022-06-23 thomas err = format_line(&wline, &width, line, wlimit + skip,
3125 05171be4 2022-06-23 thomas col_tab_align, 1);
3126 05171be4 2022-06-23 thomas if (err)
3127 41605754 2020-11-12 stsp return err;
3129 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3130 05171be4 2022-06-23 thomas n = MAX(rms - skip, 0);
3131 05171be4 2022-06-23 thomas if (n) {
3132 05171be4 2022-06-23 thomas waddnwstr(window, wline + skip, n);
3133 05171be4 2022-06-23 thomas wlimit -= n;
3134 05171be4 2022-06-23 thomas *wtotal += n;
3137 41605754 2020-11-12 stsp if (wlimit > 0) {
3138 05171be4 2022-06-23 thomas int len = rme - rms;
3140 05171be4 2022-06-23 thomas if (skip > rms) {
3141 05171be4 2022-06-23 thomas n = skip - rms;
3142 05171be4 2022-06-23 thomas len = MAX(len - n, 0);
3144 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3145 05171be4 2022-06-23 thomas if (len) {
3146 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3147 05171be4 2022-06-23 thomas waddnwstr(window, wline + rms + n, len);
3148 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3149 05171be4 2022-06-23 thomas wlimit -= len;
3150 05171be4 2022-06-23 thomas *wtotal += len;
3154 05171be4 2022-06-23 thomas if (wlimit > 0 && skip < width) { /* draw rest of line */
3156 05171be4 2022-06-23 thomas if (skip > rme)
3157 05171be4 2022-06-23 thomas n = MIN(skip - rme, width - rme);
3158 05171be4 2022-06-23 thomas waddnwstr(window, wline + rme + n, wlimit);
3161 05171be4 2022-06-23 thomas *wtotal = width;
3162 05171be4 2022-06-23 thomas free(wline);
3163 41605754 2020-11-12 stsp return NULL;
3166 41605754 2020-11-12 stsp static const struct got_error *
3167 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3169 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3170 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3171 61e69b96 2018-05-20 stsp const struct got_error *err;
3172 fe621944 2020-11-10 stsp int nprinted = 0;
3173 b304db33 2018-05-20 stsp char *line;
3174 826082fe 2020-12-10 stsp size_t linesize = 0;
3175 826082fe 2020-12-10 stsp ssize_t linelen;
3176 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3177 61e69b96 2018-05-20 stsp wchar_t *wline;
3178 e0b650dd 2018-05-20 stsp int width;
3179 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3180 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3181 fe621944 2020-11-10 stsp off_t line_offset;
3183 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3184 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3185 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3187 f7d12f7e 2018-08-01 stsp werase(view->window);
3189 a3404814 2018-09-02 stsp if (header) {
3190 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3191 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3192 135a2da0 2020-11-11 stsp header) == -1)
3193 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3194 05171be4 2022-06-23 thomas err = format_line(&wline, &width, line, view->ncols, 0, 0);
3195 135a2da0 2020-11-11 stsp free(line);
3197 a3404814 2018-09-02 stsp return err;
3199 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3200 a3404814 2018-09-02 stsp wstandout(view->window);
3201 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3202 e54cc94a 2020-11-11 stsp free(wline);
3203 e54cc94a 2020-11-11 stsp wline = NULL;
3204 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3205 a3404814 2018-09-02 stsp wstandend(view->window);
3206 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3207 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3209 a3404814 2018-09-02 stsp if (max_lines <= 1)
3210 a3404814 2018-09-02 stsp return NULL;
3211 a3404814 2018-09-02 stsp max_lines--;
3214 89f1a395 2020-12-01 naddy s->eof = 0;
3215 05171be4 2022-06-23 thomas view->maxx = 0;
3216 826082fe 2020-12-10 stsp line = NULL;
3217 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3218 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3219 826082fe 2020-12-10 stsp if (linelen == -1) {
3220 826082fe 2020-12-10 stsp if (feof(s->f)) {
3221 826082fe 2020-12-10 stsp s->eof = 1;
3224 826082fe 2020-12-10 stsp free(line);
3225 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3228 05171be4 2022-06-23 thomas view->maxx = MAX(view->maxx, linelen);
3230 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3232 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3233 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3234 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3235 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3236 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3237 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3238 41605754 2020-11-12 stsp if (err) {
3239 41605754 2020-11-12 stsp free(line);
3240 41605754 2020-11-12 stsp return err;
3243 05171be4 2022-06-23 thomas err = format_line(&wline, &width, line,
3244 05171be4 2022-06-23 thomas view->x + view->ncols, 0, view->x ? 1 : 0);
3245 41605754 2020-11-12 stsp if (err) {
3246 41605754 2020-11-12 stsp free(line);
3247 41605754 2020-11-12 stsp return err;
3249 05171be4 2022-06-23 thomas if (view->x < width - 1)
3250 05171be4 2022-06-23 thomas waddwstr(view->window, wline + view->x);
3251 41605754 2020-11-12 stsp free(wline);
3252 41605754 2020-11-12 stsp wline = NULL;
3255 6d17833f 2019-11-08 stsp wattr_off(view->window,
3256 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3257 05171be4 2022-06-23 thomas if (width - view->x <= view->ncols - 1)
3258 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3259 fe621944 2020-11-10 stsp nprinted++;
3261 826082fe 2020-12-10 stsp free(line);
3262 fe621944 2020-11-10 stsp if (nprinted >= 1)
3263 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3264 89f1a395 2020-12-01 naddy (nprinted - 1);
3266 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3268 1a57306a 2018-09-02 stsp view_vborder(view);
3270 89f1a395 2020-12-01 naddy if (s->eof) {
3271 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3272 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3273 c3e9aa98 2019-05-13 jcs nprinted++;
3276 05171be4 2022-06-23 thomas err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols,
3279 c3e9aa98 2019-05-13 jcs return err;
3282 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3283 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3284 e54cc94a 2020-11-11 stsp free(wline);
3285 e54cc94a 2020-11-11 stsp wline = NULL;
3286 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3289 26ed57b2 2018-05-19 stsp return NULL;
3292 abd2672a 2018-12-23 stsp static char *
3293 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3295 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3296 09867e48 2019-08-13 stsp char *p, *s;
3298 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3299 09867e48 2019-08-13 stsp if (tm == NULL)
3300 09867e48 2019-08-13 stsp return NULL;
3301 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3302 09867e48 2019-08-13 stsp if (s == NULL)
3303 09867e48 2019-08-13 stsp return NULL;
3304 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3306 abd2672a 2018-12-23 stsp *p = '\0';
3310 4ed7e80c 2018-05-20 stsp static const struct got_error *
3311 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3312 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3314 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3315 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3316 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3317 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3319 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3320 0208f208 2020-05-05 stsp if (qid != NULL) {
3321 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3322 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3323 ec242592 2022-04-22 thomas &qid->id);
3325 0208f208 2020-05-05 stsp return err;
3327 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3328 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3329 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3330 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3331 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3333 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3337 0208f208 2020-05-05 stsp if (tree_id1) {
3338 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3340 0208f208 2020-05-05 stsp goto done;
3343 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3344 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3346 0208f208 2020-05-05 stsp goto done;
3348 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3349 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3351 0208f208 2020-05-05 stsp if (tree1)
3352 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3353 0208f208 2020-05-05 stsp if (tree2)
3354 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3355 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3356 0208f208 2020-05-05 stsp return err;
3359 0208f208 2020-05-05 stsp static const struct got_error *
3360 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3364 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3365 fe621944 2020-11-10 stsp if (p == NULL)
3366 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3367 fe621944 2020-11-10 stsp *line_offsets = p;
3368 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3369 fe621944 2020-11-10 stsp (*nlines)++;
3370 fe621944 2020-11-10 stsp return NULL;
3373 fe621944 2020-11-10 stsp static const struct got_error *
3374 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3375 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3376 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3378 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3379 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3380 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3381 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3382 45d799e2 2018-12-23 stsp time_t committer_time;
3383 45d799e2 2018-12-23 stsp const char *author, *committer;
3384 8b473291 2019-02-21 stsp char *refs_str = NULL;
3385 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3386 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3387 fe621944 2020-11-10 stsp off_t outoff = 0;
3390 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3392 8b473291 2019-02-21 stsp if (refs) {
3393 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3395 8b473291 2019-02-21 stsp return err;
3398 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3400 abd2672a 2018-12-23 stsp return err;
3402 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3403 15a94983 2018-12-23 stsp if (err) {
3404 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3405 15a94983 2018-12-23 stsp goto done;
3408 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3410 fe621944 2020-11-10 stsp goto done;
3412 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3413 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3414 fe621944 2020-11-10 stsp if (n < 0) {
3415 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3416 abd2672a 2018-12-23 stsp goto done;
3418 fe621944 2020-11-10 stsp outoff += n;
3419 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3421 fe621944 2020-11-10 stsp goto done;
3423 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3424 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3425 fe621944 2020-11-10 stsp if (n < 0) {
3426 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3427 abd2672a 2018-12-23 stsp goto done;
3429 fe621944 2020-11-10 stsp outoff += n;
3430 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3432 fe621944 2020-11-10 stsp goto done;
3434 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3435 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3436 fe621944 2020-11-10 stsp if (datestr) {
3437 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3438 fe621944 2020-11-10 stsp if (n < 0) {
3439 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3440 fe621944 2020-11-10 stsp goto done;
3442 fe621944 2020-11-10 stsp outoff += n;
3443 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3445 fe621944 2020-11-10 stsp goto done;
3447 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3448 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3449 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3450 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3451 fe621944 2020-11-10 stsp if (n < 0) {
3452 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3453 fe621944 2020-11-10 stsp goto done;
3455 fe621944 2020-11-10 stsp outoff += n;
3456 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3458 fe621944 2020-11-10 stsp goto done;
3460 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3461 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3462 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3463 ac4dc263 2021-09-24 thomas int pn = 1;
3464 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3465 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3466 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3467 ac4dc263 2021-09-24 thomas if (err)
3468 ac4dc263 2021-09-24 thomas goto done;
3469 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3470 ac4dc263 2021-09-24 thomas if (n < 0) {
3471 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3472 ac4dc263 2021-09-24 thomas goto done;
3474 ac4dc263 2021-09-24 thomas outoff += n;
3475 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3476 ac4dc263 2021-09-24 thomas if (err)
3477 ac4dc263 2021-09-24 thomas goto done;
3478 ac4dc263 2021-09-24 thomas free(id_str);
3479 ac4dc263 2021-09-24 thomas id_str = NULL;
3483 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3485 5943eee2 2019-08-13 stsp goto done;
3486 fe621944 2020-11-10 stsp s = logmsg;
3487 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3488 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3489 fe621944 2020-11-10 stsp if (n < 0) {
3490 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3491 fe621944 2020-11-10 stsp goto done;
3493 fe621944 2020-11-10 stsp outoff += n;
3494 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3496 fe621944 2020-11-10 stsp goto done;
3499 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3501 0208f208 2020-05-05 stsp goto done;
3502 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3503 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3504 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3505 fe621944 2020-11-10 stsp if (n < 0) {
3506 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3507 fe621944 2020-11-10 stsp goto done;
3509 fe621944 2020-11-10 stsp outoff += n;
3510 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3512 fe621944 2020-11-10 stsp goto done;
3513 0208f208 2020-05-05 stsp free((char *)pe->path);
3514 0208f208 2020-05-05 stsp free(pe->data);
3517 0208f208 2020-05-05 stsp fputc('\n', outfile);
3519 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3521 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3522 abd2672a 2018-12-23 stsp free(id_str);
3523 5943eee2 2019-08-13 stsp free(logmsg);
3524 8b473291 2019-02-21 stsp free(refs_str);
3525 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3526 7510f233 2020-08-09 stsp if (err) {
3527 ae6a6978 2020-08-09 stsp free(*line_offsets);
3528 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3529 ae6a6978 2020-08-09 stsp *nlines = 0;
3531 fe621944 2020-11-10 stsp return err;
3534 abd2672a 2018-12-23 stsp static const struct got_error *
3535 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3537 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3538 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3539 15a94983 2018-12-23 stsp int obj_type;
3541 fe621944 2020-11-10 stsp free(s->line_offsets);
3542 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3543 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3544 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3545 fe621944 2020-11-10 stsp s->nlines = 0;
3547 511a516b 2018-05-19 stsp f = got_opentemp();
3548 48ae06ee 2018-10-18 stsp if (f == NULL) {
3549 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3550 48ae06ee 2018-10-18 stsp goto done;
3552 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3553 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3554 fb43ecf1 2019-02-11 stsp goto done;
3558 15a94983 2018-12-23 stsp if (s->id1)
3559 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3561 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3563 15a94983 2018-12-23 stsp goto done;
3565 15a94983 2018-12-23 stsp switch (obj_type) {
3566 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3567 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3568 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3569 a0f32f33 2022-06-13 thomas s->diff_context, s->ignore_whitespace, s->force_text_diff,
3570 a0f32f33 2022-06-13 thomas s->repo, s->f);
3572 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3573 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3574 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3575 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3577 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3578 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3579 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3580 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3581 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3583 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3585 3ffacbe1 2020-02-02 tracey goto done;
3586 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3587 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3588 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3589 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3590 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3591 f44b1f58 2020-02-02 tracey if (err)
3592 f44b1f58 2020-02-02 tracey goto done;
3593 f44b1f58 2020-02-02 tracey } else {
3594 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3595 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3596 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3597 fe621944 2020-11-10 stsp err = write_commit_info(
3598 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3599 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3600 f44b1f58 2020-02-02 tracey if (err)
3601 f44b1f58 2020-02-02 tracey goto done;
3606 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3608 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3609 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3610 a0f32f33 2022-06-13 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3614 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3617 f44b1f58 2020-02-02 tracey if (err)
3618 f44b1f58 2020-02-02 tracey goto done;
3620 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3621 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3622 48ae06ee 2018-10-18 stsp return err;
3625 f5215bb9 2019-02-22 stsp static void
3626 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3628 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3629 f5215bb9 2019-02-22 stsp update_panels();
3630 f5215bb9 2019-02-22 stsp doupdate();
3633 f44b1f58 2020-02-02 tracey static const struct got_error *
3634 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3636 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3638 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3639 f44b1f58 2020-02-02 tracey return NULL;
3642 f44b1f58 2020-02-02 tracey static const struct got_error *
3643 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3645 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3646 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
3647 f44b1f58 2020-02-02 tracey int lineno;
3648 05171be4 2022-06-23 thomas char *exstr = NULL, *line = NULL;
3649 826082fe 2020-12-10 stsp size_t linesize = 0;
3650 826082fe 2020-12-10 stsp ssize_t linelen;
3652 f44b1f58 2020-02-02 tracey if (!view->searching) {
3653 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3654 f44b1f58 2020-02-02 tracey return NULL;
3657 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3658 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3659 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3661 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3663 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3665 f44b1f58 2020-02-02 tracey while (1) {
3666 f44b1f58 2020-02-02 tracey off_t offset;
3668 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3669 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3670 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3674 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3675 f44b1f58 2020-02-02 tracey lineno = 1;
3677 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3680 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3681 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3682 f44b1f58 2020-02-02 tracey free(line);
3683 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3685 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3686 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
3687 05171be4 2022-06-23 thomas if (err)
3689 826082fe 2020-12-10 stsp if (linelen != -1 &&
3690 05171be4 2022-06-23 thomas match_line(exstr, &view->regex, 1, &view->regmatch)) {
3691 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3692 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3695 05171be4 2022-06-23 thomas free(exstr);
3696 05171be4 2022-06-23 thomas exstr = NULL;
3697 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3698 f44b1f58 2020-02-02 tracey lineno++;
3700 f44b1f58 2020-02-02 tracey lineno--;
3702 826082fe 2020-12-10 stsp free(line);
3703 05171be4 2022-06-23 thomas free(exstr);
3705 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3706 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3707 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3710 05171be4 2022-06-23 thomas return err;
3713 48ae06ee 2018-10-18 stsp static const struct got_error *
3714 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
3716 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
3717 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
3719 a0f32f33 2022-06-13 thomas free(s->id1);
3720 a0f32f33 2022-06-13 thomas s->id1 = NULL;
3721 a0f32f33 2022-06-13 thomas free(s->id2);
3722 a0f32f33 2022-06-13 thomas s->id2 = NULL;
3723 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
3724 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3725 a0f32f33 2022-06-13 thomas s->f = NULL;
3726 a0f32f33 2022-06-13 thomas if (s->f1 && fclose(s->f1) == EOF)
3727 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3728 a0f32f33 2022-06-13 thomas s->f1 = NULL;
3729 a0f32f33 2022-06-13 thomas if (s->f2 && fclose(s->f2) == EOF)
3730 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3731 a0f32f33 2022-06-13 thomas s->f2 = NULL;
3732 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
3733 a0f32f33 2022-06-13 thomas free(s->line_offsets);
3734 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
3735 a0f32f33 2022-06-13 thomas s->nlines = 0;
3736 a0f32f33 2022-06-13 thomas return err;
3739 a0f32f33 2022-06-13 thomas static const struct got_error *
3740 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3741 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3742 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3743 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3745 48ae06ee 2018-10-18 stsp const struct got_error *err;
3746 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3748 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
3750 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3751 15a94983 2018-12-23 stsp int type1, type2;
3752 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3754 15a94983 2018-12-23 stsp return err;
3755 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3757 15a94983 2018-12-23 stsp return err;
3759 15a94983 2018-12-23 stsp if (type1 != type2)
3760 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3762 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3763 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3764 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3765 f44b1f58 2020-02-02 tracey s->repo = repo;
3766 f44b1f58 2020-02-02 tracey s->id1 = id1;
3767 f44b1f58 2020-02-02 tracey s->id2 = id2;
3768 3dbaef42 2020-11-24 stsp s->label1 = label1;
3769 3dbaef42 2020-11-24 stsp s->label2 = label2;
3771 15a94983 2018-12-23 stsp if (id1) {
3772 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3773 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3774 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3775 a0f32f33 2022-06-13 thomas s->f1 = got_opentemp();
3776 a0f32f33 2022-06-13 thomas if (s->f1 == NULL) {
3777 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3778 a0f32f33 2022-06-13 thomas goto done;
3781 5465d566 2020-02-01 tracey s->id1 = NULL;
3783 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3784 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3785 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
3786 a0f32f33 2022-06-13 thomas goto done;
3789 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
3790 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
3791 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3792 a0f32f33 2022-06-13 thomas goto done;
3795 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3796 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3797 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3798 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3799 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3800 5465d566 2020-02-01 tracey s->log_view = log_view;
3801 5465d566 2020-02-01 tracey s->repo = repo;
3803 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3804 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3805 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3806 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3807 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3809 a0f32f33 2022-06-13 thomas goto done;
3810 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3811 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3812 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3813 a0f32f33 2022-06-13 thomas if (err)
3814 a0f32f33 2022-06-13 thomas goto done;
3815 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3816 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3817 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3818 a0f32f33 2022-06-13 thomas if (err)
3819 a0f32f33 2022-06-13 thomas goto done;
3821 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3822 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3823 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3824 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3825 a0f32f33 2022-06-13 thomas if (err)
3826 a0f32f33 2022-06-13 thomas goto done;
3828 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3829 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3830 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3831 a0f32f33 2022-06-13 thomas if (err)
3832 a0f32f33 2022-06-13 thomas goto done;
3834 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3835 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3836 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3837 a0f32f33 2022-06-13 thomas if (err)
3838 a0f32f33 2022-06-13 thomas goto done;
3841 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3842 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3843 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3845 5465d566 2020-02-01 tracey err = create_diff(s);
3847 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3848 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3849 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3850 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3851 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3853 a0f32f33 2022-06-13 thomas if (err)
3854 a0f32f33 2022-06-13 thomas close_diff_view(view);
3855 e5a0f69f 2018-08-18 stsp return err;
3858 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3859 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3861 a3404814 2018-09-02 stsp const struct got_error *err;
3862 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3863 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3864 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3866 a3404814 2018-09-02 stsp if (s->id1) {
3867 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3869 a3404814 2018-09-02 stsp return err;
3870 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3872 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3874 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3876 a3404814 2018-09-02 stsp return err;
3877 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3879 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3880 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3881 a3404814 2018-09-02 stsp free(id_str1);
3882 a3404814 2018-09-02 stsp free(id_str2);
3883 a3404814 2018-09-02 stsp return err;
3885 a3404814 2018-09-02 stsp free(id_str1);
3886 a3404814 2018-09-02 stsp free(id_str2);
3888 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3889 267bb3b8 2021-08-01 stsp free(header);
3890 267bb3b8 2021-08-01 stsp return err;
3893 15a087fe 2019-02-21 stsp static const struct got_error *
3894 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3895 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3897 d7a04538 2019-02-21 stsp const struct got_error *err;
3898 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3899 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3900 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3902 15a087fe 2019-02-21 stsp free(s->id2);
3903 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3904 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3905 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3907 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3909 d7a04538 2019-02-21 stsp return err;
3910 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3911 15a087fe 2019-02-21 stsp free(s->id1);
3912 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3913 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3914 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3915 15a087fe 2019-02-21 stsp return NULL;
3918 0cf4efb1 2018-09-29 stsp static const struct got_error *
3919 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3921 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3922 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3923 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3924 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3925 826082fe 2020-12-10 stsp char *line = NULL;
3926 826082fe 2020-12-10 stsp size_t linesize = 0;
3927 826082fe 2020-12-10 stsp ssize_t linelen;
3928 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
3930 e5a0f69f 2018-08-18 stsp switch (ch) {
3931 05171be4 2022-06-23 thomas case '0':
3932 05171be4 2022-06-23 thomas view->x = 0;
3934 05171be4 2022-06-23 thomas case '$':
3935 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
3937 05171be4 2022-06-23 thomas case KEY_RIGHT:
3938 05171be4 2022-06-23 thomas case 'l':
3939 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
3940 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3942 05171be4 2022-06-23 thomas case KEY_LEFT:
3943 05171be4 2022-06-23 thomas case 'h':
3944 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3948 3dbaef42 2020-11-24 stsp if (ch == 'a')
3949 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3950 3dbaef42 2020-11-24 stsp if (ch == 'w')
3951 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3952 64453f7e 2020-11-21 stsp wclear(view->window);
3953 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3954 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3955 aa61903a 2021-12-31 thomas s->matched_line = 0;
3956 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3957 64453f7e 2020-11-21 stsp err = create_diff(s);
3960 912a3f79 2021-08-30 j case KEY_HOME:
3961 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3964 912a3f79 2021-08-30 j case KEY_END:
3968 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3972 1e37a5c2 2019-05-12 jcs case KEY_UP:
3973 f7140bf5 2021-10-17 thomas case CTRL('p'):
3974 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3975 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3977 70f17a53 2022-06-13 thomas case CTRL('u'):
3978 23427b14 2022-06-23 thomas case 'u':
3979 70f17a53 2022-06-13 thomas nscroll /= 2;
3980 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3981 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3982 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3983 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3986 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
3987 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3990 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3991 f7140bf5 2021-10-17 thomas case CTRL('n'):
3992 1e37a5c2 2019-05-12 jcs if (!s->eof)
3993 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3995 70f17a53 2022-06-13 thomas case CTRL('d'):
3996 23427b14 2022-06-23 thomas case 'd':
3997 70f17a53 2022-06-13 thomas nscroll /= 2;
3998 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3999 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4000 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4002 00ba99a7 2019-05-12 jcs if (s->eof)
4005 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4006 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4007 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4008 826082fe 2020-12-10 stsp if (linelen == -1) {
4009 826082fe 2020-12-10 stsp if (feof(s->f)) {
4010 826082fe 2020-12-10 stsp s->eof = 1;
4012 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4016 826082fe 2020-12-10 stsp free(line);
4019 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4020 1e37a5c2 2019-05-12 jcs s->diff_context--;
4021 aa61903a 2021-12-31 thomas s->matched_line = 0;
4022 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4023 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4024 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4025 27829c9e 2020-11-21 stsp s->nlines) {
4026 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4027 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4032 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4033 1e37a5c2 2019-05-12 jcs s->diff_context++;
4034 aa61903a 2021-12-31 thomas s->matched_line = 0;
4035 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4036 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4041 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4043 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4044 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4046 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4050 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4053 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4057 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4058 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4059 aa61903a 2021-12-31 thomas s->matched_line = 0;
4060 05171be4 2022-06-23 thomas view->x = 0;
4062 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4063 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4067 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4069 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4070 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4072 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4076 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4079 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4083 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4084 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4085 aa61903a 2021-12-31 thomas s->matched_line = 0;
4086 05171be4 2022-06-23 thomas view->x = 0;
4088 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4089 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4095 bcbd79e2 2018-08-19 stsp return err;
4098 4ed7e80c 2018-05-20 stsp static const struct got_error *
4099 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4101 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4102 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4103 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4104 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4105 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4106 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4107 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4108 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4109 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4110 3dbaef42 2020-11-24 stsp const char *errstr;
4111 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4112 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4114 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4115 26ed57b2 2018-05-19 stsp switch (ch) {
4117 64453f7e 2020-11-21 stsp force_text_diff = 1;
4120 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4122 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4123 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4124 8f666e67 2022-02-12 thomas errstr, errstr);
4126 09b5bff8 2020-02-23 naddy case 'r':
4127 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4128 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4129 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4131 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4134 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4137 17020d27 2019-03-07 stsp usage_diff();
4138 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4142 26ed57b2 2018-05-19 stsp argc -= optind;
4143 26ed57b2 2018-05-19 stsp argv += optind;
4145 26ed57b2 2018-05-19 stsp if (argc == 0) {
4146 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4147 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4148 15a94983 2018-12-23 stsp id_str1 = argv[0];
4149 15a94983 2018-12-23 stsp id_str2 = argv[1];
4151 26ed57b2 2018-05-19 stsp usage_diff();
4153 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4154 7cd52833 2022-06-23 thomas if (error)
4155 7cd52833 2022-06-23 thomas goto done;
4157 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4158 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4159 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4160 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4161 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4162 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4163 c156c7a4 2020-12-18 stsp goto done;
4164 a273ac94 2020-02-23 naddy if (worktree)
4165 a273ac94 2020-02-23 naddy repo_path =
4166 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4168 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4169 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4170 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4171 c156c7a4 2020-12-18 stsp goto done;
4175 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4176 eb6600df 2019-01-04 stsp if (error)
4177 eb6600df 2019-01-04 stsp goto done;
4179 a273ac94 2020-02-23 naddy init_curses();
4181 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4182 51a10b52 2020-12-26 stsp if (error)
4183 51a10b52 2020-12-26 stsp goto done;
4185 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4186 26ed57b2 2018-05-19 stsp if (error)
4187 26ed57b2 2018-05-19 stsp goto done;
4189 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4190 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4191 26ed57b2 2018-05-19 stsp if (error)
4192 26ed57b2 2018-05-19 stsp goto done;
4194 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4195 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4196 26ed57b2 2018-05-19 stsp if (error)
4197 26ed57b2 2018-05-19 stsp goto done;
4199 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4200 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4201 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4202 ea5e7bb5 2018-08-01 stsp goto done;
4204 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4205 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4206 5dc9f4bc 2018-08-04 stsp if (error)
4207 5dc9f4bc 2018-08-04 stsp goto done;
4208 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4210 3dbaef42 2020-11-24 stsp free(label1);
4211 3dbaef42 2020-11-24 stsp free(label2);
4212 c02c541e 2019-03-29 stsp free(repo_path);
4213 a273ac94 2020-02-23 naddy free(cwd);
4214 1d0f4054 2021-06-17 stsp if (repo) {
4215 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4216 1d0f4054 2021-06-17 stsp if (error == NULL)
4217 1d0f4054 2021-06-17 stsp error = close_err;
4219 a273ac94 2020-02-23 naddy if (worktree)
4220 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4221 7cd52833 2022-06-23 thomas if (pack_fds) {
4222 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4223 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4224 7cd52833 2022-06-23 thomas if (error == NULL)
4225 7cd52833 2022-06-23 thomas error = pack_err;
4227 51a10b52 2020-12-26 stsp tog_free_refs();
4228 26ed57b2 2018-05-19 stsp return error;
4231 4ed7e80c 2018-05-20 stsp __dead static void
4232 9f7d7167 2018-04-29 stsp usage_blame(void)
4235 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4236 9f7d7167 2018-04-29 stsp getprogname());
4240 84451b3e 2018-07-10 stsp struct tog_blame_line {
4241 84451b3e 2018-07-10 stsp int annotated;
4242 84451b3e 2018-07-10 stsp struct got_object_id *id;
4245 4ed7e80c 2018-05-20 stsp static const struct got_error *
4246 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4248 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4249 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4250 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4251 84451b3e 2018-07-10 stsp const struct got_error *err;
4252 02bce7e2 2022-06-23 thomas int lineno = 0, nprinted = 0, i;
4253 826082fe 2020-12-10 stsp char *line = NULL;
4254 826082fe 2020-12-10 stsp size_t linesize = 0;
4255 826082fe 2020-12-10 stsp ssize_t linelen;
4256 84451b3e 2018-07-10 stsp wchar_t *wline;
4257 27a741e5 2019-09-11 stsp int width;
4258 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4259 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4260 ab089a2a 2018-07-12 stsp char *id_str;
4261 11b20872 2019-11-08 stsp struct tog_color *tc;
4263 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4265 ab089a2a 2018-07-12 stsp return err;
4267 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4268 f7d12f7e 2018-08-01 stsp werase(view->window);
4270 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4271 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4272 ab089a2a 2018-07-12 stsp free(id_str);
4273 ab089a2a 2018-07-12 stsp return err;
4276 05171be4 2022-06-23 thomas err = format_line(&wline, &width, line, view->ncols, 0, 0);
4277 ab089a2a 2018-07-12 stsp free(line);
4278 2550e4c3 2018-07-13 stsp line = NULL;
4280 1cae65b4 2019-09-22 stsp return err;
4281 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4282 a3404814 2018-09-02 stsp wstandout(view->window);
4283 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4285 11b20872 2019-11-08 stsp wattr_on(view->window,
4286 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4287 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4289 11b20872 2019-11-08 stsp wattr_off(view->window,
4290 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4291 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4292 a3404814 2018-09-02 stsp wstandend(view->window);
4293 2550e4c3 2018-07-13 stsp free(wline);
4294 2550e4c3 2018-07-13 stsp wline = NULL;
4295 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4296 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4298 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4299 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4300 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4301 ab089a2a 2018-07-12 stsp free(id_str);
4302 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4304 ab089a2a 2018-07-12 stsp free(id_str);
4305 05171be4 2022-06-23 thomas err = format_line(&wline, &width, line, view->ncols, 0, 0);
4306 3f60a8ef 2018-07-10 stsp free(line);
4307 2550e4c3 2018-07-13 stsp line = NULL;
4309 3f60a8ef 2018-07-10 stsp return err;
4310 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4311 2550e4c3 2018-07-13 stsp free(wline);
4312 2550e4c3 2018-07-13 stsp wline = NULL;
4313 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4314 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4316 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4317 05171be4 2022-06-23 thomas view->maxx = 0;
4318 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4319 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4320 826082fe 2020-12-10 stsp if (linelen == -1) {
4321 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4322 826082fe 2020-12-10 stsp s->eof = 1;
4325 84451b3e 2018-07-10 stsp free(line);
4326 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4328 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4331 05171be4 2022-06-23 thomas view->maxx = MAX(view->maxx, linelen);
4333 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4334 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4336 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4337 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4338 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4339 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4340 27a741e5 2019-09-11 stsp !(view->focussed &&
4341 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4342 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4343 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4344 8d0fe45a 2019-08-12 stsp char *id_str;
4345 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4346 8d0fe45a 2019-08-12 stsp if (err) {
4347 8d0fe45a 2019-08-12 stsp free(line);
4348 8d0fe45a 2019-08-12 stsp return err;
4350 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4352 11b20872 2019-11-08 stsp wattr_on(view->window,
4353 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4354 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4356 11b20872 2019-11-08 stsp wattr_off(view->window,
4357 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4358 8d0fe45a 2019-08-12 stsp free(id_str);
4359 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4361 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4362 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4365 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4366 ee41ec32 2018-07-10 stsp prev_id = NULL;
4369 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4370 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4371 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4373 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4374 41605754 2020-11-12 stsp width = 9;
4375 41605754 2020-11-12 stsp wline = wcsdup(L"");
4376 41605754 2020-11-12 stsp if (wline == NULL) {
4377 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4378 41605754 2020-11-12 stsp free(line);
4379 41605754 2020-11-12 stsp return err;
4381 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4382 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4383 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4384 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4385 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4386 41605754 2020-11-12 stsp if (err) {
4387 41605754 2020-11-12 stsp free(line);
4388 41605754 2020-11-12 stsp return err;
4390 41605754 2020-11-12 stsp width += 9;
4392 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4393 05171be4 2022-06-23 thomas view->x + view->ncols - 9, 9, 1);
4394 02bce7e2 2022-06-23 thomas if (err) {
4395 02bce7e2 2022-06-23 thomas free(line);
4396 02bce7e2 2022-06-23 thomas return err;
4398 02bce7e2 2022-06-23 thomas if (view->x < width) {
4399 02bce7e2 2022-06-23 thomas waddwstr(view->window, wline + view->x);
4400 02bce7e2 2022-06-23 thomas for (i = 0; i < view->x; i++)
4401 02bce7e2 2022-06-23 thomas width -= wcwidth(wline[i]);
4403 02bce7e2 2022-06-23 thomas width += 9;
4404 41605754 2020-11-12 stsp free(wline);
4405 41605754 2020-11-12 stsp wline = NULL;
4408 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4409 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4410 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4411 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4413 826082fe 2020-12-10 stsp free(line);
4414 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4416 1a57306a 2018-09-02 stsp view_vborder(view);
4418 84451b3e 2018-07-10 stsp return NULL;
4421 84451b3e 2018-07-10 stsp static const struct got_error *
4422 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4423 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4425 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4426 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4427 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4428 1a76625f 2018-10-22 stsp int errcode;
4430 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4431 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4432 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4434 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4435 1a76625f 2018-10-22 stsp if (errcode)
4436 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4438 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4439 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4440 d68a0a7d 2018-07-10 stsp goto done;
4443 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4444 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4446 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4447 d68a0a7d 2018-07-10 stsp if (line->annotated)
4448 d68a0a7d 2018-07-10 stsp goto done;
4450 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4451 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4452 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4453 84451b3e 2018-07-10 stsp goto done;
4455 84451b3e 2018-07-10 stsp line->annotated = 1;
4457 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4458 1a76625f 2018-10-22 stsp if (errcode)
4459 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4460 84451b3e 2018-07-10 stsp return err;
4463 84451b3e 2018-07-10 stsp static void *
4464 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4466 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4467 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4468 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4469 1a76625f 2018-10-22 stsp int errcode;
4471 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4473 61266923 2020-01-14 stsp return (void *)err;
4475 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4476 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4477 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4478 fc06ba56 2019-08-22 stsp err = NULL;
4480 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4481 1a76625f 2018-10-22 stsp if (errcode)
4482 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4483 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4485 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4486 1d0f4054 2021-06-17 stsp if (err == NULL)
4487 1d0f4054 2021-06-17 stsp err = close_err;
4488 c9beca56 2018-07-22 stsp ta->repo = NULL;
4489 c9beca56 2018-07-22 stsp *ta->complete = 1;
4491 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4492 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4493 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4495 18430de3 2018-07-10 stsp return (void *)err;
4498 245d91c1 2018-07-12 stsp static struct got_object_id *
4499 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4500 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4502 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4504 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4505 8d0fe45a 2019-08-12 stsp return NULL;
4507 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4508 245d91c1 2018-07-12 stsp if (!line->annotated)
4509 245d91c1 2018-07-12 stsp return NULL;
4511 245d91c1 2018-07-12 stsp return line->id;
4514 b880a918 2018-07-10 stsp static const struct got_error *
4515 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4517 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4520 245d91c1 2018-07-12 stsp if (blame->thread) {
4521 1a76625f 2018-10-22 stsp int errcode;
4522 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4523 1a76625f 2018-10-22 stsp if (errcode)
4524 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4525 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4526 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4527 1a76625f 2018-10-22 stsp if (errcode)
4528 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4529 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4530 1a76625f 2018-10-22 stsp if (errcode)
4531 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4532 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4533 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4534 245d91c1 2018-07-12 stsp err = NULL;
4535 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4537 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4538 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4539 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4540 1d0f4054 2021-06-17 stsp if (err == NULL)
4541 1d0f4054 2021-06-17 stsp err = close_err;
4542 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4544 245d91c1 2018-07-12 stsp if (blame->f) {
4545 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4546 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4547 245d91c1 2018-07-12 stsp blame->f = NULL;
4549 57670559 2018-12-23 stsp if (blame->lines) {
4550 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4551 57670559 2018-12-23 stsp free(blame->lines[i].id);
4552 57670559 2018-12-23 stsp free(blame->lines);
4553 57670559 2018-12-23 stsp blame->lines = NULL;
4555 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4556 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4557 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
4558 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4559 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
4560 7cd52833 2022-06-23 thomas if (err == NULL)
4561 7cd52833 2022-06-23 thomas err = pack_err;
4562 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
4564 245d91c1 2018-07-12 stsp return err;
4567 245d91c1 2018-07-12 stsp static const struct got_error *
4568 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4570 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4571 fc06ba56 2019-08-22 stsp int *done = arg;
4572 fc06ba56 2019-08-22 stsp int errcode;
4574 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4575 fc06ba56 2019-08-22 stsp if (errcode)
4576 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4577 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4579 fc06ba56 2019-08-22 stsp if (*done)
4580 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4582 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4583 fc06ba56 2019-08-22 stsp if (errcode)
4584 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4585 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4587 fc06ba56 2019-08-22 stsp return err;
4590 fc06ba56 2019-08-22 stsp static const struct got_error *
4591 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4593 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4594 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4595 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4596 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4597 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4598 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4599 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4600 15a94983 2018-12-23 stsp int obj_type;
4601 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4603 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4604 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4606 15a94983 2018-12-23 stsp return err;
4608 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4609 945f9229 2022-04-16 thomas if (err)
4610 945f9229 2022-04-16 thomas goto done;
4612 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4614 84451b3e 2018-07-10 stsp goto done;
4616 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4617 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4618 84451b3e 2018-07-10 stsp goto done;
4621 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4623 a70480e0 2018-06-23 stsp goto done;
4624 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4625 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4626 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4627 84451b3e 2018-07-10 stsp goto done;
4629 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4630 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4632 1fddf795 2021-01-20 stsp goto done;
4633 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4634 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4635 84451b3e 2018-07-10 stsp goto done;
4638 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4639 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4640 b02560ec 2019-08-19 stsp blame->nlines--;
4642 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4643 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4644 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4645 84451b3e 2018-07-10 stsp goto done;
4648 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
4650 bd24772e 2018-07-11 stsp goto done;
4651 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4652 7cd52833 2022-06-23 thomas pack_fds);
4653 7cd52833 2022-06-23 thomas if (err)
4654 7cd52833 2022-06-23 thomas goto done;
4656 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
4657 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4658 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4659 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4660 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4661 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4662 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4663 245d91c1 2018-07-12 stsp goto done;
4665 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4667 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4668 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4669 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4670 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4671 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4672 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4673 a5388363 2020-12-01 naddy s->blame_complete = 0;
4675 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4676 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4677 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4678 f5a09613 2020-12-13 naddy s->selected_line = 1;
4680 aa61903a 2021-12-31 thomas s->matched_line = 0;
4683 945f9229 2022-04-16 thomas if (commit)
4684 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4686 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4687 27d434c2 2018-09-15 stsp free(obj_id);
4689 245d91c1 2018-07-12 stsp stop_blame(blame);
4690 245d91c1 2018-07-12 stsp return err;
4693 245d91c1 2018-07-12 stsp static const struct got_error *
4694 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4695 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4697 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4698 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4700 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4702 c4843652 2019-08-12 stsp s->path = strdup(path);
4703 c4843652 2019-08-12 stsp if (s->path == NULL)
4704 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4706 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4707 c4843652 2019-08-12 stsp if (err) {
4708 c4843652 2019-08-12 stsp free(s->path);
4709 7cbe629d 2018-08-04 stsp return err;
4712 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4713 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4714 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4715 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4716 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4717 fb2756b9 2018-08-04 stsp s->repo = repo;
4718 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4719 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4721 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4722 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4723 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4724 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4726 11b20872 2019-11-08 stsp return err;
4729 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4730 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4731 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4732 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4733 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4735 a5388363 2020-12-01 naddy return run_blame(view);
4738 e5a0f69f 2018-08-18 stsp static const struct got_error *
4739 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4741 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4742 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4744 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4745 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4747 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4748 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4749 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4750 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4751 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4754 e5a0f69f 2018-08-18 stsp free(s->path);
4755 11b20872 2019-11-08 stsp free_colors(&s->colors);
4756 e5a0f69f 2018-08-18 stsp return err;
4759 7cbe629d 2018-08-04 stsp static const struct got_error *
4760 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4762 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4764 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4765 6c4c42e0 2019-06-24 stsp return NULL;
4768 6c4c42e0 2019-06-24 stsp static const struct got_error *
4769 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4771 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4772 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4773 6c4c42e0 2019-06-24 stsp int lineno;
4774 05171be4 2022-06-23 thomas char *exstr = NULL, *line = NULL;
4775 826082fe 2020-12-10 stsp size_t linesize = 0;
4776 826082fe 2020-12-10 stsp ssize_t linelen;
4778 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4779 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4780 6c4c42e0 2019-06-24 stsp return NULL;
4783 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4784 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4785 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4787 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4789 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4791 6c4c42e0 2019-06-24 stsp while (1) {
4792 6c4c42e0 2019-06-24 stsp off_t offset;
4794 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4795 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4796 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4800 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4801 6c4c42e0 2019-06-24 stsp lineno = 1;
4803 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4806 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4807 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4808 6c4c42e0 2019-06-24 stsp free(line);
4809 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4811 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4812 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
4813 05171be4 2022-06-23 thomas if (err)
4815 826082fe 2020-12-10 stsp if (linelen != -1 &&
4816 05171be4 2022-06-23 thomas match_line(exstr, &view->regex, 1, &view->regmatch)) {
4817 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4818 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4821 05171be4 2022-06-23 thomas free(exstr);
4822 05171be4 2022-06-23 thomas exstr = NULL;
4823 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4828 826082fe 2020-12-10 stsp free(line);
4829 05171be4 2022-06-23 thomas free(exstr);
4831 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4832 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4833 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4836 05171be4 2022-06-23 thomas return err;
4839 6c4c42e0 2019-06-24 stsp static const struct got_error *
4840 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4842 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4843 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4844 2b380cc8 2018-10-24 stsp int errcode;
4846 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4847 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4848 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4849 2b380cc8 2018-10-24 stsp if (errcode)
4850 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4852 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4855 51fe7530 2019-08-19 stsp if (s->blame_complete)
4856 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4858 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4860 669b5ffa 2018-10-07 stsp view_vborder(view);
4861 e5a0f69f 2018-08-18 stsp return err;
4864 e5a0f69f 2018-08-18 stsp static const struct got_error *
4865 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4867 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4868 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4869 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4870 70f17a53 2022-06-13 thomas int begin_x = 0, nscroll = view->nlines - 2;
4872 e5a0f69f 2018-08-18 stsp switch (ch) {
4873 05171be4 2022-06-23 thomas case '0':
4874 05171be4 2022-06-23 thomas view->x = 0;
4876 05171be4 2022-06-23 thomas case '$':
4877 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4879 05171be4 2022-06-23 thomas case KEY_RIGHT:
4880 05171be4 2022-06-23 thomas case 'l':
4881 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4882 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4884 05171be4 2022-06-23 thomas case KEY_LEFT:
4885 05171be4 2022-06-23 thomas case 'h':
4886 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4889 1e37a5c2 2019-05-12 jcs s->done = 1;
4891 4deef56f 2021-09-02 naddy case 'g':
4892 4deef56f 2021-09-02 naddy case KEY_HOME:
4893 4deef56f 2021-09-02 naddy s->selected_line = 1;
4894 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4896 4deef56f 2021-09-02 naddy case 'G':
4897 4deef56f 2021-09-02 naddy case KEY_END:
4898 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4899 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4900 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4902 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4903 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4904 4deef56f 2021-09-02 naddy (view->nlines - 3);
4908 1e37a5c2 2019-05-12 jcs case KEY_UP:
4909 f7140bf5 2021-10-17 thomas case CTRL('p'):
4910 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4911 1e37a5c2 2019-05-12 jcs s->selected_line--;
4912 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4913 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4914 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4916 70f17a53 2022-06-13 thomas case CTRL('u'):
4917 23427b14 2022-06-23 thomas case 'u':
4918 70f17a53 2022-06-13 thomas nscroll /= 2;
4919 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4920 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4921 ea025d1d 2020-02-22 naddy case CTRL('b'):
4922 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4923 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
4926 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
4927 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
4929 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4932 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4933 f7140bf5 2021-10-17 thomas case CTRL('n'):
4934 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4935 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4936 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4937 1e37a5c2 2019-05-12 jcs s->selected_line++;
4938 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4939 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4940 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4943 1e37a5c2 2019-05-12 jcs case 'p': {
4944 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4945 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4946 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4947 1e37a5c2 2019-05-12 jcs if (id == NULL)
4949 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4950 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4951 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4952 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4953 1e37a5c2 2019-05-12 jcs int obj_type;
4954 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4955 1e37a5c2 2019-05-12 jcs s->repo, id);
4958 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4959 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4960 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4961 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4964 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4965 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4966 ec242592 2022-04-22 thomas s->repo, &pid->id);
4967 945f9229 2022-04-16 thomas if (err)
4969 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4970 945f9229 2022-04-16 thomas pcommit, s->path);
4971 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4972 e5a0f69f 2018-08-18 stsp if (err) {
4973 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4974 1e37a5c2 2019-05-12 jcs err = NULL;
4975 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4978 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4980 1e37a5c2 2019-05-12 jcs free(blob_id);
4981 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4982 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4983 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4986 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4987 ec242592 2022-04-22 thomas &pid->id);
4988 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4990 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4991 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
4993 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4998 1e37a5c2 2019-05-12 jcs s->done = 1;
4999 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5000 1e37a5c2 2019-05-12 jcs s->done = 0;
5001 1e37a5c2 2019-05-12 jcs if (thread_err)
5003 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5004 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5005 a5388363 2020-12-01 naddy err = run_blame(view);
5010 1e37a5c2 2019-05-12 jcs case 'B': {
5011 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5012 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5013 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5015 1e37a5c2 2019-05-12 jcs s->done = 1;
5016 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5017 1e37a5c2 2019-05-12 jcs s->done = 0;
5018 1e37a5c2 2019-05-12 jcs if (thread_err)
5020 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5021 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5022 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5023 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5024 a5388363 2020-12-01 naddy err = run_blame(view);
5029 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5030 1e37a5c2 2019-05-12 jcs case '\r': {
5031 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5032 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5033 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5034 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5035 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5036 1e37a5c2 2019-05-12 jcs if (id == NULL)
5038 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5041 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5042 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
5043 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5044 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5045 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5046 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5047 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5048 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5051 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5052 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5053 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5055 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5058 e78dc838 2020-12-04 stsp view->focussed = 0;
5059 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5060 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5061 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5064 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
5065 e78dc838 2020-12-04 stsp view->focus_child = 1;
5067 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5072 70f17a53 2022-06-13 thomas case CTRL('d'):
5073 23427b14 2022-06-23 thomas case 'd':
5074 70f17a53 2022-06-13 thomas nscroll /= 2;
5075 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5076 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5077 ea025d1d 2020-02-22 naddy case CTRL('f'):
5079 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5080 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5081 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5084 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5085 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5086 70f17a53 2022-06-13 thomas s->selected_line +=
5087 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5088 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5090 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5091 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5093 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5094 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5096 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5097 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5098 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5099 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5105 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5108 a70480e0 2018-06-23 stsp static const struct got_error *
5109 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5111 a70480e0 2018-06-23 stsp const struct got_error *error;
5112 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5113 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5114 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5115 0587e10c 2020-07-23 stsp char *link_target = NULL;
5116 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5117 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5118 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5120 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5121 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5123 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5124 a70480e0 2018-06-23 stsp switch (ch) {
5126 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5129 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5130 69069811 2018-08-02 stsp if (repo_path == NULL)
5131 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5135 17020d27 2019-03-07 stsp usage_blame();
5136 a70480e0 2018-06-23 stsp /* NOTREACHED */
5140 a70480e0 2018-06-23 stsp argc -= optind;
5141 a70480e0 2018-06-23 stsp argv += optind;
5143 f135c941 2020-02-20 stsp if (argc != 1)
5144 a70480e0 2018-06-23 stsp usage_blame();
5146 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5147 7cd52833 2022-06-23 thomas if (error != NULL)
5148 7cd52833 2022-06-23 thomas goto done;
5150 69069811 2018-08-02 stsp if (repo_path == NULL) {
5151 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5152 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5153 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5154 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5155 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5156 c156c7a4 2020-12-18 stsp goto done;
5157 f135c941 2020-02-20 stsp if (worktree)
5158 eb41ed75 2019-02-05 stsp repo_path =
5159 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5161 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5162 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5163 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5164 c156c7a4 2020-12-18 stsp goto done;
5168 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5169 c02c541e 2019-03-29 stsp if (error != NULL)
5170 8e94dd5b 2019-01-04 stsp goto done;
5172 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5173 f135c941 2020-02-20 stsp worktree);
5174 c02c541e 2019-03-29 stsp if (error)
5175 92205607 2019-01-04 stsp goto done;
5177 f135c941 2020-02-20 stsp init_curses();
5179 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5180 51a10b52 2020-12-26 stsp if (error)
5181 51a10b52 2020-12-26 stsp goto done;
5183 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5184 eb41ed75 2019-02-05 stsp if (error)
5185 69069811 2018-08-02 stsp goto done;
5187 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5188 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5189 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5190 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5191 a70480e0 2018-06-23 stsp if (error != NULL)
5192 66b4983c 2018-06-23 stsp goto done;
5193 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5194 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5196 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5197 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5199 a19e88aa 2018-06-23 stsp if (error != NULL)
5200 8b473291 2019-02-21 stsp goto done;
5202 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5203 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5204 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5205 e1cd8fed 2018-08-01 stsp goto done;
5208 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5209 945f9229 2022-04-16 thomas if (error)
5210 945f9229 2022-04-16 thomas goto done;
5212 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5213 945f9229 2022-04-16 thomas commit, repo);
5214 7cbe629d 2018-08-04 stsp if (error)
5215 7cbe629d 2018-08-04 stsp goto done;
5217 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5218 78756c87 2020-11-24 stsp commit_id, repo);
5219 0587e10c 2020-07-23 stsp if (error)
5220 0587e10c 2020-07-23 stsp goto done;
5221 12314ad4 2019-08-31 stsp if (worktree) {
5222 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5223 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5224 12314ad4 2019-08-31 stsp worktree = NULL;
5226 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5228 69069811 2018-08-02 stsp free(repo_path);
5229 f135c941 2020-02-20 stsp free(in_repo_path);
5230 0587e10c 2020-07-23 stsp free(link_target);
5231 69069811 2018-08-02 stsp free(cwd);
5232 a70480e0 2018-06-23 stsp free(commit_id);
5233 945f9229 2022-04-16 thomas if (commit)
5234 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5235 eb41ed75 2019-02-05 stsp if (worktree)
5236 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5237 1d0f4054 2021-06-17 stsp if (repo) {
5238 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5239 1d0f4054 2021-06-17 stsp if (error == NULL)
5240 1d0f4054 2021-06-17 stsp error = close_err;
5242 7cd52833 2022-06-23 thomas if (pack_fds) {
5243 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5244 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5245 7cd52833 2022-06-23 thomas if (error == NULL)
5246 7cd52833 2022-06-23 thomas error = pack_err;
5248 51a10b52 2020-12-26 stsp tog_free_refs();
5249 a70480e0 2018-06-23 stsp return error;
5252 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5253 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5255 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5256 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5257 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5258 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5259 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5260 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5261 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5263 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5265 f7d12f7e 2018-08-01 stsp werase(view->window);
5267 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5268 ffd1d5e5 2018-06-23 stsp return NULL;
5270 05171be4 2022-06-23 thomas err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5272 ffd1d5e5 2018-06-23 stsp return err;
5273 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5274 a3404814 2018-09-02 stsp wstandout(view->window);
5275 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5277 11b20872 2019-11-08 stsp wattr_on(view->window,
5278 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5279 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5281 11b20872 2019-11-08 stsp wattr_off(view->window,
5282 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5283 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5284 a3404814 2018-09-02 stsp wstandend(view->window);
5285 2550e4c3 2018-07-13 stsp free(wline);
5286 2550e4c3 2018-07-13 stsp wline = NULL;
5287 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5288 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5289 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5290 ffd1d5e5 2018-06-23 stsp return NULL;
5291 05171be4 2022-06-23 thomas err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5293 ce52c690 2018-06-23 stsp return err;
5294 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5295 2550e4c3 2018-07-13 stsp free(wline);
5296 2550e4c3 2018-07-13 stsp wline = NULL;
5297 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5298 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5299 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5300 ffd1d5e5 2018-06-23 stsp return NULL;
5301 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5302 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5303 a1eca9bb 2018-06-23 stsp return NULL;
5305 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5306 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5307 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5308 0cf4efb1 2018-09-29 stsp if (view->focussed)
5309 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5310 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5312 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5313 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5314 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5315 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5316 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5317 ffd1d5e5 2018-06-23 stsp return NULL;
5321 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5324 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5325 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5326 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5327 848d6979 2019-08-12 stsp const char *modestr = "";
5328 56e0773d 2019-11-28 stsp mode_t mode;
5330 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5331 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5333 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5334 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5335 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5337 638f9024 2019-05-13 stsp return got_error_from_errno(
5338 230a42bd 2019-05-11 jcs "got_object_id_str");
5340 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5341 63c5ca5d 2019-08-24 stsp modestr = "$";
5342 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5345 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5346 d86d3b18 2020-12-01 naddy te, s->repo);
5347 0d6c6ee3 2020-05-20 stsp if (err) {
5348 0d6c6ee3 2020-05-20 stsp free(id_str);
5349 0d6c6ee3 2020-05-20 stsp return err;
5351 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5352 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5353 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5355 848d6979 2019-08-12 stsp modestr = "@";
5357 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5358 848d6979 2019-08-12 stsp modestr = "/";
5359 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5360 848d6979 2019-08-12 stsp modestr = "*";
5361 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5362 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5363 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5364 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5365 1d13200f 2018-07-12 stsp free(id_str);
5366 0d6c6ee3 2020-05-20 stsp free(link_target);
5367 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5369 1d13200f 2018-07-12 stsp free(id_str);
5370 0d6c6ee3 2020-05-20 stsp free(link_target);
5371 05171be4 2022-06-23 thomas err = format_line(&wline, &width, line, view->ncols, 0, 0);
5372 ffd1d5e5 2018-06-23 stsp if (err) {
5373 ffd1d5e5 2018-06-23 stsp free(line);
5376 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5377 0cf4efb1 2018-09-29 stsp if (view->focussed)
5378 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5379 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5381 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5383 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5384 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5385 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5387 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5388 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5389 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5390 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5391 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5392 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5393 ffd1d5e5 2018-06-23 stsp free(line);
5394 2550e4c3 2018-07-13 stsp free(wline);
5395 2550e4c3 2018-07-13 stsp wline = NULL;
5397 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5398 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5399 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5403 ffd1d5e5 2018-06-23 stsp return err;
5406 ffd1d5e5 2018-06-23 stsp static void
5407 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5409 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5410 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5411 fa86c4bf 2020-11-29 stsp int i = 0;
5413 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5416 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5417 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5418 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5419 fa86c4bf 2020-11-29 stsp if (!isroot)
5420 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5423 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5424 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5428 fa86c4bf 2020-11-29 stsp static void
5429 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5431 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5432 ffd1d5e5 2018-06-23 stsp int n = 0;
5434 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5435 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5436 694d3271 2020-12-01 naddy s->first_displayed_entry);
5438 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5440 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5441 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5442 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5443 768394f3 2019-01-24 stsp if (last) {
5444 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5445 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5450 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5451 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5452 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5454 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5455 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5456 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5458 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5459 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5460 56e0773d 2019-11-28 stsp + 1 /* slash */;
5462 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5464 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5465 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5466 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5468 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5469 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5470 d9765a41 2018-06-23 stsp while (pt) {
5471 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5472 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5473 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5474 cb2ebc8a 2018-06-23 stsp goto done;
5476 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5477 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5478 cb2ebc8a 2018-06-23 stsp goto done;
5480 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5483 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5484 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5485 ce52c690 2018-06-23 stsp goto done;
5489 ce52c690 2018-06-23 stsp if (err) {
5490 ce52c690 2018-06-23 stsp free(*path);
5491 ce52c690 2018-06-23 stsp *path = NULL;
5493 ce52c690 2018-06-23 stsp return err;
5496 ce52c690 2018-06-23 stsp static const struct got_error *
5497 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5498 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5499 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5501 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5502 ce52c690 2018-06-23 stsp char *path;
5503 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5505 a0de39f3 2019-08-09 stsp *new_view = NULL;
5507 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5509 ce52c690 2018-06-23 stsp return err;
5511 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5512 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5513 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5514 83ce39e3 2019-08-12 stsp goto done;
5517 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5518 e5a0f69f 2018-08-18 stsp if (err) {
5519 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5520 fc06ba56 2019-08-22 stsp err = NULL;
5521 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5523 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5525 83ce39e3 2019-08-12 stsp free(path);
5526 69efd4c4 2018-07-18 stsp return err;
5529 69efd4c4 2018-07-18 stsp static const struct got_error *
5530 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5531 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5533 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5534 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5535 69efd4c4 2018-07-18 stsp char *path;
5537 a0de39f3 2019-08-09 stsp *new_view = NULL;
5539 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5540 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5541 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5543 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5545 69efd4c4 2018-07-18 stsp return err;
5547 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5550 e5a0f69f 2018-08-18 stsp view_close(log_view);
5552 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5553 cb2ebc8a 2018-06-23 stsp free(path);
5554 cb2ebc8a 2018-06-23 stsp return err;
5557 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5558 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5559 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5561 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5562 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5563 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5564 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5566 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5567 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5569 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5570 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5571 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5573 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5575 bc573f3b 2021-07-10 stsp goto done;
5578 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5579 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5580 bc573f3b 2021-07-10 stsp * closed on demand.
5582 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5583 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5585 bc573f3b 2021-07-10 stsp goto done;
5586 bc573f3b 2021-07-10 stsp s->tree = s->root;
5588 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5589 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5590 ffd1d5e5 2018-06-23 stsp goto done;
5592 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5593 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5594 ffd1d5e5 2018-06-23 stsp goto done;
5597 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5598 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5599 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5600 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5601 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5602 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5603 9cd7cbd1 2020-12-07 stsp goto done;
5606 fb2756b9 2018-08-04 stsp s->repo = repo;
5608 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5609 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5610 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5611 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5613 c0b01bdb 2019-11-08 stsp goto done;
5614 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5615 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5617 c0b01bdb 2019-11-08 stsp goto done;
5618 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5619 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5620 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5622 c0b01bdb 2019-11-08 stsp goto done;
5624 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5625 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5626 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5628 11b20872 2019-11-08 stsp goto done;
5630 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5631 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5633 c0b01bdb 2019-11-08 stsp goto done;
5636 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5637 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5638 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5639 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5640 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5642 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5643 bc573f3b 2021-07-10 stsp if (commit)
5644 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5646 bc573f3b 2021-07-10 stsp close_tree_view(view);
5647 ad80ab7b 2018-08-04 stsp return err;
5650 e5a0f69f 2018-08-18 stsp static const struct got_error *
5651 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5653 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5655 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5656 fb2756b9 2018-08-04 stsp free(s->tree_label);
5657 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5658 6484ec90 2018-09-29 stsp free(s->commit_id);
5659 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5660 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5661 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5662 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5663 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5664 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5665 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5666 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5667 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5668 ad80ab7b 2018-08-04 stsp free(parent);
5671 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5672 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5673 bc573f3b 2021-07-10 stsp if (s->root)
5674 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5675 7c32bd05 2019-06-22 stsp return NULL;
5678 7c32bd05 2019-06-22 stsp static const struct got_error *
5679 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5681 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5683 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5684 7c32bd05 2019-06-22 stsp return NULL;
5687 7c32bd05 2019-06-22 stsp static int
5688 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5690 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5692 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5696 7c32bd05 2019-06-22 stsp static const struct got_error *
5697 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5699 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5700 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5702 7c32bd05 2019-06-22 stsp if (!view->searching) {
5703 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5704 7c32bd05 2019-06-22 stsp return NULL;
5707 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5708 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5709 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5710 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5711 56e0773d 2019-11-28 stsp s->selected_entry);
5713 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5715 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5716 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5718 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5719 56e0773d 2019-11-28 stsp s->selected_entry);
5722 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5723 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5724 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5725 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5727 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5730 7c32bd05 2019-06-22 stsp while (1) {
5731 56e0773d 2019-11-28 stsp if (te == NULL) {
5732 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5733 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5734 ac66afb8 2019-06-24 stsp return NULL;
5736 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5737 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5739 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5742 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5743 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5744 56e0773d 2019-11-28 stsp s->matched_entry = te;
5748 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5749 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5751 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5754 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5755 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5756 7c32bd05 2019-06-22 stsp s->selected = 0;
5759 e5a0f69f 2018-08-18 stsp return NULL;
5762 ad80ab7b 2018-08-04 stsp static const struct got_error *
5763 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5765 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5766 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5767 e5a0f69f 2018-08-18 stsp char *parent_path;
5769 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5771 e5a0f69f 2018-08-18 stsp return err;
5773 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5774 e5a0f69f 2018-08-18 stsp free(parent_path);
5776 669b5ffa 2018-10-07 stsp view_vborder(view);
5777 e5a0f69f 2018-08-18 stsp return err;
5780 e5a0f69f 2018-08-18 stsp static const struct got_error *
5781 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5783 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5784 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5785 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5786 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5787 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 3;
5789 e5a0f69f 2018-08-18 stsp switch (ch) {
5791 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5794 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5796 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5797 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5798 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5799 e78dc838 2020-12-04 stsp view->focussed = 0;
5800 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5801 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5802 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5804 1e37a5c2 2019-05-12 jcs return err;
5805 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5806 e78dc838 2020-12-04 stsp view->focus_child = 1;
5808 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5811 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5812 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5813 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5814 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5815 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5816 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5817 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5818 152c1c93 2020-11-29 stsp if (err) {
5819 152c1c93 2020-11-29 stsp view_close(ref_view);
5820 152c1c93 2020-11-29 stsp return err;
5822 e78dc838 2020-12-04 stsp view->focussed = 0;
5823 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5824 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5825 152c1c93 2020-11-29 stsp err = view_close_child(view);
5827 152c1c93 2020-11-29 stsp return err;
5828 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5829 e78dc838 2020-12-04 stsp view->focus_child = 1;
5831 152c1c93 2020-11-29 stsp *new_view = ref_view;
5833 e4526bf5 2021-09-03 naddy case 'g':
5834 e4526bf5 2021-09-03 naddy case KEY_HOME:
5835 e4526bf5 2021-09-03 naddy s->selected = 0;
5836 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5837 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5838 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5840 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5842 e4526bf5 2021-09-03 naddy case 'G':
5843 e4526bf5 2021-09-03 naddy case KEY_END:
5844 e4526bf5 2021-09-03 naddy s->selected = 0;
5845 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5846 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5847 e4526bf5 2021-09-03 naddy if (te == NULL) {
5848 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5849 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5854 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5855 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5857 e4526bf5 2021-09-03 naddy if (n > 0)
5858 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5861 1e37a5c2 2019-05-12 jcs case KEY_UP:
5862 f7140bf5 2021-10-17 thomas case CTRL('p'):
5863 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5864 1e37a5c2 2019-05-12 jcs s->selected--;
5867 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5869 70f17a53 2022-06-13 thomas case CTRL('u'):
5870 23427b14 2022-06-23 thomas case 'u':
5871 70f17a53 2022-06-13 thomas nscroll /= 2;
5872 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5873 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5874 ea025d1d 2020-02-22 naddy case CTRL('b'):
5875 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5876 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5877 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5878 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5880 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5881 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5883 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
5886 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5887 f7140bf5 2021-10-17 thomas case CTRL('n'):
5888 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5889 1e37a5c2 2019-05-12 jcs s->selected++;
5892 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5894 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5896 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5898 70f17a53 2022-06-13 thomas case CTRL('d'):
5899 23427b14 2022-06-23 thomas case 'd':
5900 70f17a53 2022-06-13 thomas nscroll /= 2;
5901 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5902 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5903 ea025d1d 2020-02-22 naddy case CTRL('f'):
5904 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5906 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5907 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5908 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
5909 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
5912 70f17a53 2022-06-13 thomas tree_scroll_down(s, nscroll);
5914 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5916 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5917 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5918 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5919 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5920 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5922 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5923 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5925 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5926 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5927 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5928 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5929 1e37a5c2 2019-05-12 jcs s->selected_entry =
5930 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5931 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5932 1e37a5c2 2019-05-12 jcs free(parent);
5933 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5934 56e0773d 2019-11-28 stsp s->selected_entry))) {
5935 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5936 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5937 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5940 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5941 941e9f74 2019-05-21 stsp if (err) {
5942 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5945 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5946 56e0773d 2019-11-28 stsp s->selected_entry))) {
5947 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5948 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5949 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5951 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5952 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5953 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5956 e78dc838 2020-12-04 stsp view->focussed = 0;
5957 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5958 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5959 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5961 669b5ffa 2018-10-07 stsp return err;
5962 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5963 e78dc838 2020-12-04 stsp view->focus_child = 1;
5965 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5968 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5969 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5970 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5976 ffd1d5e5 2018-06-23 stsp return err;
5979 ffd1d5e5 2018-06-23 stsp __dead static void
5980 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5983 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5984 ffd1d5e5 2018-06-23 stsp getprogname());
5988 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5989 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5991 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5992 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5993 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5994 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5995 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5996 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5997 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5998 4e97c21c 2020-12-06 stsp char *label = NULL;
5999 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6000 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6002 5221c383 2018-08-01 stsp struct tog_view *view;
6003 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6005 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6006 ffd1d5e5 2018-06-23 stsp switch (ch) {
6008 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6011 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6012 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6013 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6017 e99e2d15 2020-11-24 naddy usage_tree();
6018 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6022 ffd1d5e5 2018-06-23 stsp argc -= optind;
6023 ffd1d5e5 2018-06-23 stsp argv += optind;
6025 55cccc34 2020-02-20 stsp if (argc > 1)
6026 e99e2d15 2020-11-24 naddy usage_tree();
6028 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6029 7cd52833 2022-06-23 thomas if (error != NULL)
6030 7cd52833 2022-06-23 thomas goto done;
6032 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6033 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6034 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6035 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6036 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6037 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6038 c156c7a4 2020-12-18 stsp goto done;
6039 55cccc34 2020-02-20 stsp if (worktree)
6040 52185f70 2019-02-05 stsp repo_path =
6041 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6043 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6044 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6045 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6046 c156c7a4 2020-12-18 stsp goto done;
6050 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6051 c02c541e 2019-03-29 stsp if (error != NULL)
6052 52185f70 2019-02-05 stsp goto done;
6054 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6055 55cccc34 2020-02-20 stsp repo, worktree);
6056 55cccc34 2020-02-20 stsp if (error)
6057 55cccc34 2020-02-20 stsp goto done;
6059 55cccc34 2020-02-20 stsp init_curses();
6061 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6062 c02c541e 2019-03-29 stsp if (error)
6063 52185f70 2019-02-05 stsp goto done;
6065 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6066 51a10b52 2020-12-26 stsp if (error)
6067 51a10b52 2020-12-26 stsp goto done;
6069 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6070 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6071 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6072 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6073 4e97c21c 2020-12-06 stsp if (error)
6074 4e97c21c 2020-12-06 stsp goto done;
6075 4e97c21c 2020-12-06 stsp head_ref_name = label;
6077 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6078 4e97c21c 2020-12-06 stsp if (error == NULL)
6079 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6080 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6081 4e97c21c 2020-12-06 stsp goto done;
6082 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6083 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6084 4e97c21c 2020-12-06 stsp if (error)
6085 4e97c21c 2020-12-06 stsp goto done;
6088 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6089 945f9229 2022-04-16 thomas if (error)
6090 945f9229 2022-04-16 thomas goto done;
6092 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6093 5221c383 2018-08-01 stsp if (view == NULL) {
6094 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6095 5221c383 2018-08-01 stsp goto done;
6097 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6098 ad80ab7b 2018-08-04 stsp if (error)
6099 ad80ab7b 2018-08-04 stsp goto done;
6100 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6101 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
6102 d91faf3b 2020-12-01 naddy in_repo_path);
6103 55cccc34 2020-02-20 stsp if (error)
6104 55cccc34 2020-02-20 stsp goto done;
6107 55cccc34 2020-02-20 stsp if (worktree) {
6108 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6109 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6110 55cccc34 2020-02-20 stsp worktree = NULL;
6112 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6114 52185f70 2019-02-05 stsp free(repo_path);
6115 e4a0e26d 2020-02-20 stsp free(cwd);
6116 ffd1d5e5 2018-06-23 stsp free(commit_id);
6117 4e97c21c 2020-12-06 stsp free(label);
6119 486cd271 2020-12-06 stsp got_ref_close(ref);
6120 1d0f4054 2021-06-17 stsp if (repo) {
6121 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6122 1d0f4054 2021-06-17 stsp if (error == NULL)
6123 1d0f4054 2021-06-17 stsp error = close_err;
6125 7cd52833 2022-06-23 thomas if (pack_fds) {
6126 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6127 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6128 7cd52833 2022-06-23 thomas if (error == NULL)
6129 7cd52833 2022-06-23 thomas error = pack_err;
6131 51a10b52 2020-12-26 stsp tog_free_refs();
6132 ffd1d5e5 2018-06-23 stsp return error;
6135 6458efa5 2020-11-24 stsp static const struct got_error *
6136 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6138 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6139 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6141 6458efa5 2020-11-24 stsp s->nrefs = 0;
6142 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6143 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
6144 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
6145 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
6146 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
6149 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6150 6458efa5 2020-11-24 stsp if (re == NULL)
6151 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6153 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6154 8924d611 2020-12-26 stsp if (re->ref == NULL)
6155 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6156 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6157 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6160 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6161 6458efa5 2020-11-24 stsp return NULL;
6165 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6167 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6169 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6170 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6171 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6172 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6177 6458efa5 2020-11-24 stsp static const struct got_error *
6178 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6180 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6181 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6183 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6184 6458efa5 2020-11-24 stsp s->repo = repo;
6186 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6187 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6189 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6191 6458efa5 2020-11-24 stsp return err;
6193 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6194 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6195 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6196 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6198 6458efa5 2020-11-24 stsp goto done;
6200 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6201 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6202 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6204 6458efa5 2020-11-24 stsp goto done;
6206 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6207 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6208 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6209 2183bbf6 2022-01-23 thomas if (err)
6210 2183bbf6 2022-01-23 thomas goto done;
6212 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
6213 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
6214 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
6216 6458efa5 2020-11-24 stsp goto done;
6219 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6220 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6221 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6222 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6223 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6226 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6227 6458efa5 2020-11-24 stsp return err;
6230 6458efa5 2020-11-24 stsp static const struct got_error *
6231 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6233 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6235 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6236 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6238 6458efa5 2020-11-24 stsp return NULL;
6241 6458efa5 2020-11-24 stsp static const struct got_error *
6242 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6243 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6245 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6246 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6247 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6248 6458efa5 2020-11-24 stsp int obj_type;
6250 c42c9805 2020-11-24 stsp *commit_id = NULL;
6252 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6254 6458efa5 2020-11-24 stsp return err;
6256 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6258 6458efa5 2020-11-24 stsp goto done;
6260 6458efa5 2020-11-24 stsp switch (obj_type) {
6261 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6262 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6264 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6265 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6267 6458efa5 2020-11-24 stsp goto done;
6268 c42c9805 2020-11-24 stsp free(obj_id);
6269 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6270 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6272 6458efa5 2020-11-24 stsp goto done;
6273 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6274 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6275 c42c9805 2020-11-24 stsp goto done;
6277 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6278 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6279 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6280 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6281 c42c9805 2020-11-24 stsp goto done;
6285 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6291 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6292 c42c9805 2020-11-24 stsp if (err) {
6293 c42c9805 2020-11-24 stsp free(*commit_id);
6294 c42c9805 2020-11-24 stsp *commit_id = NULL;
6296 c42c9805 2020-11-24 stsp return err;
6299 c42c9805 2020-11-24 stsp static const struct got_error *
6300 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6301 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6303 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6304 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6305 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6307 c42c9805 2020-11-24 stsp *new_view = NULL;
6309 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6310 c42c9805 2020-11-24 stsp if (err) {
6311 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6312 c42c9805 2020-11-24 stsp return err;
6314 c42c9805 2020-11-24 stsp return NULL;
6317 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6318 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6319 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6320 6458efa5 2020-11-24 stsp goto done;
6323 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6324 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6327 6458efa5 2020-11-24 stsp view_close(log_view);
6329 6458efa5 2020-11-24 stsp *new_view = log_view;
6330 c42c9805 2020-11-24 stsp free(commit_id);
6331 6458efa5 2020-11-24 stsp return err;
6334 ce5b7c56 2019-07-09 stsp static void
6335 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6337 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6338 34ba6917 2020-11-30 stsp int i = 0;
6340 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6343 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6344 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6345 34ba6917 2020-11-30 stsp if (re == NULL)
6347 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6348 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6352 34ba6917 2020-11-30 stsp static void
6353 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6355 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6356 6458efa5 2020-11-24 stsp int n = 0;
6358 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6359 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6361 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6363 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6364 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6365 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6366 6458efa5 2020-11-24 stsp if (last) {
6367 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6368 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6373 6458efa5 2020-11-24 stsp static const struct got_error *
6374 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6376 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6378 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6379 6458efa5 2020-11-24 stsp return NULL;
6382 6458efa5 2020-11-24 stsp static int
6383 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6385 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6387 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6391 6458efa5 2020-11-24 stsp static const struct got_error *
6392 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6394 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6395 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6397 6458efa5 2020-11-24 stsp if (!view->searching) {
6398 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6399 6458efa5 2020-11-24 stsp return NULL;
6402 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6403 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6404 6458efa5 2020-11-24 stsp if (s->selected_entry)
6405 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6407 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6408 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6410 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6411 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6413 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6414 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6417 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6418 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6419 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6420 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6422 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6425 6458efa5 2020-11-24 stsp while (1) {
6426 6458efa5 2020-11-24 stsp if (re == NULL) {
6427 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6428 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6429 6458efa5 2020-11-24 stsp return NULL;
6431 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6432 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6434 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6437 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6438 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6439 6458efa5 2020-11-24 stsp s->matched_entry = re;
6443 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6444 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6446 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6449 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6450 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6451 6458efa5 2020-11-24 stsp s->selected = 0;
6454 6458efa5 2020-11-24 stsp return NULL;
6457 6458efa5 2020-11-24 stsp static const struct got_error *
6458 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6460 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6461 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6462 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6463 6458efa5 2020-11-24 stsp char *line = NULL;
6464 6458efa5 2020-11-24 stsp wchar_t *wline;
6465 6458efa5 2020-11-24 stsp struct tog_color *tc;
6466 6458efa5 2020-11-24 stsp int width, n;
6467 6458efa5 2020-11-24 stsp int limit = view->nlines;
6469 6458efa5 2020-11-24 stsp werase(view->window);
6471 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6473 6458efa5 2020-11-24 stsp if (limit == 0)
6474 6458efa5 2020-11-24 stsp return NULL;
6476 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6478 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6479 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6480 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6482 05171be4 2022-06-23 thomas err = format_line(&wline, &width, line, view->ncols, 0, 0);
6483 6458efa5 2020-11-24 stsp if (err) {
6484 6458efa5 2020-11-24 stsp free(line);
6485 6458efa5 2020-11-24 stsp return err;
6487 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6488 6458efa5 2020-11-24 stsp wstandout(view->window);
6489 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6490 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6491 6458efa5 2020-11-24 stsp wstandend(view->window);
6492 6458efa5 2020-11-24 stsp free(wline);
6493 6458efa5 2020-11-24 stsp wline = NULL;
6494 6458efa5 2020-11-24 stsp free(line);
6495 6458efa5 2020-11-24 stsp line = NULL;
6496 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6497 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6498 6458efa5 2020-11-24 stsp if (--limit <= 0)
6499 6458efa5 2020-11-24 stsp return NULL;
6502 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6503 6458efa5 2020-11-24 stsp char *line = NULL;
6504 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6506 84227eb1 2022-06-23 thomas if (s->show_date) {
6507 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
6508 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
6509 84227eb1 2022-06-23 thomas struct got_object_id *id;
6510 84227eb1 2022-06-23 thomas struct tm tm;
6511 84227eb1 2022-06-23 thomas time_t t;
6513 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
6514 84227eb1 2022-06-23 thomas if (err)
6515 84227eb1 2022-06-23 thomas return err;
6516 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
6517 84227eb1 2022-06-23 thomas if (err) {
6518 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
6519 84227eb1 2022-06-23 thomas free(id);
6520 84227eb1 2022-06-23 thomas return err;
6522 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
6524 84227eb1 2022-06-23 thomas if (err) {
6525 84227eb1 2022-06-23 thomas free(id);
6526 84227eb1 2022-06-23 thomas return err;
6528 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
6529 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
6530 84227eb1 2022-06-23 thomas } else {
6531 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
6532 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
6534 84227eb1 2022-06-23 thomas free(id);
6535 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
6536 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
6537 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6538 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
6540 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6541 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
6542 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
6543 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6544 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6545 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6546 6458efa5 2020-11-24 stsp struct got_object_id *id;
6547 6458efa5 2020-11-24 stsp char *id_str;
6548 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6550 6458efa5 2020-11-24 stsp return err;
6551 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6552 6458efa5 2020-11-24 stsp if (err) {
6554 6458efa5 2020-11-24 stsp return err;
6556 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6557 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6558 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6560 6458efa5 2020-11-24 stsp free(id_str);
6561 6458efa5 2020-11-24 stsp return err;
6564 6458efa5 2020-11-24 stsp free(id_str);
6565 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6566 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
6567 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
6569 05171be4 2022-06-23 thomas err = format_line(&wline, &width, line, view->ncols, 0, 0);
6570 6458efa5 2020-11-24 stsp if (err) {
6571 6458efa5 2020-11-24 stsp free(line);
6572 6458efa5 2020-11-24 stsp return err;
6574 6458efa5 2020-11-24 stsp if (n == s->selected) {
6575 6458efa5 2020-11-24 stsp if (view->focussed)
6576 6458efa5 2020-11-24 stsp wstandout(view->window);
6577 6458efa5 2020-11-24 stsp s->selected_entry = re;
6579 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6581 6458efa5 2020-11-24 stsp wattr_on(view->window,
6582 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6583 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6585 6458efa5 2020-11-24 stsp wattr_off(view->window,
6586 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6587 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6588 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6589 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6590 6458efa5 2020-11-24 stsp wstandend(view->window);
6591 6458efa5 2020-11-24 stsp free(line);
6592 6458efa5 2020-11-24 stsp free(wline);
6593 6458efa5 2020-11-24 stsp wline = NULL;
6595 6458efa5 2020-11-24 stsp s->ndisplayed++;
6596 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6599 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6602 6458efa5 2020-11-24 stsp view_vborder(view);
6603 6458efa5 2020-11-24 stsp return err;
6606 6458efa5 2020-11-24 stsp static const struct got_error *
6607 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6608 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6610 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6611 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6612 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6614 c42c9805 2020-11-24 stsp *new_view = NULL;
6616 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6617 c42c9805 2020-11-24 stsp if (err) {
6618 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6619 c42c9805 2020-11-24 stsp return err;
6621 c42c9805 2020-11-24 stsp return NULL;
6625 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6626 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6627 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6628 c42c9805 2020-11-24 stsp goto done;
6631 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6632 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6634 c42c9805 2020-11-24 stsp goto done;
6636 c42c9805 2020-11-24 stsp *new_view = tree_view;
6638 c42c9805 2020-11-24 stsp free(commit_id);
6639 c42c9805 2020-11-24 stsp return err;
6641 c42c9805 2020-11-24 stsp static const struct got_error *
6642 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6644 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6645 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6646 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6647 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6648 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
6650 6458efa5 2020-11-24 stsp switch (ch) {
6652 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6654 84227eb1 2022-06-23 thomas case 'm':
6655 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
6657 98182bd0 2021-11-20 thomas case 'o':
6658 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6659 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6660 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6661 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6662 c591440f 2021-11-20 thomas if (err)
6664 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6665 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6666 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6667 3bfadbd4 2021-11-20 thomas if (err)
6669 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6670 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6672 6458efa5 2020-11-24 stsp case KEY_ENTER:
6673 6458efa5 2020-11-24 stsp case '\r':
6674 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6676 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6677 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6678 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6680 e78dc838 2020-12-04 stsp view->focussed = 0;
6681 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6682 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6683 6458efa5 2020-11-24 stsp err = view_close_child(view);
6685 6458efa5 2020-11-24 stsp return err;
6686 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6687 e78dc838 2020-12-04 stsp view->focus_child = 1;
6689 6458efa5 2020-11-24 stsp *new_view = log_view;
6692 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6694 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6695 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6696 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6698 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6700 e78dc838 2020-12-04 stsp view->focussed = 0;
6701 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6702 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6703 c42c9805 2020-11-24 stsp err = view_close_child(view);
6705 c42c9805 2020-11-24 stsp return err;
6706 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6707 e78dc838 2020-12-04 stsp view->focus_child = 1;
6709 c42c9805 2020-11-24 stsp *new_view = tree_view;
6711 e4526bf5 2021-09-03 naddy case 'g':
6712 e4526bf5 2021-09-03 naddy case KEY_HOME:
6713 e4526bf5 2021-09-03 naddy s->selected = 0;
6714 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6716 e4526bf5 2021-09-03 naddy case 'G':
6717 e4526bf5 2021-09-03 naddy case KEY_END:
6718 e4526bf5 2021-09-03 naddy s->selected = 0;
6719 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6720 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6721 e4526bf5 2021-09-03 naddy if (re == NULL)
6723 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6724 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6726 e4526bf5 2021-09-03 naddy if (n > 0)
6727 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6730 6458efa5 2020-11-24 stsp case KEY_UP:
6731 f7140bf5 2021-10-17 thomas case CTRL('p'):
6732 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6733 6458efa5 2020-11-24 stsp s->selected--;
6736 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6738 70f17a53 2022-06-13 thomas case CTRL('u'):
6739 23427b14 2022-06-23 thomas case 'u':
6740 70f17a53 2022-06-13 thomas nscroll /= 2;
6741 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6742 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6743 6458efa5 2020-11-24 stsp case CTRL('b'):
6744 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6745 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
6746 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
6749 6458efa5 2020-11-24 stsp case KEY_DOWN:
6750 f7140bf5 2021-10-17 thomas case CTRL('n'):
6751 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6752 6458efa5 2020-11-24 stsp s->selected++;
6755 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6756 6458efa5 2020-11-24 stsp /* can't scroll any further */
6758 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6760 70f17a53 2022-06-13 thomas case CTRL('d'):
6761 23427b14 2022-06-23 thomas case 'd':
6762 70f17a53 2022-06-13 thomas nscroll /= 2;
6763 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6764 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6765 6458efa5 2020-11-24 stsp case CTRL('f'):
6766 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6767 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6768 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6769 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6770 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6773 70f17a53 2022-06-13 thomas ref_scroll_down(s, nscroll);
6775 6458efa5 2020-11-24 stsp case CTRL('l'):
6776 8924d611 2020-12-26 stsp tog_free_refs();
6777 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6780 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6781 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6783 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6784 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6785 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6791 6458efa5 2020-11-24 stsp return err;
6794 6458efa5 2020-11-24 stsp __dead static void
6795 6458efa5 2020-11-24 stsp usage_ref(void)
6798 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6799 6458efa5 2020-11-24 stsp getprogname());
6803 6458efa5 2020-11-24 stsp static const struct got_error *
6804 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6806 6458efa5 2020-11-24 stsp const struct got_error *error;
6807 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6808 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6809 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6811 6458efa5 2020-11-24 stsp struct tog_view *view;
6812 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6814 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6815 6458efa5 2020-11-24 stsp switch (ch) {
6817 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6818 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6819 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6823 e99e2d15 2020-11-24 naddy usage_ref();
6824 6458efa5 2020-11-24 stsp /* NOTREACHED */
6828 6458efa5 2020-11-24 stsp argc -= optind;
6829 6458efa5 2020-11-24 stsp argv += optind;
6831 6458efa5 2020-11-24 stsp if (argc > 1)
6832 e99e2d15 2020-11-24 naddy usage_ref();
6834 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6835 7cd52833 2022-06-23 thomas if (error != NULL)
6836 7cd52833 2022-06-23 thomas goto done;
6838 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6839 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6840 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6841 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6842 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6843 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6844 c156c7a4 2020-12-18 stsp goto done;
6845 6458efa5 2020-11-24 stsp if (worktree)
6846 6458efa5 2020-11-24 stsp repo_path =
6847 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6849 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6850 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6851 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6852 c156c7a4 2020-12-18 stsp goto done;
6856 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6857 6458efa5 2020-11-24 stsp if (error != NULL)
6858 6458efa5 2020-11-24 stsp goto done;
6860 6458efa5 2020-11-24 stsp init_curses();
6862 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6863 51a10b52 2020-12-26 stsp if (error)
6864 51a10b52 2020-12-26 stsp goto done;
6866 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6867 6458efa5 2020-11-24 stsp if (error)
6868 6458efa5 2020-11-24 stsp goto done;
6870 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6871 6458efa5 2020-11-24 stsp if (view == NULL) {
6872 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6873 6458efa5 2020-11-24 stsp goto done;
6876 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6877 6458efa5 2020-11-24 stsp if (error)
6878 6458efa5 2020-11-24 stsp goto done;
6880 6458efa5 2020-11-24 stsp if (worktree) {
6881 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6882 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6883 6458efa5 2020-11-24 stsp worktree = NULL;
6885 6458efa5 2020-11-24 stsp error = view_loop(view);
6887 6458efa5 2020-11-24 stsp free(repo_path);
6888 6458efa5 2020-11-24 stsp free(cwd);
6889 1d0f4054 2021-06-17 stsp if (repo) {
6890 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6891 1d0f4054 2021-06-17 stsp if (close_err)
6892 1d0f4054 2021-06-17 stsp error = close_err;
6894 7cd52833 2022-06-23 thomas if (pack_fds) {
6895 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6896 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6897 7cd52833 2022-06-23 thomas if (error == NULL)
6898 7cd52833 2022-06-23 thomas error = pack_err;
6900 51a10b52 2020-12-26 stsp tog_free_refs();
6901 6458efa5 2020-11-24 stsp return error;
6904 6458efa5 2020-11-24 stsp static void
6905 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6909 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6910 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6911 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6912 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6914 6879ba42 2020-10-01 naddy fputc('\n', fp);
6917 4ed7e80c 2018-05-20 stsp __dead static void
6918 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6920 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6922 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6923 6879ba42 2020-10-01 naddy getprogname());
6924 6879ba42 2020-10-01 naddy if (hflag) {
6925 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6926 6879ba42 2020-10-01 naddy list_commands(fp);
6928 6879ba42 2020-10-01 naddy exit(status);
6931 c2301be8 2018-04-30 stsp static char **
6932 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6934 ee85c5e8 2020-02-29 stsp va_list ap;
6935 c2301be8 2018-04-30 stsp char **argv;
6938 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6940 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6941 c2301be8 2018-04-30 stsp if (argv == NULL)
6942 c2301be8 2018-04-30 stsp err(1, "calloc");
6943 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6944 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6945 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6946 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6949 ee85c5e8 2020-02-29 stsp va_end(ap);
6950 c2301be8 2018-04-30 stsp return argv;
6954 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6955 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6956 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6957 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6958 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6960 ee85c5e8 2020-02-29 stsp static const struct got_error *
6961 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6963 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6964 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6965 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6966 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6967 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6968 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6969 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6970 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6971 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6973 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6974 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6975 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6977 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6978 7cd52833 2022-06-23 thomas if (error != NULL)
6979 7cd52833 2022-06-23 thomas goto done;
6981 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6982 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6983 ee85c5e8 2020-02-29 stsp goto done;
6985 ee85c5e8 2020-02-29 stsp if (worktree)
6986 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6988 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6989 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6990 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6991 ee85c5e8 2020-02-29 stsp goto done;
6994 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6995 ee85c5e8 2020-02-29 stsp if (error != NULL)
6996 ee85c5e8 2020-02-29 stsp goto done;
6998 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6999 ee85c5e8 2020-02-29 stsp repo, worktree);
7000 ee85c5e8 2020-02-29 stsp if (error)
7001 ee85c5e8 2020-02-29 stsp goto done;
7003 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7004 84de9106 2020-12-26 stsp if (error)
7005 84de9106 2020-12-26 stsp goto done;
7006 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7007 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7008 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7009 ee85c5e8 2020-02-29 stsp if (error)
7010 ee85c5e8 2020-02-29 stsp goto done;
7012 ee85c5e8 2020-02-29 stsp if (worktree) {
7013 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7014 ee85c5e8 2020-02-29 stsp worktree = NULL;
7017 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7018 945f9229 2022-04-16 thomas if (error)
7019 945f9229 2022-04-16 thomas goto done;
7021 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7022 ee85c5e8 2020-02-29 stsp if (error) {
7023 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7024 ee85c5e8 2020-02-29 stsp goto done;
7025 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7026 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7027 6879ba42 2020-10-01 naddy usage(1, 1);
7028 ee85c5e8 2020-02-29 stsp /* not reached */
7031 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7032 1d0f4054 2021-06-17 stsp if (error == NULL)
7033 1d0f4054 2021-06-17 stsp error = close_err;
7034 ee85c5e8 2020-02-29 stsp repo = NULL;
7036 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7037 ee85c5e8 2020-02-29 stsp if (error)
7038 ee85c5e8 2020-02-29 stsp goto done;
7040 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7042 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7043 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7045 1d0f4054 2021-06-17 stsp if (repo) {
7046 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7047 1d0f4054 2021-06-17 stsp if (error == NULL)
7048 1d0f4054 2021-06-17 stsp error = close_err;
7050 945f9229 2022-04-16 thomas if (commit)
7051 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7052 ee85c5e8 2020-02-29 stsp if (worktree)
7053 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7054 7cd52833 2022-06-23 thomas if (pack_fds) {
7055 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7056 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7057 7cd52833 2022-06-23 thomas if (error == NULL)
7058 7cd52833 2022-06-23 thomas error = pack_err;
7061 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7062 ee85c5e8 2020-02-29 stsp free(commit_id);
7063 ee85c5e8 2020-02-29 stsp free(cwd);
7064 ee85c5e8 2020-02-29 stsp free(repo_path);
7065 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7066 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7068 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7069 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7070 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7072 87670572 2020-12-26 naddy tog_free_refs();
7073 ee85c5e8 2020-02-29 stsp return error;
7077 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7079 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7080 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
7081 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7082 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7083 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
7084 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7085 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7088 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7090 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7091 9f7d7167 2018-04-29 stsp switch (ch) {
7093 9f7d7167 2018-04-29 stsp hflag = 1;
7096 53ccebc2 2019-07-30 stsp Vflag = 1;
7099 6879ba42 2020-10-01 naddy usage(hflag, 1);
7100 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7104 9f7d7167 2018-04-29 stsp argc -= optind;
7105 9f7d7167 2018-04-29 stsp argv += optind;
7106 9814e6a3 2020-09-27 naddy optind = 1;
7107 c2301be8 2018-04-30 stsp optreset = 1;
7109 53ccebc2 2019-07-30 stsp if (Vflag) {
7110 53ccebc2 2019-07-30 stsp got_version_print_str();
7111 6879ba42 2020-10-01 naddy return 0;
7114 4010e238 2020-12-04 stsp #ifndef PROFILE
7115 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7116 4010e238 2020-12-04 stsp NULL) == -1)
7117 4010e238 2020-12-04 stsp err(1, "pledge");
7120 c2301be8 2018-04-30 stsp if (argc == 0) {
7121 f29d3e89 2018-06-23 stsp if (hflag)
7122 6879ba42 2020-10-01 naddy usage(hflag, 0);
7123 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7124 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7126 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7130 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7131 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7132 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7133 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7134 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7140 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7141 ee85c5e8 2020-02-29 stsp if (argc != 1)
7142 6879ba42 2020-10-01 naddy usage(0, 1);
7143 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7144 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7146 ee85c5e8 2020-02-29 stsp if (hflag)
7147 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7149 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7153 b46c1e04 2020-09-20 naddy putchar('\n');
7154 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7156 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7157 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7158 a2f4a359 2020-02-28 stsp free(cmd_argv);
7161 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7162 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);