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 3bfadbd4 2021-11-20 thomas int nrefs, ndisplayed, selected, 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 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
511 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
513 669b5ffa 2018-10-07 stsp struct tog_view *parent;
514 669b5ffa 2018-10-07 stsp struct tog_view *child;
517 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
518 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
519 e78dc838 2020-12-04 stsp * between parent and child.
520 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
521 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
522 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
523 e78dc838 2020-12-04 stsp * situations.
525 e78dc838 2020-12-04 stsp int focus_child;
527 5dc9f4bc 2018-08-04 stsp /* type-specific state */
528 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
530 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
531 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
532 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
533 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
534 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
537 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
538 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
539 e78dc838 2020-12-04 stsp struct tog_view *, int);
540 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
542 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
543 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
544 c0c4acc8 2021-01-24 stsp int search_started;
545 60493ae3 2019-06-20 stsp int searching;
546 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
547 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
548 60493ae3 2019-06-20 stsp int search_next_done;
549 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
550 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
551 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
552 1803e47f 2019-06-22 stsp regex_t regex;
553 41605754 2020-11-12 stsp regmatch_t regmatch;
556 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
557 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
558 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
559 78756c87 2020-11-24 stsp struct got_repository *);
560 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
561 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
562 e78dc838 2020-12-04 stsp struct tog_view *, int);
563 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
564 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
565 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
567 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
568 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
569 78756c87 2020-11-24 stsp const char *, const char *, int);
570 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
572 e78dc838 2020-12-04 stsp struct tog_view *, int);
573 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
574 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
575 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
577 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
578 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
579 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
583 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
584 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
586 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
587 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
588 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
592 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
593 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
595 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
596 6458efa5 2020-11-24 stsp struct got_repository *);
597 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
598 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
599 e78dc838 2020-12-04 stsp struct tog_view *, int);
600 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
601 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
602 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
604 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
605 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
606 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
607 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
608 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
610 25791caa 2018-10-24 stsp static void
611 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
613 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
616 83baff54 2019-08-12 stsp static void
617 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
619 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
622 61266923 2020-01-14 stsp static void
623 61266923 2020-01-14 stsp tog_sigcont(int signo)
625 61266923 2020-01-14 stsp tog_sigcont_received = 1;
628 296152d1 2022-05-31 thomas static void
629 296152d1 2022-05-31 thomas tog_sigint(int signo)
631 296152d1 2022-05-31 thomas tog_sigint_received = 1;
634 296152d1 2022-05-31 thomas static void
635 296152d1 2022-05-31 thomas tog_sigterm(int signo)
637 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
640 296152d1 2022-05-31 thomas static int
641 296152d1 2022-05-31 thomas tog_fatal_signal_received()
643 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
644 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
648 e5a0f69f 2018-08-18 stsp static const struct got_error *
649 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
651 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
653 669b5ffa 2018-10-07 stsp if (view->child) {
654 669b5ffa 2018-10-07 stsp view_close(view->child);
655 669b5ffa 2018-10-07 stsp view->child = NULL;
657 e5a0f69f 2018-08-18 stsp if (view->close)
658 e5a0f69f 2018-08-18 stsp err = view->close(view);
659 ea5e7bb5 2018-08-01 stsp if (view->panel)
660 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
661 ea5e7bb5 2018-08-01 stsp if (view->window)
662 ea5e7bb5 2018-08-01 stsp delwin(view->window);
663 ea5e7bb5 2018-08-01 stsp free(view);
664 e5a0f69f 2018-08-18 stsp return err;
667 ea5e7bb5 2018-08-01 stsp static struct tog_view *
668 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
669 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
671 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
673 ea5e7bb5 2018-08-01 stsp if (view == NULL)
674 ea5e7bb5 2018-08-01 stsp return NULL;
676 d6b05b5b 2018-08-04 stsp view->type = type;
677 f7d12f7e 2018-08-01 stsp view->lines = LINES;
678 f7d12f7e 2018-08-01 stsp view->cols = COLS;
679 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
680 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
681 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
682 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
683 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
684 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
685 96a765a8 2018-08-04 stsp view_close(view);
686 ea5e7bb5 2018-08-01 stsp return NULL;
688 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
689 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
690 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
691 96a765a8 2018-08-04 stsp view_close(view);
692 ea5e7bb5 2018-08-01 stsp return NULL;
695 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
696 ea5e7bb5 2018-08-01 stsp return view;
700 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
702 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
704 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
707 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
709 5c60c32a 2018-10-18 stsp static const struct got_error *
710 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
712 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
714 5c60c32a 2018-10-18 stsp view->begin_y = 0;
715 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
716 5c60c32a 2018-10-18 stsp view->nlines = LINES;
717 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
718 5c60c32a 2018-10-18 stsp view->lines = LINES;
719 5c60c32a 2018-10-18 stsp view->cols = COLS;
720 5c60c32a 2018-10-18 stsp err = view_resize(view);
722 5c60c32a 2018-10-18 stsp return err;
724 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
725 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
727 5c60c32a 2018-10-18 stsp return NULL;
730 5c60c32a 2018-10-18 stsp static const struct got_error *
731 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
733 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
735 5c60c32a 2018-10-18 stsp view->begin_x = 0;
736 5c60c32a 2018-10-18 stsp view->begin_y = 0;
737 5c60c32a 2018-10-18 stsp view->nlines = LINES;
738 5c60c32a 2018-10-18 stsp view->ncols = COLS;
739 5c60c32a 2018-10-18 stsp view->lines = LINES;
740 5c60c32a 2018-10-18 stsp view->cols = COLS;
741 5c60c32a 2018-10-18 stsp err = view_resize(view);
743 5c60c32a 2018-10-18 stsp return err;
745 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
746 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
748 5c60c32a 2018-10-18 stsp return NULL;
752 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
754 5c60c32a 2018-10-18 stsp return view->parent == NULL;
757 4d8c2215 2018-08-19 stsp static const struct got_error *
758 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
760 f7d12f7e 2018-08-01 stsp int nlines, ncols;
762 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
763 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
765 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
767 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
768 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
770 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
772 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
773 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
774 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
775 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
776 25791caa 2018-10-24 stsp wclear(view->window);
778 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
779 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
780 0cf4efb1 2018-09-29 stsp view->lines = LINES;
781 0cf4efb1 2018-09-29 stsp view->cols = COLS;
783 6e3e5d9c 2018-10-18 stsp if (view->child) {
784 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
785 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
786 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
787 5c60c32a 2018-10-18 stsp if (view->child->focussed)
788 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
790 5c60c32a 2018-10-18 stsp show_panel(view->panel);
792 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
793 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
797 5c60c32a 2018-10-18 stsp return NULL;
800 669b5ffa 2018-10-07 stsp static const struct got_error *
801 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
803 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
805 669b5ffa 2018-10-07 stsp if (view->child == NULL)
806 669b5ffa 2018-10-07 stsp return NULL;
808 669b5ffa 2018-10-07 stsp err = view_close(view->child);
809 669b5ffa 2018-10-07 stsp view->child = NULL;
810 669b5ffa 2018-10-07 stsp return err;
813 72a9cb46 2020-12-03 stsp static void
814 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
816 669b5ffa 2018-10-07 stsp view->child = child;
817 669b5ffa 2018-10-07 stsp child->parent = view;
821 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
823 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
826 34bc9ec9 2019-02-22 stsp static void
827 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
829 25791caa 2018-10-24 stsp int cols, lines;
830 25791caa 2018-10-24 stsp struct winsize size;
832 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
833 25791caa 2018-10-24 stsp cols = 80; /* Default */
834 25791caa 2018-10-24 stsp lines = 24;
836 25791caa 2018-10-24 stsp cols = size.ws_col;
837 25791caa 2018-10-24 stsp lines = size.ws_row;
839 25791caa 2018-10-24 stsp resize_term(lines, cols);
842 2b49a8ae 2019-06-22 stsp static const struct got_error *
843 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
845 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
846 2b49a8ae 2019-06-22 stsp char pattern[1024];
849 c0c4acc8 2021-01-24 stsp if (view->search_started) {
850 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
851 c0c4acc8 2021-01-24 stsp view->searching = 0;
852 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
854 c0c4acc8 2021-01-24 stsp view->search_started = 0;
856 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
857 2b49a8ae 2019-06-22 stsp return NULL;
859 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
860 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
862 2b49a8ae 2019-06-22 stsp nocbreak();
864 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
867 2b49a8ae 2019-06-22 stsp if (ret == ERR)
868 2b49a8ae 2019-06-22 stsp return NULL;
870 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
871 7c32bd05 2019-06-22 stsp err = view->search_start(view);
873 7c32bd05 2019-06-22 stsp regfree(&view->regex);
874 7c32bd05 2019-06-22 stsp return err;
876 c0c4acc8 2021-01-24 stsp view->search_started = 1;
877 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
878 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
879 2b49a8ae 2019-06-22 stsp view->search_next(view);
882 2b49a8ae 2019-06-22 stsp return NULL;
885 0cf4efb1 2018-09-29 stsp static const struct got_error *
886 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
887 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
889 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
890 669b5ffa 2018-10-07 stsp struct tog_view *v;
891 1a76625f 2018-10-22 stsp int ch, errcode;
893 e5a0f69f 2018-08-18 stsp *new = NULL;
895 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
896 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
897 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
898 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
900 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
901 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
902 82954512 2020-02-03 stsp if (errcode)
903 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
904 82954512 2020-02-03 stsp "pthread_mutex_unlock");
905 3da8ef85 2021-09-21 stsp sched_yield();
906 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
907 82954512 2020-02-03 stsp if (errcode)
908 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
909 82954512 2020-02-03 stsp "pthread_mutex_lock");
910 60493ae3 2019-06-20 stsp view->search_next(view);
911 60493ae3 2019-06-20 stsp return NULL;
914 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
915 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
916 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
917 1a76625f 2018-10-22 stsp if (errcode)
918 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
919 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
920 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
921 1a76625f 2018-10-22 stsp if (errcode)
922 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
923 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
925 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
926 25791caa 2018-10-24 stsp tog_resizeterm();
927 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
928 61266923 2020-01-14 stsp tog_sigcont_received = 0;
929 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
930 25791caa 2018-10-24 stsp err = view_resize(v);
932 25791caa 2018-10-24 stsp return err;
933 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
935 25791caa 2018-10-24 stsp return err;
936 cdfcfb03 2020-12-06 stsp if (v->child) {
937 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
939 cdfcfb03 2020-12-06 stsp return err;
940 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
941 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
943 cdfcfb03 2020-12-06 stsp return err;
948 e5a0f69f 2018-08-18 stsp switch (ch) {
950 1e37a5c2 2019-05-12 jcs if (view->child) {
951 e78dc838 2020-12-04 stsp view->focussed = 0;
952 e78dc838 2020-12-04 stsp view->child->focussed = 1;
953 e78dc838 2020-12-04 stsp view->focus_child = 1;
954 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
955 e78dc838 2020-12-04 stsp view->focussed = 0;
956 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
957 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
961 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
962 9970f7fc 2020-12-03 stsp view->dying = 1;
968 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
969 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
971 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
972 e78dc838 2020-12-04 stsp view->focussed = 0;
973 e78dc838 2020-12-04 stsp view->child->focussed = 1;
974 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
976 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
979 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
980 9970f7fc 2020-12-03 stsp KEY_RESIZE);
982 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
983 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
984 e78dc838 2020-12-04 stsp view->focussed = 1;
985 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
987 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
991 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
994 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
997 60493ae3 2019-06-20 stsp if (view->search_start)
998 2b49a8ae 2019-06-22 stsp view_search_start(view);
1000 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1004 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1005 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1006 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1007 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1008 60493ae3 2019-06-20 stsp view->search_next(view);
1010 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1013 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1017 e5a0f69f 2018-08-18 stsp return err;
1021 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1023 0cf4efb1 2018-09-29 stsp PANEL *panel;
1024 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1026 669b5ffa 2018-10-07 stsp if (view->parent)
1027 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1029 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1030 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1033 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1034 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1035 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1039 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1041 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1042 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1044 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1046 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1049 669b5ffa 2018-10-07 stsp return view->focussed;
1052 bcbd79e2 2018-08-19 stsp static const struct got_error *
1053 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1055 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1056 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1057 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1058 fd823528 2018-10-22 stsp int fast_refresh = 10;
1059 1a76625f 2018-10-22 stsp int done = 0, errcode;
1061 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1062 1a76625f 2018-10-22 stsp if (errcode)
1063 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1065 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1066 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1068 1004088d 2018-09-29 stsp view->focussed = 1;
1069 878940b7 2018-09-29 stsp err = view->show(view);
1071 0cf4efb1 2018-09-29 stsp return err;
1072 0cf4efb1 2018-09-29 stsp update_panels();
1073 0cf4efb1 2018-09-29 stsp doupdate();
1074 296152d1 2022-05-31 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1075 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1076 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1077 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1079 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1082 9970f7fc 2020-12-03 stsp if (view->dying) {
1083 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1085 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1086 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1088 e78dc838 2020-12-04 stsp else if (view->parent)
1089 669b5ffa 2018-10-07 stsp prev = view->parent;
1091 e78dc838 2020-12-04 stsp if (view->parent) {
1092 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1093 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1095 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1097 9970f7fc 2020-12-03 stsp err = view_close(view);
1099 e5a0f69f 2018-08-18 stsp goto done;
1101 e78dc838 2020-12-04 stsp view = NULL;
1102 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1103 e78dc838 2020-12-04 stsp if (v->focussed)
1106 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1107 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1109 e78dc838 2020-12-04 stsp view = prev;
1110 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1111 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1112 e78dc838 2020-12-04 stsp tog_view_list_head);
1114 e78dc838 2020-12-04 stsp if (view) {
1115 e78dc838 2020-12-04 stsp if (view->focus_child) {
1116 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1117 e78dc838 2020-12-04 stsp view = view->child;
1119 e78dc838 2020-12-04 stsp view->focussed = 1;
1123 bcbd79e2 2018-08-19 stsp if (new_view) {
1124 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1125 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1126 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1127 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1129 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1130 86c66b02 2018-10-18 stsp err = view_close(v);
1132 86c66b02 2018-10-18 stsp goto done;
1135 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1136 fed7eaa8 2018-10-24 stsp view = new_view;
1138 669b5ffa 2018-10-07 stsp if (view) {
1139 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1140 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1141 e78dc838 2020-12-04 stsp view = view->child;
1143 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1144 e78dc838 2020-12-04 stsp view = view->parent;
1146 e78dc838 2020-12-04 stsp show_panel(view->panel);
1147 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1148 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1149 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1150 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1152 1a76625f 2018-10-22 stsp goto done;
1154 669b5ffa 2018-10-07 stsp err = view->show(view);
1156 1a76625f 2018-10-22 stsp goto done;
1157 669b5ffa 2018-10-07 stsp if (view->child) {
1158 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1160 1a76625f 2018-10-22 stsp goto done;
1162 1a76625f 2018-10-22 stsp update_panels();
1163 1a76625f 2018-10-22 stsp doupdate();
1167 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1168 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1169 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1170 e5a0f69f 2018-08-18 stsp view_close(view);
1173 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1174 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1175 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1177 e5a0f69f 2018-08-18 stsp return err;
1180 4ed7e80c 2018-05-20 stsp __dead static void
1181 9f7d7167 2018-04-29 stsp usage_log(void)
1184 c70c5802 2018-08-01 stsp fprintf(stderr,
1185 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1186 9f7d7167 2018-04-29 stsp getprogname());
1190 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1191 80ddbec8 2018-04-29 stsp static const struct got_error *
1192 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1194 00dfcb92 2018-06-11 stsp char *vis = NULL;
1195 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1197 963b370f 2018-05-20 stsp *ws = NULL;
1198 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1199 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1200 00dfcb92 2018-06-11 stsp int vislen;
1201 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1202 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1204 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1205 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1207 00dfcb92 2018-06-11 stsp return err;
1208 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1209 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1210 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1211 a7f50699 2018-06-11 stsp goto done;
1215 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1216 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1217 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1218 a7f50699 2018-06-11 stsp goto done;
1221 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1222 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1224 00dfcb92 2018-06-11 stsp free(vis);
1225 963b370f 2018-05-20 stsp if (err) {
1226 963b370f 2018-05-20 stsp free(*ws);
1227 963b370f 2018-05-20 stsp *ws = NULL;
1228 963b370f 2018-05-20 stsp *wlen = 0;
1230 963b370f 2018-05-20 stsp return err;
1233 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1234 963b370f 2018-05-20 stsp static const struct got_error *
1235 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1236 27a741e5 2019-09-11 stsp int col_tab_align)
1238 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1239 963b370f 2018-05-20 stsp int cols = 0;
1240 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1241 963b370f 2018-05-20 stsp size_t wlen;
1244 963b370f 2018-05-20 stsp *wlinep = NULL;
1245 b700b5d6 2018-07-10 stsp *widthp = 0;
1247 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1249 963b370f 2018-05-20 stsp return err;
1251 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1252 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1255 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1256 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1261 27a741e5 2019-09-11 stsp while (i < wlen) {
1262 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1264 27a741e5 2019-09-11 stsp if (width == 0) {
1269 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1270 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1272 27a741e5 2019-09-11 stsp cols += width;
1274 27a741e5 2019-09-11 stsp } else if (width == -1) {
1275 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1276 27a741e5 2019-09-11 stsp width = TABSIZE -
1277 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1279 748d5cab 2020-12-14 naddy width = 1;
1280 748d5cab 2020-12-14 naddy wline[i] = L'.';
1282 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1284 748d5cab 2020-12-14 naddy cols += width;
1287 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1288 963b370f 2018-05-20 stsp goto done;
1291 963b370f 2018-05-20 stsp wline[i] = L'\0';
1292 b700b5d6 2018-07-10 stsp if (widthp)
1293 b700b5d6 2018-07-10 stsp *widthp = cols;
1296 963b370f 2018-05-20 stsp free(wline);
1298 963b370f 2018-05-20 stsp *wlinep = wline;
1299 963b370f 2018-05-20 stsp return err;
1302 8b473291 2019-02-21 stsp static const struct got_error*
1303 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1304 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1306 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1307 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1309 8b473291 2019-02-21 stsp const char *name;
1311 8b473291 2019-02-21 stsp *refs_str = NULL;
1313 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1314 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1315 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1318 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1319 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1321 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1322 8b473291 2019-02-21 stsp name += 5;
1323 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1324 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1326 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1327 8b473291 2019-02-21 stsp name += 6;
1328 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1329 8b473291 2019-02-21 stsp name += 8;
1330 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1331 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1334 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1337 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1338 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1339 5d844a1e 2019-08-13 stsp if (err) {
1340 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1341 48cae60d 2020-09-22 stsp free(ref_id);
1344 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1345 5d844a1e 2019-08-13 stsp err = NULL;
1346 5d844a1e 2019-08-13 stsp tag = NULL;
1349 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1350 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1351 48cae60d 2020-09-22 stsp free(ref_id);
1353 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1354 52b5abe1 2019-08-13 stsp if (cmp != 0)
1356 8b473291 2019-02-21 stsp s = *refs_str;
1357 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1358 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1359 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1361 8b473291 2019-02-21 stsp *refs_str = NULL;
1367 8b473291 2019-02-21 stsp return err;
1370 963b370f 2018-05-20 stsp static const struct got_error *
1371 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1372 27a741e5 2019-09-11 stsp int col_tab_align)
1374 e6b8b890 2020-12-29 naddy char *smallerthan;
1376 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1377 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1378 5813d178 2019-03-09 stsp author = smallerthan + 1;
1379 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1380 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1383 5813d178 2019-03-09 stsp static const struct got_error *
1384 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1385 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1386 8fdc79fe 2020-12-01 naddy int author_display_cols)
1388 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1389 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1390 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1391 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1392 5813d178 2019-03-09 stsp char *author = NULL;
1393 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1394 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1395 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1396 bb737323 2018-05-20 stsp int col, limit;
1397 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1398 ccb26ccd 2018-11-05 stsp struct tm tm;
1399 45d799e2 2018-12-23 stsp time_t committer_time;
1400 11b20872 2019-11-08 stsp struct tog_color *tc;
1402 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1403 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1404 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1405 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1406 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1408 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1409 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1411 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1412 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1414 11b20872 2019-11-08 stsp wattr_on(view->window,
1415 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1416 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1418 11b20872 2019-11-08 stsp wattr_off(view->window,
1419 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1420 27a741e5 2019-09-11 stsp col = limit;
1421 b39d25c7 2018-07-10 stsp if (col > avail)
1422 b39d25c7 2018-07-10 stsp goto done;
1424 6570a66d 2019-11-08 stsp if (avail >= 120) {
1425 6570a66d 2019-11-08 stsp char *id_str;
1426 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1428 6570a66d 2019-11-08 stsp goto done;
1429 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1431 11b20872 2019-11-08 stsp wattr_on(view->window,
1432 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1433 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1435 11b20872 2019-11-08 stsp wattr_off(view->window,
1436 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1437 6570a66d 2019-11-08 stsp free(id_str);
1439 6570a66d 2019-11-08 stsp if (col > avail)
1440 6570a66d 2019-11-08 stsp goto done;
1443 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1444 5813d178 2019-03-09 stsp if (author == NULL) {
1445 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1446 80ddbec8 2018-04-29 stsp goto done;
1448 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1450 bb737323 2018-05-20 stsp goto done;
1451 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1453 11b20872 2019-11-08 stsp wattr_on(view->window,
1454 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1455 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1457 11b20872 2019-11-08 stsp wattr_off(view->window,
1458 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1459 bb737323 2018-05-20 stsp col += author_width;
1460 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1461 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1463 bb737323 2018-05-20 stsp author_width++;
1465 9c2eaf34 2018-05-20 stsp if (col > avail)
1466 9c2eaf34 2018-05-20 stsp goto done;
1468 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1470 6d9fbc00 2018-04-29 stsp goto done;
1471 bb737323 2018-05-20 stsp logmsg = logmsg0;
1472 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1474 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1475 bb737323 2018-05-20 stsp if (newline)
1476 bb737323 2018-05-20 stsp *newline = '\0';
1477 bb737323 2018-05-20 stsp limit = avail - col;
1478 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1480 bb737323 2018-05-20 stsp goto done;
1481 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1482 bb737323 2018-05-20 stsp col += logmsg_width;
1483 27a741e5 2019-09-11 stsp while (col < avail) {
1484 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1488 80ddbec8 2018-04-29 stsp free(logmsg0);
1489 bb737323 2018-05-20 stsp free(wlogmsg);
1490 5813d178 2019-03-09 stsp free(author);
1491 bb737323 2018-05-20 stsp free(wauthor);
1492 80ddbec8 2018-04-29 stsp free(line);
1493 80ddbec8 2018-04-29 stsp return err;
1496 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1497 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1498 899d86c2 2018-05-10 stsp struct got_object_id *id)
1500 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1502 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1503 80ddbec8 2018-04-29 stsp if (entry == NULL)
1504 899d86c2 2018-05-10 stsp return NULL;
1506 899d86c2 2018-05-10 stsp entry->id = id;
1507 99db9666 2018-05-07 stsp entry->commit = commit;
1508 899d86c2 2018-05-10 stsp return entry;
1511 99db9666 2018-05-07 stsp static void
1512 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1514 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1516 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1517 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1518 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1519 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1520 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1521 99db9666 2018-05-07 stsp free(entry);
1524 99db9666 2018-05-07 stsp static void
1525 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1527 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1528 99db9666 2018-05-07 stsp pop_commit(commits);
1531 c4972b91 2018-05-07 stsp static const struct got_error *
1532 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1533 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1535 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1536 13add988 2019-10-15 stsp regmatch_t regmatch;
1537 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1539 13add988 2019-10-15 stsp *have_match = 0;
1541 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1543 13add988 2019-10-15 stsp return err;
1545 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1547 13add988 2019-10-15 stsp goto done;
1549 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1550 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1551 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1552 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1553 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1554 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1555 13add988 2019-10-15 stsp *have_match = 1;
1557 13add988 2019-10-15 stsp free(id_str);
1558 13add988 2019-10-15 stsp free(logmsg);
1559 13add988 2019-10-15 stsp return err;
1562 13add988 2019-10-15 stsp static const struct got_error *
1563 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1565 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1568 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1569 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1570 1a76625f 2018-10-22 stsp * while updating the display.
1573 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1574 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1575 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1576 1a76625f 2018-10-22 stsp int errcode;
1578 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1579 4e0d2870 2020-12-07 naddy NULL, NULL);
1580 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1583 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1586 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1587 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1588 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1592 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1593 1a76625f 2018-10-22 stsp if (errcode) {
1594 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1595 13add988 2019-10-15 stsp "pthread_mutex_lock");
1599 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1600 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1601 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1603 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1604 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1605 7c1452c1 2020-03-26 stsp int have_match;
1606 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1609 7c1452c1 2020-03-26 stsp if (have_match)
1610 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1613 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1614 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1615 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1616 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1619 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1621 9ba79e04 2018-06-11 stsp return err;
1624 2b779855 2020-12-05 naddy static void
1625 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1627 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1628 2b779855 2020-12-05 naddy int ncommits = 0;
1630 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1631 2b779855 2020-12-05 naddy while (entry) {
1632 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1633 2b779855 2020-12-05 naddy s->selected_entry = entry;
1636 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1637 2b779855 2020-12-05 naddy ncommits++;
1641 0553a4e3 2018-04-30 stsp static const struct got_error *
1642 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1644 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1645 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1646 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1647 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1648 60493ae3 2019-06-20 stsp int width;
1649 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1650 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1651 8b473291 2019-02-21 stsp char *refs_str = NULL;
1652 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1653 11b20872 2019-11-08 stsp struct tog_color *tc;
1654 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1656 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1657 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1658 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1659 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1661 ecb28ae0 2018-07-16 stsp return err;
1662 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1663 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1664 d2075bf3 2020-12-25 stsp if (refs) {
1665 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1666 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1668 d2075bf3 2020-12-25 stsp goto done;
1672 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1673 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1675 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1676 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1677 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1678 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1679 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1680 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1681 8f4ed634 2020-03-26 stsp goto done;
1684 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1686 f9686aa5 2020-03-27 stsp if (view->searching) {
1687 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1688 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1689 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1690 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1691 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1692 f9686aa5 2020-03-27 stsp search_str = "searching...";
1695 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1696 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1697 f9686aa5 2020-03-27 stsp search_str ? search_str :
1698 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1699 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1700 8f4ed634 2020-03-26 stsp goto done;
1704 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1705 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1706 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1707 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1708 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1709 1a76625f 2018-10-22 stsp header = NULL;
1710 1a76625f 2018-10-22 stsp goto done;
1712 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1713 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1714 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1715 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1716 1a76625f 2018-10-22 stsp header = NULL;
1717 1a76625f 2018-10-22 stsp goto done;
1719 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1721 1a76625f 2018-10-22 stsp goto done;
1723 2814baeb 2018-08-01 stsp werase(view->window);
1725 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1726 a3404814 2018-09-02 stsp wstandout(view->window);
1727 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1729 11b20872 2019-11-08 stsp wattr_on(view->window,
1730 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1731 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1733 11b20872 2019-11-08 stsp wattr_off(view->window,
1734 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1735 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1736 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1739 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1740 a3404814 2018-09-02 stsp wstandend(view->window);
1741 ecb28ae0 2018-07-16 stsp free(wline);
1742 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1743 1a76625f 2018-10-22 stsp goto done;
1745 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1746 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1747 5813d178 2019-03-09 stsp ncommits = 0;
1748 5813d178 2019-03-09 stsp while (entry) {
1749 5813d178 2019-03-09 stsp char *author;
1750 5813d178 2019-03-09 stsp wchar_t *wauthor;
1751 5813d178 2019-03-09 stsp int width;
1752 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1754 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1755 5813d178 2019-03-09 stsp if (author == NULL) {
1756 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1757 5813d178 2019-03-09 stsp goto done;
1759 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1760 27a741e5 2019-09-11 stsp date_display_cols);
1761 5813d178 2019-03-09 stsp if (author_cols < width)
1762 5813d178 2019-03-09 stsp author_cols = width;
1763 5813d178 2019-03-09 stsp free(wauthor);
1764 5813d178 2019-03-09 stsp free(author);
1765 7ca04879 2019-10-19 stsp ncommits++;
1766 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1769 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1770 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1771 867c6645 2018-07-10 stsp ncommits = 0;
1772 899d86c2 2018-05-10 stsp while (entry) {
1773 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1775 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1776 2814baeb 2018-08-01 stsp wstandout(view->window);
1777 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1778 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1779 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1780 2814baeb 2018-08-01 stsp wstandend(view->window);
1782 60493ae3 2019-06-20 stsp goto done;
1783 0553a4e3 2018-04-30 stsp ncommits++;
1784 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1785 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1788 1a57306a 2018-09-02 stsp view_vborder(view);
1789 dd038bc6 2021-09-21 thomas.ad update_panels();
1790 dd038bc6 2021-09-21 thomas.ad doupdate();
1792 1a76625f 2018-10-22 stsp free(id_str);
1793 8b473291 2019-02-21 stsp free(refs_str);
1794 1a76625f 2018-10-22 stsp free(ncommits_str);
1795 1a76625f 2018-10-22 stsp free(header);
1796 80ddbec8 2018-04-29 stsp return err;
1799 07b55e75 2018-05-10 stsp static void
1800 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1802 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1803 07b55e75 2018-05-10 stsp int nscrolled = 0;
1805 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1806 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1809 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1810 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1811 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1812 07b55e75 2018-05-10 stsp if (entry) {
1813 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1814 07b55e75 2018-05-10 stsp nscrolled++;
1819 aa075928 2018-05-10 stsp static const struct got_error *
1820 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1822 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1823 5e224a3e 2019-02-22 stsp int errcode;
1825 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1827 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1828 ffe38506 2020-12-01 naddy if (ta->log_complete)
1831 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1832 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1833 7aafa0d1 2019-02-22 stsp if (errcode)
1834 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1835 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1838 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1839 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1841 7c1452c1 2020-03-26 stsp if (!wait)
1844 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1845 ffe38506 2020-12-01 naddy show_log_view(view);
1846 7c1452c1 2020-03-26 stsp update_panels();
1847 7c1452c1 2020-03-26 stsp doupdate();
1849 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1850 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1851 82954512 2020-02-03 stsp if (errcode)
1852 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1853 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1855 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1856 ffe38506 2020-12-01 naddy show_log_view(view);
1857 7c1452c1 2020-03-26 stsp update_panels();
1858 7c1452c1 2020-03-26 stsp doupdate();
1861 5e224a3e 2019-02-22 stsp return NULL;
1864 5e224a3e 2019-02-22 stsp static const struct got_error *
1865 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1867 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1868 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1869 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1870 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1872 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1873 5e224a3e 2019-02-22 stsp return NULL;
1875 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1876 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1877 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1879 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1881 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1882 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1884 5e224a3e 2019-02-22 stsp return err;
1888 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1889 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1892 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1894 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1895 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1897 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1898 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1900 dd0a52c1 2018-05-20 stsp return err;
1903 cd0acaa7 2018-05-20 stsp static const struct got_error *
1904 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1905 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1906 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1908 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1909 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1910 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1912 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1913 15a94983 2018-12-23 stsp if (diff_view == NULL)
1914 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1916 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1917 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1918 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1919 e5a0f69f 2018-08-18 stsp if (err == NULL)
1920 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1921 cd0acaa7 2018-05-20 stsp return err;
1924 80ddbec8 2018-04-29 stsp static const struct got_error *
1925 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1926 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1928 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1930 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1931 941e9f74 2019-05-21 stsp if (parent == NULL)
1932 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1934 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1935 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1936 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1937 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1938 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1939 941e9f74 2019-05-21 stsp s->tree = subtree;
1940 941e9f74 2019-05-21 stsp s->selected = 0;
1941 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1942 941e9f74 2019-05-21 stsp return NULL;
1945 941e9f74 2019-05-21 stsp static const struct got_error *
1946 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1947 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1949 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1950 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1951 941e9f74 2019-05-21 stsp const char *p;
1952 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1954 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1956 941e9f74 2019-05-21 stsp while (*p) {
1957 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1958 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1959 56e0773d 2019-11-28 stsp char *te_name;
1961 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1964 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1965 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1966 941e9f74 2019-05-21 stsp if (slash == NULL)
1967 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1969 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1970 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1971 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1974 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1975 56e0773d 2019-11-28 stsp if (te == NULL) {
1976 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1977 56e0773d 2019-11-28 stsp free(te_name);
1980 56e0773d 2019-11-28 stsp free(te_name);
1981 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1983 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1984 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1986 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1987 941e9f74 2019-05-21 stsp if (slash)
1988 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1990 941e9f74 2019-05-21 stsp subpath = strdup(path);
1991 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1992 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1996 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2001 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2002 941e9f74 2019-05-21 stsp free(tree_id);
2006 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2007 941e9f74 2019-05-21 stsp if (err) {
2008 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2011 941e9f74 2019-05-21 stsp if (slash == NULL)
2013 941e9f74 2019-05-21 stsp free(subpath);
2014 941e9f74 2019-05-21 stsp subpath = NULL;
2015 941e9f74 2019-05-21 stsp p = slash;
2018 941e9f74 2019-05-21 stsp free(subpath);
2019 1a76625f 2018-10-22 stsp return err;
2022 61266923 2020-01-14 stsp static const struct got_error *
2023 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2024 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2025 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2027 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2028 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2029 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2031 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2032 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2033 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2035 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2037 55cccc34 2020-02-20 stsp return err;
2038 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2040 55cccc34 2020-02-20 stsp *new_view = tree_view;
2042 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2043 55cccc34 2020-02-20 stsp return NULL;
2045 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2048 55cccc34 2020-02-20 stsp static const struct got_error *
2049 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2051 61266923 2020-01-14 stsp sigset_t sigset;
2052 61266923 2020-01-14 stsp int errcode;
2054 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2055 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2057 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2058 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2059 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2060 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2061 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2062 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2063 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2064 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2065 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2067 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2068 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2069 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2071 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2072 61266923 2020-01-14 stsp if (errcode)
2073 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2075 61266923 2020-01-14 stsp return NULL;
2078 1a76625f 2018-10-22 stsp static void *
2079 1a76625f 2018-10-22 stsp log_thread(void *arg)
2081 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2082 1a76625f 2018-10-22 stsp int errcode = 0;
2083 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2084 1a76625f 2018-10-22 stsp int done = 0;
2086 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2088 61266923 2020-01-14 stsp return (void *)err;
2090 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2091 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2092 1a76625f 2018-10-22 stsp if (err) {
2093 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2094 1a76625f 2018-10-22 stsp return (void *)err;
2095 1a76625f 2018-10-22 stsp err = NULL;
2097 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2098 1a76625f 2018-10-22 stsp a->commits_needed--;
2100 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2101 3abe8080 2019-04-10 stsp if (errcode) {
2102 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2103 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2105 3abe8080 2019-04-10 stsp } else if (*a->quit)
2107 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2108 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2109 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2110 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2113 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2114 7c1452c1 2020-03-26 stsp if (errcode) {
2115 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2116 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2117 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2122 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2124 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2125 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2126 7c1452c1 2020-03-26 stsp &tog_mutex);
2127 7c1452c1 2020-03-26 stsp if (errcode)
2128 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2129 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2130 21355643 2020-12-06 stsp if (*a->quit)
2135 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2136 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2137 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2138 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2140 3abe8080 2019-04-10 stsp a->log_complete = 1;
2141 1a76625f 2018-10-22 stsp return (void *)err;
2144 1a76625f 2018-10-22 stsp static const struct got_error *
2145 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2147 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2148 1a76625f 2018-10-22 stsp int errcode;
2150 1a76625f 2018-10-22 stsp if (s->thread) {
2151 1a76625f 2018-10-22 stsp s->quit = 1;
2152 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2153 1a76625f 2018-10-22 stsp if (errcode)
2154 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2155 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2156 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2157 1a76625f 2018-10-22 stsp if (errcode)
2158 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2159 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2160 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2161 1a76625f 2018-10-22 stsp if (errcode)
2162 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2163 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2164 1a76625f 2018-10-22 stsp if (errcode)
2165 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2166 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2167 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2170 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2171 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2172 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2175 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2176 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2177 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2178 1af5eddf 2022-06-23 thomas if (err == NULL)
2179 1af5eddf 2022-06-23 thomas err = pack_err;
2180 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2183 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2184 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2185 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2188 9343a5fb 2018-06-23 stsp return err;
2191 9343a5fb 2018-06-23 stsp static const struct got_error *
2192 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2194 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2195 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2196 276b94a1 2020-11-13 naddy int errcode;
2198 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2200 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2201 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2202 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2204 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2205 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2206 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2208 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2209 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2210 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2211 1a76625f 2018-10-22 stsp free(s->start_id);
2212 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2213 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2214 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2215 1a76625f 2018-10-22 stsp return err;
2218 1a76625f 2018-10-22 stsp static const struct got_error *
2219 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2221 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2223 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2224 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2225 60493ae3 2019-06-20 stsp return NULL;
2228 60493ae3 2019-06-20 stsp static const struct got_error *
2229 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2231 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2232 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2233 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2235 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2236 f9686aa5 2020-03-27 stsp show_log_view(view);
2237 f9686aa5 2020-03-27 stsp update_panels();
2238 f9686aa5 2020-03-27 stsp doupdate();
2240 96e2b566 2019-07-08 stsp if (s->search_entry) {
2241 13add988 2019-10-15 stsp int errcode, ch;
2242 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2243 13add988 2019-10-15 stsp if (errcode)
2244 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2245 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2246 13add988 2019-10-15 stsp ch = wgetch(view->window);
2247 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2248 13add988 2019-10-15 stsp if (errcode)
2249 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2250 13add988 2019-10-15 stsp "pthread_mutex_lock");
2251 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2252 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2253 678cbce5 2019-07-28 stsp return NULL;
2255 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2256 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2258 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2259 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2260 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2261 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2262 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2264 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2265 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2267 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2270 60493ae3 2019-06-20 stsp while (1) {
2271 13add988 2019-10-15 stsp int have_match = 0;
2273 60493ae3 2019-06-20 stsp if (entry == NULL) {
2274 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2275 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2276 f9967bca 2020-03-27 stsp view->search_next_done =
2277 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2278 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2279 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2280 f801134a 2019-06-25 stsp return NULL;
2283 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2284 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2285 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2287 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2288 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2291 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2292 13add988 2019-10-15 stsp &view->regex);
2295 13add988 2019-10-15 stsp if (have_match) {
2296 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2297 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2301 96e2b566 2019-07-08 stsp s->search_entry = entry;
2302 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2303 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2305 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2308 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2309 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2310 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2311 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2313 60493ae3 2019-06-20 stsp return err;
2316 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2317 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2319 60493ae3 2019-06-20 stsp return err;
2324 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2326 60493ae3 2019-06-20 stsp return NULL;
2329 60493ae3 2019-06-20 stsp static const struct got_error *
2330 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2331 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2332 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2334 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2335 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2336 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2337 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2338 1a76625f 2018-10-22 stsp int errcode;
2340 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2341 f135c941 2020-02-20 stsp free(s->in_repo_path);
2342 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2343 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2344 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2347 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2348 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2349 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2351 fb2756b9 2018-08-04 stsp s->repo = repo;
2352 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2353 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2354 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2355 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2356 9cd7cbd1 2020-12-07 stsp goto done;
2359 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2360 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2361 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2362 5036bf37 2018-09-24 stsp goto done;
2364 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2366 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2367 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2368 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2369 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2371 11b20872 2019-11-08 stsp goto done;
2372 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2373 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2374 11b20872 2019-11-08 stsp if (err) {
2375 11b20872 2019-11-08 stsp free_colors(&s->colors);
2376 11b20872 2019-11-08 stsp goto done;
2378 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2379 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2380 11b20872 2019-11-08 stsp if (err) {
2381 11b20872 2019-11-08 stsp free_colors(&s->colors);
2382 11b20872 2019-11-08 stsp goto done;
2386 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2387 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2388 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2389 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2390 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2392 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2393 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2394 1af5eddf 2022-06-23 thomas if (err)
2395 1af5eddf 2022-06-23 thomas goto done;
2397 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2398 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2399 7cd52833 2022-06-23 thomas if (err)
2400 7cd52833 2022-06-23 thomas goto done;
2401 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2402 b672a97a 2020-01-27 stsp !s->log_branches);
2404 1a76625f 2018-10-22 stsp goto done;
2405 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2406 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2408 c5b78334 2020-01-12 stsp goto done;
2410 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2411 1a76625f 2018-10-22 stsp if (errcode) {
2412 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2413 1a76625f 2018-10-22 stsp goto done;
2415 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2416 7c1452c1 2020-03-26 stsp if (errcode) {
2417 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2418 7c1452c1 2020-03-26 stsp goto done;
2421 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2422 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2423 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2424 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2425 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2426 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2427 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2428 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2429 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2430 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2431 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2432 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2433 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2436 1a76625f 2018-10-22 stsp close_log_view(view);
2437 ba4f502b 2018-08-04 stsp return err;
2440 e5a0f69f 2018-08-18 stsp static const struct got_error *
2441 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2443 f2f6d207 2020-11-24 stsp const struct got_error *err;
2444 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2446 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2447 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2448 2b380cc8 2018-10-24 stsp &s->thread_args);
2449 2b380cc8 2018-10-24 stsp if (errcode)
2450 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2451 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2452 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2454 f2f6d207 2020-11-24 stsp return err;
2458 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2461 e5a0f69f 2018-08-18 stsp static const struct got_error *
2462 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2464 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2465 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2466 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2467 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2468 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2469 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
2471 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2472 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2473 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2474 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2475 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2476 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2477 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2478 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2479 fb280deb 2021-08-30 stsp select_commit(s);
2481 528dedf3 2021-08-30 stsp return NULL;
2484 528dedf3 2021-08-30 stsp switch (ch) {
2486 1e37a5c2 2019-05-12 jcs s->quit = 1;
2489 1e37a5c2 2019-05-12 jcs case KEY_UP:
2492 f7140bf5 2021-10-17 thomas case CTRL('p'):
2493 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2495 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2496 1e37a5c2 2019-05-12 jcs s->selected--;
2498 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2499 912a3f79 2021-08-30 j select_commit(s);
2502 912a3f79 2021-08-30 j case KEY_HOME:
2503 912a3f79 2021-08-30 j s->selected = 0;
2504 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2505 2b779855 2020-12-05 naddy select_commit(s);
2507 70f17a53 2022-06-13 thomas case CTRL('u'):
2508 23427b14 2022-06-23 thomas case 'u':
2509 70f17a53 2022-06-13 thomas nscroll /= 2;
2510 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2511 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2512 a4292ac5 2019-05-12 jcs case CTRL('b'):
2513 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2515 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2516 70f17a53 2022-06-13 thomas s->selected = MAX(0, s->selected - nscroll - 1);
2518 70f17a53 2022-06-13 thomas log_scroll_up(s, nscroll);
2519 2b779855 2020-12-05 naddy select_commit(s);
2522 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2525 f7140bf5 2021-10-17 thomas case CTRL('n'):
2526 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2528 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2529 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2530 1e37a5c2 2019-05-12 jcs s->selected++;
2532 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2536 912a3f79 2021-08-30 j select_commit(s);
2539 912a3f79 2021-08-30 j case KEY_END: {
2540 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2541 912a3f79 2021-08-30 j * traverse them all. */
2542 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2543 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2544 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2547 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2548 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2549 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2550 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2552 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2553 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2555 f3bc9f1d 2021-09-05 naddy if (n > 0)
2556 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2557 2b779855 2020-12-05 naddy select_commit(s);
2560 bccd1d5d 2022-06-13 thomas case CTRL('d'):
2561 23427b14 2022-06-23 thomas case 'd':
2562 70f17a53 2022-06-13 thomas nscroll /= 2;
2563 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2564 70f17a53 2022-06-13 thomas case KEY_NPAGE:
2565 70f17a53 2022-06-13 thomas case CTRL('f'): {
2566 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2567 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2568 1e37a5c2 2019-05-12 jcs if (first == NULL)
2570 70f17a53 2022-06-13 thomas err = log_scroll_down(view, nscroll);
2573 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2574 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2575 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2576 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2577 70f17a53 2022-06-13 thomas s->selected += MIN(s->last_displayed_entry->idx -
2578 70f17a53 2022-06-13 thomas s->selected_entry->idx, nscroll + 1);
2580 2b779855 2020-12-05 naddy select_commit(s);
2583 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2584 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2585 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2586 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2587 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2588 2b779855 2020-12-05 naddy select_commit(s);
2589 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2590 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2591 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2592 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2593 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2596 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2599 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2601 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2602 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2603 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2604 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2605 78756c87 2020-11-24 stsp view, s->repo);
2608 e78dc838 2020-12-04 stsp view->focussed = 0;
2609 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2610 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2611 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2613 1e37a5c2 2019-05-12 jcs return err;
2614 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2615 e78dc838 2020-12-04 stsp view->focus_child = 1;
2617 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2620 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2623 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2624 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2625 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2629 e78dc838 2020-12-04 stsp view->focussed = 0;
2630 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2631 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2632 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2634 1e37a5c2 2019-05-12 jcs return err;
2635 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2636 e78dc838 2020-12-04 stsp view->focus_child = 1;
2638 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2640 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2641 21355643 2020-12-06 stsp case CTRL('l'):
2643 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2644 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2646 21355643 2020-12-06 stsp err = stop_log_thread(s);
2648 74cfe85e 2020-10-20 stsp return err;
2649 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2650 21355643 2020-12-06 stsp char *parent_path;
2651 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2653 21355643 2020-12-06 stsp return err;
2654 21355643 2020-12-06 stsp free(s->in_repo_path);
2655 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2656 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2657 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2658 21355643 2020-12-06 stsp struct got_object_id *start_id;
2659 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2660 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2661 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2663 21355643 2020-12-06 stsp return err;
2664 21355643 2020-12-06 stsp free(s->start_id);
2665 21355643 2020-12-06 stsp s->start_id = start_id;
2666 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2667 21355643 2020-12-06 stsp } else /* 'B' */
2668 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2670 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
2671 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
2672 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2674 21355643 2020-12-06 stsp return err;
2675 51a10b52 2020-12-26 stsp tog_free_refs();
2676 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2678 ca51c541 2020-12-07 stsp return err;
2679 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2680 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2682 d01904d4 2019-06-25 stsp return err;
2683 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2684 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2686 b672a97a 2020-01-27 stsp return err;
2687 21355643 2020-12-06 stsp free_commits(&s->commits);
2688 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2689 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2690 21355643 2020-12-06 stsp s->selected_entry = NULL;
2691 21355643 2020-12-06 stsp s->selected = 0;
2692 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2693 21355643 2020-12-06 stsp s->quit = 0;
2694 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2697 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2698 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2699 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2700 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2701 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2702 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2703 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2704 6458efa5 2020-11-24 stsp if (err) {
2705 6458efa5 2020-11-24 stsp view_close(ref_view);
2706 6458efa5 2020-11-24 stsp return err;
2708 e78dc838 2020-12-04 stsp view->focussed = 0;
2709 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2710 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2711 6458efa5 2020-11-24 stsp err = view_close_child(view);
2713 6458efa5 2020-11-24 stsp return err;
2714 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2715 e78dc838 2020-12-04 stsp view->focus_child = 1;
2717 6458efa5 2020-11-24 stsp *new_view = ref_view;
2723 80ddbec8 2018-04-29 stsp return err;
2726 4ed7e80c 2018-05-20 stsp static const struct got_error *
2727 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2729 c2db6724 2019-01-04 stsp const struct got_error *error;
2731 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2732 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2733 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2735 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2736 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2738 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2739 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2741 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2742 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2744 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2745 c2db6724 2019-01-04 stsp if (error != NULL)
2746 c2db6724 2019-01-04 stsp return error;
2748 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2749 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2751 c2db6724 2019-01-04 stsp return NULL;
2754 a915003a 2019-02-05 stsp static void
2755 a915003a 2019-02-05 stsp init_curses(void)
2758 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
2759 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
2760 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
2762 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
2763 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
2764 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
2765 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
2766 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
2768 a915003a 2019-02-05 stsp initscr();
2770 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2773 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2774 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2775 a915003a 2019-02-05 stsp curs_set(0);
2776 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2777 6d17833f 2019-11-08 stsp start_color();
2778 6d17833f 2019-11-08 stsp use_default_colors();
2782 c2db6724 2019-01-04 stsp static const struct got_error *
2783 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2784 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2786 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2788 f135c941 2020-02-20 stsp if (argc == 0) {
2789 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2790 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2791 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2792 f135c941 2020-02-20 stsp return NULL;
2795 f135c941 2020-02-20 stsp if (worktree) {
2796 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2799 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2801 f135c941 2020-02-20 stsp return err;
2802 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2803 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2804 bfd61697 2020-11-14 stsp p) == -1) {
2805 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2806 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2810 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2812 f135c941 2020-02-20 stsp return err;
2815 f135c941 2020-02-20 stsp static const struct got_error *
2816 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2818 80ddbec8 2018-04-29 stsp const struct got_error *error;
2819 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2820 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2821 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2822 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2823 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2824 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2825 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2826 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2827 04cc582a 2018-08-01 stsp struct tog_view *view;
2828 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
2830 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2831 80ddbec8 2018-04-29 stsp switch (ch) {
2833 b672a97a 2020-01-27 stsp log_branches = 1;
2836 80ddbec8 2018-04-29 stsp start_commit = optarg;
2839 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2840 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2841 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2845 17020d27 2019-03-07 stsp usage_log();
2846 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2850 80ddbec8 2018-04-29 stsp argc -= optind;
2851 80ddbec8 2018-04-29 stsp argv += optind;
2853 f135c941 2020-02-20 stsp if (argc > 1)
2854 f135c941 2020-02-20 stsp usage_log();
2856 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
2857 7cd52833 2022-06-23 thomas if (error != NULL)
2858 7cd52833 2022-06-23 thomas goto done;
2860 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2861 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2862 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2863 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2864 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2865 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2866 c156c7a4 2020-12-18 stsp goto done;
2867 a1fbf39a 2019-08-11 stsp if (worktree)
2868 6962eb72 2020-02-20 stsp repo_path =
2869 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2871 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2872 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2873 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2874 c156c7a4 2020-12-18 stsp goto done;
2878 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2879 80ddbec8 2018-04-29 stsp if (error != NULL)
2880 ecb28ae0 2018-07-16 stsp goto done;
2882 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2883 f135c941 2020-02-20 stsp repo, worktree);
2884 f135c941 2020-02-20 stsp if (error)
2885 f135c941 2020-02-20 stsp goto done;
2887 f135c941 2020-02-20 stsp init_curses();
2889 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2890 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2891 c02c541e 2019-03-29 stsp if (error)
2892 c02c541e 2019-03-29 stsp goto done;
2894 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2895 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2896 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
2897 87670572 2020-12-26 naddy if (error)
2898 87670572 2020-12-26 naddy goto done;
2901 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2902 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2903 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2904 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2905 d8f38dc4 2020-12-05 stsp if (error)
2906 d8f38dc4 2020-12-05 stsp goto done;
2907 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2909 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2910 d8f38dc4 2020-12-05 stsp if (error == NULL)
2911 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2912 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2913 d8f38dc4 2020-12-05 stsp goto done;
2914 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2915 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2916 d8f38dc4 2020-12-05 stsp if (error)
2917 d8f38dc4 2020-12-05 stsp goto done;
2920 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2921 04cc582a 2018-08-01 stsp if (view == NULL) {
2922 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2923 04cc582a 2018-08-01 stsp goto done;
2925 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2926 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2927 ba4f502b 2018-08-04 stsp if (error)
2928 ba4f502b 2018-08-04 stsp goto done;
2929 2fc00ff4 2019-08-31 stsp if (worktree) {
2930 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2931 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2932 2fc00ff4 2019-08-31 stsp worktree = NULL;
2934 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2936 f135c941 2020-02-20 stsp free(in_repo_path);
2937 ecb28ae0 2018-07-16 stsp free(repo_path);
2938 ecb28ae0 2018-07-16 stsp free(cwd);
2939 899d86c2 2018-05-10 stsp free(start_id);
2940 d8f38dc4 2020-12-05 stsp free(label);
2942 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2943 1d0f4054 2021-06-17 stsp if (repo) {
2944 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2945 1d0f4054 2021-06-17 stsp if (error == NULL)
2946 1d0f4054 2021-06-17 stsp error = close_err;
2948 ec142235 2019-03-07 stsp if (worktree)
2949 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2950 7cd52833 2022-06-23 thomas if (pack_fds) {
2951 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
2952 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
2953 7cd52833 2022-06-23 thomas if (error == NULL)
2954 7cd52833 2022-06-23 thomas error = pack_err;
2956 51a10b52 2020-12-26 stsp tog_free_refs();
2957 80ddbec8 2018-04-29 stsp return error;
2960 4ed7e80c 2018-05-20 stsp __dead static void
2961 9f7d7167 2018-04-29 stsp usage_diff(void)
2964 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2965 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2969 6d17833f 2019-11-08 stsp static int
2970 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2971 41605754 2020-11-12 stsp regmatch_t *regmatch)
2973 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2976 f26dddb7 2019-11-08 stsp struct tog_color *
2977 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2979 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2981 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2982 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2983 6d17833f 2019-11-08 stsp return tc;
2986 6d17833f 2019-11-08 stsp return NULL;
2989 4ed7e80c 2018-05-20 stsp static const struct got_error *
2990 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2991 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2993 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2994 41605754 2020-11-12 stsp wchar_t *wline;
2995 41605754 2020-11-12 stsp int width;
2998 41605754 2020-11-12 stsp *wtotal = 0;
3000 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
3001 41605754 2020-11-12 stsp if (s == NULL)
3002 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
3004 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
3005 41605754 2020-11-12 stsp if (err) {
3007 41605754 2020-11-12 stsp return err;
3009 41605754 2020-11-12 stsp waddwstr(window, wline);
3010 41605754 2020-11-12 stsp free(wline);
3012 41605754 2020-11-12 stsp wlimit -= width;
3013 41605754 2020-11-12 stsp *wtotal += width;
3015 41605754 2020-11-12 stsp if (wlimit > 0) {
3016 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
3017 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
3018 41605754 2020-11-12 stsp if (s == NULL) {
3019 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
3021 41605754 2020-11-12 stsp return err;
3023 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
3024 41605754 2020-11-12 stsp if (err) {
3026 41605754 2020-11-12 stsp return err;
3028 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
3029 41605754 2020-11-12 stsp waddwstr(window, wline);
3030 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
3031 41605754 2020-11-12 stsp free(wline);
3033 41605754 2020-11-12 stsp wlimit -= width;
3034 41605754 2020-11-12 stsp *wtotal += width;
3037 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3038 41605754 2020-11-12 stsp err = format_line(&wline, &width,
3039 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
3041 41605754 2020-11-12 stsp return err;
3042 41605754 2020-11-12 stsp waddwstr(window, wline);
3043 41605754 2020-11-12 stsp free(wline);
3044 41605754 2020-11-12 stsp *wtotal += width;
3047 41605754 2020-11-12 stsp return NULL;
3050 41605754 2020-11-12 stsp static const struct got_error *
3051 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3053 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3054 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3055 61e69b96 2018-05-20 stsp const struct got_error *err;
3056 fe621944 2020-11-10 stsp int nprinted = 0;
3057 b304db33 2018-05-20 stsp char *line;
3058 826082fe 2020-12-10 stsp size_t linesize = 0;
3059 826082fe 2020-12-10 stsp ssize_t linelen;
3060 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3061 61e69b96 2018-05-20 stsp wchar_t *wline;
3062 e0b650dd 2018-05-20 stsp int width;
3063 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3064 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3065 fe621944 2020-11-10 stsp off_t line_offset;
3067 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3068 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3069 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3071 f7d12f7e 2018-08-01 stsp werase(view->window);
3073 a3404814 2018-09-02 stsp if (header) {
3074 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3075 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3076 135a2da0 2020-11-11 stsp header) == -1)
3077 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3078 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3079 135a2da0 2020-11-11 stsp free(line);
3081 a3404814 2018-09-02 stsp return err;
3083 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3084 a3404814 2018-09-02 stsp wstandout(view->window);
3085 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3086 e54cc94a 2020-11-11 stsp free(wline);
3087 e54cc94a 2020-11-11 stsp wline = NULL;
3088 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3089 a3404814 2018-09-02 stsp wstandend(view->window);
3090 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3091 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3093 a3404814 2018-09-02 stsp if (max_lines <= 1)
3094 a3404814 2018-09-02 stsp return NULL;
3095 a3404814 2018-09-02 stsp max_lines--;
3098 89f1a395 2020-12-01 naddy s->eof = 0;
3099 826082fe 2020-12-10 stsp line = NULL;
3100 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3101 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3102 826082fe 2020-12-10 stsp if (linelen == -1) {
3103 826082fe 2020-12-10 stsp if (feof(s->f)) {
3104 826082fe 2020-12-10 stsp s->eof = 1;
3107 826082fe 2020-12-10 stsp free(line);
3108 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3111 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3113 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3114 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3115 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3116 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3117 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3118 41605754 2020-11-12 stsp view->window, regmatch);
3119 41605754 2020-11-12 stsp if (err) {
3120 41605754 2020-11-12 stsp free(line);
3121 41605754 2020-11-12 stsp return err;
3124 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3125 41605754 2020-11-12 stsp if (err) {
3126 41605754 2020-11-12 stsp free(line);
3127 41605754 2020-11-12 stsp return err;
3129 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3130 41605754 2020-11-12 stsp free(wline);
3131 41605754 2020-11-12 stsp wline = NULL;
3134 6d17833f 2019-11-08 stsp wattr_off(view->window,
3135 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3136 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3137 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3138 fe621944 2020-11-10 stsp nprinted++;
3140 826082fe 2020-12-10 stsp free(line);
3141 fe621944 2020-11-10 stsp if (nprinted >= 1)
3142 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3143 89f1a395 2020-12-01 naddy (nprinted - 1);
3145 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3147 1a57306a 2018-09-02 stsp view_vborder(view);
3149 89f1a395 2020-12-01 naddy if (s->eof) {
3150 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3151 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3152 c3e9aa98 2019-05-13 jcs nprinted++;
3155 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3157 c3e9aa98 2019-05-13 jcs return err;
3160 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3161 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3162 e54cc94a 2020-11-11 stsp free(wline);
3163 e54cc94a 2020-11-11 stsp wline = NULL;
3164 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3167 26ed57b2 2018-05-19 stsp return NULL;
3170 abd2672a 2018-12-23 stsp static char *
3171 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3173 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3174 09867e48 2019-08-13 stsp char *p, *s;
3176 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3177 09867e48 2019-08-13 stsp if (tm == NULL)
3178 09867e48 2019-08-13 stsp return NULL;
3179 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3180 09867e48 2019-08-13 stsp if (s == NULL)
3181 09867e48 2019-08-13 stsp return NULL;
3182 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3184 abd2672a 2018-12-23 stsp *p = '\0';
3188 4ed7e80c 2018-05-20 stsp static const struct got_error *
3189 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3190 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3192 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3193 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3194 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3195 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3197 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3198 0208f208 2020-05-05 stsp if (qid != NULL) {
3199 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3200 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3201 ec242592 2022-04-22 thomas &qid->id);
3203 0208f208 2020-05-05 stsp return err;
3205 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3206 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3207 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3208 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3209 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3211 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3215 0208f208 2020-05-05 stsp if (tree_id1) {
3216 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3218 0208f208 2020-05-05 stsp goto done;
3221 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3222 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3224 0208f208 2020-05-05 stsp goto done;
3226 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3227 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3229 0208f208 2020-05-05 stsp if (tree1)
3230 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3231 0208f208 2020-05-05 stsp if (tree2)
3232 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3233 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3234 0208f208 2020-05-05 stsp return err;
3237 0208f208 2020-05-05 stsp static const struct got_error *
3238 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3242 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3243 fe621944 2020-11-10 stsp if (p == NULL)
3244 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3245 fe621944 2020-11-10 stsp *line_offsets = p;
3246 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3247 fe621944 2020-11-10 stsp (*nlines)++;
3248 fe621944 2020-11-10 stsp return NULL;
3251 fe621944 2020-11-10 stsp static const struct got_error *
3252 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3253 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3254 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3256 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3257 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3258 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3259 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3260 45d799e2 2018-12-23 stsp time_t committer_time;
3261 45d799e2 2018-12-23 stsp const char *author, *committer;
3262 8b473291 2019-02-21 stsp char *refs_str = NULL;
3263 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3264 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3265 fe621944 2020-11-10 stsp off_t outoff = 0;
3268 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3270 8b473291 2019-02-21 stsp if (refs) {
3271 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3273 8b473291 2019-02-21 stsp return err;
3276 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3278 abd2672a 2018-12-23 stsp return err;
3280 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3281 15a94983 2018-12-23 stsp if (err) {
3282 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3283 15a94983 2018-12-23 stsp goto done;
3286 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3288 fe621944 2020-11-10 stsp goto done;
3290 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3291 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3292 fe621944 2020-11-10 stsp if (n < 0) {
3293 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3294 abd2672a 2018-12-23 stsp goto done;
3296 fe621944 2020-11-10 stsp outoff += n;
3297 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3299 fe621944 2020-11-10 stsp goto done;
3301 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3302 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3303 fe621944 2020-11-10 stsp if (n < 0) {
3304 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3305 abd2672a 2018-12-23 stsp goto done;
3307 fe621944 2020-11-10 stsp outoff += n;
3308 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3310 fe621944 2020-11-10 stsp goto done;
3312 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3313 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3314 fe621944 2020-11-10 stsp if (datestr) {
3315 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3316 fe621944 2020-11-10 stsp if (n < 0) {
3317 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3318 fe621944 2020-11-10 stsp goto done;
3320 fe621944 2020-11-10 stsp outoff += n;
3321 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3323 fe621944 2020-11-10 stsp goto done;
3325 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3326 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3327 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3328 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3329 fe621944 2020-11-10 stsp if (n < 0) {
3330 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3331 fe621944 2020-11-10 stsp goto done;
3333 fe621944 2020-11-10 stsp outoff += n;
3334 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3336 fe621944 2020-11-10 stsp goto done;
3338 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3339 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3340 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3341 ac4dc263 2021-09-24 thomas int pn = 1;
3342 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3343 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3344 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3345 ac4dc263 2021-09-24 thomas if (err)
3346 ac4dc263 2021-09-24 thomas goto done;
3347 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3348 ac4dc263 2021-09-24 thomas if (n < 0) {
3349 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3350 ac4dc263 2021-09-24 thomas goto done;
3352 ac4dc263 2021-09-24 thomas outoff += n;
3353 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3354 ac4dc263 2021-09-24 thomas if (err)
3355 ac4dc263 2021-09-24 thomas goto done;
3356 ac4dc263 2021-09-24 thomas free(id_str);
3357 ac4dc263 2021-09-24 thomas id_str = NULL;
3361 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3363 5943eee2 2019-08-13 stsp goto done;
3364 fe621944 2020-11-10 stsp s = logmsg;
3365 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3366 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3367 fe621944 2020-11-10 stsp if (n < 0) {
3368 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3369 fe621944 2020-11-10 stsp goto done;
3371 fe621944 2020-11-10 stsp outoff += n;
3372 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3374 fe621944 2020-11-10 stsp goto done;
3377 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3379 0208f208 2020-05-05 stsp goto done;
3380 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3381 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3382 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3383 fe621944 2020-11-10 stsp if (n < 0) {
3384 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3385 fe621944 2020-11-10 stsp goto done;
3387 fe621944 2020-11-10 stsp outoff += n;
3388 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3390 fe621944 2020-11-10 stsp goto done;
3391 0208f208 2020-05-05 stsp free((char *)pe->path);
3392 0208f208 2020-05-05 stsp free(pe->data);
3395 0208f208 2020-05-05 stsp fputc('\n', outfile);
3397 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3399 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3400 abd2672a 2018-12-23 stsp free(id_str);
3401 5943eee2 2019-08-13 stsp free(logmsg);
3402 8b473291 2019-02-21 stsp free(refs_str);
3403 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3404 7510f233 2020-08-09 stsp if (err) {
3405 ae6a6978 2020-08-09 stsp free(*line_offsets);
3406 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3407 ae6a6978 2020-08-09 stsp *nlines = 0;
3409 fe621944 2020-11-10 stsp return err;
3412 abd2672a 2018-12-23 stsp static const struct got_error *
3413 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3415 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3416 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3417 15a94983 2018-12-23 stsp int obj_type;
3419 fe621944 2020-11-10 stsp free(s->line_offsets);
3420 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3421 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3422 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3423 fe621944 2020-11-10 stsp s->nlines = 0;
3425 511a516b 2018-05-19 stsp f = got_opentemp();
3426 48ae06ee 2018-10-18 stsp if (f == NULL) {
3427 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3428 48ae06ee 2018-10-18 stsp goto done;
3430 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3431 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3432 fb43ecf1 2019-02-11 stsp goto done;
3436 15a94983 2018-12-23 stsp if (s->id1)
3437 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3439 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3441 15a94983 2018-12-23 stsp goto done;
3443 15a94983 2018-12-23 stsp switch (obj_type) {
3444 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3445 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3446 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3447 a0f32f33 2022-06-13 thomas s->diff_context, s->ignore_whitespace, s->force_text_diff,
3448 a0f32f33 2022-06-13 thomas s->repo, s->f);
3450 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3451 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3452 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3453 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3455 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3456 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3457 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3458 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3459 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3461 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3463 3ffacbe1 2020-02-02 tracey goto done;
3464 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3465 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3466 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3467 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3468 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3469 f44b1f58 2020-02-02 tracey if (err)
3470 f44b1f58 2020-02-02 tracey goto done;
3471 f44b1f58 2020-02-02 tracey } else {
3472 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3473 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3474 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3475 fe621944 2020-11-10 stsp err = write_commit_info(
3476 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3477 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3478 f44b1f58 2020-02-02 tracey if (err)
3479 f44b1f58 2020-02-02 tracey goto done;
3484 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3486 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3487 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3488 a0f32f33 2022-06-13 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3492 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3495 f44b1f58 2020-02-02 tracey if (err)
3496 f44b1f58 2020-02-02 tracey goto done;
3498 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3499 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3500 48ae06ee 2018-10-18 stsp return err;
3503 f5215bb9 2019-02-22 stsp static void
3504 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3506 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3507 f5215bb9 2019-02-22 stsp update_panels();
3508 f5215bb9 2019-02-22 stsp doupdate();
3511 f44b1f58 2020-02-02 tracey static const struct got_error *
3512 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3514 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3516 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3517 f44b1f58 2020-02-02 tracey return NULL;
3520 f44b1f58 2020-02-02 tracey static const struct got_error *
3521 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3523 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3524 f44b1f58 2020-02-02 tracey int lineno;
3525 826082fe 2020-12-10 stsp char *line = NULL;
3526 826082fe 2020-12-10 stsp size_t linesize = 0;
3527 826082fe 2020-12-10 stsp ssize_t linelen;
3529 f44b1f58 2020-02-02 tracey if (!view->searching) {
3530 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3531 f44b1f58 2020-02-02 tracey return NULL;
3534 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3535 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3536 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3538 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3540 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3542 f44b1f58 2020-02-02 tracey while (1) {
3543 f44b1f58 2020-02-02 tracey off_t offset;
3545 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3546 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3547 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3551 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3552 f44b1f58 2020-02-02 tracey lineno = 1;
3554 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3557 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3558 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3559 f44b1f58 2020-02-02 tracey free(line);
3560 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3562 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3563 826082fe 2020-12-10 stsp if (linelen != -1 &&
3564 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3565 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3566 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3569 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3570 f44b1f58 2020-02-02 tracey lineno++;
3572 f44b1f58 2020-02-02 tracey lineno--;
3574 826082fe 2020-12-10 stsp free(line);
3576 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3577 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3578 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3581 f44b1f58 2020-02-02 tracey return NULL;
3584 48ae06ee 2018-10-18 stsp static const struct got_error *
3585 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
3587 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
3588 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
3590 a0f32f33 2022-06-13 thomas free(s->id1);
3591 a0f32f33 2022-06-13 thomas s->id1 = NULL;
3592 a0f32f33 2022-06-13 thomas free(s->id2);
3593 a0f32f33 2022-06-13 thomas s->id2 = NULL;
3594 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
3595 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3596 a0f32f33 2022-06-13 thomas s->f = NULL;
3597 a0f32f33 2022-06-13 thomas if (s->f1 && fclose(s->f1) == EOF)
3598 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3599 a0f32f33 2022-06-13 thomas s->f1 = NULL;
3600 a0f32f33 2022-06-13 thomas if (s->f2 && fclose(s->f2) == EOF)
3601 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3602 a0f32f33 2022-06-13 thomas s->f2 = NULL;
3603 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
3604 a0f32f33 2022-06-13 thomas free(s->line_offsets);
3605 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
3606 a0f32f33 2022-06-13 thomas s->nlines = 0;
3607 a0f32f33 2022-06-13 thomas return err;
3610 a0f32f33 2022-06-13 thomas static const struct got_error *
3611 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3612 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3613 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3614 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3616 48ae06ee 2018-10-18 stsp const struct got_error *err;
3617 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3619 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
3621 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3622 15a94983 2018-12-23 stsp int type1, type2;
3623 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3625 15a94983 2018-12-23 stsp return err;
3626 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3628 15a94983 2018-12-23 stsp return err;
3630 15a94983 2018-12-23 stsp if (type1 != type2)
3631 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3633 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3634 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3635 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3636 f44b1f58 2020-02-02 tracey s->repo = repo;
3637 f44b1f58 2020-02-02 tracey s->id1 = id1;
3638 f44b1f58 2020-02-02 tracey s->id2 = id2;
3639 3dbaef42 2020-11-24 stsp s->label1 = label1;
3640 3dbaef42 2020-11-24 stsp s->label2 = label2;
3642 15a94983 2018-12-23 stsp if (id1) {
3643 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3644 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3645 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3646 a0f32f33 2022-06-13 thomas s->f1 = got_opentemp();
3647 a0f32f33 2022-06-13 thomas if (s->f1 == NULL) {
3648 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3649 a0f32f33 2022-06-13 thomas goto done;
3652 5465d566 2020-02-01 tracey s->id1 = NULL;
3654 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3655 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3656 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
3657 a0f32f33 2022-06-13 thomas goto done;
3660 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
3661 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
3662 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3663 a0f32f33 2022-06-13 thomas goto done;
3666 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3667 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3668 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3669 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3670 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3671 5465d566 2020-02-01 tracey s->log_view = log_view;
3672 5465d566 2020-02-01 tracey s->repo = repo;
3674 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3675 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3676 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3677 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3678 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3680 a0f32f33 2022-06-13 thomas goto done;
3681 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3682 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3683 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3684 a0f32f33 2022-06-13 thomas if (err)
3685 a0f32f33 2022-06-13 thomas goto done;
3686 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3687 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3688 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3689 a0f32f33 2022-06-13 thomas if (err)
3690 a0f32f33 2022-06-13 thomas goto done;
3692 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3693 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3694 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3695 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3696 a0f32f33 2022-06-13 thomas if (err)
3697 a0f32f33 2022-06-13 thomas goto done;
3699 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3700 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3701 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3702 a0f32f33 2022-06-13 thomas if (err)
3703 a0f32f33 2022-06-13 thomas goto done;
3705 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3706 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3707 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3708 a0f32f33 2022-06-13 thomas if (err)
3709 a0f32f33 2022-06-13 thomas goto done;
3712 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3713 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3714 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3716 5465d566 2020-02-01 tracey err = create_diff(s);
3718 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3719 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3720 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3721 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3722 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3724 a0f32f33 2022-06-13 thomas if (err)
3725 a0f32f33 2022-06-13 thomas close_diff_view(view);
3726 e5a0f69f 2018-08-18 stsp return err;
3729 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3730 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3732 a3404814 2018-09-02 stsp const struct got_error *err;
3733 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3734 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3735 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3737 a3404814 2018-09-02 stsp if (s->id1) {
3738 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3740 a3404814 2018-09-02 stsp return err;
3741 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3743 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3745 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3747 a3404814 2018-09-02 stsp return err;
3748 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3750 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3751 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3752 a3404814 2018-09-02 stsp free(id_str1);
3753 a3404814 2018-09-02 stsp free(id_str2);
3754 a3404814 2018-09-02 stsp return err;
3756 a3404814 2018-09-02 stsp free(id_str1);
3757 a3404814 2018-09-02 stsp free(id_str2);
3759 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3760 267bb3b8 2021-08-01 stsp free(header);
3761 267bb3b8 2021-08-01 stsp return err;
3764 15a087fe 2019-02-21 stsp static const struct got_error *
3765 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3766 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3768 d7a04538 2019-02-21 stsp const struct got_error *err;
3769 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3770 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3771 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3773 15a087fe 2019-02-21 stsp free(s->id2);
3774 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3775 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3776 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3778 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3780 d7a04538 2019-02-21 stsp return err;
3781 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3782 15a087fe 2019-02-21 stsp free(s->id1);
3783 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3784 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3785 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3786 15a087fe 2019-02-21 stsp return NULL;
3789 0cf4efb1 2018-09-29 stsp static const struct got_error *
3790 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3792 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3793 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3794 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3795 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3796 826082fe 2020-12-10 stsp char *line = NULL;
3797 826082fe 2020-12-10 stsp size_t linesize = 0;
3798 826082fe 2020-12-10 stsp ssize_t linelen;
3799 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
3801 e5a0f69f 2018-08-18 stsp switch (ch) {
3804 3dbaef42 2020-11-24 stsp if (ch == 'a')
3805 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3806 3dbaef42 2020-11-24 stsp if (ch == 'w')
3807 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3808 64453f7e 2020-11-21 stsp wclear(view->window);
3809 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3810 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3811 aa61903a 2021-12-31 thomas s->matched_line = 0;
3812 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3813 64453f7e 2020-11-21 stsp err = create_diff(s);
3816 912a3f79 2021-08-30 j case KEY_HOME:
3817 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3820 912a3f79 2021-08-30 j case KEY_END:
3824 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3828 1e37a5c2 2019-05-12 jcs case KEY_UP:
3829 f7140bf5 2021-10-17 thomas case CTRL('p'):
3830 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3831 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3833 70f17a53 2022-06-13 thomas case CTRL('u'):
3834 23427b14 2022-06-23 thomas case 'u':
3835 70f17a53 2022-06-13 thomas nscroll /= 2;
3836 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3837 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3838 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3839 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3842 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
3843 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3846 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3847 f7140bf5 2021-10-17 thomas case CTRL('n'):
3848 1e37a5c2 2019-05-12 jcs if (!s->eof)
3849 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3851 70f17a53 2022-06-13 thomas case CTRL('d'):
3852 23427b14 2022-06-23 thomas case 'd':
3853 70f17a53 2022-06-13 thomas nscroll /= 2;
3854 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3855 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3856 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3858 00ba99a7 2019-05-12 jcs if (s->eof)
3861 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
3862 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3863 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3864 826082fe 2020-12-10 stsp if (linelen == -1) {
3865 826082fe 2020-12-10 stsp if (feof(s->f)) {
3866 826082fe 2020-12-10 stsp s->eof = 1;
3868 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3872 826082fe 2020-12-10 stsp free(line);
3875 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3876 1e37a5c2 2019-05-12 jcs s->diff_context--;
3877 aa61903a 2021-12-31 thomas s->matched_line = 0;
3878 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3879 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3880 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3881 27829c9e 2020-11-21 stsp s->nlines) {
3882 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3883 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3888 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3889 1e37a5c2 2019-05-12 jcs s->diff_context++;
3890 aa61903a 2021-12-31 thomas s->matched_line = 0;
3891 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3892 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3897 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3899 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3900 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3902 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3906 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3909 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3913 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3914 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3915 aa61903a 2021-12-31 thomas s->matched_line = 0;
3917 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3918 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3922 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3924 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3925 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3927 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3931 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3934 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3938 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3939 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3940 aa61903a 2021-12-31 thomas s->matched_line = 0;
3942 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3943 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3949 bcbd79e2 2018-08-19 stsp return err;
3952 4ed7e80c 2018-05-20 stsp static const struct got_error *
3953 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3955 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3956 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3957 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3958 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3959 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3960 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3961 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3962 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3963 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3964 3dbaef42 2020-11-24 stsp const char *errstr;
3965 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3966 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3968 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3969 26ed57b2 2018-05-19 stsp switch (ch) {
3971 64453f7e 2020-11-21 stsp force_text_diff = 1;
3974 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3976 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3977 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
3978 8f666e67 2022-02-12 thomas errstr, errstr);
3980 09b5bff8 2020-02-23 naddy case 'r':
3981 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3982 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3983 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3985 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3988 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3991 17020d27 2019-03-07 stsp usage_diff();
3992 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3996 26ed57b2 2018-05-19 stsp argc -= optind;
3997 26ed57b2 2018-05-19 stsp argv += optind;
3999 26ed57b2 2018-05-19 stsp if (argc == 0) {
4000 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4001 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4002 15a94983 2018-12-23 stsp id_str1 = argv[0];
4003 15a94983 2018-12-23 stsp id_str2 = argv[1];
4005 26ed57b2 2018-05-19 stsp usage_diff();
4007 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4008 7cd52833 2022-06-23 thomas if (error)
4009 7cd52833 2022-06-23 thomas goto done;
4011 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4012 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4013 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4014 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4015 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4016 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4017 c156c7a4 2020-12-18 stsp goto done;
4018 a273ac94 2020-02-23 naddy if (worktree)
4019 a273ac94 2020-02-23 naddy repo_path =
4020 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4022 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4023 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4024 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4025 c156c7a4 2020-12-18 stsp goto done;
4029 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4030 eb6600df 2019-01-04 stsp if (error)
4031 eb6600df 2019-01-04 stsp goto done;
4033 a273ac94 2020-02-23 naddy init_curses();
4035 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4036 51a10b52 2020-12-26 stsp if (error)
4037 51a10b52 2020-12-26 stsp goto done;
4039 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4040 26ed57b2 2018-05-19 stsp if (error)
4041 26ed57b2 2018-05-19 stsp goto done;
4043 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4044 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4045 26ed57b2 2018-05-19 stsp if (error)
4046 26ed57b2 2018-05-19 stsp goto done;
4048 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4049 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4050 26ed57b2 2018-05-19 stsp if (error)
4051 26ed57b2 2018-05-19 stsp goto done;
4053 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4054 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4055 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4056 ea5e7bb5 2018-08-01 stsp goto done;
4058 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4059 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4060 5dc9f4bc 2018-08-04 stsp if (error)
4061 5dc9f4bc 2018-08-04 stsp goto done;
4062 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4064 3dbaef42 2020-11-24 stsp free(label1);
4065 3dbaef42 2020-11-24 stsp free(label2);
4066 c02c541e 2019-03-29 stsp free(repo_path);
4067 a273ac94 2020-02-23 naddy free(cwd);
4068 1d0f4054 2021-06-17 stsp if (repo) {
4069 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4070 1d0f4054 2021-06-17 stsp if (error == NULL)
4071 1d0f4054 2021-06-17 stsp error = close_err;
4073 a273ac94 2020-02-23 naddy if (worktree)
4074 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4075 7cd52833 2022-06-23 thomas if (pack_fds) {
4076 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4077 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4078 7cd52833 2022-06-23 thomas if (error == NULL)
4079 7cd52833 2022-06-23 thomas error = pack_err;
4081 51a10b52 2020-12-26 stsp tog_free_refs();
4082 26ed57b2 2018-05-19 stsp return error;
4085 4ed7e80c 2018-05-20 stsp __dead static void
4086 9f7d7167 2018-04-29 stsp usage_blame(void)
4089 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4090 9f7d7167 2018-04-29 stsp getprogname());
4094 84451b3e 2018-07-10 stsp struct tog_blame_line {
4095 84451b3e 2018-07-10 stsp int annotated;
4096 84451b3e 2018-07-10 stsp struct got_object_id *id;
4099 4ed7e80c 2018-05-20 stsp static const struct got_error *
4100 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4102 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4103 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4104 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4105 84451b3e 2018-07-10 stsp const struct got_error *err;
4106 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4107 826082fe 2020-12-10 stsp char *line = NULL;
4108 826082fe 2020-12-10 stsp size_t linesize = 0;
4109 826082fe 2020-12-10 stsp ssize_t linelen;
4110 84451b3e 2018-07-10 stsp wchar_t *wline;
4111 27a741e5 2019-09-11 stsp int width;
4112 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4113 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4114 ab089a2a 2018-07-12 stsp char *id_str;
4115 11b20872 2019-11-08 stsp struct tog_color *tc;
4117 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4119 ab089a2a 2018-07-12 stsp return err;
4121 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4122 f7d12f7e 2018-08-01 stsp werase(view->window);
4124 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4125 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4126 ab089a2a 2018-07-12 stsp free(id_str);
4127 ab089a2a 2018-07-12 stsp return err;
4130 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4131 ab089a2a 2018-07-12 stsp free(line);
4132 2550e4c3 2018-07-13 stsp line = NULL;
4134 1cae65b4 2019-09-22 stsp return err;
4135 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4136 a3404814 2018-09-02 stsp wstandout(view->window);
4137 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4139 11b20872 2019-11-08 stsp wattr_on(view->window,
4140 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4141 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4143 11b20872 2019-11-08 stsp wattr_off(view->window,
4144 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4145 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4146 a3404814 2018-09-02 stsp wstandend(view->window);
4147 2550e4c3 2018-07-13 stsp free(wline);
4148 2550e4c3 2018-07-13 stsp wline = NULL;
4149 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4150 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4152 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4153 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4154 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4155 ab089a2a 2018-07-12 stsp free(id_str);
4156 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4158 ab089a2a 2018-07-12 stsp free(id_str);
4159 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4160 3f60a8ef 2018-07-10 stsp free(line);
4161 2550e4c3 2018-07-13 stsp line = NULL;
4163 3f60a8ef 2018-07-10 stsp return err;
4164 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4165 2550e4c3 2018-07-13 stsp free(wline);
4166 2550e4c3 2018-07-13 stsp wline = NULL;
4167 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4168 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4170 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4171 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4172 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4173 826082fe 2020-12-10 stsp if (linelen == -1) {
4174 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4175 826082fe 2020-12-10 stsp s->eof = 1;
4178 84451b3e 2018-07-10 stsp free(line);
4179 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4181 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4184 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4185 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4187 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4188 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4189 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4190 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4191 27a741e5 2019-09-11 stsp !(view->focussed &&
4192 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4193 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4194 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4195 8d0fe45a 2019-08-12 stsp char *id_str;
4196 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4197 8d0fe45a 2019-08-12 stsp if (err) {
4198 8d0fe45a 2019-08-12 stsp free(line);
4199 8d0fe45a 2019-08-12 stsp return err;
4201 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4203 11b20872 2019-11-08 stsp wattr_on(view->window,
4204 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4205 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4207 11b20872 2019-11-08 stsp wattr_off(view->window,
4208 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4209 8d0fe45a 2019-08-12 stsp free(id_str);
4210 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4212 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4213 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4216 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4217 ee41ec32 2018-07-10 stsp prev_id = NULL;
4220 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4221 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4222 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4224 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4225 41605754 2020-11-12 stsp width = 9;
4226 41605754 2020-11-12 stsp wline = wcsdup(L"");
4227 41605754 2020-11-12 stsp if (wline == NULL) {
4228 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4229 41605754 2020-11-12 stsp free(line);
4230 41605754 2020-11-12 stsp return err;
4232 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4233 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4234 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4235 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4236 41605754 2020-11-12 stsp view->window, regmatch);
4237 41605754 2020-11-12 stsp if (err) {
4238 41605754 2020-11-12 stsp free(line);
4239 41605754 2020-11-12 stsp return err;
4241 41605754 2020-11-12 stsp width += 9;
4243 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4244 41605754 2020-11-12 stsp view->ncols - 9, 9);
4245 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4246 41605754 2020-11-12 stsp free(wline);
4247 41605754 2020-11-12 stsp wline = NULL;
4248 41605754 2020-11-12 stsp width += 9;
4251 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4252 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4253 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4254 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4256 826082fe 2020-12-10 stsp free(line);
4257 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4259 1a57306a 2018-09-02 stsp view_vborder(view);
4261 84451b3e 2018-07-10 stsp return NULL;
4264 84451b3e 2018-07-10 stsp static const struct got_error *
4265 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4266 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4268 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4269 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4270 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4271 1a76625f 2018-10-22 stsp int errcode;
4273 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4274 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4275 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4277 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4278 1a76625f 2018-10-22 stsp if (errcode)
4279 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4281 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4282 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4283 d68a0a7d 2018-07-10 stsp goto done;
4286 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4287 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4289 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4290 d68a0a7d 2018-07-10 stsp if (line->annotated)
4291 d68a0a7d 2018-07-10 stsp goto done;
4293 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4294 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4295 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4296 84451b3e 2018-07-10 stsp goto done;
4298 84451b3e 2018-07-10 stsp line->annotated = 1;
4300 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4301 1a76625f 2018-10-22 stsp if (errcode)
4302 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4303 84451b3e 2018-07-10 stsp return err;
4306 84451b3e 2018-07-10 stsp static void *
4307 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4309 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4310 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4311 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4312 1a76625f 2018-10-22 stsp int errcode;
4314 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4316 61266923 2020-01-14 stsp return (void *)err;
4318 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4319 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4320 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4321 fc06ba56 2019-08-22 stsp err = NULL;
4323 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4324 1a76625f 2018-10-22 stsp if (errcode)
4325 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4326 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4328 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4329 1d0f4054 2021-06-17 stsp if (err == NULL)
4330 1d0f4054 2021-06-17 stsp err = close_err;
4331 c9beca56 2018-07-22 stsp ta->repo = NULL;
4332 c9beca56 2018-07-22 stsp *ta->complete = 1;
4334 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4335 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4336 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4338 18430de3 2018-07-10 stsp return (void *)err;
4341 245d91c1 2018-07-12 stsp static struct got_object_id *
4342 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4343 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4345 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4347 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4348 8d0fe45a 2019-08-12 stsp return NULL;
4350 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4351 245d91c1 2018-07-12 stsp if (!line->annotated)
4352 245d91c1 2018-07-12 stsp return NULL;
4354 245d91c1 2018-07-12 stsp return line->id;
4357 b880a918 2018-07-10 stsp static const struct got_error *
4358 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4360 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4363 245d91c1 2018-07-12 stsp if (blame->thread) {
4364 1a76625f 2018-10-22 stsp int errcode;
4365 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4366 1a76625f 2018-10-22 stsp if (errcode)
4367 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4368 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4369 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4370 1a76625f 2018-10-22 stsp if (errcode)
4371 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4372 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4373 1a76625f 2018-10-22 stsp if (errcode)
4374 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4375 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4376 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4377 245d91c1 2018-07-12 stsp err = NULL;
4378 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4380 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4381 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4382 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4383 1d0f4054 2021-06-17 stsp if (err == NULL)
4384 1d0f4054 2021-06-17 stsp err = close_err;
4385 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4387 245d91c1 2018-07-12 stsp if (blame->f) {
4388 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4389 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4390 245d91c1 2018-07-12 stsp blame->f = NULL;
4392 57670559 2018-12-23 stsp if (blame->lines) {
4393 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4394 57670559 2018-12-23 stsp free(blame->lines[i].id);
4395 57670559 2018-12-23 stsp free(blame->lines);
4396 57670559 2018-12-23 stsp blame->lines = NULL;
4398 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4399 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4400 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
4401 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4402 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
4403 7cd52833 2022-06-23 thomas if (err == NULL)
4404 7cd52833 2022-06-23 thomas err = pack_err;
4405 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
4407 245d91c1 2018-07-12 stsp return err;
4410 245d91c1 2018-07-12 stsp static const struct got_error *
4411 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4413 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4414 fc06ba56 2019-08-22 stsp int *done = arg;
4415 fc06ba56 2019-08-22 stsp int errcode;
4417 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4418 fc06ba56 2019-08-22 stsp if (errcode)
4419 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4420 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4422 fc06ba56 2019-08-22 stsp if (*done)
4423 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4425 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4426 fc06ba56 2019-08-22 stsp if (errcode)
4427 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4428 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4430 fc06ba56 2019-08-22 stsp return err;
4433 fc06ba56 2019-08-22 stsp static const struct got_error *
4434 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4436 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4437 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4438 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4439 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4440 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4441 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4442 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4443 15a94983 2018-12-23 stsp int obj_type;
4444 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4446 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4447 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4449 15a94983 2018-12-23 stsp return err;
4451 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4452 945f9229 2022-04-16 thomas if (err)
4453 945f9229 2022-04-16 thomas goto done;
4455 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4457 84451b3e 2018-07-10 stsp goto done;
4459 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4460 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4461 84451b3e 2018-07-10 stsp goto done;
4464 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4466 a70480e0 2018-06-23 stsp goto done;
4467 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4468 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4469 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4470 84451b3e 2018-07-10 stsp goto done;
4472 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4473 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4475 1fddf795 2021-01-20 stsp goto done;
4476 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4477 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4478 84451b3e 2018-07-10 stsp goto done;
4481 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4482 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4483 b02560ec 2019-08-19 stsp blame->nlines--;
4485 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4486 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4487 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4488 84451b3e 2018-07-10 stsp goto done;
4491 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
4493 bd24772e 2018-07-11 stsp goto done;
4494 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4495 7cd52833 2022-06-23 thomas pack_fds);
4496 7cd52833 2022-06-23 thomas if (err)
4497 7cd52833 2022-06-23 thomas goto done;
4499 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
4500 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4501 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4502 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4503 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4504 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4505 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4506 245d91c1 2018-07-12 stsp goto done;
4508 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4510 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4511 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4512 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4513 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4514 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4515 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4516 a5388363 2020-12-01 naddy s->blame_complete = 0;
4518 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4519 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4520 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4521 f5a09613 2020-12-13 naddy s->selected_line = 1;
4523 aa61903a 2021-12-31 thomas s->matched_line = 0;
4526 945f9229 2022-04-16 thomas if (commit)
4527 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4529 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4530 27d434c2 2018-09-15 stsp free(obj_id);
4532 245d91c1 2018-07-12 stsp stop_blame(blame);
4533 245d91c1 2018-07-12 stsp return err;
4536 245d91c1 2018-07-12 stsp static const struct got_error *
4537 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4538 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4540 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4541 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4543 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4545 c4843652 2019-08-12 stsp s->path = strdup(path);
4546 c4843652 2019-08-12 stsp if (s->path == NULL)
4547 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4549 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4550 c4843652 2019-08-12 stsp if (err) {
4551 c4843652 2019-08-12 stsp free(s->path);
4552 7cbe629d 2018-08-04 stsp return err;
4555 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4556 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4557 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4558 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4559 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4560 fb2756b9 2018-08-04 stsp s->repo = repo;
4561 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4562 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4564 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4565 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4566 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4567 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4569 11b20872 2019-11-08 stsp return err;
4572 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4573 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4574 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4575 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4576 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4578 a5388363 2020-12-01 naddy return run_blame(view);
4581 e5a0f69f 2018-08-18 stsp static const struct got_error *
4582 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4584 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4585 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4587 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4588 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4590 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4591 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4592 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4593 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4594 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4597 e5a0f69f 2018-08-18 stsp free(s->path);
4598 11b20872 2019-11-08 stsp free_colors(&s->colors);
4599 e5a0f69f 2018-08-18 stsp return err;
4602 7cbe629d 2018-08-04 stsp static const struct got_error *
4603 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4605 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4607 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4608 6c4c42e0 2019-06-24 stsp return NULL;
4611 6c4c42e0 2019-06-24 stsp static const struct got_error *
4612 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4614 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4615 6c4c42e0 2019-06-24 stsp int lineno;
4616 826082fe 2020-12-10 stsp char *line = NULL;
4617 826082fe 2020-12-10 stsp size_t linesize = 0;
4618 826082fe 2020-12-10 stsp ssize_t linelen;
4620 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4621 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4622 6c4c42e0 2019-06-24 stsp return NULL;
4625 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4626 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4627 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4629 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4631 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4633 6c4c42e0 2019-06-24 stsp while (1) {
4634 6c4c42e0 2019-06-24 stsp off_t offset;
4636 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4637 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4638 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4642 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4643 6c4c42e0 2019-06-24 stsp lineno = 1;
4645 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4648 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4649 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4650 6c4c42e0 2019-06-24 stsp free(line);
4651 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4653 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4654 826082fe 2020-12-10 stsp if (linelen != -1 &&
4655 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4656 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4657 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4660 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4665 826082fe 2020-12-10 stsp free(line);
4667 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4668 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4669 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4672 6c4c42e0 2019-06-24 stsp return NULL;
4675 6c4c42e0 2019-06-24 stsp static const struct got_error *
4676 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4678 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4679 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4680 2b380cc8 2018-10-24 stsp int errcode;
4682 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4683 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4684 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4685 2b380cc8 2018-10-24 stsp if (errcode)
4686 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4688 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4691 51fe7530 2019-08-19 stsp if (s->blame_complete)
4692 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4694 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4696 669b5ffa 2018-10-07 stsp view_vborder(view);
4697 e5a0f69f 2018-08-18 stsp return err;
4700 e5a0f69f 2018-08-18 stsp static const struct got_error *
4701 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4703 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4704 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4705 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4706 70f17a53 2022-06-13 thomas int begin_x = 0, nscroll = view->nlines - 2;
4708 e5a0f69f 2018-08-18 stsp switch (ch) {
4710 1e37a5c2 2019-05-12 jcs s->done = 1;
4712 4deef56f 2021-09-02 naddy case 'g':
4713 4deef56f 2021-09-02 naddy case KEY_HOME:
4714 4deef56f 2021-09-02 naddy s->selected_line = 1;
4715 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4717 4deef56f 2021-09-02 naddy case 'G':
4718 4deef56f 2021-09-02 naddy case KEY_END:
4719 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4720 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4721 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4723 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4724 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4725 4deef56f 2021-09-02 naddy (view->nlines - 3);
4729 1e37a5c2 2019-05-12 jcs case KEY_UP:
4730 f7140bf5 2021-10-17 thomas case CTRL('p'):
4731 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4732 1e37a5c2 2019-05-12 jcs s->selected_line--;
4733 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4734 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4735 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4737 70f17a53 2022-06-13 thomas case CTRL('u'):
4738 23427b14 2022-06-23 thomas case 'u':
4739 70f17a53 2022-06-13 thomas nscroll /= 2;
4740 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4741 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4742 ea025d1d 2020-02-22 naddy case CTRL('b'):
4743 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4744 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
4747 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
4748 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
4750 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4753 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4754 f7140bf5 2021-10-17 thomas case CTRL('n'):
4755 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4756 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4757 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4758 1e37a5c2 2019-05-12 jcs s->selected_line++;
4759 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4760 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4761 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4764 1e37a5c2 2019-05-12 jcs case 'p': {
4765 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4766 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4767 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4768 1e37a5c2 2019-05-12 jcs if (id == NULL)
4770 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4771 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4772 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4773 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4774 1e37a5c2 2019-05-12 jcs int obj_type;
4775 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4776 1e37a5c2 2019-05-12 jcs s->repo, id);
4779 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4780 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4781 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4782 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4785 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4786 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4787 ec242592 2022-04-22 thomas s->repo, &pid->id);
4788 945f9229 2022-04-16 thomas if (err)
4790 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4791 945f9229 2022-04-16 thomas pcommit, s->path);
4792 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4793 e5a0f69f 2018-08-18 stsp if (err) {
4794 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4795 1e37a5c2 2019-05-12 jcs err = NULL;
4796 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4799 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4801 1e37a5c2 2019-05-12 jcs free(blob_id);
4802 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4803 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4804 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4807 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4808 ec242592 2022-04-22 thomas &pid->id);
4809 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4811 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4812 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
4814 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4819 1e37a5c2 2019-05-12 jcs s->done = 1;
4820 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4821 1e37a5c2 2019-05-12 jcs s->done = 0;
4822 1e37a5c2 2019-05-12 jcs if (thread_err)
4824 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4825 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4826 a5388363 2020-12-01 naddy err = run_blame(view);
4831 1e37a5c2 2019-05-12 jcs case 'B': {
4832 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4833 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4834 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
4836 1e37a5c2 2019-05-12 jcs s->done = 1;
4837 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4838 1e37a5c2 2019-05-12 jcs s->done = 0;
4839 1e37a5c2 2019-05-12 jcs if (thread_err)
4841 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4842 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4843 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4844 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4845 a5388363 2020-12-01 naddy err = run_blame(view);
4850 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4851 1e37a5c2 2019-05-12 jcs case '\r': {
4852 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4853 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4854 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4855 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4856 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4857 1e37a5c2 2019-05-12 jcs if (id == NULL)
4859 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4862 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4863 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4864 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4865 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4866 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4867 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4868 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4869 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4872 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4873 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4874 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4876 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4879 e78dc838 2020-12-04 stsp view->focussed = 0;
4880 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4881 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4882 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4885 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4886 e78dc838 2020-12-04 stsp view->focus_child = 1;
4888 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4893 70f17a53 2022-06-13 thomas case CTRL('d'):
4894 23427b14 2022-06-23 thomas case 'd':
4895 70f17a53 2022-06-13 thomas nscroll /= 2;
4896 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4897 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4898 ea025d1d 2020-02-22 naddy case CTRL('f'):
4900 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4901 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4902 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4905 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4906 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4907 70f17a53 2022-06-13 thomas s->selected_line +=
4908 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
4909 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
4911 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
4912 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
4914 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4915 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
4917 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4918 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4919 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4920 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4926 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4929 a70480e0 2018-06-23 stsp static const struct got_error *
4930 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4932 a70480e0 2018-06-23 stsp const struct got_error *error;
4933 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4934 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4935 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4936 0587e10c 2020-07-23 stsp char *link_target = NULL;
4937 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4938 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4939 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4941 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4942 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4944 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4945 a70480e0 2018-06-23 stsp switch (ch) {
4947 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4950 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4951 69069811 2018-08-02 stsp if (repo_path == NULL)
4952 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4956 17020d27 2019-03-07 stsp usage_blame();
4957 a70480e0 2018-06-23 stsp /* NOTREACHED */
4961 a70480e0 2018-06-23 stsp argc -= optind;
4962 a70480e0 2018-06-23 stsp argv += optind;
4964 f135c941 2020-02-20 stsp if (argc != 1)
4965 a70480e0 2018-06-23 stsp usage_blame();
4967 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4968 7cd52833 2022-06-23 thomas if (error != NULL)
4969 7cd52833 2022-06-23 thomas goto done;
4971 69069811 2018-08-02 stsp if (repo_path == NULL) {
4972 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4973 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4974 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4975 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4976 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4977 c156c7a4 2020-12-18 stsp goto done;
4978 f135c941 2020-02-20 stsp if (worktree)
4979 eb41ed75 2019-02-05 stsp repo_path =
4980 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4982 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4983 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4984 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4985 c156c7a4 2020-12-18 stsp goto done;
4989 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4990 c02c541e 2019-03-29 stsp if (error != NULL)
4991 8e94dd5b 2019-01-04 stsp goto done;
4993 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4994 f135c941 2020-02-20 stsp worktree);
4995 c02c541e 2019-03-29 stsp if (error)
4996 92205607 2019-01-04 stsp goto done;
4998 f135c941 2020-02-20 stsp init_curses();
5000 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5001 51a10b52 2020-12-26 stsp if (error)
5002 51a10b52 2020-12-26 stsp goto done;
5004 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5005 eb41ed75 2019-02-05 stsp if (error)
5006 69069811 2018-08-02 stsp goto done;
5008 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5009 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5010 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5011 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5012 a70480e0 2018-06-23 stsp if (error != NULL)
5013 66b4983c 2018-06-23 stsp goto done;
5014 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5015 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5017 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5018 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5020 a19e88aa 2018-06-23 stsp if (error != NULL)
5021 8b473291 2019-02-21 stsp goto done;
5023 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5024 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5025 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5026 e1cd8fed 2018-08-01 stsp goto done;
5029 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5030 945f9229 2022-04-16 thomas if (error)
5031 945f9229 2022-04-16 thomas goto done;
5033 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5034 945f9229 2022-04-16 thomas commit, repo);
5035 7cbe629d 2018-08-04 stsp if (error)
5036 7cbe629d 2018-08-04 stsp goto done;
5038 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5039 78756c87 2020-11-24 stsp commit_id, repo);
5040 0587e10c 2020-07-23 stsp if (error)
5041 0587e10c 2020-07-23 stsp goto done;
5042 12314ad4 2019-08-31 stsp if (worktree) {
5043 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5044 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5045 12314ad4 2019-08-31 stsp worktree = NULL;
5047 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5049 69069811 2018-08-02 stsp free(repo_path);
5050 f135c941 2020-02-20 stsp free(in_repo_path);
5051 0587e10c 2020-07-23 stsp free(link_target);
5052 69069811 2018-08-02 stsp free(cwd);
5053 a70480e0 2018-06-23 stsp free(commit_id);
5054 945f9229 2022-04-16 thomas if (commit)
5055 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5056 eb41ed75 2019-02-05 stsp if (worktree)
5057 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5058 1d0f4054 2021-06-17 stsp if (repo) {
5059 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5060 1d0f4054 2021-06-17 stsp if (error == NULL)
5061 1d0f4054 2021-06-17 stsp error = close_err;
5063 7cd52833 2022-06-23 thomas if (pack_fds) {
5064 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5065 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5066 7cd52833 2022-06-23 thomas if (error == NULL)
5067 7cd52833 2022-06-23 thomas error = pack_err;
5069 51a10b52 2020-12-26 stsp tog_free_refs();
5070 a70480e0 2018-06-23 stsp return error;
5073 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5074 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5076 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5077 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5078 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5079 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5080 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5081 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5082 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5084 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5086 f7d12f7e 2018-08-01 stsp werase(view->window);
5088 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5089 ffd1d5e5 2018-06-23 stsp return NULL;
5091 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5093 ffd1d5e5 2018-06-23 stsp return err;
5094 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5095 a3404814 2018-09-02 stsp wstandout(view->window);
5096 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5098 11b20872 2019-11-08 stsp wattr_on(view->window,
5099 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5100 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5102 11b20872 2019-11-08 stsp wattr_off(view->window,
5103 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5104 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5105 a3404814 2018-09-02 stsp wstandend(view->window);
5106 2550e4c3 2018-07-13 stsp free(wline);
5107 2550e4c3 2018-07-13 stsp wline = NULL;
5108 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5109 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5110 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5111 ffd1d5e5 2018-06-23 stsp return NULL;
5112 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
5114 ce52c690 2018-06-23 stsp return err;
5115 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5116 2550e4c3 2018-07-13 stsp free(wline);
5117 2550e4c3 2018-07-13 stsp wline = NULL;
5118 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5119 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5120 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5121 ffd1d5e5 2018-06-23 stsp return NULL;
5122 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5123 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5124 a1eca9bb 2018-06-23 stsp return NULL;
5126 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5127 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5128 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5129 0cf4efb1 2018-09-29 stsp if (view->focussed)
5130 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5131 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5133 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5134 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5135 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5136 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5137 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5138 ffd1d5e5 2018-06-23 stsp return NULL;
5142 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5145 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5146 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5147 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5148 848d6979 2019-08-12 stsp const char *modestr = "";
5149 56e0773d 2019-11-28 stsp mode_t mode;
5151 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5152 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5154 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5155 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5156 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5158 638f9024 2019-05-13 stsp return got_error_from_errno(
5159 230a42bd 2019-05-11 jcs "got_object_id_str");
5161 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5162 63c5ca5d 2019-08-24 stsp modestr = "$";
5163 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5166 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5167 d86d3b18 2020-12-01 naddy te, s->repo);
5168 0d6c6ee3 2020-05-20 stsp if (err) {
5169 0d6c6ee3 2020-05-20 stsp free(id_str);
5170 0d6c6ee3 2020-05-20 stsp return err;
5172 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5173 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5174 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5176 848d6979 2019-08-12 stsp modestr = "@";
5178 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5179 848d6979 2019-08-12 stsp modestr = "/";
5180 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5181 848d6979 2019-08-12 stsp modestr = "*";
5182 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5183 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5184 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5185 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5186 1d13200f 2018-07-12 stsp free(id_str);
5187 0d6c6ee3 2020-05-20 stsp free(link_target);
5188 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5190 1d13200f 2018-07-12 stsp free(id_str);
5191 0d6c6ee3 2020-05-20 stsp free(link_target);
5192 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5193 ffd1d5e5 2018-06-23 stsp if (err) {
5194 ffd1d5e5 2018-06-23 stsp free(line);
5197 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5198 0cf4efb1 2018-09-29 stsp if (view->focussed)
5199 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5200 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5202 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5204 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5205 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5206 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5208 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5209 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5210 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5211 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5212 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5213 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5214 ffd1d5e5 2018-06-23 stsp free(line);
5215 2550e4c3 2018-07-13 stsp free(wline);
5216 2550e4c3 2018-07-13 stsp wline = NULL;
5218 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5219 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5220 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5224 ffd1d5e5 2018-06-23 stsp return err;
5227 ffd1d5e5 2018-06-23 stsp static void
5228 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5230 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5231 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5232 fa86c4bf 2020-11-29 stsp int i = 0;
5234 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5237 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5238 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5239 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5240 fa86c4bf 2020-11-29 stsp if (!isroot)
5241 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5244 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5245 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5249 fa86c4bf 2020-11-29 stsp static void
5250 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5252 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5253 ffd1d5e5 2018-06-23 stsp int n = 0;
5255 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5256 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5257 694d3271 2020-12-01 naddy s->first_displayed_entry);
5259 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5261 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5262 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5263 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5264 768394f3 2019-01-24 stsp if (last) {
5265 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5266 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5271 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5272 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5273 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5275 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5276 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5277 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5279 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5280 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5281 56e0773d 2019-11-28 stsp + 1 /* slash */;
5283 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5285 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5286 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5287 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5289 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5290 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5291 d9765a41 2018-06-23 stsp while (pt) {
5292 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5293 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5294 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5295 cb2ebc8a 2018-06-23 stsp goto done;
5297 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5298 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5299 cb2ebc8a 2018-06-23 stsp goto done;
5301 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5304 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5305 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5306 ce52c690 2018-06-23 stsp goto done;
5310 ce52c690 2018-06-23 stsp if (err) {
5311 ce52c690 2018-06-23 stsp free(*path);
5312 ce52c690 2018-06-23 stsp *path = NULL;
5314 ce52c690 2018-06-23 stsp return err;
5317 ce52c690 2018-06-23 stsp static const struct got_error *
5318 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5319 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5320 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5322 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5323 ce52c690 2018-06-23 stsp char *path;
5324 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5326 a0de39f3 2019-08-09 stsp *new_view = NULL;
5328 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5330 ce52c690 2018-06-23 stsp return err;
5332 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5333 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5334 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5335 83ce39e3 2019-08-12 stsp goto done;
5338 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5339 e5a0f69f 2018-08-18 stsp if (err) {
5340 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5341 fc06ba56 2019-08-22 stsp err = NULL;
5342 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5344 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5346 83ce39e3 2019-08-12 stsp free(path);
5347 69efd4c4 2018-07-18 stsp return err;
5350 69efd4c4 2018-07-18 stsp static const struct got_error *
5351 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5352 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5354 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5355 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5356 69efd4c4 2018-07-18 stsp char *path;
5358 a0de39f3 2019-08-09 stsp *new_view = NULL;
5360 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5361 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5362 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5364 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5366 69efd4c4 2018-07-18 stsp return err;
5368 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5371 e5a0f69f 2018-08-18 stsp view_close(log_view);
5373 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5374 cb2ebc8a 2018-06-23 stsp free(path);
5375 cb2ebc8a 2018-06-23 stsp return err;
5378 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5379 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5380 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5382 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5383 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5384 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5385 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5387 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5388 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5390 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5391 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5392 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5394 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5396 bc573f3b 2021-07-10 stsp goto done;
5399 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5400 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5401 bc573f3b 2021-07-10 stsp * closed on demand.
5403 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5404 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5406 bc573f3b 2021-07-10 stsp goto done;
5407 bc573f3b 2021-07-10 stsp s->tree = s->root;
5409 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5410 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5411 ffd1d5e5 2018-06-23 stsp goto done;
5413 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5414 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5415 ffd1d5e5 2018-06-23 stsp goto done;
5418 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5419 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5420 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5421 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5422 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5423 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5424 9cd7cbd1 2020-12-07 stsp goto done;
5427 fb2756b9 2018-08-04 stsp s->repo = repo;
5429 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5430 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5431 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5432 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5434 c0b01bdb 2019-11-08 stsp goto done;
5435 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5436 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5438 c0b01bdb 2019-11-08 stsp goto done;
5439 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5440 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5441 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5443 c0b01bdb 2019-11-08 stsp goto done;
5445 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5446 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5447 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5449 11b20872 2019-11-08 stsp goto done;
5451 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5452 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5454 c0b01bdb 2019-11-08 stsp goto done;
5457 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5458 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5459 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5460 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5461 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5463 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5464 bc573f3b 2021-07-10 stsp if (commit)
5465 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5467 bc573f3b 2021-07-10 stsp close_tree_view(view);
5468 ad80ab7b 2018-08-04 stsp return err;
5471 e5a0f69f 2018-08-18 stsp static const struct got_error *
5472 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5474 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5476 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5477 fb2756b9 2018-08-04 stsp free(s->tree_label);
5478 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5479 6484ec90 2018-09-29 stsp free(s->commit_id);
5480 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5481 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5482 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5483 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5484 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5485 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5486 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5487 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5488 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5489 ad80ab7b 2018-08-04 stsp free(parent);
5492 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5493 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5494 bc573f3b 2021-07-10 stsp if (s->root)
5495 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5496 7c32bd05 2019-06-22 stsp return NULL;
5499 7c32bd05 2019-06-22 stsp static const struct got_error *
5500 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5502 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5504 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5505 7c32bd05 2019-06-22 stsp return NULL;
5508 7c32bd05 2019-06-22 stsp static int
5509 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5511 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5513 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5517 7c32bd05 2019-06-22 stsp static const struct got_error *
5518 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5520 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5521 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5523 7c32bd05 2019-06-22 stsp if (!view->searching) {
5524 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5525 7c32bd05 2019-06-22 stsp return NULL;
5528 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5529 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5530 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5531 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5532 56e0773d 2019-11-28 stsp s->selected_entry);
5534 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5536 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5537 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5539 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5540 56e0773d 2019-11-28 stsp s->selected_entry);
5543 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5544 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5545 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5546 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5548 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5551 7c32bd05 2019-06-22 stsp while (1) {
5552 56e0773d 2019-11-28 stsp if (te == NULL) {
5553 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5554 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5555 ac66afb8 2019-06-24 stsp return NULL;
5557 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5558 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5560 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5563 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5564 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5565 56e0773d 2019-11-28 stsp s->matched_entry = te;
5569 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5570 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5572 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5575 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5576 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5577 7c32bd05 2019-06-22 stsp s->selected = 0;
5580 e5a0f69f 2018-08-18 stsp return NULL;
5583 ad80ab7b 2018-08-04 stsp static const struct got_error *
5584 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5586 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5587 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5588 e5a0f69f 2018-08-18 stsp char *parent_path;
5590 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5592 e5a0f69f 2018-08-18 stsp return err;
5594 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5595 e5a0f69f 2018-08-18 stsp free(parent_path);
5597 669b5ffa 2018-10-07 stsp view_vborder(view);
5598 e5a0f69f 2018-08-18 stsp return err;
5601 e5a0f69f 2018-08-18 stsp static const struct got_error *
5602 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5604 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5605 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5606 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5607 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5608 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 3;
5610 e5a0f69f 2018-08-18 stsp switch (ch) {
5612 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5615 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5617 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5618 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5619 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5620 e78dc838 2020-12-04 stsp view->focussed = 0;
5621 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5623 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5625 1e37a5c2 2019-05-12 jcs return err;
5626 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5627 e78dc838 2020-12-04 stsp view->focus_child = 1;
5629 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5632 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5633 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5634 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5635 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5636 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5637 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5638 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5639 152c1c93 2020-11-29 stsp if (err) {
5640 152c1c93 2020-11-29 stsp view_close(ref_view);
5641 152c1c93 2020-11-29 stsp return err;
5643 e78dc838 2020-12-04 stsp view->focussed = 0;
5644 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5645 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5646 152c1c93 2020-11-29 stsp err = view_close_child(view);
5648 152c1c93 2020-11-29 stsp return err;
5649 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5650 e78dc838 2020-12-04 stsp view->focus_child = 1;
5652 152c1c93 2020-11-29 stsp *new_view = ref_view;
5654 e4526bf5 2021-09-03 naddy case 'g':
5655 e4526bf5 2021-09-03 naddy case KEY_HOME:
5656 e4526bf5 2021-09-03 naddy s->selected = 0;
5657 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5658 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5659 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5661 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5663 e4526bf5 2021-09-03 naddy case 'G':
5664 e4526bf5 2021-09-03 naddy case KEY_END:
5665 e4526bf5 2021-09-03 naddy s->selected = 0;
5666 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5667 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5668 e4526bf5 2021-09-03 naddy if (te == NULL) {
5669 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5670 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5675 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5676 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5678 e4526bf5 2021-09-03 naddy if (n > 0)
5679 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5682 1e37a5c2 2019-05-12 jcs case KEY_UP:
5683 f7140bf5 2021-10-17 thomas case CTRL('p'):
5684 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5685 1e37a5c2 2019-05-12 jcs s->selected--;
5688 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5690 70f17a53 2022-06-13 thomas case CTRL('u'):
5691 23427b14 2022-06-23 thomas case 'u':
5692 70f17a53 2022-06-13 thomas nscroll /= 2;
5693 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5694 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5695 ea025d1d 2020-02-22 naddy case CTRL('b'):
5696 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5697 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5698 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5699 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5701 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5702 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5704 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
5707 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5708 f7140bf5 2021-10-17 thomas case CTRL('n'):
5709 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5710 1e37a5c2 2019-05-12 jcs s->selected++;
5713 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5715 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5717 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5719 70f17a53 2022-06-13 thomas case CTRL('d'):
5720 23427b14 2022-06-23 thomas case 'd':
5721 70f17a53 2022-06-13 thomas nscroll /= 2;
5722 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5723 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5724 ea025d1d 2020-02-22 naddy case CTRL('f'):
5725 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5727 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5728 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5729 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
5730 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
5733 70f17a53 2022-06-13 thomas tree_scroll_down(s, nscroll);
5735 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5737 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5738 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5739 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5740 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5741 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5743 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5744 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5746 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5747 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5748 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5749 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5750 1e37a5c2 2019-05-12 jcs s->selected_entry =
5751 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5752 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5753 1e37a5c2 2019-05-12 jcs free(parent);
5754 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5755 56e0773d 2019-11-28 stsp s->selected_entry))) {
5756 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5757 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5758 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5761 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5762 941e9f74 2019-05-21 stsp if (err) {
5763 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5766 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5767 56e0773d 2019-11-28 stsp s->selected_entry))) {
5768 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5769 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5770 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5772 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5773 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5774 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5777 e78dc838 2020-12-04 stsp view->focussed = 0;
5778 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5779 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5780 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5782 669b5ffa 2018-10-07 stsp return err;
5783 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5784 e78dc838 2020-12-04 stsp view->focus_child = 1;
5786 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5789 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5790 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5791 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5797 ffd1d5e5 2018-06-23 stsp return err;
5800 ffd1d5e5 2018-06-23 stsp __dead static void
5801 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5804 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5805 ffd1d5e5 2018-06-23 stsp getprogname());
5809 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5810 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5812 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5813 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5814 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5815 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5816 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5817 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5818 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5819 4e97c21c 2020-12-06 stsp char *label = NULL;
5820 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5821 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5823 5221c383 2018-08-01 stsp struct tog_view *view;
5824 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5826 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5827 ffd1d5e5 2018-06-23 stsp switch (ch) {
5829 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5832 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5833 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5834 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5838 e99e2d15 2020-11-24 naddy usage_tree();
5839 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5843 ffd1d5e5 2018-06-23 stsp argc -= optind;
5844 ffd1d5e5 2018-06-23 stsp argv += optind;
5846 55cccc34 2020-02-20 stsp if (argc > 1)
5847 e99e2d15 2020-11-24 naddy usage_tree();
5849 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5850 7cd52833 2022-06-23 thomas if (error != NULL)
5851 7cd52833 2022-06-23 thomas goto done;
5853 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5854 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5855 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5856 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5857 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5858 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5859 c156c7a4 2020-12-18 stsp goto done;
5860 55cccc34 2020-02-20 stsp if (worktree)
5861 52185f70 2019-02-05 stsp repo_path =
5862 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5864 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5865 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5866 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5867 c156c7a4 2020-12-18 stsp goto done;
5871 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5872 c02c541e 2019-03-29 stsp if (error != NULL)
5873 52185f70 2019-02-05 stsp goto done;
5875 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5876 55cccc34 2020-02-20 stsp repo, worktree);
5877 55cccc34 2020-02-20 stsp if (error)
5878 55cccc34 2020-02-20 stsp goto done;
5880 55cccc34 2020-02-20 stsp init_curses();
5882 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5883 c02c541e 2019-03-29 stsp if (error)
5884 52185f70 2019-02-05 stsp goto done;
5886 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5887 51a10b52 2020-12-26 stsp if (error)
5888 51a10b52 2020-12-26 stsp goto done;
5890 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5891 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5892 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5893 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5894 4e97c21c 2020-12-06 stsp if (error)
5895 4e97c21c 2020-12-06 stsp goto done;
5896 4e97c21c 2020-12-06 stsp head_ref_name = label;
5898 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5899 4e97c21c 2020-12-06 stsp if (error == NULL)
5900 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5901 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5902 4e97c21c 2020-12-06 stsp goto done;
5903 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5904 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5905 4e97c21c 2020-12-06 stsp if (error)
5906 4e97c21c 2020-12-06 stsp goto done;
5909 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5910 945f9229 2022-04-16 thomas if (error)
5911 945f9229 2022-04-16 thomas goto done;
5913 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5914 5221c383 2018-08-01 stsp if (view == NULL) {
5915 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5916 5221c383 2018-08-01 stsp goto done;
5918 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5919 ad80ab7b 2018-08-04 stsp if (error)
5920 ad80ab7b 2018-08-04 stsp goto done;
5921 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5922 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
5923 d91faf3b 2020-12-01 naddy in_repo_path);
5924 55cccc34 2020-02-20 stsp if (error)
5925 55cccc34 2020-02-20 stsp goto done;
5928 55cccc34 2020-02-20 stsp if (worktree) {
5929 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5930 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5931 55cccc34 2020-02-20 stsp worktree = NULL;
5933 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5935 52185f70 2019-02-05 stsp free(repo_path);
5936 e4a0e26d 2020-02-20 stsp free(cwd);
5937 ffd1d5e5 2018-06-23 stsp free(commit_id);
5938 4e97c21c 2020-12-06 stsp free(label);
5940 486cd271 2020-12-06 stsp got_ref_close(ref);
5941 1d0f4054 2021-06-17 stsp if (repo) {
5942 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5943 1d0f4054 2021-06-17 stsp if (error == NULL)
5944 1d0f4054 2021-06-17 stsp error = close_err;
5946 7cd52833 2022-06-23 thomas if (pack_fds) {
5947 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5948 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5949 7cd52833 2022-06-23 thomas if (error == NULL)
5950 7cd52833 2022-06-23 thomas error = pack_err;
5952 51a10b52 2020-12-26 stsp tog_free_refs();
5953 ffd1d5e5 2018-06-23 stsp return error;
5956 6458efa5 2020-11-24 stsp static const struct got_error *
5957 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5959 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5960 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5962 6458efa5 2020-11-24 stsp s->nrefs = 0;
5963 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5964 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
5965 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
5966 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
5967 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
5970 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5971 6458efa5 2020-11-24 stsp if (re == NULL)
5972 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5974 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5975 8924d611 2020-12-26 stsp if (re->ref == NULL)
5976 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5977 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5978 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5981 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5982 6458efa5 2020-11-24 stsp return NULL;
5986 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5988 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5990 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5991 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5992 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5993 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5998 6458efa5 2020-11-24 stsp static const struct got_error *
5999 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6001 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6002 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6004 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6005 6458efa5 2020-11-24 stsp s->repo = repo;
6007 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6008 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6010 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6012 6458efa5 2020-11-24 stsp return err;
6014 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6015 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6016 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6017 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6019 6458efa5 2020-11-24 stsp goto done;
6021 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6022 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6023 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6025 6458efa5 2020-11-24 stsp goto done;
6027 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6028 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6029 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6030 2183bbf6 2022-01-23 thomas if (err)
6031 2183bbf6 2022-01-23 thomas goto done;
6033 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
6034 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
6035 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
6037 6458efa5 2020-11-24 stsp goto done;
6040 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6041 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6042 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6043 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6044 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6047 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6048 6458efa5 2020-11-24 stsp return err;
6051 6458efa5 2020-11-24 stsp static const struct got_error *
6052 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6054 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6056 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6057 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6059 6458efa5 2020-11-24 stsp return NULL;
6062 6458efa5 2020-11-24 stsp static const struct got_error *
6063 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6064 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6066 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6067 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6068 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6069 6458efa5 2020-11-24 stsp int obj_type;
6071 c42c9805 2020-11-24 stsp *commit_id = NULL;
6073 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6075 6458efa5 2020-11-24 stsp return err;
6077 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6079 6458efa5 2020-11-24 stsp goto done;
6081 6458efa5 2020-11-24 stsp switch (obj_type) {
6082 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6083 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6085 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6086 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6088 6458efa5 2020-11-24 stsp goto done;
6089 c42c9805 2020-11-24 stsp free(obj_id);
6090 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6091 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6093 6458efa5 2020-11-24 stsp goto done;
6094 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6095 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6096 c42c9805 2020-11-24 stsp goto done;
6098 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6099 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6100 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6101 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6102 c42c9805 2020-11-24 stsp goto done;
6106 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6112 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6113 c42c9805 2020-11-24 stsp if (err) {
6114 c42c9805 2020-11-24 stsp free(*commit_id);
6115 c42c9805 2020-11-24 stsp *commit_id = NULL;
6117 c42c9805 2020-11-24 stsp return err;
6120 c42c9805 2020-11-24 stsp static const struct got_error *
6121 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6122 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6124 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6125 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6126 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6128 c42c9805 2020-11-24 stsp *new_view = NULL;
6130 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6131 c42c9805 2020-11-24 stsp if (err) {
6132 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6133 c42c9805 2020-11-24 stsp return err;
6135 c42c9805 2020-11-24 stsp return NULL;
6138 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6139 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6140 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6141 6458efa5 2020-11-24 stsp goto done;
6144 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6145 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6148 6458efa5 2020-11-24 stsp view_close(log_view);
6150 6458efa5 2020-11-24 stsp *new_view = log_view;
6151 c42c9805 2020-11-24 stsp free(commit_id);
6152 6458efa5 2020-11-24 stsp return err;
6155 ce5b7c56 2019-07-09 stsp static void
6156 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6158 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6159 34ba6917 2020-11-30 stsp int i = 0;
6161 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6164 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6165 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6166 34ba6917 2020-11-30 stsp if (re == NULL)
6168 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6169 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6173 34ba6917 2020-11-30 stsp static void
6174 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6176 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6177 6458efa5 2020-11-24 stsp int n = 0;
6179 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6180 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6182 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6184 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6185 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6186 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6187 6458efa5 2020-11-24 stsp if (last) {
6188 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6189 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6194 6458efa5 2020-11-24 stsp static const struct got_error *
6195 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6197 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6199 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6200 6458efa5 2020-11-24 stsp return NULL;
6203 6458efa5 2020-11-24 stsp static int
6204 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6206 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6208 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6212 6458efa5 2020-11-24 stsp static const struct got_error *
6213 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6215 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6216 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6218 6458efa5 2020-11-24 stsp if (!view->searching) {
6219 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6220 6458efa5 2020-11-24 stsp return NULL;
6223 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6224 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6225 6458efa5 2020-11-24 stsp if (s->selected_entry)
6226 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6228 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6229 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6231 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6232 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6234 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6235 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6238 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6239 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6240 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6241 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6243 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6246 6458efa5 2020-11-24 stsp while (1) {
6247 6458efa5 2020-11-24 stsp if (re == NULL) {
6248 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6249 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6250 6458efa5 2020-11-24 stsp return NULL;
6252 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6253 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6255 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6258 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6259 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6260 6458efa5 2020-11-24 stsp s->matched_entry = re;
6264 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6265 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6267 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6270 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6271 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6272 6458efa5 2020-11-24 stsp s->selected = 0;
6275 6458efa5 2020-11-24 stsp return NULL;
6278 6458efa5 2020-11-24 stsp static const struct got_error *
6279 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6281 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6282 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6283 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6284 6458efa5 2020-11-24 stsp char *line = NULL;
6285 6458efa5 2020-11-24 stsp wchar_t *wline;
6286 6458efa5 2020-11-24 stsp struct tog_color *tc;
6287 6458efa5 2020-11-24 stsp int width, n;
6288 6458efa5 2020-11-24 stsp int limit = view->nlines;
6290 6458efa5 2020-11-24 stsp werase(view->window);
6292 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6294 6458efa5 2020-11-24 stsp if (limit == 0)
6295 6458efa5 2020-11-24 stsp return NULL;
6297 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6299 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6300 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6301 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6303 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6304 6458efa5 2020-11-24 stsp if (err) {
6305 6458efa5 2020-11-24 stsp free(line);
6306 6458efa5 2020-11-24 stsp return err;
6308 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6309 6458efa5 2020-11-24 stsp wstandout(view->window);
6310 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6311 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6312 6458efa5 2020-11-24 stsp wstandend(view->window);
6313 6458efa5 2020-11-24 stsp free(wline);
6314 6458efa5 2020-11-24 stsp wline = NULL;
6315 6458efa5 2020-11-24 stsp free(line);
6316 6458efa5 2020-11-24 stsp line = NULL;
6317 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6318 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6319 6458efa5 2020-11-24 stsp if (--limit <= 0)
6320 6458efa5 2020-11-24 stsp return NULL;
6323 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6324 6458efa5 2020-11-24 stsp char *line = NULL;
6326 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6327 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6328 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6329 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6330 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6331 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6332 6458efa5 2020-11-24 stsp struct got_object_id *id;
6333 6458efa5 2020-11-24 stsp char *id_str;
6334 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6336 6458efa5 2020-11-24 stsp return err;
6337 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6338 6458efa5 2020-11-24 stsp if (err) {
6340 6458efa5 2020-11-24 stsp return err;
6342 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6343 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6344 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6346 6458efa5 2020-11-24 stsp free(id_str);
6347 6458efa5 2020-11-24 stsp return err;
6350 6458efa5 2020-11-24 stsp free(id_str);
6352 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6353 6458efa5 2020-11-24 stsp if (line == NULL)
6354 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6357 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6358 6458efa5 2020-11-24 stsp if (err) {
6359 6458efa5 2020-11-24 stsp free(line);
6360 6458efa5 2020-11-24 stsp return err;
6362 6458efa5 2020-11-24 stsp if (n == s->selected) {
6363 6458efa5 2020-11-24 stsp if (view->focussed)
6364 6458efa5 2020-11-24 stsp wstandout(view->window);
6365 6458efa5 2020-11-24 stsp s->selected_entry = re;
6367 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6369 6458efa5 2020-11-24 stsp wattr_on(view->window,
6370 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6371 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6373 6458efa5 2020-11-24 stsp wattr_off(view->window,
6374 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6375 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6376 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6377 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6378 6458efa5 2020-11-24 stsp wstandend(view->window);
6379 6458efa5 2020-11-24 stsp free(line);
6380 6458efa5 2020-11-24 stsp free(wline);
6381 6458efa5 2020-11-24 stsp wline = NULL;
6383 6458efa5 2020-11-24 stsp s->ndisplayed++;
6384 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6387 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6390 6458efa5 2020-11-24 stsp view_vborder(view);
6391 6458efa5 2020-11-24 stsp return err;
6394 6458efa5 2020-11-24 stsp static const struct got_error *
6395 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6396 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6398 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6399 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6400 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6402 c42c9805 2020-11-24 stsp *new_view = NULL;
6404 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6405 c42c9805 2020-11-24 stsp if (err) {
6406 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6407 c42c9805 2020-11-24 stsp return err;
6409 c42c9805 2020-11-24 stsp return NULL;
6413 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6414 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6415 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6416 c42c9805 2020-11-24 stsp goto done;
6419 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6420 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6422 c42c9805 2020-11-24 stsp goto done;
6424 c42c9805 2020-11-24 stsp *new_view = tree_view;
6426 c42c9805 2020-11-24 stsp free(commit_id);
6427 c42c9805 2020-11-24 stsp return err;
6429 c42c9805 2020-11-24 stsp static const struct got_error *
6430 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6432 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6433 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6434 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6435 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6436 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
6438 6458efa5 2020-11-24 stsp switch (ch) {
6440 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6442 98182bd0 2021-11-20 thomas case 'o':
6443 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6444 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6445 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6446 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6447 c591440f 2021-11-20 thomas if (err)
6449 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6450 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6451 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6452 3bfadbd4 2021-11-20 thomas if (err)
6454 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6455 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6457 6458efa5 2020-11-24 stsp case KEY_ENTER:
6458 6458efa5 2020-11-24 stsp case '\r':
6459 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6461 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6462 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6463 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6465 e78dc838 2020-12-04 stsp view->focussed = 0;
6466 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6467 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6468 6458efa5 2020-11-24 stsp err = view_close_child(view);
6470 6458efa5 2020-11-24 stsp return err;
6471 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6472 e78dc838 2020-12-04 stsp view->focus_child = 1;
6474 6458efa5 2020-11-24 stsp *new_view = log_view;
6477 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6479 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6480 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6481 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6483 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6485 e78dc838 2020-12-04 stsp view->focussed = 0;
6486 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6487 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6488 c42c9805 2020-11-24 stsp err = view_close_child(view);
6490 c42c9805 2020-11-24 stsp return err;
6491 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6492 e78dc838 2020-12-04 stsp view->focus_child = 1;
6494 c42c9805 2020-11-24 stsp *new_view = tree_view;
6496 e4526bf5 2021-09-03 naddy case 'g':
6497 e4526bf5 2021-09-03 naddy case KEY_HOME:
6498 e4526bf5 2021-09-03 naddy s->selected = 0;
6499 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6501 e4526bf5 2021-09-03 naddy case 'G':
6502 e4526bf5 2021-09-03 naddy case KEY_END:
6503 e4526bf5 2021-09-03 naddy s->selected = 0;
6504 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6505 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6506 e4526bf5 2021-09-03 naddy if (re == NULL)
6508 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6509 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6511 e4526bf5 2021-09-03 naddy if (n > 0)
6512 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6515 6458efa5 2020-11-24 stsp case KEY_UP:
6516 f7140bf5 2021-10-17 thomas case CTRL('p'):
6517 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6518 6458efa5 2020-11-24 stsp s->selected--;
6521 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6523 70f17a53 2022-06-13 thomas case CTRL('u'):
6524 23427b14 2022-06-23 thomas case 'u':
6525 70f17a53 2022-06-13 thomas nscroll /= 2;
6526 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6527 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6528 6458efa5 2020-11-24 stsp case CTRL('b'):
6529 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6530 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
6531 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
6534 6458efa5 2020-11-24 stsp case KEY_DOWN:
6535 f7140bf5 2021-10-17 thomas case CTRL('n'):
6536 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6537 6458efa5 2020-11-24 stsp s->selected++;
6540 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6541 6458efa5 2020-11-24 stsp /* can't scroll any further */
6543 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6545 70f17a53 2022-06-13 thomas case CTRL('d'):
6546 23427b14 2022-06-23 thomas case 'd':
6547 70f17a53 2022-06-13 thomas nscroll /= 2;
6548 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6549 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6550 6458efa5 2020-11-24 stsp case CTRL('f'):
6551 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6552 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6553 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6554 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6555 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6558 70f17a53 2022-06-13 thomas ref_scroll_down(s, nscroll);
6560 6458efa5 2020-11-24 stsp case CTRL('l'):
6561 8924d611 2020-12-26 stsp tog_free_refs();
6562 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6565 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6566 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6568 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6569 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6570 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6576 6458efa5 2020-11-24 stsp return err;
6579 6458efa5 2020-11-24 stsp __dead static void
6580 6458efa5 2020-11-24 stsp usage_ref(void)
6583 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6584 6458efa5 2020-11-24 stsp getprogname());
6588 6458efa5 2020-11-24 stsp static const struct got_error *
6589 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6591 6458efa5 2020-11-24 stsp const struct got_error *error;
6592 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6593 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6594 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6596 6458efa5 2020-11-24 stsp struct tog_view *view;
6597 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6599 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6600 6458efa5 2020-11-24 stsp switch (ch) {
6602 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6603 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6604 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6608 e99e2d15 2020-11-24 naddy usage_ref();
6609 6458efa5 2020-11-24 stsp /* NOTREACHED */
6613 6458efa5 2020-11-24 stsp argc -= optind;
6614 6458efa5 2020-11-24 stsp argv += optind;
6616 6458efa5 2020-11-24 stsp if (argc > 1)
6617 e99e2d15 2020-11-24 naddy usage_ref();
6619 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6620 7cd52833 2022-06-23 thomas if (error != NULL)
6621 7cd52833 2022-06-23 thomas goto done;
6623 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6624 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6625 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6626 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6627 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6628 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6629 c156c7a4 2020-12-18 stsp goto done;
6630 6458efa5 2020-11-24 stsp if (worktree)
6631 6458efa5 2020-11-24 stsp repo_path =
6632 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6634 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6635 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6636 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6637 c156c7a4 2020-12-18 stsp goto done;
6641 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6642 6458efa5 2020-11-24 stsp if (error != NULL)
6643 6458efa5 2020-11-24 stsp goto done;
6645 6458efa5 2020-11-24 stsp init_curses();
6647 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6648 51a10b52 2020-12-26 stsp if (error)
6649 51a10b52 2020-12-26 stsp goto done;
6651 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6652 6458efa5 2020-11-24 stsp if (error)
6653 6458efa5 2020-11-24 stsp goto done;
6655 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6656 6458efa5 2020-11-24 stsp if (view == NULL) {
6657 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6658 6458efa5 2020-11-24 stsp goto done;
6661 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6662 6458efa5 2020-11-24 stsp if (error)
6663 6458efa5 2020-11-24 stsp goto done;
6665 6458efa5 2020-11-24 stsp if (worktree) {
6666 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6667 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6668 6458efa5 2020-11-24 stsp worktree = NULL;
6670 6458efa5 2020-11-24 stsp error = view_loop(view);
6672 6458efa5 2020-11-24 stsp free(repo_path);
6673 6458efa5 2020-11-24 stsp free(cwd);
6674 1d0f4054 2021-06-17 stsp if (repo) {
6675 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6676 1d0f4054 2021-06-17 stsp if (close_err)
6677 1d0f4054 2021-06-17 stsp error = close_err;
6679 7cd52833 2022-06-23 thomas if (pack_fds) {
6680 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6681 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6682 7cd52833 2022-06-23 thomas if (error == NULL)
6683 7cd52833 2022-06-23 thomas error = pack_err;
6685 51a10b52 2020-12-26 stsp tog_free_refs();
6686 6458efa5 2020-11-24 stsp return error;
6689 6458efa5 2020-11-24 stsp static void
6690 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6694 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6695 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6696 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6697 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6699 6879ba42 2020-10-01 naddy fputc('\n', fp);
6702 4ed7e80c 2018-05-20 stsp __dead static void
6703 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6705 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6707 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6708 6879ba42 2020-10-01 naddy getprogname());
6709 6879ba42 2020-10-01 naddy if (hflag) {
6710 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6711 6879ba42 2020-10-01 naddy list_commands(fp);
6713 6879ba42 2020-10-01 naddy exit(status);
6716 c2301be8 2018-04-30 stsp static char **
6717 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6719 ee85c5e8 2020-02-29 stsp va_list ap;
6720 c2301be8 2018-04-30 stsp char **argv;
6723 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6725 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6726 c2301be8 2018-04-30 stsp if (argv == NULL)
6727 c2301be8 2018-04-30 stsp err(1, "calloc");
6728 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6729 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6730 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6731 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6734 ee85c5e8 2020-02-29 stsp va_end(ap);
6735 c2301be8 2018-04-30 stsp return argv;
6739 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6740 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6741 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6742 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6743 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6745 ee85c5e8 2020-02-29 stsp static const struct got_error *
6746 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6748 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6749 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6750 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6751 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6752 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6753 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6754 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6755 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6756 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6758 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6759 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6760 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6762 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6763 7cd52833 2022-06-23 thomas if (error != NULL)
6764 7cd52833 2022-06-23 thomas goto done;
6766 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6767 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6768 ee85c5e8 2020-02-29 stsp goto done;
6770 ee85c5e8 2020-02-29 stsp if (worktree)
6771 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6773 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6774 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6775 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6776 ee85c5e8 2020-02-29 stsp goto done;
6779 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6780 ee85c5e8 2020-02-29 stsp if (error != NULL)
6781 ee85c5e8 2020-02-29 stsp goto done;
6783 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6784 ee85c5e8 2020-02-29 stsp repo, worktree);
6785 ee85c5e8 2020-02-29 stsp if (error)
6786 ee85c5e8 2020-02-29 stsp goto done;
6788 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6789 84de9106 2020-12-26 stsp if (error)
6790 84de9106 2020-12-26 stsp goto done;
6791 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6792 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6793 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6794 ee85c5e8 2020-02-29 stsp if (error)
6795 ee85c5e8 2020-02-29 stsp goto done;
6797 ee85c5e8 2020-02-29 stsp if (worktree) {
6798 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6799 ee85c5e8 2020-02-29 stsp worktree = NULL;
6802 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6803 945f9229 2022-04-16 thomas if (error)
6804 945f9229 2022-04-16 thomas goto done;
6806 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6807 ee85c5e8 2020-02-29 stsp if (error) {
6808 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6809 ee85c5e8 2020-02-29 stsp goto done;
6810 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6811 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6812 6879ba42 2020-10-01 naddy usage(1, 1);
6813 ee85c5e8 2020-02-29 stsp /* not reached */
6816 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6817 1d0f4054 2021-06-17 stsp if (error == NULL)
6818 1d0f4054 2021-06-17 stsp error = close_err;
6819 ee85c5e8 2020-02-29 stsp repo = NULL;
6821 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6822 ee85c5e8 2020-02-29 stsp if (error)
6823 ee85c5e8 2020-02-29 stsp goto done;
6825 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6827 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6828 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6830 1d0f4054 2021-06-17 stsp if (repo) {
6831 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6832 1d0f4054 2021-06-17 stsp if (error == NULL)
6833 1d0f4054 2021-06-17 stsp error = close_err;
6835 945f9229 2022-04-16 thomas if (commit)
6836 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6837 ee85c5e8 2020-02-29 stsp if (worktree)
6838 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6839 7cd52833 2022-06-23 thomas if (pack_fds) {
6840 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6841 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6842 7cd52833 2022-06-23 thomas if (error == NULL)
6843 7cd52833 2022-06-23 thomas error = pack_err;
6846 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6847 ee85c5e8 2020-02-29 stsp free(commit_id);
6848 ee85c5e8 2020-02-29 stsp free(cwd);
6849 ee85c5e8 2020-02-29 stsp free(repo_path);
6850 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6851 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6853 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6854 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6855 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6857 87670572 2020-12-26 naddy tog_free_refs();
6858 ee85c5e8 2020-02-29 stsp return error;
6862 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6864 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6865 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6866 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6867 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6868 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
6869 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6870 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6873 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6875 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6876 9f7d7167 2018-04-29 stsp switch (ch) {
6878 9f7d7167 2018-04-29 stsp hflag = 1;
6881 53ccebc2 2019-07-30 stsp Vflag = 1;
6884 6879ba42 2020-10-01 naddy usage(hflag, 1);
6885 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6889 9f7d7167 2018-04-29 stsp argc -= optind;
6890 9f7d7167 2018-04-29 stsp argv += optind;
6891 9814e6a3 2020-09-27 naddy optind = 1;
6892 c2301be8 2018-04-30 stsp optreset = 1;
6894 53ccebc2 2019-07-30 stsp if (Vflag) {
6895 53ccebc2 2019-07-30 stsp got_version_print_str();
6896 6879ba42 2020-10-01 naddy return 0;
6899 4010e238 2020-12-04 stsp #ifndef PROFILE
6900 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6901 4010e238 2020-12-04 stsp NULL) == -1)
6902 4010e238 2020-12-04 stsp err(1, "pledge");
6905 c2301be8 2018-04-30 stsp if (argc == 0) {
6906 f29d3e89 2018-06-23 stsp if (hflag)
6907 6879ba42 2020-10-01 naddy usage(hflag, 0);
6908 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6909 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6911 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6915 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6916 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6917 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6918 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6919 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6925 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6926 ee85c5e8 2020-02-29 stsp if (argc != 1)
6927 6879ba42 2020-10-01 naddy usage(0, 1);
6928 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6929 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6931 ee85c5e8 2020-02-29 stsp if (hflag)
6932 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6934 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6938 b46c1e04 2020-09-20 naddy putchar('\n');
6939 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6941 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6942 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6943 a2f4a359 2020-02-28 stsp free(cmd_argv);
6946 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6947 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);