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 1a76625f 2018-10-22 stsp int log_complete;
349 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
350 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
351 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
352 13add988 2019-10-15 stsp int *searching;
353 13add988 2019-10-15 stsp int *search_next_done;
354 13add988 2019-10-15 stsp regex_t *regex;
357 1a76625f 2018-10-22 stsp struct tog_log_view_state {
358 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
359 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
360 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
361 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
362 b01e7d3b 2018-08-04 stsp int selected;
363 b01e7d3b 2018-08-04 stsp char *in_repo_path;
364 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
365 b672a97a 2020-01-27 stsp int log_branches;
366 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
367 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp sig_atomic_t quit;
369 1a76625f 2018-10-22 stsp pthread_t thread;
370 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
371 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
372 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
373 11b20872 2019-11-08 stsp struct tog_colors colors;
376 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
377 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
378 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
380 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
381 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
382 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
384 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
385 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
386 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
387 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
388 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
389 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
390 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
392 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
393 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
394 e9424729 2018-08-04 stsp int nlines;
396 e9424729 2018-08-04 stsp struct tog_view *view;
397 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
401 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
402 e9424729 2018-08-04 stsp const char *path;
403 e9424729 2018-08-04 stsp struct got_repository *repo;
404 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
405 e9424729 2018-08-04 stsp int *complete;
406 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
407 fc06ba56 2019-08-22 stsp void *cancel_arg;
410 e9424729 2018-08-04 stsp struct tog_blame {
412 be659d10 2020-11-18 stsp off_t filesize;
413 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
414 6fcac457 2018-11-19 stsp int nlines;
415 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
416 e9424729 2018-08-04 stsp pthread_t thread;
417 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
418 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
419 e9424729 2018-08-04 stsp const char *path;
422 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
423 7cbe629d 2018-08-04 stsp int first_displayed_line;
424 7cbe629d 2018-08-04 stsp int last_displayed_line;
425 7cbe629d 2018-08-04 stsp int selected_line;
426 7cbe629d 2018-08-04 stsp int blame_complete;
429 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
430 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
431 e5a0f69f 2018-08-18 stsp char *path;
432 7cbe629d 2018-08-04 stsp struct got_repository *repo;
433 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
434 e9424729 2018-08-04 stsp struct tog_blame blame;
435 6c4c42e0 2019-06-24 stsp int matched_line;
436 11b20872 2019-11-08 stsp struct tog_colors colors;
439 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
440 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
441 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
442 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
443 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
444 ad80ab7b 2018-08-04 stsp int selected;
447 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
449 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
450 ad80ab7b 2018-08-04 stsp char *tree_label;
451 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
452 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
453 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
454 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
458 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
459 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
460 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
461 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
462 6458efa5 2020-11-24 stsp struct tog_colors colors;
465 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
466 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
467 6458efa5 2020-11-24 stsp struct got_reference *ref;
471 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
473 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
474 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
475 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
476 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
478 3bfadbd4 2021-11-20 thomas int nrefs, ndisplayed, selected, show_ids, sort_by_date;
479 6458efa5 2020-11-24 stsp struct got_repository *repo;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
481 bddb1296 2019-11-08 stsp struct tog_colors colors;
485 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
487 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
488 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
489 669b5ffa 2018-10-07 stsp * there is enough screen estate.
491 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
492 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
494 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
495 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
496 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
498 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
499 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
501 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
503 cc3c9aac 2018-08-01 stsp struct tog_view {
504 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
505 26ed57b2 2018-05-19 stsp WINDOW *window;
506 26ed57b2 2018-05-19 stsp PANEL *panel;
507 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
508 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
509 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
511 669b5ffa 2018-10-07 stsp struct tog_view *parent;
512 669b5ffa 2018-10-07 stsp struct tog_view *child;
515 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
516 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
517 e78dc838 2020-12-04 stsp * between parent and child.
518 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
519 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
520 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
521 e78dc838 2020-12-04 stsp * situations.
523 e78dc838 2020-12-04 stsp int focus_child;
525 5dc9f4bc 2018-08-04 stsp /* type-specific state */
526 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
528 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
529 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
530 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
531 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
532 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
535 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
536 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
537 e78dc838 2020-12-04 stsp struct tog_view *, int);
538 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
540 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
541 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
542 c0c4acc8 2021-01-24 stsp int search_started;
543 60493ae3 2019-06-20 stsp int searching;
544 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
545 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
546 60493ae3 2019-06-20 stsp int search_next_done;
547 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
548 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
549 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
550 1803e47f 2019-06-22 stsp regex_t regex;
551 41605754 2020-11-12 stsp regmatch_t regmatch;
554 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
555 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
556 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
557 78756c87 2020-11-24 stsp struct got_repository *);
558 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
559 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
560 e78dc838 2020-12-04 stsp struct tog_view *, int);
561 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
562 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
563 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
565 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
566 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
567 78756c87 2020-11-24 stsp const char *, const char *, int);
568 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
570 e78dc838 2020-12-04 stsp struct tog_view *, int);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
572 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
573 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
575 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
576 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
577 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
579 e78dc838 2020-12-04 stsp struct tog_view *, int);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
581 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
582 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
584 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
585 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
586 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
588 e78dc838 2020-12-04 stsp struct tog_view *, int);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
590 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
591 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
593 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
594 6458efa5 2020-11-24 stsp struct got_repository *);
595 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
596 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
599 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
600 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
602 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
603 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
604 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
605 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
606 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
608 25791caa 2018-10-24 stsp static void
609 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
611 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
614 83baff54 2019-08-12 stsp static void
615 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
617 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
620 61266923 2020-01-14 stsp static void
621 61266923 2020-01-14 stsp tog_sigcont(int signo)
623 61266923 2020-01-14 stsp tog_sigcont_received = 1;
626 296152d1 2022-05-31 thomas static void
627 296152d1 2022-05-31 thomas tog_sigint(int signo)
629 296152d1 2022-05-31 thomas tog_sigint_received = 1;
632 296152d1 2022-05-31 thomas static void
633 296152d1 2022-05-31 thomas tog_sigterm(int signo)
635 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
638 296152d1 2022-05-31 thomas static int
639 296152d1 2022-05-31 thomas tog_fatal_signal_received()
641 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
642 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
646 e5a0f69f 2018-08-18 stsp static const struct got_error *
647 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
649 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
651 669b5ffa 2018-10-07 stsp if (view->child) {
652 669b5ffa 2018-10-07 stsp view_close(view->child);
653 669b5ffa 2018-10-07 stsp view->child = NULL;
655 e5a0f69f 2018-08-18 stsp if (view->close)
656 e5a0f69f 2018-08-18 stsp err = view->close(view);
657 ea5e7bb5 2018-08-01 stsp if (view->panel)
658 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
659 ea5e7bb5 2018-08-01 stsp if (view->window)
660 ea5e7bb5 2018-08-01 stsp delwin(view->window);
661 ea5e7bb5 2018-08-01 stsp free(view);
662 e5a0f69f 2018-08-18 stsp return err;
665 ea5e7bb5 2018-08-01 stsp static struct tog_view *
666 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
667 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
669 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
671 ea5e7bb5 2018-08-01 stsp if (view == NULL)
672 ea5e7bb5 2018-08-01 stsp return NULL;
674 d6b05b5b 2018-08-04 stsp view->type = type;
675 f7d12f7e 2018-08-01 stsp view->lines = LINES;
676 f7d12f7e 2018-08-01 stsp view->cols = COLS;
677 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
678 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
679 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
680 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
681 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
682 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
683 96a765a8 2018-08-04 stsp view_close(view);
684 ea5e7bb5 2018-08-01 stsp return NULL;
686 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
687 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
688 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
689 96a765a8 2018-08-04 stsp view_close(view);
690 ea5e7bb5 2018-08-01 stsp return NULL;
693 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
694 ea5e7bb5 2018-08-01 stsp return view;
698 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
700 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
702 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
705 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
707 5c60c32a 2018-10-18 stsp static const struct got_error *
708 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
710 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
712 5c60c32a 2018-10-18 stsp view->begin_y = 0;
713 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
714 5c60c32a 2018-10-18 stsp view->nlines = LINES;
715 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
716 5c60c32a 2018-10-18 stsp view->lines = LINES;
717 5c60c32a 2018-10-18 stsp view->cols = COLS;
718 5c60c32a 2018-10-18 stsp err = view_resize(view);
720 5c60c32a 2018-10-18 stsp return err;
722 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
723 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
725 5c60c32a 2018-10-18 stsp return NULL;
728 5c60c32a 2018-10-18 stsp static const struct got_error *
729 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
731 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
733 5c60c32a 2018-10-18 stsp view->begin_x = 0;
734 5c60c32a 2018-10-18 stsp view->begin_y = 0;
735 5c60c32a 2018-10-18 stsp view->nlines = LINES;
736 5c60c32a 2018-10-18 stsp view->ncols = COLS;
737 5c60c32a 2018-10-18 stsp view->lines = LINES;
738 5c60c32a 2018-10-18 stsp view->cols = COLS;
739 5c60c32a 2018-10-18 stsp err = view_resize(view);
741 5c60c32a 2018-10-18 stsp return err;
743 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
744 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
746 5c60c32a 2018-10-18 stsp return NULL;
750 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
752 5c60c32a 2018-10-18 stsp return view->parent == NULL;
755 4d8c2215 2018-08-19 stsp static const struct got_error *
756 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
758 f7d12f7e 2018-08-01 stsp int nlines, ncols;
760 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
761 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
763 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
765 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
766 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
768 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
770 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
771 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
772 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
773 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
774 25791caa 2018-10-24 stsp wclear(view->window);
776 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
777 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
778 0cf4efb1 2018-09-29 stsp view->lines = LINES;
779 0cf4efb1 2018-09-29 stsp view->cols = COLS;
781 6e3e5d9c 2018-10-18 stsp if (view->child) {
782 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
783 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
784 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
785 5c60c32a 2018-10-18 stsp if (view->child->focussed)
786 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
788 5c60c32a 2018-10-18 stsp show_panel(view->panel);
790 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
791 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
795 5c60c32a 2018-10-18 stsp return NULL;
798 669b5ffa 2018-10-07 stsp static const struct got_error *
799 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
801 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
803 669b5ffa 2018-10-07 stsp if (view->child == NULL)
804 669b5ffa 2018-10-07 stsp return NULL;
806 669b5ffa 2018-10-07 stsp err = view_close(view->child);
807 669b5ffa 2018-10-07 stsp view->child = NULL;
808 669b5ffa 2018-10-07 stsp return err;
811 72a9cb46 2020-12-03 stsp static void
812 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
814 669b5ffa 2018-10-07 stsp view->child = child;
815 669b5ffa 2018-10-07 stsp child->parent = view;
819 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
821 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
824 34bc9ec9 2019-02-22 stsp static void
825 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
827 25791caa 2018-10-24 stsp int cols, lines;
828 25791caa 2018-10-24 stsp struct winsize size;
830 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
831 25791caa 2018-10-24 stsp cols = 80; /* Default */
832 25791caa 2018-10-24 stsp lines = 24;
834 25791caa 2018-10-24 stsp cols = size.ws_col;
835 25791caa 2018-10-24 stsp lines = size.ws_row;
837 25791caa 2018-10-24 stsp resize_term(lines, cols);
840 2b49a8ae 2019-06-22 stsp static const struct got_error *
841 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
843 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
844 2b49a8ae 2019-06-22 stsp char pattern[1024];
847 c0c4acc8 2021-01-24 stsp if (view->search_started) {
848 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
849 c0c4acc8 2021-01-24 stsp view->searching = 0;
850 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
852 c0c4acc8 2021-01-24 stsp view->search_started = 0;
854 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
855 2b49a8ae 2019-06-22 stsp return NULL;
857 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
858 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
860 2b49a8ae 2019-06-22 stsp nocbreak();
862 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
865 2b49a8ae 2019-06-22 stsp if (ret == ERR)
866 2b49a8ae 2019-06-22 stsp return NULL;
868 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
869 7c32bd05 2019-06-22 stsp err = view->search_start(view);
871 7c32bd05 2019-06-22 stsp regfree(&view->regex);
872 7c32bd05 2019-06-22 stsp return err;
874 c0c4acc8 2021-01-24 stsp view->search_started = 1;
875 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
876 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
877 2b49a8ae 2019-06-22 stsp view->search_next(view);
880 2b49a8ae 2019-06-22 stsp return NULL;
883 0cf4efb1 2018-09-29 stsp static const struct got_error *
884 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
885 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
887 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
888 669b5ffa 2018-10-07 stsp struct tog_view *v;
889 1a76625f 2018-10-22 stsp int ch, errcode;
891 e5a0f69f 2018-08-18 stsp *new = NULL;
893 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
894 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
895 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
896 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
898 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
899 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
900 82954512 2020-02-03 stsp if (errcode)
901 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
902 82954512 2020-02-03 stsp "pthread_mutex_unlock");
903 3da8ef85 2021-09-21 stsp sched_yield();
904 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
905 82954512 2020-02-03 stsp if (errcode)
906 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
907 82954512 2020-02-03 stsp "pthread_mutex_lock");
908 60493ae3 2019-06-20 stsp view->search_next(view);
909 60493ae3 2019-06-20 stsp return NULL;
912 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
913 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
914 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
915 1a76625f 2018-10-22 stsp if (errcode)
916 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
917 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
918 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
919 1a76625f 2018-10-22 stsp if (errcode)
920 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
921 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
923 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
924 25791caa 2018-10-24 stsp tog_resizeterm();
925 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
926 61266923 2020-01-14 stsp tog_sigcont_received = 0;
927 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
928 25791caa 2018-10-24 stsp err = view_resize(v);
930 25791caa 2018-10-24 stsp return err;
931 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
933 25791caa 2018-10-24 stsp return err;
934 cdfcfb03 2020-12-06 stsp if (v->child) {
935 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
937 cdfcfb03 2020-12-06 stsp return err;
938 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
939 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
941 cdfcfb03 2020-12-06 stsp return err;
946 e5a0f69f 2018-08-18 stsp switch (ch) {
948 1e37a5c2 2019-05-12 jcs if (view->child) {
949 e78dc838 2020-12-04 stsp view->focussed = 0;
950 e78dc838 2020-12-04 stsp view->child->focussed = 1;
951 e78dc838 2020-12-04 stsp view->focus_child = 1;
952 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
953 e78dc838 2020-12-04 stsp view->focussed = 0;
954 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
955 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
959 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
960 9970f7fc 2020-12-03 stsp view->dying = 1;
966 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
967 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
969 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
970 e78dc838 2020-12-04 stsp view->focussed = 0;
971 e78dc838 2020-12-04 stsp view->child->focussed = 1;
972 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
974 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
977 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
978 9970f7fc 2020-12-03 stsp KEY_RESIZE);
980 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
981 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
982 e78dc838 2020-12-04 stsp view->focussed = 1;
983 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
985 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
989 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
992 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
995 60493ae3 2019-06-20 stsp if (view->search_start)
996 2b49a8ae 2019-06-22 stsp view_search_start(view);
998 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1002 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1003 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1004 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1005 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1006 60493ae3 2019-06-20 stsp view->search_next(view);
1008 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1011 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1015 e5a0f69f 2018-08-18 stsp return err;
1019 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1021 0cf4efb1 2018-09-29 stsp PANEL *panel;
1022 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1024 669b5ffa 2018-10-07 stsp if (view->parent)
1025 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1027 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1028 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1031 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1032 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1033 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1037 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1039 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1040 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1042 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1044 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1047 669b5ffa 2018-10-07 stsp return view->focussed;
1050 bcbd79e2 2018-08-19 stsp static const struct got_error *
1051 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1053 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1054 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1055 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1056 fd823528 2018-10-22 stsp int fast_refresh = 10;
1057 1a76625f 2018-10-22 stsp int done = 0, errcode;
1059 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1060 1a76625f 2018-10-22 stsp if (errcode)
1061 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1063 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1064 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1066 1004088d 2018-09-29 stsp view->focussed = 1;
1067 878940b7 2018-09-29 stsp err = view->show(view);
1069 0cf4efb1 2018-09-29 stsp return err;
1070 0cf4efb1 2018-09-29 stsp update_panels();
1071 0cf4efb1 2018-09-29 stsp doupdate();
1072 296152d1 2022-05-31 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1073 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1074 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1075 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1077 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1080 9970f7fc 2020-12-03 stsp if (view->dying) {
1081 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1083 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1084 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1086 e78dc838 2020-12-04 stsp else if (view->parent)
1087 669b5ffa 2018-10-07 stsp prev = view->parent;
1089 e78dc838 2020-12-04 stsp if (view->parent) {
1090 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1091 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1093 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1095 9970f7fc 2020-12-03 stsp err = view_close(view);
1097 e5a0f69f 2018-08-18 stsp goto done;
1099 e78dc838 2020-12-04 stsp view = NULL;
1100 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1101 e78dc838 2020-12-04 stsp if (v->focussed)
1104 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1105 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1107 e78dc838 2020-12-04 stsp view = prev;
1108 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1109 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1110 e78dc838 2020-12-04 stsp tog_view_list_head);
1112 e78dc838 2020-12-04 stsp if (view) {
1113 e78dc838 2020-12-04 stsp if (view->focus_child) {
1114 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1115 e78dc838 2020-12-04 stsp view = view->child;
1117 e78dc838 2020-12-04 stsp view->focussed = 1;
1121 bcbd79e2 2018-08-19 stsp if (new_view) {
1122 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1123 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1124 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1125 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1127 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1128 86c66b02 2018-10-18 stsp err = view_close(v);
1130 86c66b02 2018-10-18 stsp goto done;
1133 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1134 fed7eaa8 2018-10-24 stsp view = new_view;
1136 669b5ffa 2018-10-07 stsp if (view) {
1137 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1138 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1139 e78dc838 2020-12-04 stsp view = view->child;
1141 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1142 e78dc838 2020-12-04 stsp view = view->parent;
1144 e78dc838 2020-12-04 stsp show_panel(view->panel);
1145 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1146 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1147 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1148 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1150 1a76625f 2018-10-22 stsp goto done;
1152 669b5ffa 2018-10-07 stsp err = view->show(view);
1154 1a76625f 2018-10-22 stsp goto done;
1155 669b5ffa 2018-10-07 stsp if (view->child) {
1156 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1158 1a76625f 2018-10-22 stsp goto done;
1160 1a76625f 2018-10-22 stsp update_panels();
1161 1a76625f 2018-10-22 stsp doupdate();
1165 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1166 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1167 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1168 e5a0f69f 2018-08-18 stsp view_close(view);
1171 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1172 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1173 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1175 e5a0f69f 2018-08-18 stsp return err;
1178 4ed7e80c 2018-05-20 stsp __dead static void
1179 9f7d7167 2018-04-29 stsp usage_log(void)
1182 c70c5802 2018-08-01 stsp fprintf(stderr,
1183 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1184 9f7d7167 2018-04-29 stsp getprogname());
1188 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1189 80ddbec8 2018-04-29 stsp static const struct got_error *
1190 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1192 00dfcb92 2018-06-11 stsp char *vis = NULL;
1193 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1195 963b370f 2018-05-20 stsp *ws = NULL;
1196 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1197 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1198 00dfcb92 2018-06-11 stsp int vislen;
1199 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1200 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1202 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1203 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1205 00dfcb92 2018-06-11 stsp return err;
1206 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1207 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1208 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1209 a7f50699 2018-06-11 stsp goto done;
1213 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1214 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1215 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1216 a7f50699 2018-06-11 stsp goto done;
1219 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1220 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1222 00dfcb92 2018-06-11 stsp free(vis);
1223 963b370f 2018-05-20 stsp if (err) {
1224 963b370f 2018-05-20 stsp free(*ws);
1225 963b370f 2018-05-20 stsp *ws = NULL;
1226 963b370f 2018-05-20 stsp *wlen = 0;
1228 963b370f 2018-05-20 stsp return err;
1231 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1232 963b370f 2018-05-20 stsp static const struct got_error *
1233 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1234 27a741e5 2019-09-11 stsp int col_tab_align)
1236 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1237 963b370f 2018-05-20 stsp int cols = 0;
1238 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1239 963b370f 2018-05-20 stsp size_t wlen;
1242 963b370f 2018-05-20 stsp *wlinep = NULL;
1243 b700b5d6 2018-07-10 stsp *widthp = 0;
1245 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1247 963b370f 2018-05-20 stsp return err;
1249 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1250 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1253 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1254 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1259 27a741e5 2019-09-11 stsp while (i < wlen) {
1260 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1262 27a741e5 2019-09-11 stsp if (width == 0) {
1267 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1268 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1270 27a741e5 2019-09-11 stsp cols += width;
1272 27a741e5 2019-09-11 stsp } else if (width == -1) {
1273 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1274 27a741e5 2019-09-11 stsp width = TABSIZE -
1275 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1277 748d5cab 2020-12-14 naddy width = 1;
1278 748d5cab 2020-12-14 naddy wline[i] = L'.';
1280 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1282 748d5cab 2020-12-14 naddy cols += width;
1285 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1286 963b370f 2018-05-20 stsp goto done;
1289 963b370f 2018-05-20 stsp wline[i] = L'\0';
1290 b700b5d6 2018-07-10 stsp if (widthp)
1291 b700b5d6 2018-07-10 stsp *widthp = cols;
1294 963b370f 2018-05-20 stsp free(wline);
1296 963b370f 2018-05-20 stsp *wlinep = wline;
1297 963b370f 2018-05-20 stsp return err;
1300 8b473291 2019-02-21 stsp static const struct got_error*
1301 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1302 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1304 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1305 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1307 8b473291 2019-02-21 stsp const char *name;
1309 8b473291 2019-02-21 stsp *refs_str = NULL;
1311 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1312 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1313 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1316 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1317 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1319 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1320 8b473291 2019-02-21 stsp name += 5;
1321 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1322 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1324 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1325 8b473291 2019-02-21 stsp name += 6;
1326 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1327 8b473291 2019-02-21 stsp name += 8;
1328 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1329 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1332 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1335 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1336 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1337 5d844a1e 2019-08-13 stsp if (err) {
1338 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1339 48cae60d 2020-09-22 stsp free(ref_id);
1342 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1343 5d844a1e 2019-08-13 stsp err = NULL;
1344 5d844a1e 2019-08-13 stsp tag = NULL;
1347 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1348 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1349 48cae60d 2020-09-22 stsp free(ref_id);
1351 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1352 52b5abe1 2019-08-13 stsp if (cmp != 0)
1354 8b473291 2019-02-21 stsp s = *refs_str;
1355 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1356 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1357 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1359 8b473291 2019-02-21 stsp *refs_str = NULL;
1365 8b473291 2019-02-21 stsp return err;
1368 963b370f 2018-05-20 stsp static const struct got_error *
1369 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1370 27a741e5 2019-09-11 stsp int col_tab_align)
1372 e6b8b890 2020-12-29 naddy char *smallerthan;
1374 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1375 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1376 5813d178 2019-03-09 stsp author = smallerthan + 1;
1377 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1378 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1381 5813d178 2019-03-09 stsp static const struct got_error *
1382 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1383 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1384 8fdc79fe 2020-12-01 naddy int author_display_cols)
1386 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1387 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1388 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1389 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1390 5813d178 2019-03-09 stsp char *author = NULL;
1391 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1392 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1393 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1394 bb737323 2018-05-20 stsp int col, limit;
1395 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1396 ccb26ccd 2018-11-05 stsp struct tm tm;
1397 45d799e2 2018-12-23 stsp time_t committer_time;
1398 11b20872 2019-11-08 stsp struct tog_color *tc;
1400 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1401 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1402 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1403 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1404 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1406 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1407 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1409 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1410 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1412 11b20872 2019-11-08 stsp wattr_on(view->window,
1413 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1414 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1416 11b20872 2019-11-08 stsp wattr_off(view->window,
1417 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1418 27a741e5 2019-09-11 stsp col = limit;
1419 b39d25c7 2018-07-10 stsp if (col > avail)
1420 b39d25c7 2018-07-10 stsp goto done;
1422 6570a66d 2019-11-08 stsp if (avail >= 120) {
1423 6570a66d 2019-11-08 stsp char *id_str;
1424 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1426 6570a66d 2019-11-08 stsp goto done;
1427 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1429 11b20872 2019-11-08 stsp wattr_on(view->window,
1430 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1431 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1433 11b20872 2019-11-08 stsp wattr_off(view->window,
1434 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1435 6570a66d 2019-11-08 stsp free(id_str);
1437 6570a66d 2019-11-08 stsp if (col > avail)
1438 6570a66d 2019-11-08 stsp goto done;
1441 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1442 5813d178 2019-03-09 stsp if (author == NULL) {
1443 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1444 80ddbec8 2018-04-29 stsp goto done;
1446 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1448 bb737323 2018-05-20 stsp goto done;
1449 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1451 11b20872 2019-11-08 stsp wattr_on(view->window,
1452 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1453 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1455 11b20872 2019-11-08 stsp wattr_off(view->window,
1456 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1457 bb737323 2018-05-20 stsp col += author_width;
1458 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1459 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1461 bb737323 2018-05-20 stsp author_width++;
1463 9c2eaf34 2018-05-20 stsp if (col > avail)
1464 9c2eaf34 2018-05-20 stsp goto done;
1466 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1468 6d9fbc00 2018-04-29 stsp goto done;
1469 bb737323 2018-05-20 stsp logmsg = logmsg0;
1470 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1472 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1473 bb737323 2018-05-20 stsp if (newline)
1474 bb737323 2018-05-20 stsp *newline = '\0';
1475 bb737323 2018-05-20 stsp limit = avail - col;
1476 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1478 bb737323 2018-05-20 stsp goto done;
1479 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1480 bb737323 2018-05-20 stsp col += logmsg_width;
1481 27a741e5 2019-09-11 stsp while (col < avail) {
1482 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1486 80ddbec8 2018-04-29 stsp free(logmsg0);
1487 bb737323 2018-05-20 stsp free(wlogmsg);
1488 5813d178 2019-03-09 stsp free(author);
1489 bb737323 2018-05-20 stsp free(wauthor);
1490 80ddbec8 2018-04-29 stsp free(line);
1491 80ddbec8 2018-04-29 stsp return err;
1494 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1495 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1496 899d86c2 2018-05-10 stsp struct got_object_id *id)
1498 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1500 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1501 80ddbec8 2018-04-29 stsp if (entry == NULL)
1502 899d86c2 2018-05-10 stsp return NULL;
1504 899d86c2 2018-05-10 stsp entry->id = id;
1505 99db9666 2018-05-07 stsp entry->commit = commit;
1506 899d86c2 2018-05-10 stsp return entry;
1509 99db9666 2018-05-07 stsp static void
1510 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1512 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1514 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1515 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1516 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1517 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1518 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1519 99db9666 2018-05-07 stsp free(entry);
1522 99db9666 2018-05-07 stsp static void
1523 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1525 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1526 99db9666 2018-05-07 stsp pop_commit(commits);
1529 c4972b91 2018-05-07 stsp static const struct got_error *
1530 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1531 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1533 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1534 13add988 2019-10-15 stsp regmatch_t regmatch;
1535 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1537 13add988 2019-10-15 stsp *have_match = 0;
1539 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1541 13add988 2019-10-15 stsp return err;
1543 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1545 13add988 2019-10-15 stsp goto done;
1547 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1548 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1549 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1550 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1551 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1552 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1553 13add988 2019-10-15 stsp *have_match = 1;
1555 13add988 2019-10-15 stsp free(id_str);
1556 13add988 2019-10-15 stsp free(logmsg);
1557 13add988 2019-10-15 stsp return err;
1560 13add988 2019-10-15 stsp static const struct got_error *
1561 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1563 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1566 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1567 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1568 1a76625f 2018-10-22 stsp * while updating the display.
1571 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1572 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1573 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1574 1a76625f 2018-10-22 stsp int errcode;
1576 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1577 4e0d2870 2020-12-07 naddy NULL, NULL);
1578 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1581 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1584 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1585 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1586 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1590 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1591 1a76625f 2018-10-22 stsp if (errcode) {
1592 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1593 13add988 2019-10-15 stsp "pthread_mutex_lock");
1597 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1598 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1599 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1601 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1602 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1603 7c1452c1 2020-03-26 stsp int have_match;
1604 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1607 7c1452c1 2020-03-26 stsp if (have_match)
1608 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1611 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1612 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1613 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1614 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1617 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1619 9ba79e04 2018-06-11 stsp return err;
1622 2b779855 2020-12-05 naddy static void
1623 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1625 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1626 2b779855 2020-12-05 naddy int ncommits = 0;
1628 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1629 2b779855 2020-12-05 naddy while (entry) {
1630 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1631 2b779855 2020-12-05 naddy s->selected_entry = entry;
1634 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1635 2b779855 2020-12-05 naddy ncommits++;
1639 0553a4e3 2018-04-30 stsp static const struct got_error *
1640 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1642 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1643 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1644 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1645 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1646 60493ae3 2019-06-20 stsp int width;
1647 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1648 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1649 8b473291 2019-02-21 stsp char *refs_str = NULL;
1650 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1651 11b20872 2019-11-08 stsp struct tog_color *tc;
1652 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1654 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1655 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1656 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1657 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1659 ecb28ae0 2018-07-16 stsp return err;
1660 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1661 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1662 d2075bf3 2020-12-25 stsp if (refs) {
1663 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1664 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1666 d2075bf3 2020-12-25 stsp goto done;
1670 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1671 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1673 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1674 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1675 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1676 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1677 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1678 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1679 8f4ed634 2020-03-26 stsp goto done;
1682 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1684 f9686aa5 2020-03-27 stsp if (view->searching) {
1685 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1686 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1687 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1688 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1689 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1690 f9686aa5 2020-03-27 stsp search_str = "searching...";
1693 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1694 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1695 f9686aa5 2020-03-27 stsp search_str ? search_str :
1696 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1697 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1698 8f4ed634 2020-03-26 stsp goto done;
1702 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1703 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1704 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1705 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1706 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1707 1a76625f 2018-10-22 stsp header = NULL;
1708 1a76625f 2018-10-22 stsp goto done;
1710 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1711 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1712 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1713 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1714 1a76625f 2018-10-22 stsp header = NULL;
1715 1a76625f 2018-10-22 stsp goto done;
1717 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1719 1a76625f 2018-10-22 stsp goto done;
1721 2814baeb 2018-08-01 stsp werase(view->window);
1723 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1724 a3404814 2018-09-02 stsp wstandout(view->window);
1725 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1727 11b20872 2019-11-08 stsp wattr_on(view->window,
1728 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1729 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1731 11b20872 2019-11-08 stsp wattr_off(view->window,
1732 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1733 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1734 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1737 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1738 a3404814 2018-09-02 stsp wstandend(view->window);
1739 ecb28ae0 2018-07-16 stsp free(wline);
1740 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1741 1a76625f 2018-10-22 stsp goto done;
1743 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1744 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1745 5813d178 2019-03-09 stsp ncommits = 0;
1746 5813d178 2019-03-09 stsp while (entry) {
1747 5813d178 2019-03-09 stsp char *author;
1748 5813d178 2019-03-09 stsp wchar_t *wauthor;
1749 5813d178 2019-03-09 stsp int width;
1750 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1752 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1753 5813d178 2019-03-09 stsp if (author == NULL) {
1754 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1755 5813d178 2019-03-09 stsp goto done;
1757 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1758 27a741e5 2019-09-11 stsp date_display_cols);
1759 5813d178 2019-03-09 stsp if (author_cols < width)
1760 5813d178 2019-03-09 stsp author_cols = width;
1761 5813d178 2019-03-09 stsp free(wauthor);
1762 5813d178 2019-03-09 stsp free(author);
1763 7ca04879 2019-10-19 stsp ncommits++;
1764 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1767 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1768 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1769 867c6645 2018-07-10 stsp ncommits = 0;
1770 899d86c2 2018-05-10 stsp while (entry) {
1771 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1773 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1774 2814baeb 2018-08-01 stsp wstandout(view->window);
1775 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1776 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1777 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1778 2814baeb 2018-08-01 stsp wstandend(view->window);
1780 60493ae3 2019-06-20 stsp goto done;
1781 0553a4e3 2018-04-30 stsp ncommits++;
1782 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1783 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1786 1a57306a 2018-09-02 stsp view_vborder(view);
1787 dd038bc6 2021-09-21 thomas.ad update_panels();
1788 dd038bc6 2021-09-21 thomas.ad doupdate();
1790 1a76625f 2018-10-22 stsp free(id_str);
1791 8b473291 2019-02-21 stsp free(refs_str);
1792 1a76625f 2018-10-22 stsp free(ncommits_str);
1793 1a76625f 2018-10-22 stsp free(header);
1794 80ddbec8 2018-04-29 stsp return err;
1797 07b55e75 2018-05-10 stsp static void
1798 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1800 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1801 07b55e75 2018-05-10 stsp int nscrolled = 0;
1803 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1804 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1807 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1808 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1809 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1810 07b55e75 2018-05-10 stsp if (entry) {
1811 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1812 07b55e75 2018-05-10 stsp nscrolled++;
1817 aa075928 2018-05-10 stsp static const struct got_error *
1818 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1820 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1821 5e224a3e 2019-02-22 stsp int errcode;
1823 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1825 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1826 ffe38506 2020-12-01 naddy if (ta->log_complete)
1829 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1830 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1831 7aafa0d1 2019-02-22 stsp if (errcode)
1832 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1833 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1836 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1837 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1839 7c1452c1 2020-03-26 stsp if (!wait)
1842 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1843 ffe38506 2020-12-01 naddy show_log_view(view);
1844 7c1452c1 2020-03-26 stsp update_panels();
1845 7c1452c1 2020-03-26 stsp doupdate();
1847 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1848 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1849 82954512 2020-02-03 stsp if (errcode)
1850 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1851 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1853 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1854 ffe38506 2020-12-01 naddy show_log_view(view);
1855 7c1452c1 2020-03-26 stsp update_panels();
1856 7c1452c1 2020-03-26 stsp doupdate();
1859 5e224a3e 2019-02-22 stsp return NULL;
1862 5e224a3e 2019-02-22 stsp static const struct got_error *
1863 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1865 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1866 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1867 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1868 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1870 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1871 5e224a3e 2019-02-22 stsp return NULL;
1873 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1874 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1875 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1877 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1879 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1880 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1882 5e224a3e 2019-02-22 stsp return err;
1886 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1887 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1890 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1892 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1893 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1895 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1896 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1898 dd0a52c1 2018-05-20 stsp return err;
1901 cd0acaa7 2018-05-20 stsp static const struct got_error *
1902 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1903 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1904 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1906 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1907 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1908 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1910 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1911 15a94983 2018-12-23 stsp if (diff_view == NULL)
1912 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1914 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1915 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1916 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1917 e5a0f69f 2018-08-18 stsp if (err == NULL)
1918 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1919 cd0acaa7 2018-05-20 stsp return err;
1922 80ddbec8 2018-04-29 stsp static const struct got_error *
1923 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1924 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1926 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1928 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1929 941e9f74 2019-05-21 stsp if (parent == NULL)
1930 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1932 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1933 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1934 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1935 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1936 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1937 941e9f74 2019-05-21 stsp s->tree = subtree;
1938 941e9f74 2019-05-21 stsp s->selected = 0;
1939 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1940 941e9f74 2019-05-21 stsp return NULL;
1943 941e9f74 2019-05-21 stsp static const struct got_error *
1944 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1945 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1947 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1948 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1949 941e9f74 2019-05-21 stsp const char *p;
1950 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1952 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1954 941e9f74 2019-05-21 stsp while (*p) {
1955 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1956 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1957 56e0773d 2019-11-28 stsp char *te_name;
1959 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1962 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1963 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1964 941e9f74 2019-05-21 stsp if (slash == NULL)
1965 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1967 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1968 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1969 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1972 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1973 56e0773d 2019-11-28 stsp if (te == NULL) {
1974 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1975 56e0773d 2019-11-28 stsp free(te_name);
1978 56e0773d 2019-11-28 stsp free(te_name);
1979 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1981 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1982 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1984 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1985 941e9f74 2019-05-21 stsp if (slash)
1986 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1988 941e9f74 2019-05-21 stsp subpath = strdup(path);
1989 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1990 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1994 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
1999 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2000 941e9f74 2019-05-21 stsp free(tree_id);
2004 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2005 941e9f74 2019-05-21 stsp if (err) {
2006 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2009 941e9f74 2019-05-21 stsp if (slash == NULL)
2011 941e9f74 2019-05-21 stsp free(subpath);
2012 941e9f74 2019-05-21 stsp subpath = NULL;
2013 941e9f74 2019-05-21 stsp p = slash;
2016 941e9f74 2019-05-21 stsp free(subpath);
2017 1a76625f 2018-10-22 stsp return err;
2020 61266923 2020-01-14 stsp static const struct got_error *
2021 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2022 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2023 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2025 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2026 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2027 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2029 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2030 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2031 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2033 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2035 55cccc34 2020-02-20 stsp return err;
2036 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2038 55cccc34 2020-02-20 stsp *new_view = tree_view;
2040 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2041 55cccc34 2020-02-20 stsp return NULL;
2043 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2046 55cccc34 2020-02-20 stsp static const struct got_error *
2047 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2049 61266923 2020-01-14 stsp sigset_t sigset;
2050 61266923 2020-01-14 stsp int errcode;
2052 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2053 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2055 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2056 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2057 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2058 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2059 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2060 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2061 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2062 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2063 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2065 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2066 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2067 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2069 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2070 61266923 2020-01-14 stsp if (errcode)
2071 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2073 61266923 2020-01-14 stsp return NULL;
2076 1a76625f 2018-10-22 stsp static void *
2077 1a76625f 2018-10-22 stsp log_thread(void *arg)
2079 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2080 1a76625f 2018-10-22 stsp int errcode = 0;
2081 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2082 1a76625f 2018-10-22 stsp int done = 0;
2084 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2086 61266923 2020-01-14 stsp return (void *)err;
2088 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2089 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2090 1a76625f 2018-10-22 stsp if (err) {
2091 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2092 1a76625f 2018-10-22 stsp return (void *)err;
2093 1a76625f 2018-10-22 stsp err = NULL;
2095 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2096 1a76625f 2018-10-22 stsp a->commits_needed--;
2098 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2099 3abe8080 2019-04-10 stsp if (errcode) {
2100 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2101 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2103 3abe8080 2019-04-10 stsp } else if (*a->quit)
2105 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2106 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2107 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2108 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2111 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2112 7c1452c1 2020-03-26 stsp if (errcode) {
2113 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2114 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2115 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2120 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2122 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2123 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2124 7c1452c1 2020-03-26 stsp &tog_mutex);
2125 7c1452c1 2020-03-26 stsp if (errcode)
2126 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2127 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2128 21355643 2020-12-06 stsp if (*a->quit)
2133 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2134 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2135 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2136 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2138 3abe8080 2019-04-10 stsp a->log_complete = 1;
2139 1a76625f 2018-10-22 stsp return (void *)err;
2142 1a76625f 2018-10-22 stsp static const struct got_error *
2143 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2145 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2146 1a76625f 2018-10-22 stsp int errcode;
2148 1a76625f 2018-10-22 stsp if (s->thread) {
2149 1a76625f 2018-10-22 stsp s->quit = 1;
2150 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2151 1a76625f 2018-10-22 stsp if (errcode)
2152 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2153 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2154 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2155 1a76625f 2018-10-22 stsp if (errcode)
2156 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2157 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2158 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2159 1a76625f 2018-10-22 stsp if (errcode)
2160 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2161 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2162 1a76625f 2018-10-22 stsp if (errcode)
2163 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2164 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2165 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2168 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2169 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2170 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2173 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2174 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2175 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2178 9343a5fb 2018-06-23 stsp return err;
2181 9343a5fb 2018-06-23 stsp static const struct got_error *
2182 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2184 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2185 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2186 276b94a1 2020-11-13 naddy int errcode;
2188 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2190 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2191 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2192 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2194 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2195 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2196 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2198 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2199 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2200 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2201 1a76625f 2018-10-22 stsp free(s->start_id);
2202 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2203 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2204 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2205 1a76625f 2018-10-22 stsp return err;
2208 1a76625f 2018-10-22 stsp static const struct got_error *
2209 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2211 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2213 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2214 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2215 60493ae3 2019-06-20 stsp return NULL;
2218 60493ae3 2019-06-20 stsp static const struct got_error *
2219 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2221 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2222 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2223 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2225 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2226 f9686aa5 2020-03-27 stsp show_log_view(view);
2227 f9686aa5 2020-03-27 stsp update_panels();
2228 f9686aa5 2020-03-27 stsp doupdate();
2230 96e2b566 2019-07-08 stsp if (s->search_entry) {
2231 13add988 2019-10-15 stsp int errcode, ch;
2232 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2233 13add988 2019-10-15 stsp if (errcode)
2234 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2235 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2236 13add988 2019-10-15 stsp ch = wgetch(view->window);
2237 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2238 13add988 2019-10-15 stsp if (errcode)
2239 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2240 13add988 2019-10-15 stsp "pthread_mutex_lock");
2241 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2242 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2243 678cbce5 2019-07-28 stsp return NULL;
2245 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2246 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2248 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2249 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2250 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2251 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2252 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2254 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2255 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2257 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2260 60493ae3 2019-06-20 stsp while (1) {
2261 13add988 2019-10-15 stsp int have_match = 0;
2263 60493ae3 2019-06-20 stsp if (entry == NULL) {
2264 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2265 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2266 f9967bca 2020-03-27 stsp view->search_next_done =
2267 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2268 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2269 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2270 f801134a 2019-06-25 stsp return NULL;
2273 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2274 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2275 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2277 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2278 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2281 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2282 13add988 2019-10-15 stsp &view->regex);
2285 13add988 2019-10-15 stsp if (have_match) {
2286 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2287 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2291 96e2b566 2019-07-08 stsp s->search_entry = entry;
2292 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2293 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2295 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2298 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2299 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2300 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2301 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2303 60493ae3 2019-06-20 stsp return err;
2306 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2307 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2309 60493ae3 2019-06-20 stsp return err;
2314 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2316 60493ae3 2019-06-20 stsp return NULL;
2319 60493ae3 2019-06-20 stsp static const struct got_error *
2320 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2321 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2322 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2324 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2325 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2326 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2327 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2328 1a76625f 2018-10-22 stsp int errcode;
2330 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2331 f135c941 2020-02-20 stsp free(s->in_repo_path);
2332 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2333 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2334 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2337 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2338 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2339 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2341 fb2756b9 2018-08-04 stsp s->repo = repo;
2342 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2343 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2344 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2345 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2346 9cd7cbd1 2020-12-07 stsp goto done;
2349 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2350 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2351 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2352 5036bf37 2018-09-24 stsp goto done;
2354 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2356 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2357 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2358 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2359 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2361 11b20872 2019-11-08 stsp goto done;
2362 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2363 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2364 11b20872 2019-11-08 stsp if (err) {
2365 11b20872 2019-11-08 stsp free_colors(&s->colors);
2366 11b20872 2019-11-08 stsp goto done;
2368 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2369 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2370 11b20872 2019-11-08 stsp if (err) {
2371 11b20872 2019-11-08 stsp free_colors(&s->colors);
2372 11b20872 2019-11-08 stsp goto done;
2376 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2377 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2378 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2379 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2380 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2382 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2384 1a76625f 2018-10-22 stsp goto done;
2385 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2386 b672a97a 2020-01-27 stsp !s->log_branches);
2388 1a76625f 2018-10-22 stsp goto done;
2389 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2390 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2392 c5b78334 2020-01-12 stsp goto done;
2394 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2395 1a76625f 2018-10-22 stsp if (errcode) {
2396 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2397 1a76625f 2018-10-22 stsp goto done;
2399 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2400 7c1452c1 2020-03-26 stsp if (errcode) {
2401 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2402 7c1452c1 2020-03-26 stsp goto done;
2405 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2406 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2407 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2408 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2409 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2410 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2411 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2412 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2413 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2414 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2415 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2416 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2417 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2420 1a76625f 2018-10-22 stsp close_log_view(view);
2421 ba4f502b 2018-08-04 stsp return err;
2424 e5a0f69f 2018-08-18 stsp static const struct got_error *
2425 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2427 f2f6d207 2020-11-24 stsp const struct got_error *err;
2428 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2430 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2431 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2432 2b380cc8 2018-10-24 stsp &s->thread_args);
2433 2b380cc8 2018-10-24 stsp if (errcode)
2434 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2435 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2436 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2438 f2f6d207 2020-11-24 stsp return err;
2442 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2445 e5a0f69f 2018-08-18 stsp static const struct got_error *
2446 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2448 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2449 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2450 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2451 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2452 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2453 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
2455 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2456 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2457 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2458 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2459 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2460 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2461 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2462 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2463 fb280deb 2021-08-30 stsp select_commit(s);
2465 528dedf3 2021-08-30 stsp return NULL;
2468 528dedf3 2021-08-30 stsp switch (ch) {
2470 1e37a5c2 2019-05-12 jcs s->quit = 1;
2473 1e37a5c2 2019-05-12 jcs case KEY_UP:
2476 f7140bf5 2021-10-17 thomas case CTRL('p'):
2477 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2479 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2480 1e37a5c2 2019-05-12 jcs s->selected--;
2482 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2483 912a3f79 2021-08-30 j select_commit(s);
2486 912a3f79 2021-08-30 j case KEY_HOME:
2487 912a3f79 2021-08-30 j s->selected = 0;
2488 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2489 2b779855 2020-12-05 naddy select_commit(s);
2491 70f17a53 2022-06-13 thomas case CTRL('u'):
2492 70f17a53 2022-06-13 thomas nscroll /= 2;
2493 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2494 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2495 a4292ac5 2019-05-12 jcs case CTRL('b'):
2496 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2498 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2499 70f17a53 2022-06-13 thomas s->selected = MAX(0, s->selected - nscroll - 1);
2501 70f17a53 2022-06-13 thomas log_scroll_up(s, nscroll);
2502 2b779855 2020-12-05 naddy select_commit(s);
2505 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2508 f7140bf5 2021-10-17 thomas case CTRL('n'):
2509 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2511 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2512 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2513 1e37a5c2 2019-05-12 jcs s->selected++;
2515 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2519 912a3f79 2021-08-30 j select_commit(s);
2522 912a3f79 2021-08-30 j case KEY_END: {
2523 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2524 912a3f79 2021-08-30 j * traverse them all. */
2525 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2526 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2527 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2530 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2531 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2532 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2533 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2535 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2536 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2538 f3bc9f1d 2021-09-05 naddy if (n > 0)
2539 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2540 2b779855 2020-12-05 naddy select_commit(s);
2543 bccd1d5d 2022-06-13 thomas case CTRL('d'):
2544 70f17a53 2022-06-13 thomas nscroll /= 2;
2545 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2546 70f17a53 2022-06-13 thomas case KEY_NPAGE:
2547 70f17a53 2022-06-13 thomas case CTRL('f'): {
2548 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2549 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2550 1e37a5c2 2019-05-12 jcs if (first == NULL)
2552 70f17a53 2022-06-13 thomas err = log_scroll_down(view, nscroll);
2555 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2556 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2557 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2558 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2559 70f17a53 2022-06-13 thomas s->selected += MIN(s->last_displayed_entry->idx -
2560 70f17a53 2022-06-13 thomas s->selected_entry->idx, nscroll + 1);
2562 2b779855 2020-12-05 naddy select_commit(s);
2565 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2566 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2567 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2568 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2569 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2570 2b779855 2020-12-05 naddy select_commit(s);
2571 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2572 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2573 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2574 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2575 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2578 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2581 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2583 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2584 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2585 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2586 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2587 78756c87 2020-11-24 stsp view, s->repo);
2590 e78dc838 2020-12-04 stsp view->focussed = 0;
2591 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2592 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2593 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2595 1e37a5c2 2019-05-12 jcs return err;
2596 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2597 e78dc838 2020-12-04 stsp view->focus_child = 1;
2599 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2602 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2604 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2605 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2606 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2607 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2611 e78dc838 2020-12-04 stsp view->focussed = 0;
2612 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2613 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2614 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2616 1e37a5c2 2019-05-12 jcs return err;
2617 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2618 e78dc838 2020-12-04 stsp view->focus_child = 1;
2620 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2622 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2623 21355643 2020-12-06 stsp case CTRL('l'):
2625 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2626 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2628 21355643 2020-12-06 stsp err = stop_log_thread(s);
2630 74cfe85e 2020-10-20 stsp return err;
2631 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2632 21355643 2020-12-06 stsp char *parent_path;
2633 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2635 21355643 2020-12-06 stsp return err;
2636 21355643 2020-12-06 stsp free(s->in_repo_path);
2637 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2638 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2639 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2640 21355643 2020-12-06 stsp struct got_object_id *start_id;
2641 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2642 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2643 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2645 21355643 2020-12-06 stsp return err;
2646 21355643 2020-12-06 stsp free(s->start_id);
2647 21355643 2020-12-06 stsp s->start_id = start_id;
2648 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2649 21355643 2020-12-06 stsp } else /* 'B' */
2650 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2652 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2653 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2655 21355643 2020-12-06 stsp return err;
2656 51a10b52 2020-12-26 stsp tog_free_refs();
2657 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2659 ca51c541 2020-12-07 stsp return err;
2660 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2661 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2663 d01904d4 2019-06-25 stsp return err;
2664 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2665 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2667 b672a97a 2020-01-27 stsp return err;
2668 21355643 2020-12-06 stsp free_commits(&s->commits);
2669 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2670 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2671 21355643 2020-12-06 stsp s->selected_entry = NULL;
2672 21355643 2020-12-06 stsp s->selected = 0;
2673 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2674 21355643 2020-12-06 stsp s->quit = 0;
2675 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2678 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2679 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2680 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2681 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2682 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2683 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2684 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2685 6458efa5 2020-11-24 stsp if (err) {
2686 6458efa5 2020-11-24 stsp view_close(ref_view);
2687 6458efa5 2020-11-24 stsp return err;
2689 e78dc838 2020-12-04 stsp view->focussed = 0;
2690 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2691 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2692 6458efa5 2020-11-24 stsp err = view_close_child(view);
2694 6458efa5 2020-11-24 stsp return err;
2695 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2696 e78dc838 2020-12-04 stsp view->focus_child = 1;
2698 6458efa5 2020-11-24 stsp *new_view = ref_view;
2704 80ddbec8 2018-04-29 stsp return err;
2707 4ed7e80c 2018-05-20 stsp static const struct got_error *
2708 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2710 c2db6724 2019-01-04 stsp const struct got_error *error;
2712 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2713 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2714 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2716 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2717 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2719 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2720 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2722 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2723 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2725 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2726 c2db6724 2019-01-04 stsp if (error != NULL)
2727 c2db6724 2019-01-04 stsp return error;
2729 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2730 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2732 c2db6724 2019-01-04 stsp return NULL;
2735 a915003a 2019-02-05 stsp static void
2736 a915003a 2019-02-05 stsp init_curses(void)
2739 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
2740 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
2741 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
2743 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
2744 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
2745 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
2746 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
2747 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
2749 a915003a 2019-02-05 stsp initscr();
2751 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2754 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2755 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2756 a915003a 2019-02-05 stsp curs_set(0);
2757 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2758 6d17833f 2019-11-08 stsp start_color();
2759 6d17833f 2019-11-08 stsp use_default_colors();
2763 c2db6724 2019-01-04 stsp static const struct got_error *
2764 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2765 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2767 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2769 f135c941 2020-02-20 stsp if (argc == 0) {
2770 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2771 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2772 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2773 f135c941 2020-02-20 stsp return NULL;
2776 f135c941 2020-02-20 stsp if (worktree) {
2777 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2780 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2782 f135c941 2020-02-20 stsp return err;
2783 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2784 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2785 bfd61697 2020-11-14 stsp p) == -1) {
2786 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2787 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2791 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2793 f135c941 2020-02-20 stsp return err;
2796 f135c941 2020-02-20 stsp static const struct got_error *
2797 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2799 80ddbec8 2018-04-29 stsp const struct got_error *error;
2800 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2801 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2802 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2803 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2804 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2805 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2806 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2807 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2808 04cc582a 2018-08-01 stsp struct tog_view *view;
2810 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2811 80ddbec8 2018-04-29 stsp switch (ch) {
2813 b672a97a 2020-01-27 stsp log_branches = 1;
2816 80ddbec8 2018-04-29 stsp start_commit = optarg;
2819 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2820 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2821 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2825 17020d27 2019-03-07 stsp usage_log();
2826 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2830 80ddbec8 2018-04-29 stsp argc -= optind;
2831 80ddbec8 2018-04-29 stsp argv += optind;
2833 f135c941 2020-02-20 stsp if (argc > 1)
2834 f135c941 2020-02-20 stsp usage_log();
2836 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2837 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2838 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2839 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2840 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2841 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2842 c156c7a4 2020-12-18 stsp goto done;
2843 a1fbf39a 2019-08-11 stsp if (worktree)
2844 6962eb72 2020-02-20 stsp repo_path =
2845 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2847 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2848 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2849 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2850 c156c7a4 2020-12-18 stsp goto done;
2854 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2855 80ddbec8 2018-04-29 stsp if (error != NULL)
2856 ecb28ae0 2018-07-16 stsp goto done;
2858 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2859 f135c941 2020-02-20 stsp repo, worktree);
2860 f135c941 2020-02-20 stsp if (error)
2861 f135c941 2020-02-20 stsp goto done;
2863 f135c941 2020-02-20 stsp init_curses();
2865 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2866 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2867 c02c541e 2019-03-29 stsp if (error)
2868 c02c541e 2019-03-29 stsp goto done;
2870 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2871 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2872 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
2873 87670572 2020-12-26 naddy if (error)
2874 87670572 2020-12-26 naddy goto done;
2877 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2878 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2879 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2880 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2881 d8f38dc4 2020-12-05 stsp if (error)
2882 d8f38dc4 2020-12-05 stsp goto done;
2883 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2885 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2886 d8f38dc4 2020-12-05 stsp if (error == NULL)
2887 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2888 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2889 d8f38dc4 2020-12-05 stsp goto done;
2890 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2891 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2892 d8f38dc4 2020-12-05 stsp if (error)
2893 d8f38dc4 2020-12-05 stsp goto done;
2896 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2897 04cc582a 2018-08-01 stsp if (view == NULL) {
2898 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2899 04cc582a 2018-08-01 stsp goto done;
2901 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2902 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2903 ba4f502b 2018-08-04 stsp if (error)
2904 ba4f502b 2018-08-04 stsp goto done;
2905 2fc00ff4 2019-08-31 stsp if (worktree) {
2906 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2907 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2908 2fc00ff4 2019-08-31 stsp worktree = NULL;
2910 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2912 f135c941 2020-02-20 stsp free(in_repo_path);
2913 ecb28ae0 2018-07-16 stsp free(repo_path);
2914 ecb28ae0 2018-07-16 stsp free(cwd);
2915 899d86c2 2018-05-10 stsp free(start_id);
2916 d8f38dc4 2020-12-05 stsp free(label);
2918 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2919 1d0f4054 2021-06-17 stsp if (repo) {
2920 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2921 1d0f4054 2021-06-17 stsp if (error == NULL)
2922 1d0f4054 2021-06-17 stsp error = close_err;
2924 ec142235 2019-03-07 stsp if (worktree)
2925 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2926 51a10b52 2020-12-26 stsp tog_free_refs();
2927 80ddbec8 2018-04-29 stsp return error;
2930 4ed7e80c 2018-05-20 stsp __dead static void
2931 9f7d7167 2018-04-29 stsp usage_diff(void)
2934 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2935 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2939 6d17833f 2019-11-08 stsp static int
2940 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2941 41605754 2020-11-12 stsp regmatch_t *regmatch)
2943 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2946 f26dddb7 2019-11-08 stsp struct tog_color *
2947 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2949 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2951 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2952 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2953 6d17833f 2019-11-08 stsp return tc;
2956 6d17833f 2019-11-08 stsp return NULL;
2959 4ed7e80c 2018-05-20 stsp static const struct got_error *
2960 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2961 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2963 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2964 41605754 2020-11-12 stsp wchar_t *wline;
2965 41605754 2020-11-12 stsp int width;
2968 41605754 2020-11-12 stsp *wtotal = 0;
2970 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2971 41605754 2020-11-12 stsp if (s == NULL)
2972 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2974 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2975 41605754 2020-11-12 stsp if (err) {
2977 41605754 2020-11-12 stsp return err;
2979 41605754 2020-11-12 stsp waddwstr(window, wline);
2980 41605754 2020-11-12 stsp free(wline);
2982 41605754 2020-11-12 stsp wlimit -= width;
2983 41605754 2020-11-12 stsp *wtotal += width;
2985 41605754 2020-11-12 stsp if (wlimit > 0) {
2986 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2987 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2988 41605754 2020-11-12 stsp if (s == NULL) {
2989 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2991 41605754 2020-11-12 stsp return err;
2993 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2994 41605754 2020-11-12 stsp if (err) {
2996 41605754 2020-11-12 stsp return err;
2998 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2999 41605754 2020-11-12 stsp waddwstr(window, wline);
3000 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
3001 41605754 2020-11-12 stsp free(wline);
3003 41605754 2020-11-12 stsp wlimit -= width;
3004 41605754 2020-11-12 stsp *wtotal += width;
3007 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3008 41605754 2020-11-12 stsp err = format_line(&wline, &width,
3009 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
3011 41605754 2020-11-12 stsp return err;
3012 41605754 2020-11-12 stsp waddwstr(window, wline);
3013 41605754 2020-11-12 stsp free(wline);
3014 41605754 2020-11-12 stsp *wtotal += width;
3017 41605754 2020-11-12 stsp return NULL;
3020 41605754 2020-11-12 stsp static const struct got_error *
3021 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3023 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3024 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3025 61e69b96 2018-05-20 stsp const struct got_error *err;
3026 fe621944 2020-11-10 stsp int nprinted = 0;
3027 b304db33 2018-05-20 stsp char *line;
3028 826082fe 2020-12-10 stsp size_t linesize = 0;
3029 826082fe 2020-12-10 stsp ssize_t linelen;
3030 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3031 61e69b96 2018-05-20 stsp wchar_t *wline;
3032 e0b650dd 2018-05-20 stsp int width;
3033 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3034 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3035 fe621944 2020-11-10 stsp off_t line_offset;
3037 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3038 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3039 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3041 f7d12f7e 2018-08-01 stsp werase(view->window);
3043 a3404814 2018-09-02 stsp if (header) {
3044 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3045 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3046 135a2da0 2020-11-11 stsp header) == -1)
3047 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3048 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3049 135a2da0 2020-11-11 stsp free(line);
3051 a3404814 2018-09-02 stsp return err;
3053 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3054 a3404814 2018-09-02 stsp wstandout(view->window);
3055 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3056 e54cc94a 2020-11-11 stsp free(wline);
3057 e54cc94a 2020-11-11 stsp wline = NULL;
3058 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3059 a3404814 2018-09-02 stsp wstandend(view->window);
3060 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3061 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3063 a3404814 2018-09-02 stsp if (max_lines <= 1)
3064 a3404814 2018-09-02 stsp return NULL;
3065 a3404814 2018-09-02 stsp max_lines--;
3068 89f1a395 2020-12-01 naddy s->eof = 0;
3069 826082fe 2020-12-10 stsp line = NULL;
3070 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3071 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3072 826082fe 2020-12-10 stsp if (linelen == -1) {
3073 826082fe 2020-12-10 stsp if (feof(s->f)) {
3074 826082fe 2020-12-10 stsp s->eof = 1;
3077 826082fe 2020-12-10 stsp free(line);
3078 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3081 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3083 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3084 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3085 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3086 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3087 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3088 41605754 2020-11-12 stsp view->window, regmatch);
3089 41605754 2020-11-12 stsp if (err) {
3090 41605754 2020-11-12 stsp free(line);
3091 41605754 2020-11-12 stsp return err;
3094 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3095 41605754 2020-11-12 stsp if (err) {
3096 41605754 2020-11-12 stsp free(line);
3097 41605754 2020-11-12 stsp return err;
3099 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3100 41605754 2020-11-12 stsp free(wline);
3101 41605754 2020-11-12 stsp wline = NULL;
3104 6d17833f 2019-11-08 stsp wattr_off(view->window,
3105 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3106 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3107 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3108 fe621944 2020-11-10 stsp nprinted++;
3110 826082fe 2020-12-10 stsp free(line);
3111 fe621944 2020-11-10 stsp if (nprinted >= 1)
3112 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3113 89f1a395 2020-12-01 naddy (nprinted - 1);
3115 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3117 1a57306a 2018-09-02 stsp view_vborder(view);
3119 89f1a395 2020-12-01 naddy if (s->eof) {
3120 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3121 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3122 c3e9aa98 2019-05-13 jcs nprinted++;
3125 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3127 c3e9aa98 2019-05-13 jcs return err;
3130 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3131 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3132 e54cc94a 2020-11-11 stsp free(wline);
3133 e54cc94a 2020-11-11 stsp wline = NULL;
3134 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3137 26ed57b2 2018-05-19 stsp return NULL;
3140 abd2672a 2018-12-23 stsp static char *
3141 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3143 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3144 09867e48 2019-08-13 stsp char *p, *s;
3146 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3147 09867e48 2019-08-13 stsp if (tm == NULL)
3148 09867e48 2019-08-13 stsp return NULL;
3149 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3150 09867e48 2019-08-13 stsp if (s == NULL)
3151 09867e48 2019-08-13 stsp return NULL;
3152 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3154 abd2672a 2018-12-23 stsp *p = '\0';
3158 4ed7e80c 2018-05-20 stsp static const struct got_error *
3159 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3160 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3162 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3163 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3164 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3165 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3167 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3168 0208f208 2020-05-05 stsp if (qid != NULL) {
3169 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3170 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3171 ec242592 2022-04-22 thomas &qid->id);
3173 0208f208 2020-05-05 stsp return err;
3175 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3176 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3177 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3178 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3179 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3181 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3185 0208f208 2020-05-05 stsp if (tree_id1) {
3186 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3188 0208f208 2020-05-05 stsp goto done;
3191 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3192 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3194 0208f208 2020-05-05 stsp goto done;
3196 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3197 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3199 0208f208 2020-05-05 stsp if (tree1)
3200 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3201 0208f208 2020-05-05 stsp if (tree2)
3202 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3203 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3204 0208f208 2020-05-05 stsp return err;
3207 0208f208 2020-05-05 stsp static const struct got_error *
3208 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3212 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3213 fe621944 2020-11-10 stsp if (p == NULL)
3214 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3215 fe621944 2020-11-10 stsp *line_offsets = p;
3216 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3217 fe621944 2020-11-10 stsp (*nlines)++;
3218 fe621944 2020-11-10 stsp return NULL;
3221 fe621944 2020-11-10 stsp static const struct got_error *
3222 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3223 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3224 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3226 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3227 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3228 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3229 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3230 45d799e2 2018-12-23 stsp time_t committer_time;
3231 45d799e2 2018-12-23 stsp const char *author, *committer;
3232 8b473291 2019-02-21 stsp char *refs_str = NULL;
3233 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3234 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3235 fe621944 2020-11-10 stsp off_t outoff = 0;
3238 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3240 8b473291 2019-02-21 stsp if (refs) {
3241 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3243 8b473291 2019-02-21 stsp return err;
3246 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3248 abd2672a 2018-12-23 stsp return err;
3250 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3251 15a94983 2018-12-23 stsp if (err) {
3252 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3253 15a94983 2018-12-23 stsp goto done;
3256 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3258 fe621944 2020-11-10 stsp goto done;
3260 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3261 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3262 fe621944 2020-11-10 stsp if (n < 0) {
3263 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3264 abd2672a 2018-12-23 stsp goto done;
3266 fe621944 2020-11-10 stsp outoff += n;
3267 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3269 fe621944 2020-11-10 stsp goto done;
3271 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3272 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3273 fe621944 2020-11-10 stsp if (n < 0) {
3274 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3275 abd2672a 2018-12-23 stsp goto done;
3277 fe621944 2020-11-10 stsp outoff += n;
3278 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3280 fe621944 2020-11-10 stsp goto done;
3282 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3283 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3284 fe621944 2020-11-10 stsp if (datestr) {
3285 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3286 fe621944 2020-11-10 stsp if (n < 0) {
3287 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3288 fe621944 2020-11-10 stsp goto done;
3290 fe621944 2020-11-10 stsp outoff += n;
3291 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3293 fe621944 2020-11-10 stsp goto done;
3295 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3296 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3297 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3298 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3299 fe621944 2020-11-10 stsp if (n < 0) {
3300 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3301 fe621944 2020-11-10 stsp goto done;
3303 fe621944 2020-11-10 stsp outoff += n;
3304 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3306 fe621944 2020-11-10 stsp goto done;
3308 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3309 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3310 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3311 ac4dc263 2021-09-24 thomas int pn = 1;
3312 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3313 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3314 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3315 ac4dc263 2021-09-24 thomas if (err)
3316 ac4dc263 2021-09-24 thomas goto done;
3317 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3318 ac4dc263 2021-09-24 thomas if (n < 0) {
3319 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3320 ac4dc263 2021-09-24 thomas goto done;
3322 ac4dc263 2021-09-24 thomas outoff += n;
3323 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3324 ac4dc263 2021-09-24 thomas if (err)
3325 ac4dc263 2021-09-24 thomas goto done;
3326 ac4dc263 2021-09-24 thomas free(id_str);
3327 ac4dc263 2021-09-24 thomas id_str = NULL;
3331 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3333 5943eee2 2019-08-13 stsp goto done;
3334 fe621944 2020-11-10 stsp s = logmsg;
3335 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3336 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3337 fe621944 2020-11-10 stsp if (n < 0) {
3338 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3339 fe621944 2020-11-10 stsp goto done;
3341 fe621944 2020-11-10 stsp outoff += n;
3342 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3344 fe621944 2020-11-10 stsp goto done;
3347 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3349 0208f208 2020-05-05 stsp goto done;
3350 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3351 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3352 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3353 fe621944 2020-11-10 stsp if (n < 0) {
3354 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3355 fe621944 2020-11-10 stsp goto done;
3357 fe621944 2020-11-10 stsp outoff += n;
3358 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3360 fe621944 2020-11-10 stsp goto done;
3361 0208f208 2020-05-05 stsp free((char *)pe->path);
3362 0208f208 2020-05-05 stsp free(pe->data);
3365 0208f208 2020-05-05 stsp fputc('\n', outfile);
3367 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3369 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3370 abd2672a 2018-12-23 stsp free(id_str);
3371 5943eee2 2019-08-13 stsp free(logmsg);
3372 8b473291 2019-02-21 stsp free(refs_str);
3373 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3374 7510f233 2020-08-09 stsp if (err) {
3375 ae6a6978 2020-08-09 stsp free(*line_offsets);
3376 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3377 ae6a6978 2020-08-09 stsp *nlines = 0;
3379 fe621944 2020-11-10 stsp return err;
3382 abd2672a 2018-12-23 stsp static const struct got_error *
3383 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3385 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3386 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3387 15a94983 2018-12-23 stsp int obj_type;
3389 fe621944 2020-11-10 stsp free(s->line_offsets);
3390 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3391 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3392 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3393 fe621944 2020-11-10 stsp s->nlines = 0;
3395 511a516b 2018-05-19 stsp f = got_opentemp();
3396 48ae06ee 2018-10-18 stsp if (f == NULL) {
3397 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3398 48ae06ee 2018-10-18 stsp goto done;
3400 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3401 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3402 fb43ecf1 2019-02-11 stsp goto done;
3406 15a94983 2018-12-23 stsp if (s->id1)
3407 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3409 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3411 15a94983 2018-12-23 stsp goto done;
3413 15a94983 2018-12-23 stsp switch (obj_type) {
3414 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3415 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3416 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3417 a0f32f33 2022-06-13 thomas s->diff_context, s->ignore_whitespace, s->force_text_diff,
3418 a0f32f33 2022-06-13 thomas s->repo, s->f);
3420 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3421 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3422 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3423 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3425 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3426 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3427 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3428 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3429 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3431 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3433 3ffacbe1 2020-02-02 tracey goto done;
3434 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3435 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3436 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3437 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3438 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3439 f44b1f58 2020-02-02 tracey if (err)
3440 f44b1f58 2020-02-02 tracey goto done;
3441 f44b1f58 2020-02-02 tracey } else {
3442 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3443 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3444 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3445 fe621944 2020-11-10 stsp err = write_commit_info(
3446 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3447 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3448 f44b1f58 2020-02-02 tracey if (err)
3449 f44b1f58 2020-02-02 tracey goto done;
3454 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3456 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3457 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3458 a0f32f33 2022-06-13 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3462 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3465 f44b1f58 2020-02-02 tracey if (err)
3466 f44b1f58 2020-02-02 tracey goto done;
3468 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3469 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3470 48ae06ee 2018-10-18 stsp return err;
3473 f5215bb9 2019-02-22 stsp static void
3474 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3476 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3477 f5215bb9 2019-02-22 stsp update_panels();
3478 f5215bb9 2019-02-22 stsp doupdate();
3481 f44b1f58 2020-02-02 tracey static const struct got_error *
3482 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3484 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3486 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3487 f44b1f58 2020-02-02 tracey return NULL;
3490 f44b1f58 2020-02-02 tracey static const struct got_error *
3491 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3493 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3494 f44b1f58 2020-02-02 tracey int lineno;
3495 826082fe 2020-12-10 stsp char *line = NULL;
3496 826082fe 2020-12-10 stsp size_t linesize = 0;
3497 826082fe 2020-12-10 stsp ssize_t linelen;
3499 f44b1f58 2020-02-02 tracey if (!view->searching) {
3500 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3501 f44b1f58 2020-02-02 tracey return NULL;
3504 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3505 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3506 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3508 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3510 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3512 f44b1f58 2020-02-02 tracey while (1) {
3513 f44b1f58 2020-02-02 tracey off_t offset;
3515 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3516 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3517 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3521 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3522 f44b1f58 2020-02-02 tracey lineno = 1;
3524 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3527 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3528 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3529 f44b1f58 2020-02-02 tracey free(line);
3530 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3532 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3533 826082fe 2020-12-10 stsp if (linelen != -1 &&
3534 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3535 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3536 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3539 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3540 f44b1f58 2020-02-02 tracey lineno++;
3542 f44b1f58 2020-02-02 tracey lineno--;
3544 826082fe 2020-12-10 stsp free(line);
3546 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3547 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3548 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3551 f44b1f58 2020-02-02 tracey return NULL;
3554 48ae06ee 2018-10-18 stsp static const struct got_error *
3555 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
3557 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
3558 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
3560 a0f32f33 2022-06-13 thomas free(s->id1);
3561 a0f32f33 2022-06-13 thomas s->id1 = NULL;
3562 a0f32f33 2022-06-13 thomas free(s->id2);
3563 a0f32f33 2022-06-13 thomas s->id2 = NULL;
3564 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
3565 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3566 a0f32f33 2022-06-13 thomas s->f = NULL;
3567 a0f32f33 2022-06-13 thomas if (s->f1 && fclose(s->f1) == EOF)
3568 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3569 a0f32f33 2022-06-13 thomas s->f1 = NULL;
3570 a0f32f33 2022-06-13 thomas if (s->f2 && fclose(s->f2) == EOF)
3571 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3572 a0f32f33 2022-06-13 thomas s->f2 = NULL;
3573 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
3574 a0f32f33 2022-06-13 thomas free(s->line_offsets);
3575 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
3576 a0f32f33 2022-06-13 thomas s->nlines = 0;
3577 a0f32f33 2022-06-13 thomas return err;
3580 a0f32f33 2022-06-13 thomas static const struct got_error *
3581 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3582 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3583 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3584 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3586 48ae06ee 2018-10-18 stsp const struct got_error *err;
3587 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3589 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
3591 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3592 15a94983 2018-12-23 stsp int type1, type2;
3593 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3595 15a94983 2018-12-23 stsp return err;
3596 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3598 15a94983 2018-12-23 stsp return err;
3600 15a94983 2018-12-23 stsp if (type1 != type2)
3601 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3603 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3604 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3605 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3606 f44b1f58 2020-02-02 tracey s->repo = repo;
3607 f44b1f58 2020-02-02 tracey s->id1 = id1;
3608 f44b1f58 2020-02-02 tracey s->id2 = id2;
3609 3dbaef42 2020-11-24 stsp s->label1 = label1;
3610 3dbaef42 2020-11-24 stsp s->label2 = label2;
3612 15a94983 2018-12-23 stsp if (id1) {
3613 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3614 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3615 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3616 a0f32f33 2022-06-13 thomas s->f1 = got_opentemp();
3617 a0f32f33 2022-06-13 thomas if (s->f1 == NULL) {
3618 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3619 a0f32f33 2022-06-13 thomas goto done;
3622 5465d566 2020-02-01 tracey s->id1 = NULL;
3624 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3625 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3626 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
3627 a0f32f33 2022-06-13 thomas goto done;
3630 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
3631 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
3632 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3633 a0f32f33 2022-06-13 thomas goto done;
3636 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3637 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3638 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3639 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3640 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3641 5465d566 2020-02-01 tracey s->log_view = log_view;
3642 5465d566 2020-02-01 tracey s->repo = repo;
3644 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3645 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3646 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3647 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3648 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3650 a0f32f33 2022-06-13 thomas goto done;
3651 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3652 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3653 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3654 a0f32f33 2022-06-13 thomas if (err)
3655 a0f32f33 2022-06-13 thomas goto done;
3656 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3657 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3658 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3659 a0f32f33 2022-06-13 thomas if (err)
3660 a0f32f33 2022-06-13 thomas goto done;
3662 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3663 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3664 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3665 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3666 a0f32f33 2022-06-13 thomas if (err)
3667 a0f32f33 2022-06-13 thomas goto done;
3669 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3670 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3671 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3672 a0f32f33 2022-06-13 thomas if (err)
3673 a0f32f33 2022-06-13 thomas goto done;
3675 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3676 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3677 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3678 a0f32f33 2022-06-13 thomas if (err)
3679 a0f32f33 2022-06-13 thomas goto done;
3682 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3683 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3684 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3686 5465d566 2020-02-01 tracey err = create_diff(s);
3688 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3689 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3690 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3691 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3692 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3694 a0f32f33 2022-06-13 thomas if (err)
3695 a0f32f33 2022-06-13 thomas close_diff_view(view);
3696 e5a0f69f 2018-08-18 stsp return err;
3699 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3700 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3702 a3404814 2018-09-02 stsp const struct got_error *err;
3703 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3704 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3705 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3707 a3404814 2018-09-02 stsp if (s->id1) {
3708 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3710 a3404814 2018-09-02 stsp return err;
3711 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3713 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3715 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3717 a3404814 2018-09-02 stsp return err;
3718 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3720 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3721 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3722 a3404814 2018-09-02 stsp free(id_str1);
3723 a3404814 2018-09-02 stsp free(id_str2);
3724 a3404814 2018-09-02 stsp return err;
3726 a3404814 2018-09-02 stsp free(id_str1);
3727 a3404814 2018-09-02 stsp free(id_str2);
3729 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3730 267bb3b8 2021-08-01 stsp free(header);
3731 267bb3b8 2021-08-01 stsp return err;
3734 15a087fe 2019-02-21 stsp static const struct got_error *
3735 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3736 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3738 d7a04538 2019-02-21 stsp const struct got_error *err;
3739 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3740 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3741 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3743 15a087fe 2019-02-21 stsp free(s->id2);
3744 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3745 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3746 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3748 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3750 d7a04538 2019-02-21 stsp return err;
3751 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3752 15a087fe 2019-02-21 stsp free(s->id1);
3753 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3754 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3755 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3756 15a087fe 2019-02-21 stsp return NULL;
3759 0cf4efb1 2018-09-29 stsp static const struct got_error *
3760 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3762 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3763 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3764 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3765 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3766 826082fe 2020-12-10 stsp char *line = NULL;
3767 826082fe 2020-12-10 stsp size_t linesize = 0;
3768 826082fe 2020-12-10 stsp ssize_t linelen;
3769 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
3771 e5a0f69f 2018-08-18 stsp switch (ch) {
3774 3dbaef42 2020-11-24 stsp if (ch == 'a')
3775 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3776 3dbaef42 2020-11-24 stsp if (ch == 'w')
3777 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3778 64453f7e 2020-11-21 stsp wclear(view->window);
3779 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3780 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3781 aa61903a 2021-12-31 thomas s->matched_line = 0;
3782 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3783 64453f7e 2020-11-21 stsp err = create_diff(s);
3786 912a3f79 2021-08-30 j case KEY_HOME:
3787 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3790 912a3f79 2021-08-30 j case KEY_END:
3794 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3798 1e37a5c2 2019-05-12 jcs case KEY_UP:
3799 f7140bf5 2021-10-17 thomas case CTRL('p'):
3800 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3801 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3803 70f17a53 2022-06-13 thomas case CTRL('u'):
3804 70f17a53 2022-06-13 thomas nscroll /= 2;
3805 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3806 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3807 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3808 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3811 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
3812 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3815 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3816 f7140bf5 2021-10-17 thomas case CTRL('n'):
3817 1e37a5c2 2019-05-12 jcs if (!s->eof)
3818 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3820 70f17a53 2022-06-13 thomas case CTRL('d'):
3821 70f17a53 2022-06-13 thomas nscroll /= 2;
3822 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3823 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3824 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3826 00ba99a7 2019-05-12 jcs if (s->eof)
3829 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
3830 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3831 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3832 826082fe 2020-12-10 stsp if (linelen == -1) {
3833 826082fe 2020-12-10 stsp if (feof(s->f)) {
3834 826082fe 2020-12-10 stsp s->eof = 1;
3836 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3840 826082fe 2020-12-10 stsp free(line);
3843 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3844 1e37a5c2 2019-05-12 jcs s->diff_context--;
3845 aa61903a 2021-12-31 thomas s->matched_line = 0;
3846 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3847 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3848 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3849 27829c9e 2020-11-21 stsp s->nlines) {
3850 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3851 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3856 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3857 1e37a5c2 2019-05-12 jcs s->diff_context++;
3858 aa61903a 2021-12-31 thomas s->matched_line = 0;
3859 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3860 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3865 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3867 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3868 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3870 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3874 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3877 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3881 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3882 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3883 aa61903a 2021-12-31 thomas s->matched_line = 0;
3885 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3886 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3890 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3892 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3893 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3895 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3899 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3902 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3906 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3907 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3908 aa61903a 2021-12-31 thomas s->matched_line = 0;
3910 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3911 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3917 bcbd79e2 2018-08-19 stsp return err;
3920 4ed7e80c 2018-05-20 stsp static const struct got_error *
3921 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3923 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3924 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3925 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3926 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3927 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3928 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3929 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3930 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3931 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3932 3dbaef42 2020-11-24 stsp const char *errstr;
3933 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3935 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3936 26ed57b2 2018-05-19 stsp switch (ch) {
3938 64453f7e 2020-11-21 stsp force_text_diff = 1;
3941 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3943 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3944 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
3945 8f666e67 2022-02-12 thomas errstr, errstr);
3947 09b5bff8 2020-02-23 naddy case 'r':
3948 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3949 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3950 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3952 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3955 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3958 17020d27 2019-03-07 stsp usage_diff();
3959 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3963 26ed57b2 2018-05-19 stsp argc -= optind;
3964 26ed57b2 2018-05-19 stsp argv += optind;
3966 26ed57b2 2018-05-19 stsp if (argc == 0) {
3967 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3968 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3969 15a94983 2018-12-23 stsp id_str1 = argv[0];
3970 15a94983 2018-12-23 stsp id_str2 = argv[1];
3972 26ed57b2 2018-05-19 stsp usage_diff();
3974 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3975 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3976 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3977 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3978 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3979 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3980 c156c7a4 2020-12-18 stsp goto done;
3981 a273ac94 2020-02-23 naddy if (worktree)
3982 a273ac94 2020-02-23 naddy repo_path =
3983 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3985 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3986 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3987 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3988 c156c7a4 2020-12-18 stsp goto done;
3992 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3993 eb6600df 2019-01-04 stsp if (error)
3994 eb6600df 2019-01-04 stsp goto done;
3996 a273ac94 2020-02-23 naddy init_curses();
3998 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3999 51a10b52 2020-12-26 stsp if (error)
4000 51a10b52 2020-12-26 stsp goto done;
4002 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4003 26ed57b2 2018-05-19 stsp if (error)
4004 26ed57b2 2018-05-19 stsp goto done;
4006 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4007 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4008 26ed57b2 2018-05-19 stsp if (error)
4009 26ed57b2 2018-05-19 stsp goto done;
4011 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4012 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4013 26ed57b2 2018-05-19 stsp if (error)
4014 26ed57b2 2018-05-19 stsp goto done;
4016 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4017 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4018 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4019 ea5e7bb5 2018-08-01 stsp goto done;
4021 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4022 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4023 5dc9f4bc 2018-08-04 stsp if (error)
4024 5dc9f4bc 2018-08-04 stsp goto done;
4025 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4027 3dbaef42 2020-11-24 stsp free(label1);
4028 3dbaef42 2020-11-24 stsp free(label2);
4029 c02c541e 2019-03-29 stsp free(repo_path);
4030 a273ac94 2020-02-23 naddy free(cwd);
4031 1d0f4054 2021-06-17 stsp if (repo) {
4032 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4033 1d0f4054 2021-06-17 stsp if (error == NULL)
4034 1d0f4054 2021-06-17 stsp error = close_err;
4036 a273ac94 2020-02-23 naddy if (worktree)
4037 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4038 51a10b52 2020-12-26 stsp tog_free_refs();
4039 26ed57b2 2018-05-19 stsp return error;
4042 4ed7e80c 2018-05-20 stsp __dead static void
4043 9f7d7167 2018-04-29 stsp usage_blame(void)
4046 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4047 9f7d7167 2018-04-29 stsp getprogname());
4051 84451b3e 2018-07-10 stsp struct tog_blame_line {
4052 84451b3e 2018-07-10 stsp int annotated;
4053 84451b3e 2018-07-10 stsp struct got_object_id *id;
4056 4ed7e80c 2018-05-20 stsp static const struct got_error *
4057 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4059 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4060 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4061 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4062 84451b3e 2018-07-10 stsp const struct got_error *err;
4063 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4064 826082fe 2020-12-10 stsp char *line = NULL;
4065 826082fe 2020-12-10 stsp size_t linesize = 0;
4066 826082fe 2020-12-10 stsp ssize_t linelen;
4067 84451b3e 2018-07-10 stsp wchar_t *wline;
4068 27a741e5 2019-09-11 stsp int width;
4069 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4070 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4071 ab089a2a 2018-07-12 stsp char *id_str;
4072 11b20872 2019-11-08 stsp struct tog_color *tc;
4074 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4076 ab089a2a 2018-07-12 stsp return err;
4078 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4079 f7d12f7e 2018-08-01 stsp werase(view->window);
4081 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4082 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4083 ab089a2a 2018-07-12 stsp free(id_str);
4084 ab089a2a 2018-07-12 stsp return err;
4087 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4088 ab089a2a 2018-07-12 stsp free(line);
4089 2550e4c3 2018-07-13 stsp line = NULL;
4091 1cae65b4 2019-09-22 stsp return err;
4092 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4093 a3404814 2018-09-02 stsp wstandout(view->window);
4094 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4096 11b20872 2019-11-08 stsp wattr_on(view->window,
4097 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4098 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4100 11b20872 2019-11-08 stsp wattr_off(view->window,
4101 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4102 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4103 a3404814 2018-09-02 stsp wstandend(view->window);
4104 2550e4c3 2018-07-13 stsp free(wline);
4105 2550e4c3 2018-07-13 stsp wline = NULL;
4106 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4107 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4109 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4110 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4111 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4112 ab089a2a 2018-07-12 stsp free(id_str);
4113 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4115 ab089a2a 2018-07-12 stsp free(id_str);
4116 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4117 3f60a8ef 2018-07-10 stsp free(line);
4118 2550e4c3 2018-07-13 stsp line = NULL;
4120 3f60a8ef 2018-07-10 stsp return err;
4121 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4122 2550e4c3 2018-07-13 stsp free(wline);
4123 2550e4c3 2018-07-13 stsp wline = NULL;
4124 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4125 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4127 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4128 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4129 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4130 826082fe 2020-12-10 stsp if (linelen == -1) {
4131 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4132 826082fe 2020-12-10 stsp s->eof = 1;
4135 84451b3e 2018-07-10 stsp free(line);
4136 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4138 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4141 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4142 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4144 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4145 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4146 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4147 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4148 27a741e5 2019-09-11 stsp !(view->focussed &&
4149 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4150 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4151 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4152 8d0fe45a 2019-08-12 stsp char *id_str;
4153 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4154 8d0fe45a 2019-08-12 stsp if (err) {
4155 8d0fe45a 2019-08-12 stsp free(line);
4156 8d0fe45a 2019-08-12 stsp return err;
4158 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4160 11b20872 2019-11-08 stsp wattr_on(view->window,
4161 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4162 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4164 11b20872 2019-11-08 stsp wattr_off(view->window,
4165 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4166 8d0fe45a 2019-08-12 stsp free(id_str);
4167 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4169 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4170 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4173 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4174 ee41ec32 2018-07-10 stsp prev_id = NULL;
4177 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4178 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4179 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4181 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4182 41605754 2020-11-12 stsp width = 9;
4183 41605754 2020-11-12 stsp wline = wcsdup(L"");
4184 41605754 2020-11-12 stsp if (wline == NULL) {
4185 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4186 41605754 2020-11-12 stsp free(line);
4187 41605754 2020-11-12 stsp return err;
4189 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4190 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4191 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4192 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4193 41605754 2020-11-12 stsp view->window, regmatch);
4194 41605754 2020-11-12 stsp if (err) {
4195 41605754 2020-11-12 stsp free(line);
4196 41605754 2020-11-12 stsp return err;
4198 41605754 2020-11-12 stsp width += 9;
4200 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4201 41605754 2020-11-12 stsp view->ncols - 9, 9);
4202 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4203 41605754 2020-11-12 stsp free(wline);
4204 41605754 2020-11-12 stsp wline = NULL;
4205 41605754 2020-11-12 stsp width += 9;
4208 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4209 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4210 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4211 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4213 826082fe 2020-12-10 stsp free(line);
4214 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4216 1a57306a 2018-09-02 stsp view_vborder(view);
4218 84451b3e 2018-07-10 stsp return NULL;
4221 84451b3e 2018-07-10 stsp static const struct got_error *
4222 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4223 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4225 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4226 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4227 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4228 1a76625f 2018-10-22 stsp int errcode;
4230 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4231 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4232 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4234 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4235 1a76625f 2018-10-22 stsp if (errcode)
4236 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4238 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4239 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4240 d68a0a7d 2018-07-10 stsp goto done;
4243 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4244 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4246 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4247 d68a0a7d 2018-07-10 stsp if (line->annotated)
4248 d68a0a7d 2018-07-10 stsp goto done;
4250 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4251 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4252 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4253 84451b3e 2018-07-10 stsp goto done;
4255 84451b3e 2018-07-10 stsp line->annotated = 1;
4257 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4258 1a76625f 2018-10-22 stsp if (errcode)
4259 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4260 84451b3e 2018-07-10 stsp return err;
4263 84451b3e 2018-07-10 stsp static void *
4264 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4266 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4267 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4268 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4269 1a76625f 2018-10-22 stsp int errcode;
4271 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4273 61266923 2020-01-14 stsp return (void *)err;
4275 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4276 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4277 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4278 fc06ba56 2019-08-22 stsp err = NULL;
4280 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4281 1a76625f 2018-10-22 stsp if (errcode)
4282 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4283 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4285 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4286 1d0f4054 2021-06-17 stsp if (err == NULL)
4287 1d0f4054 2021-06-17 stsp err = close_err;
4288 c9beca56 2018-07-22 stsp ta->repo = NULL;
4289 c9beca56 2018-07-22 stsp *ta->complete = 1;
4291 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4292 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4293 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4295 18430de3 2018-07-10 stsp return (void *)err;
4298 245d91c1 2018-07-12 stsp static struct got_object_id *
4299 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4300 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4302 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4304 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4305 8d0fe45a 2019-08-12 stsp return NULL;
4307 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4308 245d91c1 2018-07-12 stsp if (!line->annotated)
4309 245d91c1 2018-07-12 stsp return NULL;
4311 245d91c1 2018-07-12 stsp return line->id;
4314 b880a918 2018-07-10 stsp static const struct got_error *
4315 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4317 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4320 245d91c1 2018-07-12 stsp if (blame->thread) {
4321 1a76625f 2018-10-22 stsp int errcode;
4322 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4323 1a76625f 2018-10-22 stsp if (errcode)
4324 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4325 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4326 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4327 1a76625f 2018-10-22 stsp if (errcode)
4328 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4329 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4330 1a76625f 2018-10-22 stsp if (errcode)
4331 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4332 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4333 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4334 245d91c1 2018-07-12 stsp err = NULL;
4335 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4337 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4338 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4339 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4340 1d0f4054 2021-06-17 stsp if (err == NULL)
4341 1d0f4054 2021-06-17 stsp err = close_err;
4342 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4344 245d91c1 2018-07-12 stsp if (blame->f) {
4345 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4346 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4347 245d91c1 2018-07-12 stsp blame->f = NULL;
4349 57670559 2018-12-23 stsp if (blame->lines) {
4350 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4351 57670559 2018-12-23 stsp free(blame->lines[i].id);
4352 57670559 2018-12-23 stsp free(blame->lines);
4353 57670559 2018-12-23 stsp blame->lines = NULL;
4355 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4356 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4358 245d91c1 2018-07-12 stsp return err;
4361 245d91c1 2018-07-12 stsp static const struct got_error *
4362 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4364 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4365 fc06ba56 2019-08-22 stsp int *done = arg;
4366 fc06ba56 2019-08-22 stsp int errcode;
4368 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4369 fc06ba56 2019-08-22 stsp if (errcode)
4370 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4371 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4373 fc06ba56 2019-08-22 stsp if (*done)
4374 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4376 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4377 fc06ba56 2019-08-22 stsp if (errcode)
4378 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4379 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4381 fc06ba56 2019-08-22 stsp return err;
4384 fc06ba56 2019-08-22 stsp static const struct got_error *
4385 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4387 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4388 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4389 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4390 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4391 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4392 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4393 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4394 15a94983 2018-12-23 stsp int obj_type;
4396 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4397 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4399 15a94983 2018-12-23 stsp return err;
4401 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4402 945f9229 2022-04-16 thomas if (err)
4403 945f9229 2022-04-16 thomas goto done;
4405 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4407 84451b3e 2018-07-10 stsp goto done;
4409 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4410 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4411 84451b3e 2018-07-10 stsp goto done;
4414 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4416 a70480e0 2018-06-23 stsp goto done;
4417 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4418 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4419 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4420 84451b3e 2018-07-10 stsp goto done;
4422 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4423 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4425 1fddf795 2021-01-20 stsp goto done;
4426 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4427 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4428 84451b3e 2018-07-10 stsp goto done;
4431 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4432 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4433 b02560ec 2019-08-19 stsp blame->nlines--;
4435 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4436 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4437 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4438 84451b3e 2018-07-10 stsp goto done;
4441 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4443 bd24772e 2018-07-11 stsp goto done;
4445 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4446 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4447 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4448 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4449 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4450 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4451 245d91c1 2018-07-12 stsp goto done;
4453 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4455 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4456 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4457 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4458 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4459 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4460 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4461 a5388363 2020-12-01 naddy s->blame_complete = 0;
4463 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4464 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4465 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4466 f5a09613 2020-12-13 naddy s->selected_line = 1;
4468 aa61903a 2021-12-31 thomas s->matched_line = 0;
4471 945f9229 2022-04-16 thomas if (commit)
4472 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4474 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4475 27d434c2 2018-09-15 stsp free(obj_id);
4477 245d91c1 2018-07-12 stsp stop_blame(blame);
4478 245d91c1 2018-07-12 stsp return err;
4481 245d91c1 2018-07-12 stsp static const struct got_error *
4482 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4483 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4485 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4486 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4488 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4490 c4843652 2019-08-12 stsp s->path = strdup(path);
4491 c4843652 2019-08-12 stsp if (s->path == NULL)
4492 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4494 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4495 c4843652 2019-08-12 stsp if (err) {
4496 c4843652 2019-08-12 stsp free(s->path);
4497 7cbe629d 2018-08-04 stsp return err;
4500 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4501 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4502 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4503 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4504 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4505 fb2756b9 2018-08-04 stsp s->repo = repo;
4506 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4507 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4509 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4510 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4511 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4512 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4514 11b20872 2019-11-08 stsp return err;
4517 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4518 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4519 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4520 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4521 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4523 a5388363 2020-12-01 naddy return run_blame(view);
4526 e5a0f69f 2018-08-18 stsp static const struct got_error *
4527 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4529 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4530 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4532 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4533 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4535 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4536 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4537 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4538 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4539 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4542 e5a0f69f 2018-08-18 stsp free(s->path);
4543 11b20872 2019-11-08 stsp free_colors(&s->colors);
4545 e5a0f69f 2018-08-18 stsp return err;
4548 7cbe629d 2018-08-04 stsp static const struct got_error *
4549 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4551 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4553 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4554 6c4c42e0 2019-06-24 stsp return NULL;
4557 6c4c42e0 2019-06-24 stsp static const struct got_error *
4558 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4560 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4561 6c4c42e0 2019-06-24 stsp int lineno;
4562 826082fe 2020-12-10 stsp char *line = NULL;
4563 826082fe 2020-12-10 stsp size_t linesize = 0;
4564 826082fe 2020-12-10 stsp ssize_t linelen;
4566 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4567 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4568 6c4c42e0 2019-06-24 stsp return NULL;
4571 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4572 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4573 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4575 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4577 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4579 6c4c42e0 2019-06-24 stsp while (1) {
4580 6c4c42e0 2019-06-24 stsp off_t offset;
4582 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4583 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4584 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4588 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4589 6c4c42e0 2019-06-24 stsp lineno = 1;
4591 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4594 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4595 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4596 6c4c42e0 2019-06-24 stsp free(line);
4597 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4599 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4600 826082fe 2020-12-10 stsp if (linelen != -1 &&
4601 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4602 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4603 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4606 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4611 826082fe 2020-12-10 stsp free(line);
4613 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4614 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4615 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4618 6c4c42e0 2019-06-24 stsp return NULL;
4621 6c4c42e0 2019-06-24 stsp static const struct got_error *
4622 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4624 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4625 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4626 2b380cc8 2018-10-24 stsp int errcode;
4628 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4629 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4630 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4631 2b380cc8 2018-10-24 stsp if (errcode)
4632 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4634 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4637 51fe7530 2019-08-19 stsp if (s->blame_complete)
4638 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4640 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4642 669b5ffa 2018-10-07 stsp view_vborder(view);
4643 e5a0f69f 2018-08-18 stsp return err;
4646 e5a0f69f 2018-08-18 stsp static const struct got_error *
4647 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4649 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4650 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4651 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4652 70f17a53 2022-06-13 thomas int begin_x = 0, nscroll = view->nlines - 2;
4654 e5a0f69f 2018-08-18 stsp switch (ch) {
4656 1e37a5c2 2019-05-12 jcs s->done = 1;
4658 4deef56f 2021-09-02 naddy case 'g':
4659 4deef56f 2021-09-02 naddy case KEY_HOME:
4660 4deef56f 2021-09-02 naddy s->selected_line = 1;
4661 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4663 4deef56f 2021-09-02 naddy case 'G':
4664 4deef56f 2021-09-02 naddy case KEY_END:
4665 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4666 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4667 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4669 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4670 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4671 4deef56f 2021-09-02 naddy (view->nlines - 3);
4675 1e37a5c2 2019-05-12 jcs case KEY_UP:
4676 f7140bf5 2021-10-17 thomas case CTRL('p'):
4677 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4678 1e37a5c2 2019-05-12 jcs s->selected_line--;
4679 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4680 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4681 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4683 70f17a53 2022-06-13 thomas case CTRL('u'):
4684 70f17a53 2022-06-13 thomas nscroll /= 2;
4685 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4686 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4687 ea025d1d 2020-02-22 naddy case CTRL('b'):
4688 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4689 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
4692 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
4693 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
4695 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4698 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4699 f7140bf5 2021-10-17 thomas case CTRL('n'):
4700 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4701 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4702 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4703 1e37a5c2 2019-05-12 jcs s->selected_line++;
4704 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4705 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4706 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4709 1e37a5c2 2019-05-12 jcs case 'p': {
4710 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4711 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4712 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4713 1e37a5c2 2019-05-12 jcs if (id == NULL)
4715 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4716 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4717 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4718 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4719 1e37a5c2 2019-05-12 jcs int obj_type;
4720 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4721 1e37a5c2 2019-05-12 jcs s->repo, id);
4724 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4725 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4726 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4727 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4730 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4731 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4732 ec242592 2022-04-22 thomas s->repo, &pid->id);
4733 945f9229 2022-04-16 thomas if (err)
4735 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4736 945f9229 2022-04-16 thomas pcommit, s->path);
4737 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4738 e5a0f69f 2018-08-18 stsp if (err) {
4739 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4740 1e37a5c2 2019-05-12 jcs err = NULL;
4741 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4744 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4746 1e37a5c2 2019-05-12 jcs free(blob_id);
4747 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4748 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4749 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4752 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4753 ec242592 2022-04-22 thomas &pid->id);
4754 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4756 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4757 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
4759 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4764 1e37a5c2 2019-05-12 jcs s->done = 1;
4765 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4766 1e37a5c2 2019-05-12 jcs s->done = 0;
4767 1e37a5c2 2019-05-12 jcs if (thread_err)
4769 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4770 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4771 a5388363 2020-12-01 naddy err = run_blame(view);
4776 1e37a5c2 2019-05-12 jcs case 'B': {
4777 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4778 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4779 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
4781 1e37a5c2 2019-05-12 jcs s->done = 1;
4782 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4783 1e37a5c2 2019-05-12 jcs s->done = 0;
4784 1e37a5c2 2019-05-12 jcs if (thread_err)
4786 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4787 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4788 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4789 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4790 a5388363 2020-12-01 naddy err = run_blame(view);
4795 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4796 1e37a5c2 2019-05-12 jcs case '\r': {
4797 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4798 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4799 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4800 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4801 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4802 1e37a5c2 2019-05-12 jcs if (id == NULL)
4804 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4807 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4808 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4809 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4810 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4811 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4812 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4813 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4814 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4817 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4818 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4819 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4821 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4824 e78dc838 2020-12-04 stsp view->focussed = 0;
4825 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4826 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4827 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4830 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4831 e78dc838 2020-12-04 stsp view->focus_child = 1;
4833 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4838 70f17a53 2022-06-13 thomas case CTRL('d'):
4839 70f17a53 2022-06-13 thomas nscroll /= 2;
4840 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4841 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4842 ea025d1d 2020-02-22 naddy case CTRL('f'):
4844 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4845 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4846 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4849 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4850 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4851 70f17a53 2022-06-13 thomas s->selected_line +=
4852 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
4853 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
4855 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
4856 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
4858 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4859 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
4861 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4862 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4863 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4864 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4870 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4873 a70480e0 2018-06-23 stsp static const struct got_error *
4874 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4876 a70480e0 2018-06-23 stsp const struct got_error *error;
4877 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4878 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4879 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4880 0587e10c 2020-07-23 stsp char *link_target = NULL;
4881 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4882 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4883 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4885 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4887 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4888 a70480e0 2018-06-23 stsp switch (ch) {
4890 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4893 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4894 69069811 2018-08-02 stsp if (repo_path == NULL)
4895 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4899 17020d27 2019-03-07 stsp usage_blame();
4900 a70480e0 2018-06-23 stsp /* NOTREACHED */
4904 a70480e0 2018-06-23 stsp argc -= optind;
4905 a70480e0 2018-06-23 stsp argv += optind;
4907 f135c941 2020-02-20 stsp if (argc != 1)
4908 a70480e0 2018-06-23 stsp usage_blame();
4910 69069811 2018-08-02 stsp if (repo_path == NULL) {
4911 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4912 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4913 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4914 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4915 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4916 c156c7a4 2020-12-18 stsp goto done;
4917 f135c941 2020-02-20 stsp if (worktree)
4918 eb41ed75 2019-02-05 stsp repo_path =
4919 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4921 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4922 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4923 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4924 c156c7a4 2020-12-18 stsp goto done;
4928 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4929 c02c541e 2019-03-29 stsp if (error != NULL)
4930 8e94dd5b 2019-01-04 stsp goto done;
4932 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4933 f135c941 2020-02-20 stsp worktree);
4934 c02c541e 2019-03-29 stsp if (error)
4935 92205607 2019-01-04 stsp goto done;
4937 f135c941 2020-02-20 stsp init_curses();
4939 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4940 51a10b52 2020-12-26 stsp if (error)
4941 51a10b52 2020-12-26 stsp goto done;
4943 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4944 eb41ed75 2019-02-05 stsp if (error)
4945 69069811 2018-08-02 stsp goto done;
4947 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4948 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4949 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4950 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4951 a70480e0 2018-06-23 stsp if (error != NULL)
4952 66b4983c 2018-06-23 stsp goto done;
4953 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4954 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4956 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4957 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4959 a19e88aa 2018-06-23 stsp if (error != NULL)
4960 8b473291 2019-02-21 stsp goto done;
4962 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4963 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4964 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4965 e1cd8fed 2018-08-01 stsp goto done;
4968 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
4969 945f9229 2022-04-16 thomas if (error)
4970 945f9229 2022-04-16 thomas goto done;
4972 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4973 945f9229 2022-04-16 thomas commit, repo);
4974 7cbe629d 2018-08-04 stsp if (error)
4975 7cbe629d 2018-08-04 stsp goto done;
4977 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4978 78756c87 2020-11-24 stsp commit_id, repo);
4979 0587e10c 2020-07-23 stsp if (error)
4980 0587e10c 2020-07-23 stsp goto done;
4981 12314ad4 2019-08-31 stsp if (worktree) {
4982 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4983 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4984 12314ad4 2019-08-31 stsp worktree = NULL;
4986 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4988 69069811 2018-08-02 stsp free(repo_path);
4989 f135c941 2020-02-20 stsp free(in_repo_path);
4990 0587e10c 2020-07-23 stsp free(link_target);
4991 69069811 2018-08-02 stsp free(cwd);
4992 a70480e0 2018-06-23 stsp free(commit_id);
4993 945f9229 2022-04-16 thomas if (commit)
4994 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4995 eb41ed75 2019-02-05 stsp if (worktree)
4996 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4997 1d0f4054 2021-06-17 stsp if (repo) {
4998 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4999 1d0f4054 2021-06-17 stsp if (error == NULL)
5000 1d0f4054 2021-06-17 stsp error = close_err;
5002 51a10b52 2020-12-26 stsp tog_free_refs();
5003 a70480e0 2018-06-23 stsp return error;
5006 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5007 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5009 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5010 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5011 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5012 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5013 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5014 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5015 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5017 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5019 f7d12f7e 2018-08-01 stsp werase(view->window);
5021 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5022 ffd1d5e5 2018-06-23 stsp return NULL;
5024 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5026 ffd1d5e5 2018-06-23 stsp return err;
5027 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5028 a3404814 2018-09-02 stsp wstandout(view->window);
5029 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5031 11b20872 2019-11-08 stsp wattr_on(view->window,
5032 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5033 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5035 11b20872 2019-11-08 stsp wattr_off(view->window,
5036 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5037 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5038 a3404814 2018-09-02 stsp wstandend(view->window);
5039 2550e4c3 2018-07-13 stsp free(wline);
5040 2550e4c3 2018-07-13 stsp wline = NULL;
5041 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5042 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5043 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5044 ffd1d5e5 2018-06-23 stsp return NULL;
5045 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
5047 ce52c690 2018-06-23 stsp return err;
5048 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5049 2550e4c3 2018-07-13 stsp free(wline);
5050 2550e4c3 2018-07-13 stsp wline = NULL;
5051 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5052 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5053 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5054 ffd1d5e5 2018-06-23 stsp return NULL;
5055 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5056 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5057 a1eca9bb 2018-06-23 stsp return NULL;
5059 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5060 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5061 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5062 0cf4efb1 2018-09-29 stsp if (view->focussed)
5063 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5064 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5066 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5067 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5068 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5069 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5070 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5071 ffd1d5e5 2018-06-23 stsp return NULL;
5075 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5078 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5079 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5080 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5081 848d6979 2019-08-12 stsp const char *modestr = "";
5082 56e0773d 2019-11-28 stsp mode_t mode;
5084 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5085 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5087 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5088 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5089 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5091 638f9024 2019-05-13 stsp return got_error_from_errno(
5092 230a42bd 2019-05-11 jcs "got_object_id_str");
5094 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5095 63c5ca5d 2019-08-24 stsp modestr = "$";
5096 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5099 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5100 d86d3b18 2020-12-01 naddy te, s->repo);
5101 0d6c6ee3 2020-05-20 stsp if (err) {
5102 0d6c6ee3 2020-05-20 stsp free(id_str);
5103 0d6c6ee3 2020-05-20 stsp return err;
5105 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5106 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5107 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5109 848d6979 2019-08-12 stsp modestr = "@";
5111 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5112 848d6979 2019-08-12 stsp modestr = "/";
5113 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5114 848d6979 2019-08-12 stsp modestr = "*";
5115 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5116 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5117 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5118 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5119 1d13200f 2018-07-12 stsp free(id_str);
5120 0d6c6ee3 2020-05-20 stsp free(link_target);
5121 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5123 1d13200f 2018-07-12 stsp free(id_str);
5124 0d6c6ee3 2020-05-20 stsp free(link_target);
5125 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5126 ffd1d5e5 2018-06-23 stsp if (err) {
5127 ffd1d5e5 2018-06-23 stsp free(line);
5130 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5131 0cf4efb1 2018-09-29 stsp if (view->focussed)
5132 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5133 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5135 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5137 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5138 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5139 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5141 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5142 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5143 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5144 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5145 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5146 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5147 ffd1d5e5 2018-06-23 stsp free(line);
5148 2550e4c3 2018-07-13 stsp free(wline);
5149 2550e4c3 2018-07-13 stsp wline = NULL;
5151 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5152 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5153 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5157 ffd1d5e5 2018-06-23 stsp return err;
5160 ffd1d5e5 2018-06-23 stsp static void
5161 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5163 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5164 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5165 fa86c4bf 2020-11-29 stsp int i = 0;
5167 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5170 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5171 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5172 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5173 fa86c4bf 2020-11-29 stsp if (!isroot)
5174 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5177 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5178 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5182 fa86c4bf 2020-11-29 stsp static void
5183 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5185 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5186 ffd1d5e5 2018-06-23 stsp int n = 0;
5188 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5189 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5190 694d3271 2020-12-01 naddy s->first_displayed_entry);
5192 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5194 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5195 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5196 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5197 768394f3 2019-01-24 stsp if (last) {
5198 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5199 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5204 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5205 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5206 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5208 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5209 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5210 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5212 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5213 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5214 56e0773d 2019-11-28 stsp + 1 /* slash */;
5216 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5218 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5219 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5220 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5222 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5223 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5224 d9765a41 2018-06-23 stsp while (pt) {
5225 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5226 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5227 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5228 cb2ebc8a 2018-06-23 stsp goto done;
5230 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5231 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5232 cb2ebc8a 2018-06-23 stsp goto done;
5234 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5237 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5238 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5239 ce52c690 2018-06-23 stsp goto done;
5243 ce52c690 2018-06-23 stsp if (err) {
5244 ce52c690 2018-06-23 stsp free(*path);
5245 ce52c690 2018-06-23 stsp *path = NULL;
5247 ce52c690 2018-06-23 stsp return err;
5250 ce52c690 2018-06-23 stsp static const struct got_error *
5251 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5252 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5253 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5255 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5256 ce52c690 2018-06-23 stsp char *path;
5257 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5259 a0de39f3 2019-08-09 stsp *new_view = NULL;
5261 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5263 ce52c690 2018-06-23 stsp return err;
5265 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5266 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5267 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5268 83ce39e3 2019-08-12 stsp goto done;
5271 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5272 e5a0f69f 2018-08-18 stsp if (err) {
5273 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5274 fc06ba56 2019-08-22 stsp err = NULL;
5275 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5277 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5279 83ce39e3 2019-08-12 stsp free(path);
5280 69efd4c4 2018-07-18 stsp return err;
5283 69efd4c4 2018-07-18 stsp static const struct got_error *
5284 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5285 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5287 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5288 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5289 69efd4c4 2018-07-18 stsp char *path;
5291 a0de39f3 2019-08-09 stsp *new_view = NULL;
5293 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5294 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5295 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5297 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5299 69efd4c4 2018-07-18 stsp return err;
5301 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5304 e5a0f69f 2018-08-18 stsp view_close(log_view);
5306 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5307 cb2ebc8a 2018-06-23 stsp free(path);
5308 cb2ebc8a 2018-06-23 stsp return err;
5311 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5312 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5313 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5315 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5316 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5317 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5318 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5320 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5321 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5323 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5324 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5325 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5327 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5329 bc573f3b 2021-07-10 stsp goto done;
5332 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5333 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5334 bc573f3b 2021-07-10 stsp * closed on demand.
5336 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5337 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5339 bc573f3b 2021-07-10 stsp goto done;
5340 bc573f3b 2021-07-10 stsp s->tree = s->root;
5342 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5343 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5344 ffd1d5e5 2018-06-23 stsp goto done;
5346 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5347 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5348 ffd1d5e5 2018-06-23 stsp goto done;
5351 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5352 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5353 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5354 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5355 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5356 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5357 9cd7cbd1 2020-12-07 stsp goto done;
5360 fb2756b9 2018-08-04 stsp s->repo = repo;
5362 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5363 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5364 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5365 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5367 c0b01bdb 2019-11-08 stsp goto done;
5368 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5369 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5371 c0b01bdb 2019-11-08 stsp goto done;
5372 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5373 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5374 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5376 c0b01bdb 2019-11-08 stsp goto done;
5378 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5379 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5380 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5382 11b20872 2019-11-08 stsp goto done;
5384 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5385 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5387 c0b01bdb 2019-11-08 stsp goto done;
5390 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5391 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5392 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5393 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5394 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5396 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5397 bc573f3b 2021-07-10 stsp if (commit)
5398 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5400 bc573f3b 2021-07-10 stsp close_tree_view(view);
5401 ad80ab7b 2018-08-04 stsp return err;
5404 e5a0f69f 2018-08-18 stsp static const struct got_error *
5405 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5407 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5409 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5410 fb2756b9 2018-08-04 stsp free(s->tree_label);
5411 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5412 6484ec90 2018-09-29 stsp free(s->commit_id);
5413 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5414 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5415 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5416 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5417 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5418 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5419 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5420 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5421 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5422 ad80ab7b 2018-08-04 stsp free(parent);
5425 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5426 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5427 bc573f3b 2021-07-10 stsp if (s->root)
5428 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5429 7c32bd05 2019-06-22 stsp return NULL;
5432 7c32bd05 2019-06-22 stsp static const struct got_error *
5433 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5435 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5437 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5438 7c32bd05 2019-06-22 stsp return NULL;
5441 7c32bd05 2019-06-22 stsp static int
5442 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5444 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5446 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5450 7c32bd05 2019-06-22 stsp static const struct got_error *
5451 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5453 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5454 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5456 7c32bd05 2019-06-22 stsp if (!view->searching) {
5457 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5458 7c32bd05 2019-06-22 stsp return NULL;
5461 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5462 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5463 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5464 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5465 56e0773d 2019-11-28 stsp s->selected_entry);
5467 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5469 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5470 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5472 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5473 56e0773d 2019-11-28 stsp s->selected_entry);
5476 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5477 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5478 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5479 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5481 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5484 7c32bd05 2019-06-22 stsp while (1) {
5485 56e0773d 2019-11-28 stsp if (te == NULL) {
5486 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5487 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5488 ac66afb8 2019-06-24 stsp return NULL;
5490 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5491 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5493 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5496 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5497 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5498 56e0773d 2019-11-28 stsp s->matched_entry = te;
5502 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5503 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5505 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5508 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5509 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5510 7c32bd05 2019-06-22 stsp s->selected = 0;
5513 e5a0f69f 2018-08-18 stsp return NULL;
5516 ad80ab7b 2018-08-04 stsp static const struct got_error *
5517 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5519 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5520 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5521 e5a0f69f 2018-08-18 stsp char *parent_path;
5523 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5525 e5a0f69f 2018-08-18 stsp return err;
5527 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5528 e5a0f69f 2018-08-18 stsp free(parent_path);
5530 669b5ffa 2018-10-07 stsp view_vborder(view);
5531 e5a0f69f 2018-08-18 stsp return err;
5534 e5a0f69f 2018-08-18 stsp static const struct got_error *
5535 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5537 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5538 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5539 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5540 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5541 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 3;
5543 e5a0f69f 2018-08-18 stsp switch (ch) {
5545 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5548 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5550 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5551 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5552 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5553 e78dc838 2020-12-04 stsp view->focussed = 0;
5554 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5555 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5556 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5558 1e37a5c2 2019-05-12 jcs return err;
5559 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5560 e78dc838 2020-12-04 stsp view->focus_child = 1;
5562 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5565 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5566 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5567 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5568 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5569 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5570 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5571 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5572 152c1c93 2020-11-29 stsp if (err) {
5573 152c1c93 2020-11-29 stsp view_close(ref_view);
5574 152c1c93 2020-11-29 stsp return err;
5576 e78dc838 2020-12-04 stsp view->focussed = 0;
5577 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5578 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5579 152c1c93 2020-11-29 stsp err = view_close_child(view);
5581 152c1c93 2020-11-29 stsp return err;
5582 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5583 e78dc838 2020-12-04 stsp view->focus_child = 1;
5585 152c1c93 2020-11-29 stsp *new_view = ref_view;
5587 e4526bf5 2021-09-03 naddy case 'g':
5588 e4526bf5 2021-09-03 naddy case KEY_HOME:
5589 e4526bf5 2021-09-03 naddy s->selected = 0;
5590 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5591 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5592 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5594 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5596 e4526bf5 2021-09-03 naddy case 'G':
5597 e4526bf5 2021-09-03 naddy case KEY_END:
5598 e4526bf5 2021-09-03 naddy s->selected = 0;
5599 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5600 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5601 e4526bf5 2021-09-03 naddy if (te == NULL) {
5602 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5603 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5608 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5609 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5611 e4526bf5 2021-09-03 naddy if (n > 0)
5612 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5615 1e37a5c2 2019-05-12 jcs case KEY_UP:
5616 f7140bf5 2021-10-17 thomas case CTRL('p'):
5617 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5618 1e37a5c2 2019-05-12 jcs s->selected--;
5621 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5623 70f17a53 2022-06-13 thomas case CTRL('u'):
5624 70f17a53 2022-06-13 thomas nscroll /= 2;
5625 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5626 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5627 ea025d1d 2020-02-22 naddy case CTRL('b'):
5628 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5629 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5630 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5631 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5633 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5634 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5636 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
5639 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5640 f7140bf5 2021-10-17 thomas case CTRL('n'):
5641 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5642 1e37a5c2 2019-05-12 jcs s->selected++;
5645 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5647 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5649 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5651 70f17a53 2022-06-13 thomas case CTRL('d'):
5652 70f17a53 2022-06-13 thomas nscroll /= 2;
5653 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5654 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5655 ea025d1d 2020-02-22 naddy case CTRL('f'):
5656 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5658 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5659 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5660 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
5661 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
5664 70f17a53 2022-06-13 thomas tree_scroll_down(s, nscroll);
5666 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5668 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5669 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5670 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5671 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5672 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5674 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5675 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5677 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5678 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5679 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5680 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5681 1e37a5c2 2019-05-12 jcs s->selected_entry =
5682 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5683 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5684 1e37a5c2 2019-05-12 jcs free(parent);
5685 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5686 56e0773d 2019-11-28 stsp s->selected_entry))) {
5687 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5688 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5689 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5692 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5693 941e9f74 2019-05-21 stsp if (err) {
5694 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5697 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5698 56e0773d 2019-11-28 stsp s->selected_entry))) {
5699 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5700 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5701 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5703 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5704 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5705 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5708 e78dc838 2020-12-04 stsp view->focussed = 0;
5709 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5710 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5711 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5713 669b5ffa 2018-10-07 stsp return err;
5714 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5715 e78dc838 2020-12-04 stsp view->focus_child = 1;
5717 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5720 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5721 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5722 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5728 ffd1d5e5 2018-06-23 stsp return err;
5731 ffd1d5e5 2018-06-23 stsp __dead static void
5732 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5735 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5736 ffd1d5e5 2018-06-23 stsp getprogname());
5740 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5741 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5743 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5744 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5745 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5746 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5747 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5748 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5749 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5750 4e97c21c 2020-12-06 stsp char *label = NULL;
5751 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5752 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5754 5221c383 2018-08-01 stsp struct tog_view *view;
5756 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5757 ffd1d5e5 2018-06-23 stsp switch (ch) {
5759 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5762 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5763 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5764 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5768 e99e2d15 2020-11-24 naddy usage_tree();
5769 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5773 ffd1d5e5 2018-06-23 stsp argc -= optind;
5774 ffd1d5e5 2018-06-23 stsp argv += optind;
5776 55cccc34 2020-02-20 stsp if (argc > 1)
5777 e99e2d15 2020-11-24 naddy usage_tree();
5779 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5780 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5781 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5782 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5783 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5784 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5785 c156c7a4 2020-12-18 stsp goto done;
5786 55cccc34 2020-02-20 stsp if (worktree)
5787 52185f70 2019-02-05 stsp repo_path =
5788 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5790 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5791 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5792 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5793 c156c7a4 2020-12-18 stsp goto done;
5797 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5798 c02c541e 2019-03-29 stsp if (error != NULL)
5799 52185f70 2019-02-05 stsp goto done;
5801 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5802 55cccc34 2020-02-20 stsp repo, worktree);
5803 55cccc34 2020-02-20 stsp if (error)
5804 55cccc34 2020-02-20 stsp goto done;
5806 55cccc34 2020-02-20 stsp init_curses();
5808 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5809 c02c541e 2019-03-29 stsp if (error)
5810 52185f70 2019-02-05 stsp goto done;
5812 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5813 51a10b52 2020-12-26 stsp if (error)
5814 51a10b52 2020-12-26 stsp goto done;
5816 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5817 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5818 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5819 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5820 4e97c21c 2020-12-06 stsp if (error)
5821 4e97c21c 2020-12-06 stsp goto done;
5822 4e97c21c 2020-12-06 stsp head_ref_name = label;
5824 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5825 4e97c21c 2020-12-06 stsp if (error == NULL)
5826 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5827 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5828 4e97c21c 2020-12-06 stsp goto done;
5829 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5830 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5831 4e97c21c 2020-12-06 stsp if (error)
5832 4e97c21c 2020-12-06 stsp goto done;
5835 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5836 945f9229 2022-04-16 thomas if (error)
5837 945f9229 2022-04-16 thomas goto done;
5839 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5840 5221c383 2018-08-01 stsp if (view == NULL) {
5841 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5842 5221c383 2018-08-01 stsp goto done;
5844 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5845 ad80ab7b 2018-08-04 stsp if (error)
5846 ad80ab7b 2018-08-04 stsp goto done;
5847 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5848 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
5849 d91faf3b 2020-12-01 naddy in_repo_path);
5850 55cccc34 2020-02-20 stsp if (error)
5851 55cccc34 2020-02-20 stsp goto done;
5854 55cccc34 2020-02-20 stsp if (worktree) {
5855 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5856 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5857 55cccc34 2020-02-20 stsp worktree = NULL;
5859 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5861 52185f70 2019-02-05 stsp free(repo_path);
5862 e4a0e26d 2020-02-20 stsp free(cwd);
5863 ffd1d5e5 2018-06-23 stsp free(commit_id);
5864 4e97c21c 2020-12-06 stsp free(label);
5866 486cd271 2020-12-06 stsp got_ref_close(ref);
5867 1d0f4054 2021-06-17 stsp if (repo) {
5868 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5869 1d0f4054 2021-06-17 stsp if (error == NULL)
5870 1d0f4054 2021-06-17 stsp error = close_err;
5872 51a10b52 2020-12-26 stsp tog_free_refs();
5873 ffd1d5e5 2018-06-23 stsp return error;
5876 6458efa5 2020-11-24 stsp static const struct got_error *
5877 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5879 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5880 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5882 6458efa5 2020-11-24 stsp s->nrefs = 0;
5883 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5884 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
5885 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
5886 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
5887 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
5890 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5891 6458efa5 2020-11-24 stsp if (re == NULL)
5892 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5894 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5895 8924d611 2020-12-26 stsp if (re->ref == NULL)
5896 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5897 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5898 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5901 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5902 6458efa5 2020-11-24 stsp return NULL;
5906 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5908 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5910 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5911 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5912 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5913 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5918 6458efa5 2020-11-24 stsp static const struct got_error *
5919 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5921 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5922 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5924 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5925 6458efa5 2020-11-24 stsp s->repo = repo;
5927 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5928 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5930 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5932 6458efa5 2020-11-24 stsp return err;
5934 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5935 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5936 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5937 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5939 6458efa5 2020-11-24 stsp goto done;
5941 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5942 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5943 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5945 6458efa5 2020-11-24 stsp goto done;
5947 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5948 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5949 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5950 2183bbf6 2022-01-23 thomas if (err)
5951 2183bbf6 2022-01-23 thomas goto done;
5953 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
5954 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
5955 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
5957 6458efa5 2020-11-24 stsp goto done;
5960 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5961 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5962 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5963 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5964 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5967 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5968 6458efa5 2020-11-24 stsp return err;
5971 6458efa5 2020-11-24 stsp static const struct got_error *
5972 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5974 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5976 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5977 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5979 6458efa5 2020-11-24 stsp return NULL;
5982 6458efa5 2020-11-24 stsp static const struct got_error *
5983 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5984 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5986 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5987 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5988 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5989 6458efa5 2020-11-24 stsp int obj_type;
5991 c42c9805 2020-11-24 stsp *commit_id = NULL;
5993 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5995 6458efa5 2020-11-24 stsp return err;
5997 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5999 6458efa5 2020-11-24 stsp goto done;
6001 6458efa5 2020-11-24 stsp switch (obj_type) {
6002 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6003 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6005 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6006 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6008 6458efa5 2020-11-24 stsp goto done;
6009 c42c9805 2020-11-24 stsp free(obj_id);
6010 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6011 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6013 6458efa5 2020-11-24 stsp goto done;
6014 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6015 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6016 c42c9805 2020-11-24 stsp goto done;
6018 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6019 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6020 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6021 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6022 c42c9805 2020-11-24 stsp goto done;
6026 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6032 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6033 c42c9805 2020-11-24 stsp if (err) {
6034 c42c9805 2020-11-24 stsp free(*commit_id);
6035 c42c9805 2020-11-24 stsp *commit_id = NULL;
6037 c42c9805 2020-11-24 stsp return err;
6040 c42c9805 2020-11-24 stsp static const struct got_error *
6041 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6042 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6044 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6045 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6046 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6048 c42c9805 2020-11-24 stsp *new_view = NULL;
6050 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6051 c42c9805 2020-11-24 stsp if (err) {
6052 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6053 c42c9805 2020-11-24 stsp return err;
6055 c42c9805 2020-11-24 stsp return NULL;
6058 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6059 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6060 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6061 6458efa5 2020-11-24 stsp goto done;
6064 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6065 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6068 6458efa5 2020-11-24 stsp view_close(log_view);
6070 6458efa5 2020-11-24 stsp *new_view = log_view;
6071 c42c9805 2020-11-24 stsp free(commit_id);
6072 6458efa5 2020-11-24 stsp return err;
6075 ce5b7c56 2019-07-09 stsp static void
6076 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6078 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6079 34ba6917 2020-11-30 stsp int i = 0;
6081 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6084 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6085 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6086 34ba6917 2020-11-30 stsp if (re == NULL)
6088 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6089 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6093 34ba6917 2020-11-30 stsp static void
6094 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6096 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6097 6458efa5 2020-11-24 stsp int n = 0;
6099 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6100 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6102 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6104 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6105 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6106 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6107 6458efa5 2020-11-24 stsp if (last) {
6108 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6109 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6114 6458efa5 2020-11-24 stsp static const struct got_error *
6115 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6117 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6119 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6120 6458efa5 2020-11-24 stsp return NULL;
6123 6458efa5 2020-11-24 stsp static int
6124 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6126 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6128 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6132 6458efa5 2020-11-24 stsp static const struct got_error *
6133 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6135 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6136 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6138 6458efa5 2020-11-24 stsp if (!view->searching) {
6139 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6140 6458efa5 2020-11-24 stsp return NULL;
6143 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6144 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6145 6458efa5 2020-11-24 stsp if (s->selected_entry)
6146 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6148 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6149 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6151 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6152 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6154 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6155 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6158 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6159 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6160 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6161 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6163 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6166 6458efa5 2020-11-24 stsp while (1) {
6167 6458efa5 2020-11-24 stsp if (re == NULL) {
6168 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6169 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6170 6458efa5 2020-11-24 stsp return NULL;
6172 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6173 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6175 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6178 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6179 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6180 6458efa5 2020-11-24 stsp s->matched_entry = re;
6184 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6185 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6187 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6190 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6191 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6192 6458efa5 2020-11-24 stsp s->selected = 0;
6195 6458efa5 2020-11-24 stsp return NULL;
6198 6458efa5 2020-11-24 stsp static const struct got_error *
6199 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6201 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6202 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6203 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6204 6458efa5 2020-11-24 stsp char *line = NULL;
6205 6458efa5 2020-11-24 stsp wchar_t *wline;
6206 6458efa5 2020-11-24 stsp struct tog_color *tc;
6207 6458efa5 2020-11-24 stsp int width, n;
6208 6458efa5 2020-11-24 stsp int limit = view->nlines;
6210 6458efa5 2020-11-24 stsp werase(view->window);
6212 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6214 6458efa5 2020-11-24 stsp if (limit == 0)
6215 6458efa5 2020-11-24 stsp return NULL;
6217 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6219 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6220 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6221 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6223 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6224 6458efa5 2020-11-24 stsp if (err) {
6225 6458efa5 2020-11-24 stsp free(line);
6226 6458efa5 2020-11-24 stsp return err;
6228 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6229 6458efa5 2020-11-24 stsp wstandout(view->window);
6230 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6231 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6232 6458efa5 2020-11-24 stsp wstandend(view->window);
6233 6458efa5 2020-11-24 stsp free(wline);
6234 6458efa5 2020-11-24 stsp wline = NULL;
6235 6458efa5 2020-11-24 stsp free(line);
6236 6458efa5 2020-11-24 stsp line = NULL;
6237 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6238 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6239 6458efa5 2020-11-24 stsp if (--limit <= 0)
6240 6458efa5 2020-11-24 stsp return NULL;
6243 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6244 6458efa5 2020-11-24 stsp char *line = NULL;
6246 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6247 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6248 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6249 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6250 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6251 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6252 6458efa5 2020-11-24 stsp struct got_object_id *id;
6253 6458efa5 2020-11-24 stsp char *id_str;
6254 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6256 6458efa5 2020-11-24 stsp return err;
6257 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6258 6458efa5 2020-11-24 stsp if (err) {
6260 6458efa5 2020-11-24 stsp return err;
6262 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6263 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6264 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6266 6458efa5 2020-11-24 stsp free(id_str);
6267 6458efa5 2020-11-24 stsp return err;
6270 6458efa5 2020-11-24 stsp free(id_str);
6272 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6273 6458efa5 2020-11-24 stsp if (line == NULL)
6274 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6277 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6278 6458efa5 2020-11-24 stsp if (err) {
6279 6458efa5 2020-11-24 stsp free(line);
6280 6458efa5 2020-11-24 stsp return err;
6282 6458efa5 2020-11-24 stsp if (n == s->selected) {
6283 6458efa5 2020-11-24 stsp if (view->focussed)
6284 6458efa5 2020-11-24 stsp wstandout(view->window);
6285 6458efa5 2020-11-24 stsp s->selected_entry = re;
6287 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6289 6458efa5 2020-11-24 stsp wattr_on(view->window,
6290 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6291 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6293 6458efa5 2020-11-24 stsp wattr_off(view->window,
6294 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6295 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6296 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6297 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6298 6458efa5 2020-11-24 stsp wstandend(view->window);
6299 6458efa5 2020-11-24 stsp free(line);
6300 6458efa5 2020-11-24 stsp free(wline);
6301 6458efa5 2020-11-24 stsp wline = NULL;
6303 6458efa5 2020-11-24 stsp s->ndisplayed++;
6304 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6307 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6310 6458efa5 2020-11-24 stsp view_vborder(view);
6311 6458efa5 2020-11-24 stsp return err;
6314 6458efa5 2020-11-24 stsp static const struct got_error *
6315 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6316 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6318 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6319 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6320 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6322 c42c9805 2020-11-24 stsp *new_view = NULL;
6324 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6325 c42c9805 2020-11-24 stsp if (err) {
6326 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6327 c42c9805 2020-11-24 stsp return err;
6329 c42c9805 2020-11-24 stsp return NULL;
6333 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6334 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6335 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6336 c42c9805 2020-11-24 stsp goto done;
6339 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6340 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6342 c42c9805 2020-11-24 stsp goto done;
6344 c42c9805 2020-11-24 stsp *new_view = tree_view;
6346 c42c9805 2020-11-24 stsp free(commit_id);
6347 c42c9805 2020-11-24 stsp return err;
6349 c42c9805 2020-11-24 stsp static const struct got_error *
6350 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6352 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6353 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6354 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6355 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6356 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
6358 6458efa5 2020-11-24 stsp switch (ch) {
6360 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6362 98182bd0 2021-11-20 thomas case 'o':
6363 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6364 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6365 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6366 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6367 c591440f 2021-11-20 thomas if (err)
6369 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6370 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6371 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6372 3bfadbd4 2021-11-20 thomas if (err)
6374 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6375 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6377 6458efa5 2020-11-24 stsp case KEY_ENTER:
6378 6458efa5 2020-11-24 stsp case '\r':
6379 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6381 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6382 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6383 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6385 e78dc838 2020-12-04 stsp view->focussed = 0;
6386 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6387 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6388 6458efa5 2020-11-24 stsp err = view_close_child(view);
6390 6458efa5 2020-11-24 stsp return err;
6391 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6392 e78dc838 2020-12-04 stsp view->focus_child = 1;
6394 6458efa5 2020-11-24 stsp *new_view = log_view;
6397 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6399 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6400 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6401 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6403 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6405 e78dc838 2020-12-04 stsp view->focussed = 0;
6406 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6407 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6408 c42c9805 2020-11-24 stsp err = view_close_child(view);
6410 c42c9805 2020-11-24 stsp return err;
6411 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6412 e78dc838 2020-12-04 stsp view->focus_child = 1;
6414 c42c9805 2020-11-24 stsp *new_view = tree_view;
6416 e4526bf5 2021-09-03 naddy case 'g':
6417 e4526bf5 2021-09-03 naddy case KEY_HOME:
6418 e4526bf5 2021-09-03 naddy s->selected = 0;
6419 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6421 e4526bf5 2021-09-03 naddy case 'G':
6422 e4526bf5 2021-09-03 naddy case KEY_END:
6423 e4526bf5 2021-09-03 naddy s->selected = 0;
6424 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6425 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6426 e4526bf5 2021-09-03 naddy if (re == NULL)
6428 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6429 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6431 e4526bf5 2021-09-03 naddy if (n > 0)
6432 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6435 6458efa5 2020-11-24 stsp case KEY_UP:
6436 f7140bf5 2021-10-17 thomas case CTRL('p'):
6437 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6438 6458efa5 2020-11-24 stsp s->selected--;
6441 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6443 70f17a53 2022-06-13 thomas case CTRL('u'):
6444 70f17a53 2022-06-13 thomas nscroll /= 2;
6445 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6446 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6447 6458efa5 2020-11-24 stsp case CTRL('b'):
6448 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6449 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
6450 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
6453 6458efa5 2020-11-24 stsp case KEY_DOWN:
6454 f7140bf5 2021-10-17 thomas case CTRL('n'):
6455 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6456 6458efa5 2020-11-24 stsp s->selected++;
6459 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6460 6458efa5 2020-11-24 stsp /* can't scroll any further */
6462 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6464 70f17a53 2022-06-13 thomas case CTRL('d'):
6465 70f17a53 2022-06-13 thomas nscroll /= 2;
6466 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6467 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6468 6458efa5 2020-11-24 stsp case CTRL('f'):
6469 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6470 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6471 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6472 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6473 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6476 70f17a53 2022-06-13 thomas ref_scroll_down(s, nscroll);
6478 6458efa5 2020-11-24 stsp case CTRL('l'):
6479 8924d611 2020-12-26 stsp tog_free_refs();
6480 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6483 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6484 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6486 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6487 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6488 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6494 6458efa5 2020-11-24 stsp return err;
6497 6458efa5 2020-11-24 stsp __dead static void
6498 6458efa5 2020-11-24 stsp usage_ref(void)
6501 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6502 6458efa5 2020-11-24 stsp getprogname());
6506 6458efa5 2020-11-24 stsp static const struct got_error *
6507 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6509 6458efa5 2020-11-24 stsp const struct got_error *error;
6510 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6511 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6512 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6514 6458efa5 2020-11-24 stsp struct tog_view *view;
6516 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6517 6458efa5 2020-11-24 stsp switch (ch) {
6519 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6520 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6521 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6525 e99e2d15 2020-11-24 naddy usage_ref();
6526 6458efa5 2020-11-24 stsp /* NOTREACHED */
6530 6458efa5 2020-11-24 stsp argc -= optind;
6531 6458efa5 2020-11-24 stsp argv += optind;
6533 6458efa5 2020-11-24 stsp if (argc > 1)
6534 e99e2d15 2020-11-24 naddy usage_ref();
6536 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6537 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6538 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6539 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6540 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6541 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6542 c156c7a4 2020-12-18 stsp goto done;
6543 6458efa5 2020-11-24 stsp if (worktree)
6544 6458efa5 2020-11-24 stsp repo_path =
6545 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6547 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6548 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6549 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6550 c156c7a4 2020-12-18 stsp goto done;
6554 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6555 6458efa5 2020-11-24 stsp if (error != NULL)
6556 6458efa5 2020-11-24 stsp goto done;
6558 6458efa5 2020-11-24 stsp init_curses();
6560 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6561 51a10b52 2020-12-26 stsp if (error)
6562 51a10b52 2020-12-26 stsp goto done;
6564 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6565 6458efa5 2020-11-24 stsp if (error)
6566 6458efa5 2020-11-24 stsp goto done;
6568 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6569 6458efa5 2020-11-24 stsp if (view == NULL) {
6570 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6571 6458efa5 2020-11-24 stsp goto done;
6574 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6575 6458efa5 2020-11-24 stsp if (error)
6576 6458efa5 2020-11-24 stsp goto done;
6578 6458efa5 2020-11-24 stsp if (worktree) {
6579 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6580 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6581 6458efa5 2020-11-24 stsp worktree = NULL;
6583 6458efa5 2020-11-24 stsp error = view_loop(view);
6585 6458efa5 2020-11-24 stsp free(repo_path);
6586 6458efa5 2020-11-24 stsp free(cwd);
6587 1d0f4054 2021-06-17 stsp if (repo) {
6588 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6589 1d0f4054 2021-06-17 stsp if (close_err)
6590 1d0f4054 2021-06-17 stsp error = close_err;
6592 51a10b52 2020-12-26 stsp tog_free_refs();
6593 6458efa5 2020-11-24 stsp return error;
6596 6458efa5 2020-11-24 stsp static void
6597 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6601 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6602 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6603 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6604 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6606 6879ba42 2020-10-01 naddy fputc('\n', fp);
6609 4ed7e80c 2018-05-20 stsp __dead static void
6610 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6612 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6614 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6615 6879ba42 2020-10-01 naddy getprogname());
6616 6879ba42 2020-10-01 naddy if (hflag) {
6617 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6618 6879ba42 2020-10-01 naddy list_commands(fp);
6620 6879ba42 2020-10-01 naddy exit(status);
6623 c2301be8 2018-04-30 stsp static char **
6624 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6626 ee85c5e8 2020-02-29 stsp va_list ap;
6627 c2301be8 2018-04-30 stsp char **argv;
6630 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6632 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6633 c2301be8 2018-04-30 stsp if (argv == NULL)
6634 c2301be8 2018-04-30 stsp err(1, "calloc");
6635 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6636 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6637 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6638 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6641 ee85c5e8 2020-02-29 stsp va_end(ap);
6642 c2301be8 2018-04-30 stsp return argv;
6646 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6647 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6648 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6649 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6650 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6652 ee85c5e8 2020-02-29 stsp static const struct got_error *
6653 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6655 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6656 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6657 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6658 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6659 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6660 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6661 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6662 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6664 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6665 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6666 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6668 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6669 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6670 ee85c5e8 2020-02-29 stsp goto done;
6672 ee85c5e8 2020-02-29 stsp if (worktree)
6673 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6675 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6676 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6677 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6678 ee85c5e8 2020-02-29 stsp goto done;
6681 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6682 ee85c5e8 2020-02-29 stsp if (error != NULL)
6683 ee85c5e8 2020-02-29 stsp goto done;
6685 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6686 ee85c5e8 2020-02-29 stsp repo, worktree);
6687 ee85c5e8 2020-02-29 stsp if (error)
6688 ee85c5e8 2020-02-29 stsp goto done;
6690 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6691 84de9106 2020-12-26 stsp if (error)
6692 84de9106 2020-12-26 stsp goto done;
6693 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6694 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6695 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6696 ee85c5e8 2020-02-29 stsp if (error)
6697 ee85c5e8 2020-02-29 stsp goto done;
6699 ee85c5e8 2020-02-29 stsp if (worktree) {
6700 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6701 ee85c5e8 2020-02-29 stsp worktree = NULL;
6704 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6705 945f9229 2022-04-16 thomas if (error)
6706 945f9229 2022-04-16 thomas goto done;
6708 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6709 ee85c5e8 2020-02-29 stsp if (error) {
6710 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6711 ee85c5e8 2020-02-29 stsp goto done;
6712 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6713 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6714 6879ba42 2020-10-01 naddy usage(1, 1);
6715 ee85c5e8 2020-02-29 stsp /* not reached */
6718 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6719 1d0f4054 2021-06-17 stsp if (error == NULL)
6720 1d0f4054 2021-06-17 stsp error = close_err;
6721 ee85c5e8 2020-02-29 stsp repo = NULL;
6723 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6724 ee85c5e8 2020-02-29 stsp if (error)
6725 ee85c5e8 2020-02-29 stsp goto done;
6727 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6729 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6730 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6732 1d0f4054 2021-06-17 stsp if (repo) {
6733 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6734 1d0f4054 2021-06-17 stsp if (error == NULL)
6735 1d0f4054 2021-06-17 stsp error = close_err;
6737 945f9229 2022-04-16 thomas if (commit)
6738 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6739 ee85c5e8 2020-02-29 stsp if (worktree)
6740 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6742 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6743 ee85c5e8 2020-02-29 stsp free(commit_id);
6744 ee85c5e8 2020-02-29 stsp free(cwd);
6745 ee85c5e8 2020-02-29 stsp free(repo_path);
6746 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6747 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6749 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6750 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6751 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6753 87670572 2020-12-26 naddy tog_free_refs();
6754 ee85c5e8 2020-02-29 stsp return error;
6758 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6760 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6761 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6762 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6763 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6764 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
6765 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6766 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6769 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6771 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6772 9f7d7167 2018-04-29 stsp switch (ch) {
6774 9f7d7167 2018-04-29 stsp hflag = 1;
6777 53ccebc2 2019-07-30 stsp Vflag = 1;
6780 6879ba42 2020-10-01 naddy usage(hflag, 1);
6781 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6785 9f7d7167 2018-04-29 stsp argc -= optind;
6786 9f7d7167 2018-04-29 stsp argv += optind;
6787 9814e6a3 2020-09-27 naddy optind = 1;
6788 c2301be8 2018-04-30 stsp optreset = 1;
6790 53ccebc2 2019-07-30 stsp if (Vflag) {
6791 53ccebc2 2019-07-30 stsp got_version_print_str();
6792 6879ba42 2020-10-01 naddy return 0;
6795 4010e238 2020-12-04 stsp #ifndef PROFILE
6796 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6797 4010e238 2020-12-04 stsp NULL) == -1)
6798 4010e238 2020-12-04 stsp err(1, "pledge");
6801 c2301be8 2018-04-30 stsp if (argc == 0) {
6802 f29d3e89 2018-06-23 stsp if (hflag)
6803 6879ba42 2020-10-01 naddy usage(hflag, 0);
6804 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6805 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6807 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6811 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6812 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6813 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6814 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6815 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6821 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6822 ee85c5e8 2020-02-29 stsp if (argc != 1)
6823 6879ba42 2020-10-01 naddy usage(0, 1);
6824 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6825 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6827 ee85c5e8 2020-02-29 stsp if (hflag)
6828 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6830 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6834 b46c1e04 2020-09-20 naddy putchar('\n');
6835 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6837 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6838 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6839 a2f4a359 2020-02-28 stsp free(cmd_argv);
6842 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6843 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);