2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
108 9b058f45 2022-06-30 mark enum tog_view_mode {
109 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
110 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
111 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
114 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
118 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
119 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
120 ba4f502b 2018-08-04 stsp struct got_object_id *id;
121 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
124 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 ba4f502b 2018-08-04 stsp struct commit_queue {
126 ba4f502b 2018-08-04 stsp int ncommits;
127 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
130 f26dddb7 2019-11-08 stsp struct tog_color {
131 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
132 6d17833f 2019-11-08 stsp regex_t regex;
133 6d17833f 2019-11-08 stsp short colorpair;
135 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
137 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 cc488aa7 2022-01-23 stsp static const struct got_error *
142 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 cc488aa7 2022-01-23 stsp struct got_reference* re2)
145 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
146 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
147 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
149 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
150 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
154 cc488aa7 2022-01-23 stsp return NULL;
155 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
157 cc488aa7 2022-01-23 stsp return NULL;
160 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 cc488aa7 2022-01-23 stsp return NULL;
164 11b20872 2019-11-08 stsp static const struct got_error *
165 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
167 51a10b52 2020-12-26 stsp const struct got_error *err;
169 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
173 51a10b52 2020-12-26 stsp return err;
175 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
179 51a10b52 2020-12-26 stsp static void
180 51a10b52 2020-12-26 stsp tog_free_refs(void)
182 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
183 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
184 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
186 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
189 51a10b52 2020-12-26 stsp static const struct got_error *
190 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
191 11b20872 2019-11-08 stsp int idx, short color)
193 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
194 11b20872 2019-11-08 stsp struct tog_color *tc;
195 11b20872 2019-11-08 stsp int regerr = 0;
197 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
198 11b20872 2019-11-08 stsp return NULL;
200 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
202 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
203 11b20872 2019-11-08 stsp if (tc == NULL)
204 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
205 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
206 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 11b20872 2019-11-08 stsp if (regerr) {
208 11b20872 2019-11-08 stsp static char regerr_msg[512];
209 11b20872 2019-11-08 stsp static char err_msg[512];
210 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
211 11b20872 2019-11-08 stsp sizeof(regerr_msg));
212 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 11b20872 2019-11-08 stsp regerr_msg);
214 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
216 11b20872 2019-11-08 stsp return err;
218 11b20872 2019-11-08 stsp tc->colorpair = idx;
219 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
220 11b20872 2019-11-08 stsp return NULL;
223 11b20872 2019-11-08 stsp static void
224 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
226 11b20872 2019-11-08 stsp struct tog_color *tc;
228 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
229 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
230 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
231 11b20872 2019-11-08 stsp regfree(&tc->regex);
236 336075a4 2022-06-25 op static struct tog_color *
237 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
239 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
241 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
242 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
246 11b20872 2019-11-08 stsp return NULL;
250 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 11b20872 2019-11-08 stsp return COLOR_CYAN;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 11b20872 2019-11-08 stsp return COLOR_YELLOW;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 11b20872 2019-11-08 stsp return COLOR_GREEN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 91b8c405 2020-01-25 stsp return COLOR_CYAN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 11b20872 2019-11-08 stsp return COLOR_GREEN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 11b20872 2019-11-08 stsp return COLOR_CYAN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 11b20872 2019-11-08 stsp return COLOR_YELLOW;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_GREEN;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
280 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
287 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
289 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
291 11b20872 2019-11-08 stsp if (val == NULL)
292 11b20872 2019-11-08 stsp return default_color_value(envvar);
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
295 11b20872 2019-11-08 stsp return COLOR_BLACK;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
297 11b20872 2019-11-08 stsp return COLOR_RED;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
299 11b20872 2019-11-08 stsp return COLOR_GREEN;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
301 11b20872 2019-11-08 stsp return COLOR_YELLOW;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
303 11b20872 2019-11-08 stsp return COLOR_BLUE;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
305 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
307 11b20872 2019-11-08 stsp return COLOR_CYAN;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
309 11b20872 2019-11-08 stsp return COLOR_WHITE;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
313 11b20872 2019-11-08 stsp return default_color_value(envvar);
316 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
317 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
318 3dbaef42 2020-11-24 stsp const char *label1, *label2;
319 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
320 f9d37699 2022-06-28 stsp int fd1, fd2;
321 c7d5c43c 2022-08-04 mark int lineno;
322 15a087fe 2019-02-21 stsp int first_displayed_line;
323 15a087fe 2019-02-21 stsp int last_displayed_line;
325 15a087fe 2019-02-21 stsp int diff_context;
326 3dbaef42 2020-11-24 stsp int ignore_whitespace;
327 64453f7e 2020-11-21 stsp int force_text_diff;
328 15a087fe 2019-02-21 stsp struct got_repository *repo;
329 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey int matched_line;
332 f44b1f58 2020-02-02 tracey int selected_line;
334 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
335 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
338 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
341 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
342 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
343 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
344 1a76625f 2018-10-22 stsp int commits_needed;
345 fb280deb 2021-08-30 stsp int load_all;
346 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
347 1a76625f 2018-10-22 stsp struct commit_queue *commits;
348 1a76625f 2018-10-22 stsp const char *in_repo_path;
349 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
350 1a76625f 2018-10-22 stsp struct got_repository *repo;
351 74467cc8 2022-06-15 stsp int *pack_fds;
352 1a76625f 2018-10-22 stsp int log_complete;
353 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
354 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
356 13add988 2019-10-15 stsp int *searching;
357 13add988 2019-10-15 stsp int *search_next_done;
358 13add988 2019-10-15 stsp regex_t *regex;
361 1a76625f 2018-10-22 stsp struct tog_log_view_state {
362 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
363 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
366 b01e7d3b 2018-08-04 stsp int selected;
367 b01e7d3b 2018-08-04 stsp char *in_repo_path;
368 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
369 b672a97a 2020-01-27 stsp int log_branches;
370 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
371 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
372 1a76625f 2018-10-22 stsp sig_atomic_t quit;
373 1a76625f 2018-10-22 stsp pthread_t thread;
374 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
375 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
376 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
377 11b20872 2019-11-08 stsp struct tog_colors colors;
378 10aab77f 2022-07-19 op int use_committer;
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
384 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
388 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
389 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
390 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
391 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
394 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
395 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
397 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
398 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
399 e9424729 2018-08-04 stsp int nlines;
401 e9424729 2018-08-04 stsp struct tog_view *view;
402 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
406 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
407 e9424729 2018-08-04 stsp const char *path;
408 e9424729 2018-08-04 stsp struct got_repository *repo;
409 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
410 e9424729 2018-08-04 stsp int *complete;
411 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
412 fc06ba56 2019-08-22 stsp void *cancel_arg;
415 e9424729 2018-08-04 stsp struct tog_blame {
417 be659d10 2020-11-18 stsp off_t filesize;
418 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
419 6fcac457 2018-11-19 stsp int nlines;
420 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
421 e9424729 2018-08-04 stsp pthread_t thread;
422 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
424 e9424729 2018-08-04 stsp const char *path;
425 0ae84acc 2022-06-15 tracey int *pack_fds;
428 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
429 7cbe629d 2018-08-04 stsp int first_displayed_line;
430 7cbe629d 2018-08-04 stsp int last_displayed_line;
431 7cbe629d 2018-08-04 stsp int selected_line;
432 c0f61fa4 2022-07-11 mark int last_diffed_line;
433 7cbe629d 2018-08-04 stsp int blame_complete;
436 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
437 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
438 e5a0f69f 2018-08-18 stsp char *path;
439 7cbe629d 2018-08-04 stsp struct got_repository *repo;
440 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
441 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
442 e9424729 2018-08-04 stsp struct tog_blame blame;
443 6c4c42e0 2019-06-24 stsp int matched_line;
444 11b20872 2019-11-08 stsp struct tog_colors colors;
447 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
448 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
449 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 ad80ab7b 2018-08-04 stsp int selected;
455 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
458 ad80ab7b 2018-08-04 stsp char *tree_label;
459 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
460 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
461 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
465 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
466 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
467 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
468 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
469 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
470 6458efa5 2020-11-24 stsp struct tog_colors colors;
473 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
474 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
475 6458efa5 2020-11-24 stsp struct got_reference *ref;
479 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
482 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
483 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
486 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 6458efa5 2020-11-24 stsp struct got_repository *repo;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
489 bddb1296 2019-11-08 stsp struct tog_colors colors;
493 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
495 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
496 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
497 669b5ffa 2018-10-07 stsp * there is enough screen estate.
499 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
500 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
502 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
503 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
504 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
506 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
507 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
509 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
511 cc3c9aac 2018-08-01 stsp struct tog_view {
512 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
513 26ed57b2 2018-05-19 stsp WINDOW *window;
514 26ed57b2 2018-05-19 stsp PANEL *panel;
515 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
517 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
518 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
519 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
521 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
522 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
523 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
525 669b5ffa 2018-10-07 stsp struct tog_view *parent;
526 669b5ffa 2018-10-07 stsp struct tog_view *child;
529 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
530 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
531 e78dc838 2020-12-04 stsp * between parent and child.
532 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
533 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
534 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
535 e78dc838 2020-12-04 stsp * situations.
537 e78dc838 2020-12-04 stsp int focus_child;
539 9b058f45 2022-06-30 mark enum tog_view_mode mode;
540 5dc9f4bc 2018-08-04 stsp /* type-specific state */
541 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
543 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
544 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
545 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
546 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
547 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
550 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
551 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
552 e78dc838 2020-12-04 stsp struct tog_view *, int);
553 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
554 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
557 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
558 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
559 c0c4acc8 2021-01-24 stsp int search_started;
560 60493ae3 2019-06-20 stsp int searching;
561 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
562 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
563 60493ae3 2019-06-20 stsp int search_next_done;
564 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
565 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
566 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
567 1803e47f 2019-06-22 stsp regex_t regex;
568 41605754 2020-11-12 stsp regmatch_t regmatch;
571 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
572 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
573 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
574 78756c87 2020-11-24 stsp struct got_repository *);
575 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
576 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
577 e78dc838 2020-12-04 stsp struct tog_view *, int);
578 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
579 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
580 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
581 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
583 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
584 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
585 78756c87 2020-11-24 stsp const char *, const char *, int);
586 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
588 e78dc838 2020-12-04 stsp struct tog_view *, int);
589 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
590 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
592 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
594 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
595 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
596 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
597 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
598 e78dc838 2020-12-04 stsp struct tog_view *, int);
599 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
600 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
602 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
604 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
605 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
606 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
607 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
608 e78dc838 2020-12-04 stsp struct tog_view *, int);
609 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
611 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
613 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
614 6458efa5 2020-11-24 stsp struct got_repository *);
615 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
616 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
617 e78dc838 2020-12-04 stsp struct tog_view *, int);
618 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
622 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
623 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
624 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
625 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
626 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
628 25791caa 2018-10-24 stsp static void
629 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
631 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
634 83baff54 2019-08-12 stsp static void
635 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
637 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
640 61266923 2020-01-14 stsp static void
641 61266923 2020-01-14 stsp tog_sigcont(int signo)
643 61266923 2020-01-14 stsp tog_sigcont_received = 1;
646 2497f032 2022-05-31 stsp static void
647 2497f032 2022-05-31 stsp tog_sigint(int signo)
649 2497f032 2022-05-31 stsp tog_sigint_received = 1;
652 2497f032 2022-05-31 stsp static void
653 2497f032 2022-05-31 stsp tog_sigterm(int signo)
655 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
659 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
661 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
662 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
665 e5a0f69f 2018-08-18 stsp static const struct got_error *
666 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
668 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
670 669b5ffa 2018-10-07 stsp if (view->child) {
671 5629093a 2022-07-11 stsp child_err = view_close(view->child);
672 669b5ffa 2018-10-07 stsp view->child = NULL;
674 e5a0f69f 2018-08-18 stsp if (view->close)
675 e5a0f69f 2018-08-18 stsp err = view->close(view);
676 ea5e7bb5 2018-08-01 stsp if (view->panel)
677 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
678 ea5e7bb5 2018-08-01 stsp if (view->window)
679 ea5e7bb5 2018-08-01 stsp delwin(view->window);
680 ea5e7bb5 2018-08-01 stsp free(view);
681 5629093a 2022-07-11 stsp return err ? err : child_err;
684 ea5e7bb5 2018-08-01 stsp static struct tog_view *
685 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
686 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
688 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
690 ea5e7bb5 2018-08-01 stsp if (view == NULL)
691 ea5e7bb5 2018-08-01 stsp return NULL;
693 d6b05b5b 2018-08-04 stsp view->type = type;
694 f7d12f7e 2018-08-01 stsp view->lines = LINES;
695 f7d12f7e 2018-08-01 stsp view->cols = COLS;
696 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
697 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
698 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
699 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
700 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
701 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
702 96a765a8 2018-08-04 stsp view_close(view);
703 ea5e7bb5 2018-08-01 stsp return NULL;
705 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
706 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
707 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
708 96a765a8 2018-08-04 stsp view_close(view);
709 ea5e7bb5 2018-08-01 stsp return NULL;
712 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
713 ea5e7bb5 2018-08-01 stsp return view;
717 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
719 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
721 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
724 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
726 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
728 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
731 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
733 5c60c32a 2018-10-18 stsp static const struct got_error *
734 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
736 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
738 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
739 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
740 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
742 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
743 9b058f45 2022-06-30 mark view->begin_x = 0;
744 571ccd73 2022-07-19 mark } else if (!view->resized) {
745 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
746 3c1dfe12 2022-07-08 mark view->cols > 119)
747 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
749 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
750 9b058f45 2022-06-30 mark view->begin_y = 0;
752 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
753 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
754 5c60c32a 2018-10-18 stsp view->lines = LINES;
755 5c60c32a 2018-10-18 stsp view->cols = COLS;
756 5c60c32a 2018-10-18 stsp err = view_resize(view);
758 5c60c32a 2018-10-18 stsp return err;
760 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
761 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
763 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
764 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
766 5c60c32a 2018-10-18 stsp return NULL;
769 5c60c32a 2018-10-18 stsp static const struct got_error *
770 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
772 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
774 5c60c32a 2018-10-18 stsp view->begin_x = 0;
775 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
776 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
777 5c60c32a 2018-10-18 stsp view->ncols = COLS;
778 5c60c32a 2018-10-18 stsp view->lines = LINES;
779 5c60c32a 2018-10-18 stsp view->cols = COLS;
780 5c60c32a 2018-10-18 stsp err = view_resize(view);
782 5c60c32a 2018-10-18 stsp return err;
784 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
785 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
787 5c60c32a 2018-10-18 stsp return NULL;
791 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
793 5c60c32a 2018-10-18 stsp return view->parent == NULL;
797 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
799 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
803 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
805 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
809 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
811 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
812 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
815 9b058f45 2022-06-30 mark static void
816 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
818 9b058f45 2022-06-30 mark PANEL *panel;
819 9b058f45 2022-06-30 mark const struct tog_view *view_above;
821 9b058f45 2022-06-30 mark if (view->parent)
822 9b058f45 2022-06-30 mark return view_border(view->parent);
824 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
825 9b058f45 2022-06-30 mark if (panel == NULL)
828 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
829 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
830 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
831 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
832 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
834 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
835 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
838 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
839 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
840 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
841 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
842 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
844 4d8c2215 2018-08-19 stsp static const struct got_error *
845 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
847 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
848 9b058f45 2022-06-30 mark int dif, nlines, ncols;
850 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
852 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
853 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
855 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
856 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
857 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
859 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
861 4dd27a72 2022-06-29 stsp if (view->child) {
862 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
864 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
865 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
866 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
867 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
868 0dbbbe90 2022-06-17 op ncols = COLS;
870 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
871 5c60c32a 2018-10-18 stsp if (view->child->focussed)
872 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
874 5c60c32a 2018-10-18 stsp show_panel(view->panel);
876 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
878 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
879 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
882 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
883 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
884 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
887 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
889 9b058f45 2022-06-30 mark return err;
890 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
891 9b058f45 2022-06-30 mark err = offset_selection_down(view);
893 9b058f45 2022-06-30 mark return err;
895 9b058f45 2022-06-30 mark view_border(view);
896 9b058f45 2022-06-30 mark update_panels();
897 9b058f45 2022-06-30 mark doupdate();
898 9b058f45 2022-06-30 mark show_panel(view->child->panel);
899 9b058f45 2022-06-30 mark nlines = view->nlines;
901 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
902 0dbbbe90 2022-06-17 op ncols = COLS;
904 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
905 571ccd73 2022-07-19 mark err = view->resize(view, dif);
907 571ccd73 2022-07-19 mark return err;
910 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
911 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
912 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
913 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
914 0dbbbe90 2022-06-17 op wclear(view->window);
916 0dbbbe90 2022-06-17 op view->nlines = nlines;
917 0dbbbe90 2022-06-17 op view->ncols = ncols;
918 0dbbbe90 2022-06-17 op view->lines = LINES;
919 0dbbbe90 2022-06-17 op view->cols = COLS;
921 5c60c32a 2018-10-18 stsp return NULL;
924 571ccd73 2022-07-19 mark static const struct got_error *
925 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
927 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
928 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
931 6fe51fee 2022-07-22 mark if (s->selected_entry)
932 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
935 571ccd73 2022-07-19 mark * Request commits to account for the increased
936 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
938 571ccd73 2022-07-19 mark if (s->commits.ncommits < n) {
939 571ccd73 2022-07-19 mark view->nscrolled = n - s->commits.ncommits + increase + 1;
940 571ccd73 2022-07-19 mark err = request_log_commits(view);
943 571ccd73 2022-07-19 mark return err;
946 d9a7ab53 2022-07-11 mark static void
947 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
949 d9a7ab53 2022-07-11 mark if (n == 0)
952 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
953 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
954 d9a7ab53 2022-07-11 mark view->parent->offset += n;
956 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
957 d9a7ab53 2022-07-11 mark } else if (view->offset) {
958 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
959 d9a7ab53 2022-07-11 mark view->offset -= n;
961 d9a7ab53 2022-07-11 mark view->offset = 0;
965 3c1dfe12 2022-07-08 mark static const struct got_error *
966 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
968 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
969 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
971 3c1dfe12 2022-07-08 mark if (view->parent)
972 3c1dfe12 2022-07-08 mark v = view->parent;
976 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
977 3c1dfe12 2022-07-08 mark return NULL;
979 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
981 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
982 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
983 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
984 3c1dfe12 2022-07-08 mark if (view->parent)
985 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
987 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
988 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
989 3c1dfe12 2022-07-08 mark view->count = 0;
990 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
991 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
992 3c1dfe12 2022-07-08 mark view->count = 0;
993 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
995 3c1dfe12 2022-07-08 mark v->ncols = COLS;
996 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
997 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
998 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1000 3c1dfe12 2022-07-08 mark return err;
1001 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1003 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1004 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1005 3c1dfe12 2022-07-08 mark if (view->parent)
1006 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1008 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1009 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1010 3c1dfe12 2022-07-08 mark view->count = 0;
1011 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1012 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1013 3c1dfe12 2022-07-08 mark view->count = 0;
1014 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1016 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1019 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1020 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1021 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1022 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1024 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1026 3c1dfe12 2022-07-08 mark return err;
1027 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1029 3c1dfe12 2022-07-08 mark return err;
1031 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1032 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1034 3c1dfe12 2022-07-08 mark return err;
1037 6fe51fee 2022-07-22 mark if (v->resize)
1038 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1039 6fe51fee 2022-07-22 mark else if (v->child->resize)
1040 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1042 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1044 3c1dfe12 2022-07-08 mark return err;
1047 3c1dfe12 2022-07-08 mark static void
1048 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1050 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1052 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1053 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1056 669b5ffa 2018-10-07 stsp static const struct got_error *
1057 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1059 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1061 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1062 669b5ffa 2018-10-07 stsp return NULL;
1064 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1065 669b5ffa 2018-10-07 stsp view->child = NULL;
1066 669b5ffa 2018-10-07 stsp return err;
1069 0dbbbe90 2022-06-17 op static const struct got_error *
1070 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1072 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1074 669b5ffa 2018-10-07 stsp view->child = child;
1075 669b5ffa 2018-10-07 stsp child->parent = view;
1077 3c1dfe12 2022-07-08 mark err = view_resize(view);
1079 3c1dfe12 2022-07-08 mark return err;
1081 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1082 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1084 3c1dfe12 2022-07-08 mark return err;
1087 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1088 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1090 136e2bd4 2022-07-23 mark static const struct got_error *
1091 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1092 136e2bd4 2022-07-23 mark enum tog_view_type request)
1094 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1095 136e2bd4 2022-07-23 mark const struct got_error *err;
1096 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1098 136e2bd4 2022-07-23 mark *requested = NULL;
1100 136e2bd4 2022-07-23 mark if (view_is_parent_view(view))
1101 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1103 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1105 136e2bd4 2022-07-23 mark return err;
1107 136e2bd4 2022-07-23 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1108 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1110 136e2bd4 2022-07-23 mark return err;
1113 136e2bd4 2022-07-23 mark view->focussed = 0;
1114 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1115 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1116 136e2bd4 2022-07-23 mark new_view->nlines = view->lines - y;
1118 136e2bd4 2022-07-23 mark if (view_is_parent_view(view)) {
1119 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1120 136e2bd4 2022-07-23 mark err = view_close_child(view);
1122 136e2bd4 2022-07-23 mark return err;
1123 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1125 136e2bd4 2022-07-23 mark return err;
1126 136e2bd4 2022-07-23 mark view->focus_child = 1;
1128 136e2bd4 2022-07-23 mark *requested = new_view;
1130 136e2bd4 2022-07-23 mark return NULL;
1133 34bc9ec9 2019-02-22 stsp static void
1134 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1136 25791caa 2018-10-24 stsp int cols, lines;
1137 25791caa 2018-10-24 stsp struct winsize size;
1139 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1140 25791caa 2018-10-24 stsp cols = 80; /* Default */
1141 25791caa 2018-10-24 stsp lines = 24;
1143 25791caa 2018-10-24 stsp cols = size.ws_col;
1144 25791caa 2018-10-24 stsp lines = size.ws_row;
1146 25791caa 2018-10-24 stsp resize_term(lines, cols);
1149 2b49a8ae 2019-06-22 stsp static const struct got_error *
1150 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1152 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1153 9b058f45 2022-06-30 mark struct tog_view *v = view;
1154 2b49a8ae 2019-06-22 stsp char pattern[1024];
1157 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1158 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1159 c0c4acc8 2021-01-24 stsp view->searching = 0;
1160 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1162 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1164 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1165 2b49a8ae 2019-06-22 stsp return NULL;
1167 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1168 9b058f45 2022-06-30 mark v = view->child;
1170 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1171 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1173 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1174 2b49a8ae 2019-06-22 stsp nocbreak();
1176 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1177 9b058f45 2022-06-30 mark wrefresh(v->window);
1180 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1181 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1182 2b49a8ae 2019-06-22 stsp return NULL;
1184 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1185 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1186 7c32bd05 2019-06-22 stsp if (err) {
1187 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1188 7c32bd05 2019-06-22 stsp return err;
1190 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1191 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1192 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1193 2b49a8ae 2019-06-22 stsp view->search_next(view);
1196 2b49a8ae 2019-06-22 stsp return NULL;
1199 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1200 d2366e29 2022-07-07 mark static const struct got_error *
1201 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1203 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1204 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1206 d2366e29 2022-07-07 mark if (view->parent)
1207 d2366e29 2022-07-07 mark v = view->parent;
1211 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1212 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1214 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1216 7532ccda 2022-07-11 mark if (!v->child)
1217 7532ccda 2022-07-11 mark return NULL;
1218 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1219 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1221 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1222 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1223 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1224 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1225 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1228 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1229 d2366e29 2022-07-07 mark v->ncols = COLS;
1230 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1231 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1233 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1235 d2366e29 2022-07-07 mark return err;
1237 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1238 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1239 d2366e29 2022-07-07 mark v->focus_child = 1;
1241 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1243 d2366e29 2022-07-07 mark return err;
1244 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1246 d2366e29 2022-07-07 mark return err;
1248 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1249 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1250 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1251 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1253 279d2047 2022-07-29 stsp return err;
1254 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1256 279d2047 2022-07-29 stsp return err;
1258 7532ccda 2022-07-11 mark offset_selection_up(v);
1259 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1261 dff91825 2022-07-22 mark if (v->resize)
1262 dff91825 2022-07-22 mark err = v->resize(v, 0);
1263 dff91825 2022-07-22 mark else if (v->child->resize)
1264 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1266 d2366e29 2022-07-07 mark return err;
1270 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1271 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1273 640cd7ff 2022-06-22 mark static int
1274 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1276 9b058f45 2022-06-30 mark struct tog_view *v = view;
1277 9b058f45 2022-06-30 mark int x, n = 0;
1279 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1280 9b058f45 2022-06-30 mark v = view->child;
1281 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1282 9b058f45 2022-06-30 mark v = view->parent;
1284 640cd7ff 2022-06-22 mark view->count = 0;
1285 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1286 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1287 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1288 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1289 9b058f45 2022-06-30 mark waddch(v->window, ':');
1292 9b058f45 2022-06-30 mark x = getcurx(v->window);
1293 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1294 9b058f45 2022-06-30 mark waddch(v->window, c);
1295 9b058f45 2022-06-30 mark wrefresh(v->window);
1299 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1300 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1302 640cd7ff 2022-06-22 mark if (n >= 9999999)
1303 640cd7ff 2022-06-22 mark n = 9999999;
1305 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1306 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1308 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1309 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1314 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1315 640cd7ff 2022-06-22 mark view->count = n;
1320 0cf4efb1 2018-09-29 stsp static const struct got_error *
1321 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1322 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1324 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1325 669b5ffa 2018-10-07 stsp struct tog_view *v;
1326 1a76625f 2018-10-22 stsp int ch, errcode;
1328 e5a0f69f 2018-08-18 stsp *new = NULL;
1330 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1331 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1332 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1333 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1334 640cd7ff 2022-06-22 mark view->count = 0;
1337 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1338 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1339 82954512 2020-02-03 stsp if (errcode)
1340 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1341 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1342 3da8ef85 2021-09-21 stsp sched_yield();
1343 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1344 82954512 2020-02-03 stsp if (errcode)
1345 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1346 82954512 2020-02-03 stsp "pthread_mutex_lock");
1347 60493ae3 2019-06-20 stsp view->search_next(view);
1348 60493ae3 2019-06-20 stsp return NULL;
1351 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1352 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1353 1a76625f 2018-10-22 stsp if (errcode)
1354 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1355 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1356 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1358 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1359 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1360 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1361 a6d37fac 2022-07-03 mark view->count = 0;
1363 a6d37fac 2022-07-03 mark ch = view->ch;
1365 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1366 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1367 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1369 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1370 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1371 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1372 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1373 1a76625f 2018-10-22 stsp if (errcode)
1374 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1376 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1377 25791caa 2018-10-24 stsp tog_resizeterm();
1378 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1379 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1380 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1381 25791caa 2018-10-24 stsp err = view_resize(v);
1383 25791caa 2018-10-24 stsp return err;
1384 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1386 25791caa 2018-10-24 stsp return err;
1387 cdfcfb03 2020-12-06 stsp if (v->child) {
1388 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1390 cdfcfb03 2020-12-06 stsp return err;
1391 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1392 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1394 cdfcfb03 2020-12-06 stsp return err;
1395 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1396 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1398 3c1dfe12 2022-07-08 mark return err;
1404 e5a0f69f 2018-08-18 stsp switch (ch) {
1406 640cd7ff 2022-06-22 mark view->count = 0;
1407 1e37a5c2 2019-05-12 jcs if (view->child) {
1408 e78dc838 2020-12-04 stsp view->focussed = 0;
1409 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1410 e78dc838 2020-12-04 stsp view->focus_child = 1;
1411 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1412 e78dc838 2020-12-04 stsp view->focussed = 0;
1413 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1414 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1415 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1416 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1417 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1420 9b058f45 2022-06-30 mark return err;
1422 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1423 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1425 9b058f45 2022-06-30 mark return err;
1430 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1431 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1432 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1433 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1437 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1439 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1440 9970f7fc 2020-12-03 stsp view->dying = 1;
1446 640cd7ff 2022-06-22 mark view->count = 0;
1447 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1448 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1450 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1451 e78dc838 2020-12-04 stsp view->focussed = 0;
1452 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1453 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1455 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1457 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1461 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1462 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1464 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1465 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1466 e78dc838 2020-12-04 stsp view->focussed = 1;
1467 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1469 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1470 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1471 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1473 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1477 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1481 6fe51fee 2022-07-22 mark if (view->resize) {
1482 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1486 9b058f45 2022-06-30 mark if (view->parent)
1487 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1489 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1492 3c1dfe12 2022-07-08 mark view->count = 0;
1493 d2366e29 2022-07-07 mark err = switch_split(view);
1496 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1499 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1501 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1504 640cd7ff 2022-06-22 mark view->count = 0;
1505 60493ae3 2019-06-20 stsp if (view->search_start)
1506 2b49a8ae 2019-06-22 stsp view_search_start(view);
1508 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1512 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1513 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1514 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1515 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1516 60493ae3 2019-06-20 stsp view->search_next(view);
1518 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1521 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1522 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1524 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1525 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1526 917d79a7 2022-07-01 stsp if (v->reset) {
1527 917d79a7 2022-07-01 stsp err = v->reset(v);
1529 917d79a7 2022-07-01 stsp return err;
1531 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1532 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1534 917d79a7 2022-07-01 stsp return err;
1539 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1543 e5a0f69f 2018-08-18 stsp return err;
1547 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1549 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1550 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1552 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1554 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1557 669b5ffa 2018-10-07 stsp return view->focussed;
1560 bcbd79e2 2018-08-19 stsp static const struct got_error *
1561 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1563 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1564 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1565 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1566 d2366e29 2022-07-07 mark char *mode;
1567 fd823528 2018-10-22 stsp int fast_refresh = 10;
1568 1a76625f 2018-10-22 stsp int done = 0, errcode;
1570 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1571 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1572 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1574 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1576 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1577 1a76625f 2018-10-22 stsp if (errcode)
1578 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1580 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1581 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1583 1004088d 2018-09-29 stsp view->focussed = 1;
1584 878940b7 2018-09-29 stsp err = view->show(view);
1586 0cf4efb1 2018-09-29 stsp return err;
1587 0cf4efb1 2018-09-29 stsp update_panels();
1588 0cf4efb1 2018-09-29 stsp doupdate();
1589 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1590 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1591 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1592 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1593 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1595 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1598 9970f7fc 2020-12-03 stsp if (view->dying) {
1599 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1601 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1602 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1604 e78dc838 2020-12-04 stsp else if (view->parent)
1605 669b5ffa 2018-10-07 stsp prev = view->parent;
1607 e78dc838 2020-12-04 stsp if (view->parent) {
1608 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1609 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1610 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1611 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1612 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1615 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1616 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1618 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1620 9970f7fc 2020-12-03 stsp err = view_close(view);
1622 e5a0f69f 2018-08-18 stsp goto done;
1624 e78dc838 2020-12-04 stsp view = NULL;
1625 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1626 e78dc838 2020-12-04 stsp if (v->focussed)
1629 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1630 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1632 e78dc838 2020-12-04 stsp view = prev;
1633 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1634 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1635 e78dc838 2020-12-04 stsp tog_view_list_head);
1637 e78dc838 2020-12-04 stsp if (view) {
1638 e78dc838 2020-12-04 stsp if (view->focus_child) {
1639 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1640 e78dc838 2020-12-04 stsp view = view->child;
1642 e78dc838 2020-12-04 stsp view->focussed = 1;
1646 bcbd79e2 2018-08-19 stsp if (new_view) {
1647 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1648 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1649 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1650 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1652 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1653 86c66b02 2018-10-18 stsp err = view_close(v);
1655 86c66b02 2018-10-18 stsp goto done;
1658 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1659 fed7eaa8 2018-10-24 stsp view = new_view;
1661 669b5ffa 2018-10-07 stsp if (view) {
1662 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1663 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1664 e78dc838 2020-12-04 stsp view = view->child;
1666 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1667 e78dc838 2020-12-04 stsp view = view->parent;
1669 e78dc838 2020-12-04 stsp show_panel(view->panel);
1670 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1671 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1672 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1673 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1675 1a76625f 2018-10-22 stsp goto done;
1677 669b5ffa 2018-10-07 stsp err = view->show(view);
1679 1a76625f 2018-10-22 stsp goto done;
1680 669b5ffa 2018-10-07 stsp if (view->child) {
1681 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1683 1a76625f 2018-10-22 stsp goto done;
1685 1a76625f 2018-10-22 stsp update_panels();
1686 1a76625f 2018-10-22 stsp doupdate();
1690 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1691 5629093a 2022-07-11 stsp const struct got_error *close_err;
1692 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1693 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1694 5629093a 2022-07-11 stsp close_err = view_close(view);
1695 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1696 5629093a 2022-07-11 stsp err = close_err;
1699 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1700 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1701 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1703 e5a0f69f 2018-08-18 stsp return err;
1706 4ed7e80c 2018-05-20 stsp __dead static void
1707 9f7d7167 2018-04-29 stsp usage_log(void)
1710 c70c5802 2018-08-01 stsp fprintf(stderr,
1711 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1712 9f7d7167 2018-04-29 stsp getprogname());
1716 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1717 80ddbec8 2018-04-29 stsp static const struct got_error *
1718 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1720 00dfcb92 2018-06-11 stsp char *vis = NULL;
1721 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1723 963b370f 2018-05-20 stsp *ws = NULL;
1724 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1725 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1726 00dfcb92 2018-06-11 stsp int vislen;
1727 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1728 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1730 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1731 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1733 00dfcb92 2018-06-11 stsp return err;
1734 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1735 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1736 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1737 a7f50699 2018-06-11 stsp goto done;
1741 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1742 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1743 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1744 a7f50699 2018-06-11 stsp goto done;
1747 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1748 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1750 00dfcb92 2018-06-11 stsp free(vis);
1751 963b370f 2018-05-20 stsp if (err) {
1752 963b370f 2018-05-20 stsp free(*ws);
1753 963b370f 2018-05-20 stsp *ws = NULL;
1754 963b370f 2018-05-20 stsp *wlen = 0;
1756 963b370f 2018-05-20 stsp return err;
1759 145b6838 2022-06-16 stsp static const struct got_error *
1760 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1762 145b6838 2022-06-16 stsp char *dst;
1763 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1765 145b6838 2022-06-16 stsp *ptr = NULL;
1766 145b6838 2022-06-16 stsp n = len = strlen(src);
1767 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1768 145b6838 2022-06-16 stsp if (dst == NULL)
1769 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1771 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1772 145b6838 2022-06-16 stsp const char c = src[idx];
1774 145b6838 2022-06-16 stsp if (c == '\t') {
1775 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1778 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1779 6e1c41ad 2022-06-16 mark if (p == NULL) {
1780 6e1c41ad 2022-06-16 mark free(dst);
1781 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1786 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1789 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1793 145b6838 2022-06-16 stsp dst[sz] = '\0';
1794 145b6838 2022-06-16 stsp *ptr = dst;
1795 145b6838 2022-06-16 stsp return NULL;
1799 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1800 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1801 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1805 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1807 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1809 4e4a9ac8 2022-06-17 op if (n == 0) {
1810 4e4a9ac8 2022-06-17 op *rcol = cols;
1814 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1815 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1816 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1818 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1820 4e4a9ac8 2022-06-17 op if (width == -1) {
1822 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1825 4e4a9ac8 2022-06-17 op if (cols + width > n)
1827 4e4a9ac8 2022-06-17 op cols += width;
1830 4e4a9ac8 2022-06-17 op *rcol = cols;
1835 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1836 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1837 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1839 ccda2f4d 2022-06-16 stsp static const struct got_error *
1840 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1841 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1843 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1845 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1846 145b6838 2022-06-16 stsp char *exstr = NULL;
1847 963b370f 2018-05-20 stsp size_t wlen;
1848 4e4a9ac8 2022-06-17 op int i, scrollx;
1850 963b370f 2018-05-20 stsp *wlinep = NULL;
1851 b700b5d6 2018-07-10 stsp *widthp = 0;
1853 145b6838 2022-06-16 stsp if (expand) {
1854 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1856 145b6838 2022-06-16 stsp return err;
1859 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1860 145b6838 2022-06-16 stsp free(exstr);
1862 963b370f 2018-05-20 stsp return err;
1864 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1866 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1867 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1870 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1871 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1875 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1876 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1878 b700b5d6 2018-07-10 stsp if (widthp)
1879 b700b5d6 2018-07-10 stsp *widthp = cols;
1880 ccda2f4d 2022-06-16 stsp if (scrollxp)
1881 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1883 963b370f 2018-05-20 stsp free(wline);
1885 963b370f 2018-05-20 stsp *wlinep = wline;
1886 963b370f 2018-05-20 stsp return err;
1889 8b473291 2019-02-21 stsp static const struct got_error*
1890 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1891 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1893 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1894 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1896 8b473291 2019-02-21 stsp const char *name;
1898 8b473291 2019-02-21 stsp *refs_str = NULL;
1900 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1901 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1902 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1905 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1906 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1908 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1909 8b473291 2019-02-21 stsp name += 5;
1910 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1911 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1913 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1914 8b473291 2019-02-21 stsp name += 6;
1915 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1916 8b473291 2019-02-21 stsp name += 8;
1917 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1918 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1921 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1924 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1925 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1926 5d844a1e 2019-08-13 stsp if (err) {
1927 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1928 48cae60d 2020-09-22 stsp free(ref_id);
1931 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1932 5d844a1e 2019-08-13 stsp err = NULL;
1933 5d844a1e 2019-08-13 stsp tag = NULL;
1936 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1937 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1938 48cae60d 2020-09-22 stsp free(ref_id);
1940 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1941 52b5abe1 2019-08-13 stsp if (cmp != 0)
1943 8b473291 2019-02-21 stsp s = *refs_str;
1944 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1945 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1946 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1948 8b473291 2019-02-21 stsp *refs_str = NULL;
1954 8b473291 2019-02-21 stsp return err;
1957 963b370f 2018-05-20 stsp static const struct got_error *
1958 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1959 27a741e5 2019-09-11 stsp int col_tab_align)
1961 e6b8b890 2020-12-29 naddy char *smallerthan;
1963 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1964 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1965 5813d178 2019-03-09 stsp author = smallerthan + 1;
1966 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1967 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1968 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1971 5813d178 2019-03-09 stsp static const struct got_error *
1972 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1973 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1974 8fdc79fe 2020-12-01 naddy int author_display_cols)
1976 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1977 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1978 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1979 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1980 5813d178 2019-03-09 stsp char *author = NULL;
1981 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1982 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1983 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1984 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1985 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1986 ccb26ccd 2018-11-05 stsp struct tm tm;
1987 45d799e2 2018-12-23 stsp time_t committer_time;
1988 11b20872 2019-11-08 stsp struct tog_color *tc;
1990 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1991 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1992 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1993 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1994 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1996 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1997 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1999 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2000 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2002 11b20872 2019-11-08 stsp wattr_on(view->window,
2003 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2004 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2006 11b20872 2019-11-08 stsp wattr_off(view->window,
2007 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2008 27a741e5 2019-09-11 stsp col = limit;
2009 b39d25c7 2018-07-10 stsp if (col > avail)
2010 b39d25c7 2018-07-10 stsp goto done;
2012 6570a66d 2019-11-08 stsp if (avail >= 120) {
2013 6570a66d 2019-11-08 stsp char *id_str;
2014 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2016 6570a66d 2019-11-08 stsp goto done;
2017 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2019 11b20872 2019-11-08 stsp wattr_on(view->window,
2020 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2021 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2023 11b20872 2019-11-08 stsp wattr_off(view->window,
2024 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2025 6570a66d 2019-11-08 stsp free(id_str);
2027 6570a66d 2019-11-08 stsp if (col > avail)
2028 6570a66d 2019-11-08 stsp goto done;
2031 10aab77f 2022-07-19 op if (s->use_committer)
2032 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2034 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2035 5813d178 2019-03-09 stsp if (author == NULL) {
2036 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2037 80ddbec8 2018-04-29 stsp goto done;
2039 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2041 bb737323 2018-05-20 stsp goto done;
2042 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2044 11b20872 2019-11-08 stsp wattr_on(view->window,
2045 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2046 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2048 11b20872 2019-11-08 stsp wattr_off(view->window,
2049 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2050 bb737323 2018-05-20 stsp col += author_width;
2051 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2052 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2054 bb737323 2018-05-20 stsp author_width++;
2056 9c2eaf34 2018-05-20 stsp if (col > avail)
2057 9c2eaf34 2018-05-20 stsp goto done;
2059 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2061 6d9fbc00 2018-04-29 stsp goto done;
2062 bb737323 2018-05-20 stsp logmsg = logmsg0;
2063 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2065 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2066 bb737323 2018-05-20 stsp if (newline)
2067 bb737323 2018-05-20 stsp *newline = '\0';
2068 ccda2f4d 2022-06-16 stsp limit = avail - col;
2069 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2070 4d1f6af3 2022-06-17 op limit--; /* for the border */
2071 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2072 ccda2f4d 2022-06-16 stsp limit, col, 1);
2074 29688b02 2022-06-16 stsp goto done;
2075 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2076 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2077 27a741e5 2019-09-11 stsp while (col < avail) {
2078 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2082 80ddbec8 2018-04-29 stsp free(logmsg0);
2083 bb737323 2018-05-20 stsp free(wlogmsg);
2084 5813d178 2019-03-09 stsp free(author);
2085 bb737323 2018-05-20 stsp free(wauthor);
2086 80ddbec8 2018-04-29 stsp free(line);
2087 80ddbec8 2018-04-29 stsp return err;
2090 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2091 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2092 899d86c2 2018-05-10 stsp struct got_object_id *id)
2094 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2096 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2097 80ddbec8 2018-04-29 stsp if (entry == NULL)
2098 899d86c2 2018-05-10 stsp return NULL;
2100 899d86c2 2018-05-10 stsp entry->id = id;
2101 99db9666 2018-05-07 stsp entry->commit = commit;
2102 899d86c2 2018-05-10 stsp return entry;
2105 99db9666 2018-05-07 stsp static void
2106 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2108 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2110 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2111 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2112 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2113 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2114 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2115 99db9666 2018-05-07 stsp free(entry);
2118 99db9666 2018-05-07 stsp static void
2119 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2121 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2122 99db9666 2018-05-07 stsp pop_commit(commits);
2125 c4972b91 2018-05-07 stsp static const struct got_error *
2126 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2127 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2129 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2130 13add988 2019-10-15 stsp regmatch_t regmatch;
2131 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2133 13add988 2019-10-15 stsp *have_match = 0;
2135 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2137 13add988 2019-10-15 stsp return err;
2139 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2141 13add988 2019-10-15 stsp goto done;
2143 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2144 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2145 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2146 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2147 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2148 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2149 13add988 2019-10-15 stsp *have_match = 1;
2151 13add988 2019-10-15 stsp free(id_str);
2152 13add988 2019-10-15 stsp free(logmsg);
2153 13add988 2019-10-15 stsp return err;
2156 13add988 2019-10-15 stsp static const struct got_error *
2157 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2159 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2162 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2163 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2164 1a76625f 2018-10-22 stsp * while updating the display.
2167 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2168 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2169 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2170 1a76625f 2018-10-22 stsp int errcode;
2172 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2173 4e0d2870 2020-12-07 naddy NULL, NULL);
2174 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2177 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2180 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2181 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2182 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2186 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2187 1a76625f 2018-10-22 stsp if (errcode) {
2188 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2189 13add988 2019-10-15 stsp "pthread_mutex_lock");
2193 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2194 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2195 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2197 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2198 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2199 7c1452c1 2020-03-26 stsp int have_match;
2200 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2203 7c1452c1 2020-03-26 stsp if (have_match)
2204 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2207 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2208 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2209 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2210 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2213 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2215 9ba79e04 2018-06-11 stsp return err;
2218 2b779855 2020-12-05 naddy static void
2219 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2221 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2222 2b779855 2020-12-05 naddy int ncommits = 0;
2224 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2225 2b779855 2020-12-05 naddy while (entry) {
2226 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2227 2b779855 2020-12-05 naddy s->selected_entry = entry;
2230 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2231 2b779855 2020-12-05 naddy ncommits++;
2235 0553a4e3 2018-04-30 stsp static const struct got_error *
2236 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2238 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2239 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2240 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2241 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2242 60493ae3 2019-06-20 stsp int width;
2243 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2244 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2245 8b473291 2019-02-21 stsp char *refs_str = NULL;
2246 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2247 11b20872 2019-11-08 stsp struct tog_color *tc;
2248 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2250 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2251 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2253 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2254 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2255 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2256 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2258 ecb28ae0 2018-07-16 stsp return err;
2259 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2260 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2261 d2075bf3 2020-12-25 stsp if (refs) {
2262 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2263 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2265 d2075bf3 2020-12-25 stsp goto done;
2269 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2270 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2272 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2273 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2274 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2275 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2276 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2277 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2278 8f4ed634 2020-03-26 stsp goto done;
2281 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2283 f9686aa5 2020-03-27 stsp if (view->searching) {
2284 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2285 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2286 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2287 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2288 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2289 f9686aa5 2020-03-27 stsp search_str = "searching...";
2292 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2293 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2294 f9686aa5 2020-03-27 stsp search_str ? search_str :
2295 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2296 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2297 8f4ed634 2020-03-26 stsp goto done;
2301 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2302 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2303 87411fa9 2022-06-16 stsp "........................................",
2304 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2306 1a76625f 2018-10-22 stsp header = NULL;
2307 1a76625f 2018-10-22 stsp goto done;
2309 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2310 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2311 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2312 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2313 1a76625f 2018-10-22 stsp header = NULL;
2314 1a76625f 2018-10-22 stsp goto done;
2316 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2318 1a76625f 2018-10-22 stsp goto done;
2320 2814baeb 2018-08-01 stsp werase(view->window);
2322 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2323 a3404814 2018-09-02 stsp wstandout(view->window);
2324 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2326 11b20872 2019-11-08 stsp wattr_on(view->window,
2327 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2328 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2330 11b20872 2019-11-08 stsp wattr_off(view->window,
2331 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2332 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2333 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2336 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2337 a3404814 2018-09-02 stsp wstandend(view->window);
2338 ecb28ae0 2018-07-16 stsp free(wline);
2339 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2340 1a76625f 2018-10-22 stsp goto done;
2342 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2343 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2344 5813d178 2019-03-09 stsp ncommits = 0;
2345 145b6838 2022-06-16 stsp view->maxx = 0;
2346 5813d178 2019-03-09 stsp while (entry) {
2347 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2348 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2349 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2350 5813d178 2019-03-09 stsp int width;
2351 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2353 10aab77f 2022-07-19 op if (s->use_committer)
2354 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2356 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2357 5813d178 2019-03-09 stsp if (author == NULL) {
2358 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2359 5813d178 2019-03-09 stsp goto done;
2361 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2362 27a741e5 2019-09-11 stsp date_display_cols);
2363 5813d178 2019-03-09 stsp if (author_cols < width)
2364 5813d178 2019-03-09 stsp author_cols = width;
2365 5813d178 2019-03-09 stsp free(wauthor);
2366 5813d178 2019-03-09 stsp free(author);
2367 a310d9c3 2022-07-21 florian if (err)
2368 a310d9c3 2022-07-21 florian goto done;
2369 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2371 145b6838 2022-06-16 stsp goto done;
2372 145b6838 2022-06-16 stsp msg = msg0;
2373 145b6838 2022-06-16 stsp while (*msg == '\n')
2375 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2376 29688b02 2022-06-16 stsp *eol = '\0';
2377 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2378 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2380 29688b02 2022-06-16 stsp goto done;
2381 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2382 145b6838 2022-06-16 stsp free(msg0);
2383 29688b02 2022-06-16 stsp free(wmsg);
2384 7ca04879 2019-10-19 stsp ncommits++;
2385 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2388 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2389 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2390 867c6645 2018-07-10 stsp ncommits = 0;
2391 899d86c2 2018-05-10 stsp while (entry) {
2392 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2394 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2395 2814baeb 2018-08-01 stsp wstandout(view->window);
2396 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2397 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2398 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2399 2814baeb 2018-08-01 stsp wstandend(view->window);
2401 60493ae3 2019-06-20 stsp goto done;
2402 0553a4e3 2018-04-30 stsp ncommits++;
2403 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2404 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2407 9b058f45 2022-06-30 mark view_border(view);
2409 1a76625f 2018-10-22 stsp free(id_str);
2410 8b473291 2019-02-21 stsp free(refs_str);
2411 1a76625f 2018-10-22 stsp free(ncommits_str);
2412 1a76625f 2018-10-22 stsp free(header);
2413 80ddbec8 2018-04-29 stsp return err;
2416 07b55e75 2018-05-10 stsp static void
2417 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2419 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2420 07b55e75 2018-05-10 stsp int nscrolled = 0;
2422 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2423 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2426 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2427 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2428 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2429 07b55e75 2018-05-10 stsp if (entry) {
2430 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2431 07b55e75 2018-05-10 stsp nscrolled++;
2436 aa075928 2018-05-10 stsp static const struct got_error *
2437 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2439 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2440 5e224a3e 2019-02-22 stsp int errcode;
2442 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2444 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2445 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2446 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2447 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2448 7aafa0d1 2019-02-22 stsp if (errcode)
2449 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2450 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2453 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2454 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2456 7c1452c1 2020-03-26 stsp if (!wait)
2459 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2460 ffe38506 2020-12-01 naddy show_log_view(view);
2461 7c1452c1 2020-03-26 stsp update_panels();
2462 7c1452c1 2020-03-26 stsp doupdate();
2464 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2465 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2466 82954512 2020-02-03 stsp if (errcode)
2467 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2468 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2470 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2471 ffe38506 2020-12-01 naddy show_log_view(view);
2472 7c1452c1 2020-03-26 stsp update_panels();
2473 7c1452c1 2020-03-26 stsp doupdate();
2476 5e224a3e 2019-02-22 stsp return NULL;
2479 5e224a3e 2019-02-22 stsp static const struct got_error *
2480 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2482 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2483 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2485 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2486 2525dccb 2022-07-13 mark return NULL;
2488 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2489 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2490 9b058f45 2022-06-30 mark view->nscrolled = 0;
2492 9b058f45 2022-06-30 mark return err;
2495 9b058f45 2022-06-30 mark static const struct got_error *
2496 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2498 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2499 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2500 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2501 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2503 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2504 5e224a3e 2019-02-22 stsp return NULL;
2506 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2507 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2508 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2510 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2512 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2513 94b80cfa 2022-08-01 mark ncommits_needed - s->commits.ncommits;
2514 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2516 5e224a3e 2019-02-22 stsp return err;
2520 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2521 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2524 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2525 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2527 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2528 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2530 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2531 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2533 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2534 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2536 9b058f45 2022-06-30 mark view->nscrolled = 0;
2538 dd0a52c1 2018-05-20 stsp return err;
2541 cd0acaa7 2018-05-20 stsp static const struct got_error *
2542 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2543 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2544 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2546 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2547 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2548 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2550 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2551 15a94983 2018-12-23 stsp if (diff_view == NULL)
2552 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2554 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2555 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2556 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2557 e5a0f69f 2018-08-18 stsp if (err == NULL)
2558 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2559 cd0acaa7 2018-05-20 stsp return err;
2562 80ddbec8 2018-04-29 stsp static const struct got_error *
2563 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2564 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2566 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2568 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2569 941e9f74 2019-05-21 stsp if (parent == NULL)
2570 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2572 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2573 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2574 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2575 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2576 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2577 941e9f74 2019-05-21 stsp s->tree = subtree;
2578 941e9f74 2019-05-21 stsp s->selected = 0;
2579 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2580 941e9f74 2019-05-21 stsp return NULL;
2583 941e9f74 2019-05-21 stsp static const struct got_error *
2584 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2585 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2587 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2588 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2589 941e9f74 2019-05-21 stsp const char *p;
2590 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2592 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2594 941e9f74 2019-05-21 stsp while (*p) {
2595 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2596 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2597 56e0773d 2019-11-28 stsp char *te_name;
2599 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2602 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2603 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2604 941e9f74 2019-05-21 stsp if (slash == NULL)
2605 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2607 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2608 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2609 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2612 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2613 56e0773d 2019-11-28 stsp if (te == NULL) {
2614 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2615 56e0773d 2019-11-28 stsp free(te_name);
2618 56e0773d 2019-11-28 stsp free(te_name);
2619 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2621 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2622 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2624 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2625 941e9f74 2019-05-21 stsp if (slash)
2626 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2628 941e9f74 2019-05-21 stsp subpath = strdup(path);
2629 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2630 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2634 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2639 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2640 941e9f74 2019-05-21 stsp free(tree_id);
2644 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2645 941e9f74 2019-05-21 stsp if (err) {
2646 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2649 941e9f74 2019-05-21 stsp if (slash == NULL)
2651 941e9f74 2019-05-21 stsp free(subpath);
2652 941e9f74 2019-05-21 stsp subpath = NULL;
2653 941e9f74 2019-05-21 stsp p = slash;
2656 941e9f74 2019-05-21 stsp free(subpath);
2657 1a76625f 2018-10-22 stsp return err;
2660 61266923 2020-01-14 stsp static const struct got_error *
2661 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2662 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2663 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2665 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2666 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2667 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2669 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2670 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2671 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2673 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2675 55cccc34 2020-02-20 stsp return err;
2676 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2678 55cccc34 2020-02-20 stsp *new_view = tree_view;
2680 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2681 55cccc34 2020-02-20 stsp return NULL;
2683 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2686 55cccc34 2020-02-20 stsp static const struct got_error *
2687 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2689 61266923 2020-01-14 stsp sigset_t sigset;
2690 61266923 2020-01-14 stsp int errcode;
2692 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2693 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2695 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2696 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2697 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2698 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2699 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2700 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2701 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2702 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2703 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2705 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2706 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2707 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2709 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2710 61266923 2020-01-14 stsp if (errcode)
2711 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2713 61266923 2020-01-14 stsp return NULL;
2716 1a76625f 2018-10-22 stsp static void *
2717 1a76625f 2018-10-22 stsp log_thread(void *arg)
2719 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2720 1a76625f 2018-10-22 stsp int errcode = 0;
2721 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2722 1a76625f 2018-10-22 stsp int done = 0;
2725 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2726 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2728 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2729 5629093a 2022-07-11 stsp if (errcode) {
2730 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2731 61266923 2020-01-14 stsp return (void *)err;
2734 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2735 5629093a 2022-07-11 stsp if (err) {
2736 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2737 5629093a 2022-07-11 stsp goto done;
2740 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2741 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2742 5629093a 2022-07-11 stsp if (errcode) {
2743 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2744 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2745 5629093a 2022-07-11 stsp goto done;
2747 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2748 1a76625f 2018-10-22 stsp if (err) {
2749 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2750 5629093a 2022-07-11 stsp goto done;
2751 1a76625f 2018-10-22 stsp err = NULL;
2753 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2754 1a76625f 2018-10-22 stsp a->commits_needed--;
2756 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2757 3abe8080 2019-04-10 stsp if (errcode) {
2758 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2759 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2760 5629093a 2022-07-11 stsp goto done;
2761 3abe8080 2019-04-10 stsp } else if (*a->quit)
2763 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2764 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2765 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2766 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2769 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2770 7c1452c1 2020-03-26 stsp if (errcode) {
2771 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2772 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2773 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2774 5629093a 2022-07-11 stsp goto done;
2778 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2780 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2781 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2782 7c1452c1 2020-03-26 stsp &tog_mutex);
2783 5629093a 2022-07-11 stsp if (errcode) {
2784 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2785 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2786 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2787 5629093a 2022-07-11 stsp goto done;
2789 21355643 2020-12-06 stsp if (*a->quit)
2794 3abe8080 2019-04-10 stsp a->log_complete = 1;
2795 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2796 5629093a 2022-07-11 stsp if (errcode)
2797 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2799 5629093a 2022-07-11 stsp if (err) {
2800 5629093a 2022-07-11 stsp tog_thread_error = 1;
2801 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2803 1a76625f 2018-10-22 stsp return (void *)err;
2806 1a76625f 2018-10-22 stsp static const struct got_error *
2807 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2809 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2810 1a76625f 2018-10-22 stsp int errcode;
2812 1a76625f 2018-10-22 stsp if (s->thread) {
2813 1a76625f 2018-10-22 stsp s->quit = 1;
2814 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2815 1a76625f 2018-10-22 stsp if (errcode)
2816 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2817 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2818 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2819 1a76625f 2018-10-22 stsp if (errcode)
2820 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2821 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2822 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2823 1a76625f 2018-10-22 stsp if (errcode)
2824 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2825 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2826 1a76625f 2018-10-22 stsp if (errcode)
2827 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2828 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2829 1a76625f 2018-10-22 stsp s->thread = NULL;
2832 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2833 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2834 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2837 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2838 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2839 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2840 74467cc8 2022-06-15 stsp if (err == NULL)
2841 74467cc8 2022-06-15 stsp err = pack_err;
2842 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2845 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2846 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2847 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2850 5629093a 2022-07-11 stsp return err ? err : thread_err;
2853 9343a5fb 2018-06-23 stsp static const struct got_error *
2854 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2856 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2857 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2858 276b94a1 2020-11-13 naddy int errcode;
2860 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2862 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2863 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2864 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2866 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2867 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2868 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2870 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2871 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2872 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2873 1a76625f 2018-10-22 stsp free(s->start_id);
2874 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2875 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2876 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2877 1a76625f 2018-10-22 stsp return err;
2880 1a76625f 2018-10-22 stsp static const struct got_error *
2881 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2883 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2885 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2886 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2887 60493ae3 2019-06-20 stsp return NULL;
2890 60493ae3 2019-06-20 stsp static const struct got_error *
2891 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2893 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2894 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2895 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2897 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2898 f9686aa5 2020-03-27 stsp show_log_view(view);
2899 f9686aa5 2020-03-27 stsp update_panels();
2900 f9686aa5 2020-03-27 stsp doupdate();
2902 96e2b566 2019-07-08 stsp if (s->search_entry) {
2903 13add988 2019-10-15 stsp int errcode, ch;
2904 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2905 13add988 2019-10-15 stsp if (errcode)
2906 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2907 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2908 13add988 2019-10-15 stsp ch = wgetch(view->window);
2909 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2910 13add988 2019-10-15 stsp if (errcode)
2911 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2912 13add988 2019-10-15 stsp "pthread_mutex_lock");
2913 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2914 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2915 678cbce5 2019-07-28 stsp return NULL;
2917 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2918 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2920 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2921 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2922 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2923 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2924 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2927 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2928 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2929 f704b35c 2022-06-18 stsp * might have changed.
2931 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2932 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2933 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2935 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2937 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2938 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2939 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2941 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2942 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2945 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2948 60493ae3 2019-06-20 stsp while (1) {
2949 13add988 2019-10-15 stsp int have_match = 0;
2951 60493ae3 2019-06-20 stsp if (entry == NULL) {
2952 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2953 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2954 f9967bca 2020-03-27 stsp view->search_next_done =
2955 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2956 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2957 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2958 f801134a 2019-06-25 stsp return NULL;
2961 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2962 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2963 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2965 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2966 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2969 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2970 13add988 2019-10-15 stsp &view->regex);
2973 13add988 2019-10-15 stsp if (have_match) {
2974 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2975 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2979 96e2b566 2019-07-08 stsp s->search_entry = entry;
2980 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2981 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2983 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2986 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2987 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2988 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2989 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2991 60493ae3 2019-06-20 stsp return err;
2994 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2995 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2997 60493ae3 2019-06-20 stsp return err;
3002 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3004 60493ae3 2019-06-20 stsp return NULL;
3007 60493ae3 2019-06-20 stsp static const struct got_error *
3008 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3009 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3010 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3012 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3013 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3014 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3015 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3016 1a76625f 2018-10-22 stsp int errcode;
3018 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3019 f135c941 2020-02-20 stsp free(s->in_repo_path);
3020 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3021 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3022 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3025 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3026 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3027 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3029 fb2756b9 2018-08-04 stsp s->repo = repo;
3030 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3031 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3032 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3033 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3034 9cd7cbd1 2020-12-07 stsp goto done;
3037 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3038 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3039 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3040 5036bf37 2018-09-24 stsp goto done;
3042 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3044 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3045 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3046 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3047 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3049 11b20872 2019-11-08 stsp goto done;
3050 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3051 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3052 11b20872 2019-11-08 stsp if (err) {
3053 11b20872 2019-11-08 stsp free_colors(&s->colors);
3054 11b20872 2019-11-08 stsp goto done;
3056 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3057 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3058 11b20872 2019-11-08 stsp if (err) {
3059 11b20872 2019-11-08 stsp free_colors(&s->colors);
3060 11b20872 2019-11-08 stsp goto done;
3064 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3065 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3066 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3067 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3068 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3069 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3071 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3072 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3074 74467cc8 2022-06-15 stsp goto done;
3076 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3077 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3078 0ae84acc 2022-06-15 tracey if (err)
3079 0ae84acc 2022-06-15 tracey goto done;
3080 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3081 b672a97a 2020-01-27 stsp !s->log_branches);
3083 1a76625f 2018-10-22 stsp goto done;
3084 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3085 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3087 c5b78334 2020-01-12 stsp goto done;
3089 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3090 1a76625f 2018-10-22 stsp if (errcode) {
3091 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3092 1a76625f 2018-10-22 stsp goto done;
3094 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3095 7c1452c1 2020-03-26 stsp if (errcode) {
3096 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3097 7c1452c1 2020-03-26 stsp goto done;
3100 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3101 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3102 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3103 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3104 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3105 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3106 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3107 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3108 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3109 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3110 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3111 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3112 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3115 1a76625f 2018-10-22 stsp close_log_view(view);
3116 ba4f502b 2018-08-04 stsp return err;
3119 e5a0f69f 2018-08-18 stsp static const struct got_error *
3120 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3122 f2f6d207 2020-11-24 stsp const struct got_error *err;
3123 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3125 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3126 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3127 2b380cc8 2018-10-24 stsp &s->thread_args);
3128 2b380cc8 2018-10-24 stsp if (errcode)
3129 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3130 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3131 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3133 f2f6d207 2020-11-24 stsp return err;
3137 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3140 b880cc75 2022-06-30 mark static void
3141 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3143 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3145 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3146 b880cc75 2022-06-30 mark view->count = 0;
3147 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3150 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3152 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3154 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3155 b880cc75 2022-06-30 mark --s->selected;
3157 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3159 b880cc75 2022-06-30 mark select_commit(s);
3163 b880cc75 2022-06-30 mark static const struct got_error *
3164 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3166 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3167 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3169 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3170 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3171 b880cc75 2022-06-30 mark return NULL;
3173 b880cc75 2022-06-30 mark if (!page) {
3174 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3176 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3177 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3178 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3179 b880cc75 2022-06-30 mark ++s->selected;
3181 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3182 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3183 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3184 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3185 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3187 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3188 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3189 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3191 cbb0c8d7 2022-08-12 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1 &&
3192 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3193 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3194 cbb0c8d7 2022-08-12 mark s->commits.ncommits - s->selected_entry->idx - 1);
3196 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3199 b880cc75 2022-06-30 mark return err;
3202 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3203 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3205 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3206 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3208 b880cc75 2022-06-30 mark select_commit(s);
3210 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3211 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3212 b880cc75 2022-06-30 mark view->count = 0;
3214 b880cc75 2022-06-30 mark return NULL;
3217 9b058f45 2022-06-30 mark static void
3218 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3223 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3224 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3225 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3226 7532ccda 2022-07-11 mark else if (view->resized_y)
3227 7532ccda 2022-07-11 mark *y = view->resized_y;
3229 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3230 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3231 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3232 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3233 7532ccda 2022-07-11 mark else if (view->resized_x)
3234 7532ccda 2022-07-11 mark *x = view->resized_x;
3236 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3240 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3241 e5a0f69f 2018-08-18 stsp static const struct got_error *
3242 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3244 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3246 9b058f45 2022-06-30 mark view->nlines = y;
3247 d2366e29 2022-07-07 mark view->ncols = COLS;
3248 9b058f45 2022-06-30 mark err = view_resize(view);
3250 9b058f45 2022-06-30 mark return err;
3252 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3254 9b058f45 2022-06-30 mark return err;
3257 94b80cfa 2022-08-01 mark static const struct got_error *
3258 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3260 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3261 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3262 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3264 94b80cfa 2022-08-01 mark g = view->gline;
3265 94b80cfa 2022-08-01 mark view->gline = 0;
3267 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3268 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3269 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3270 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3271 94b80cfa 2022-08-01 mark select_commit(s);
3272 94b80cfa 2022-08-01 mark return NULL;
3275 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3276 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3277 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3278 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3279 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3281 94b80cfa 2022-08-01 mark return err;
3282 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3283 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3285 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3286 94b80cfa 2022-08-01 mark s->selected = g - 1;
3288 94b80cfa 2022-08-01 mark select_commit(s);
3289 94b80cfa 2022-08-01 mark return NULL;
3293 9b058f45 2022-06-30 mark static const struct got_error *
3294 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3296 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3297 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3298 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3299 136e2bd4 2022-07-23 mark int eos, n, nscroll;
3301 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3302 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3303 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3304 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3305 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3306 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3308 b880cc75 2022-06-30 mark return err;
3311 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3312 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3313 0dca135e 2022-06-30 mark --eos; /* border */
3315 94b80cfa 2022-08-01 mark if (view->gline)
3316 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3318 528dedf3 2021-08-30 stsp switch (ch) {
3320 1e37a5c2 2019-05-12 jcs s->quit = 1;
3323 145b6838 2022-06-16 stsp view->x = 0;
3326 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3327 640cd7ff 2022-06-22 mark view->count = 0;
3329 145b6838 2022-06-16 stsp case KEY_RIGHT:
3331 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3332 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3334 640cd7ff 2022-06-22 mark view->count = 0;
3336 145b6838 2022-06-16 stsp case KEY_LEFT:
3338 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3339 640cd7ff 2022-06-22 mark if (view->x <= 0)
3340 640cd7ff 2022-06-22 mark view->count = 0;
3343 1e37a5c2 2019-05-12 jcs case KEY_UP:
3346 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3347 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3350 912a3f79 2021-08-30 j case KEY_HOME:
3351 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3352 640cd7ff 2022-06-22 mark view->count = 0;
3354 83cc4199 2022-06-13 stsp case CTRL('u'):
3356 83cc4199 2022-06-13 stsp nscroll /= 2;
3357 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3358 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3359 a4292ac5 2019-05-12 jcs case CTRL('b'):
3361 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3364 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3367 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3368 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3371 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3374 912a3f79 2021-08-30 j case KEY_END: {
3375 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3376 912a3f79 2021-08-30 j * traverse them all. */
3377 640cd7ff 2022-06-22 mark view->count = 0;
3378 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3379 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3380 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3383 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3384 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3385 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3386 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3388 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3389 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3391 f3bc9f1d 2021-09-05 naddy if (n > 0)
3392 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3393 2b779855 2020-12-05 naddy select_commit(s);
3396 80b7e8da 2022-06-11 stsp case CTRL('d'):
3398 83cc4199 2022-06-13 stsp nscroll /= 2;
3399 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3400 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3401 61417565 2022-06-20 mark case CTRL('f'):
3402 48bb96f0 2022-06-20 naddy case 'f':
3404 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3406 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3407 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3408 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3409 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3410 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3411 2b779855 2020-12-05 naddy select_commit(s);
3412 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3413 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3414 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3415 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3416 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3419 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3420 49b24ee5 2022-07-03 mark case '\r':
3421 640cd7ff 2022-06-22 mark view->count = 0;
3422 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3424 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3427 640cd7ff 2022-06-22 mark view->count = 0;
3428 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3430 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3432 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3433 21355643 2020-12-06 stsp case CTRL('l'):
3435 640cd7ff 2022-06-22 mark view->count = 0;
3436 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3437 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3439 21355643 2020-12-06 stsp err = stop_log_thread(s);
3441 74cfe85e 2020-10-20 stsp return err;
3442 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3443 21355643 2020-12-06 stsp char *parent_path;
3444 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3446 21355643 2020-12-06 stsp return err;
3447 21355643 2020-12-06 stsp free(s->in_repo_path);
3448 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3449 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3450 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3451 21355643 2020-12-06 stsp struct got_object_id *start_id;
3452 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3453 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3454 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3456 21355643 2020-12-06 stsp return err;
3457 21355643 2020-12-06 stsp free(s->start_id);
3458 21355643 2020-12-06 stsp s->start_id = start_id;
3459 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3460 21355643 2020-12-06 stsp } else /* 'B' */
3461 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3463 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3464 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3466 b0dd8d27 2022-06-16 stsp return err;
3468 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3469 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3470 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3472 21355643 2020-12-06 stsp return err;
3473 51a10b52 2020-12-26 stsp tog_free_refs();
3474 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3476 ca51c541 2020-12-07 stsp return err;
3477 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3478 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3480 d01904d4 2019-06-25 stsp return err;
3481 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3482 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3484 b672a97a 2020-01-27 stsp return err;
3485 21355643 2020-12-06 stsp free_commits(&s->commits);
3486 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3487 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3488 21355643 2020-12-06 stsp s->selected_entry = NULL;
3489 21355643 2020-12-06 stsp s->selected = 0;
3490 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3491 21355643 2020-12-06 stsp s->quit = 0;
3492 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3493 dfee752e 2022-06-18 op s->matched_entry = NULL;
3494 dfee752e 2022-06-18 op s->search_entry = NULL;
3495 01a7bcaf 2022-07-22 mark view->offset = 0;
3498 640cd7ff 2022-06-22 mark view->count = 0;
3499 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3502 640cd7ff 2022-06-22 mark view->count = 0;
3506 80ddbec8 2018-04-29 stsp return err;
3509 4ed7e80c 2018-05-20 stsp static const struct got_error *
3510 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3512 c2db6724 2019-01-04 stsp const struct got_error *error;
3514 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3515 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3516 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3518 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3519 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3521 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3522 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3524 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3525 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3527 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3528 c2db6724 2019-01-04 stsp if (error != NULL)
3529 c2db6724 2019-01-04 stsp return error;
3531 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3532 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3534 c2db6724 2019-01-04 stsp return NULL;
3537 a915003a 2019-02-05 stsp static void
3538 a915003a 2019-02-05 stsp init_curses(void)
3541 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3542 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3543 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3545 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3546 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3547 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3548 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3549 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3551 a915003a 2019-02-05 stsp initscr();
3553 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3556 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3557 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3558 a915003a 2019-02-05 stsp curs_set(0);
3559 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3560 6d17833f 2019-11-08 stsp start_color();
3561 6d17833f 2019-11-08 stsp use_default_colors();
3565 c2db6724 2019-01-04 stsp static const struct got_error *
3566 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3567 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3569 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3571 f135c941 2020-02-20 stsp if (argc == 0) {
3572 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3573 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3574 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3575 f135c941 2020-02-20 stsp return NULL;
3578 f135c941 2020-02-20 stsp if (worktree) {
3579 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3582 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3584 f135c941 2020-02-20 stsp return err;
3585 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3586 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3587 bfd61697 2020-11-14 stsp p) == -1) {
3588 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3589 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3593 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3595 f135c941 2020-02-20 stsp return err;
3598 f135c941 2020-02-20 stsp static const struct got_error *
3599 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3601 80ddbec8 2018-04-29 stsp const struct got_error *error;
3602 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3603 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3604 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3605 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3606 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3607 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3608 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3609 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3610 04cc582a 2018-08-01 stsp struct tog_view *view;
3611 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3613 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3614 80ddbec8 2018-04-29 stsp switch (ch) {
3616 b672a97a 2020-01-27 stsp log_branches = 1;
3619 80ddbec8 2018-04-29 stsp start_commit = optarg;
3622 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3623 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3624 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3628 17020d27 2019-03-07 stsp usage_log();
3629 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3633 80ddbec8 2018-04-29 stsp argc -= optind;
3634 80ddbec8 2018-04-29 stsp argv += optind;
3636 f135c941 2020-02-20 stsp if (argc > 1)
3637 f135c941 2020-02-20 stsp usage_log();
3639 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3640 0ae84acc 2022-06-15 tracey if (error != NULL)
3641 0ae84acc 2022-06-15 tracey goto done;
3643 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3644 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3645 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3646 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3647 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3648 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3649 c156c7a4 2020-12-18 stsp goto done;
3650 a1fbf39a 2019-08-11 stsp if (worktree)
3651 6962eb72 2020-02-20 stsp repo_path =
3652 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3654 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3655 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3656 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3657 c156c7a4 2020-12-18 stsp goto done;
3661 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3662 80ddbec8 2018-04-29 stsp if (error != NULL)
3663 ecb28ae0 2018-07-16 stsp goto done;
3665 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3666 f135c941 2020-02-20 stsp repo, worktree);
3667 f135c941 2020-02-20 stsp if (error)
3668 f135c941 2020-02-20 stsp goto done;
3670 f135c941 2020-02-20 stsp init_curses();
3672 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3673 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3674 c02c541e 2019-03-29 stsp if (error)
3675 c02c541e 2019-03-29 stsp goto done;
3677 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3678 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3679 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3680 87670572 2020-12-26 naddy if (error)
3681 87670572 2020-12-26 naddy goto done;
3684 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3685 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3686 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3687 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3688 d8f38dc4 2020-12-05 stsp if (error)
3689 d8f38dc4 2020-12-05 stsp goto done;
3690 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3692 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3693 d8f38dc4 2020-12-05 stsp if (error == NULL)
3694 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3695 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3696 d8f38dc4 2020-12-05 stsp goto done;
3697 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3698 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3699 d8f38dc4 2020-12-05 stsp if (error)
3700 d8f38dc4 2020-12-05 stsp goto done;
3703 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3704 04cc582a 2018-08-01 stsp if (view == NULL) {
3705 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3706 04cc582a 2018-08-01 stsp goto done;
3708 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3709 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3710 ba4f502b 2018-08-04 stsp if (error)
3711 ba4f502b 2018-08-04 stsp goto done;
3712 2fc00ff4 2019-08-31 stsp if (worktree) {
3713 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3714 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3715 2fc00ff4 2019-08-31 stsp worktree = NULL;
3717 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3719 f135c941 2020-02-20 stsp free(in_repo_path);
3720 ecb28ae0 2018-07-16 stsp free(repo_path);
3721 ecb28ae0 2018-07-16 stsp free(cwd);
3722 899d86c2 2018-05-10 stsp free(start_id);
3723 d8f38dc4 2020-12-05 stsp free(label);
3725 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3726 1d0f4054 2021-06-17 stsp if (repo) {
3727 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3728 1d0f4054 2021-06-17 stsp if (error == NULL)
3729 1d0f4054 2021-06-17 stsp error = close_err;
3731 ec142235 2019-03-07 stsp if (worktree)
3732 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3733 0ae84acc 2022-06-15 tracey if (pack_fds) {
3734 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3735 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3736 0ae84acc 2022-06-15 tracey if (error == NULL)
3737 0ae84acc 2022-06-15 tracey error = pack_err;
3739 51a10b52 2020-12-26 stsp tog_free_refs();
3740 80ddbec8 2018-04-29 stsp return error;
3743 4ed7e80c 2018-05-20 stsp __dead static void
3744 9f7d7167 2018-04-29 stsp usage_diff(void)
3747 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3748 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3752 6d17833f 2019-11-08 stsp static int
3753 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3754 41605754 2020-11-12 stsp regmatch_t *regmatch)
3756 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3759 336075a4 2022-06-25 op static struct tog_color *
3760 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3762 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3764 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3765 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3766 6d17833f 2019-11-08 stsp return tc;
3769 6d17833f 2019-11-08 stsp return NULL;
3772 4ed7e80c 2018-05-20 stsp static const struct got_error *
3773 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3774 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3776 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3777 44a87665 2022-06-16 stsp char *exstr = NULL;
3778 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3779 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3780 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3781 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3783 41605754 2020-11-12 stsp *wtotal = 0;
3785 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3786 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3788 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3790 44a87665 2022-06-16 stsp return err;
3792 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3793 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3794 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3795 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3796 44a87665 2022-06-16 stsp goto done;
3798 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3799 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3800 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3801 1853e0f4 2022-06-16 stsp goto done;
3803 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3804 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3805 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3806 1853e0f4 2022-06-16 stsp goto done;
3809 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3810 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3811 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3813 1853e0f4 2022-06-16 stsp goto done;
3814 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3816 1853e0f4 2022-06-16 stsp free(wline);
3817 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3818 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3820 1853e0f4 2022-06-16 stsp goto done;
3821 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3822 1853e0f4 2022-06-16 stsp wlimit -= width;
3823 1853e0f4 2022-06-16 stsp *wtotal += width;
3826 41605754 2020-11-12 stsp if (wlimit > 0) {
3827 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3828 1853e0f4 2022-06-16 stsp size_t wlen;
3830 1853e0f4 2022-06-16 stsp free(wline);
3831 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3832 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3834 1853e0f4 2022-06-16 stsp goto done;
3835 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3836 1853e0f4 2022-06-16 stsp while (i < wlen) {
3837 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3838 1853e0f4 2022-06-16 stsp if (width == -1) {
3839 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3840 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3841 1853e0f4 2022-06-16 stsp goto done;
3843 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3845 61417565 2022-06-20 mark w += width;
3848 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3849 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3850 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3851 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3852 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3853 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3854 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3858 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3859 1853e0f4 2022-06-16 stsp free(wline);
3860 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3861 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3862 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3863 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3865 1853e0f4 2022-06-16 stsp goto done;
3866 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3868 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3869 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3871 1853e0f4 2022-06-16 stsp goto done;
3872 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3874 1853e0f4 2022-06-16 stsp *wtotal += width2;
3877 145b6838 2022-06-16 stsp free(wline);
3878 44a87665 2022-06-16 stsp free(exstr);
3879 1853e0f4 2022-06-16 stsp free(seg0);
3880 1853e0f4 2022-06-16 stsp free(seg1);
3881 1853e0f4 2022-06-16 stsp free(seg2);
3882 1853e0f4 2022-06-16 stsp return err;
3885 94b80cfa 2022-08-01 mark static int
3886 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
3888 94b80cfa 2022-08-01 mark FILE *f = NULL;
3889 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
3891 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
3892 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
3894 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
3895 94b80cfa 2022-08-01 mark selected = first;
3896 94b80cfa 2022-08-01 mark eof = &s->eof;
3898 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
3899 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
3901 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
3902 94b80cfa 2022-08-01 mark selected = &s->selected_line;
3903 94b80cfa 2022-08-01 mark eof = &s->eof;
3904 94b80cfa 2022-08-01 mark f = s->blame.f;
3908 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
3909 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
3911 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3912 94b80cfa 2022-08-01 mark rewind(f);
3914 94b80cfa 2022-08-01 mark *first = 1;
3915 94b80cfa 2022-08-01 mark *lineno = 0;
3916 94b80cfa 2022-08-01 mark *nprinted = 0;
3920 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
3921 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
3922 94b80cfa 2022-08-01 mark view->gline = 0;
3927 41605754 2020-11-12 stsp static const struct got_error *
3928 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3930 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3931 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3932 61e69b96 2018-05-20 stsp const struct got_error *err;
3933 c7d5c43c 2022-08-04 mark int nprinted = 0;
3934 b304db33 2018-05-20 stsp char *line;
3935 826082fe 2020-12-10 stsp size_t linesize = 0;
3936 826082fe 2020-12-10 stsp ssize_t linelen;
3937 61e69b96 2018-05-20 stsp wchar_t *wline;
3938 e0b650dd 2018-05-20 stsp int width;
3939 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3940 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3941 fe621944 2020-11-10 stsp off_t line_offset;
3943 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
3944 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
3945 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3946 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3948 f7d12f7e 2018-08-01 stsp werase(view->window);
3950 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
3951 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
3953 a3404814 2018-09-02 stsp if (header) {
3954 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3955 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
3956 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
3958 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3959 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3960 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3962 135a2da0 2020-11-11 stsp free(line);
3964 a3404814 2018-09-02 stsp return err;
3966 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3967 a3404814 2018-09-02 stsp wstandout(view->window);
3968 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3969 e54cc94a 2020-11-11 stsp free(wline);
3970 e54cc94a 2020-11-11 stsp wline = NULL;
3971 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3972 a3404814 2018-09-02 stsp wstandend(view->window);
3973 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3974 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3976 a3404814 2018-09-02 stsp if (max_lines <= 1)
3977 a3404814 2018-09-02 stsp return NULL;
3978 a3404814 2018-09-02 stsp max_lines--;
3981 89f1a395 2020-12-01 naddy s->eof = 0;
3982 145b6838 2022-06-16 stsp view->maxx = 0;
3983 826082fe 2020-12-10 stsp line = NULL;
3984 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3985 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
3986 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
3988 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3989 826082fe 2020-12-10 stsp if (linelen == -1) {
3990 826082fe 2020-12-10 stsp if (feof(s->f)) {
3991 826082fe 2020-12-10 stsp s->eof = 1;
3994 826082fe 2020-12-10 stsp free(line);
3995 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3998 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4000 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4002 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4003 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4005 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4006 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4007 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4008 1853e0f4 2022-06-16 stsp if (err) {
4009 1853e0f4 2022-06-16 stsp free(line);
4010 1853e0f4 2022-06-16 stsp return err;
4012 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4013 1853e0f4 2022-06-16 stsp free(wline);
4014 1853e0f4 2022-06-16 stsp wline = NULL;
4016 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4017 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4018 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4019 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4021 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4022 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4023 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4024 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4025 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4026 41605754 2020-11-12 stsp if (err) {
4027 41605754 2020-11-12 stsp free(line);
4028 41605754 2020-11-12 stsp return err;
4032 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4033 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4034 969c159c 2022-06-16 stsp if (err) {
4035 969c159c 2022-06-16 stsp free(line);
4036 969c159c 2022-06-16 stsp return err;
4038 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4039 969c159c 2022-06-16 stsp free(wline);
4040 41605754 2020-11-12 stsp wline = NULL;
4042 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4043 94b80cfa 2022-08-01 mark /* highlight full gline length */
4044 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4045 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4047 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4048 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4051 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4052 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4053 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4055 826082fe 2020-12-10 stsp free(line);
4056 fe621944 2020-11-10 stsp if (nprinted >= 1)
4057 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4058 89f1a395 2020-12-01 naddy (nprinted - 1);
4060 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4062 9b058f45 2022-06-30 mark view_border(view);
4064 89f1a395 2020-12-01 naddy if (s->eof) {
4065 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4066 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4067 c3e9aa98 2019-05-13 jcs nprinted++;
4070 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4071 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4073 c3e9aa98 2019-05-13 jcs return err;
4076 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4077 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4078 e54cc94a 2020-11-11 stsp free(wline);
4079 e54cc94a 2020-11-11 stsp wline = NULL;
4080 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4083 26ed57b2 2018-05-19 stsp return NULL;
4086 abd2672a 2018-12-23 stsp static char *
4087 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4089 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4090 09867e48 2019-08-13 stsp char *p, *s;
4092 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4093 09867e48 2019-08-13 stsp if (tm == NULL)
4094 09867e48 2019-08-13 stsp return NULL;
4095 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4096 09867e48 2019-08-13 stsp if (s == NULL)
4097 09867e48 2019-08-13 stsp return NULL;
4098 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4100 abd2672a 2018-12-23 stsp *p = '\0';
4104 4ed7e80c 2018-05-20 stsp static const struct got_error *
4105 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4106 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4108 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4109 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4110 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4111 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4113 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4114 0208f208 2020-05-05 stsp if (qid != NULL) {
4115 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4116 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4117 d7b5a0e8 2022-04-20 stsp &qid->id);
4119 0208f208 2020-05-05 stsp return err;
4121 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4122 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4123 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4124 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4125 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4127 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4131 0208f208 2020-05-05 stsp if (tree_id1) {
4132 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4134 0208f208 2020-05-05 stsp goto done;
4137 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4138 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4140 0208f208 2020-05-05 stsp goto done;
4142 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4143 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4145 0208f208 2020-05-05 stsp if (tree1)
4146 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4147 0208f208 2020-05-05 stsp if (tree2)
4148 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4149 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4150 0208f208 2020-05-05 stsp return err;
4153 0208f208 2020-05-05 stsp static const struct got_error *
4154 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4155 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4157 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4159 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4160 fe621944 2020-11-10 stsp if (p == NULL)
4161 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4162 c7d5c43c 2022-08-04 mark *lines = p;
4163 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4164 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4165 fe621944 2020-11-10 stsp (*nlines)++;
4167 fe621944 2020-11-10 stsp return NULL;
4170 fe621944 2020-11-10 stsp static const struct got_error *
4171 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4172 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4173 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4175 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4176 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4177 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4178 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4179 45d799e2 2018-12-23 stsp time_t committer_time;
4180 45d799e2 2018-12-23 stsp const char *author, *committer;
4181 8b473291 2019-02-21 stsp char *refs_str = NULL;
4182 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4183 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4184 fe621944 2020-11-10 stsp off_t outoff = 0;
4187 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4189 8b473291 2019-02-21 stsp if (refs) {
4190 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4192 8b473291 2019-02-21 stsp return err;
4195 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4197 abd2672a 2018-12-23 stsp return err;
4199 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4200 15a94983 2018-12-23 stsp if (err) {
4201 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4202 15a94983 2018-12-23 stsp goto done;
4205 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4207 fe621944 2020-11-10 stsp goto done;
4209 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4210 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4211 fe621944 2020-11-10 stsp if (n < 0) {
4212 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4213 abd2672a 2018-12-23 stsp goto done;
4215 fe621944 2020-11-10 stsp outoff += n;
4216 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4218 fe621944 2020-11-10 stsp goto done;
4220 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4221 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4222 fe621944 2020-11-10 stsp if (n < 0) {
4223 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4224 abd2672a 2018-12-23 stsp goto done;
4226 fe621944 2020-11-10 stsp outoff += n;
4227 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4229 fe621944 2020-11-10 stsp goto done;
4231 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4232 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4233 fe621944 2020-11-10 stsp if (datestr) {
4234 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4235 fe621944 2020-11-10 stsp if (n < 0) {
4236 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4237 fe621944 2020-11-10 stsp goto done;
4239 fe621944 2020-11-10 stsp outoff += n;
4240 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4241 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_DATE);
4243 fe621944 2020-11-10 stsp goto done;
4245 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4246 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4247 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4248 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4249 fe621944 2020-11-10 stsp if (n < 0) {
4250 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4251 fe621944 2020-11-10 stsp goto done;
4253 fe621944 2020-11-10 stsp outoff += n;
4254 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4255 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_AUTHOR);
4257 fe621944 2020-11-10 stsp goto done;
4259 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4260 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4261 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4262 9f98ca05 2021-09-24 stsp int pn = 1;
4263 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4264 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4265 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4267 9f98ca05 2021-09-24 stsp goto done;
4268 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4269 9f98ca05 2021-09-24 stsp if (n < 0) {
4270 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4271 9f98ca05 2021-09-24 stsp goto done;
4273 9f98ca05 2021-09-24 stsp outoff += n;
4274 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4275 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4277 9f98ca05 2021-09-24 stsp goto done;
4278 9f98ca05 2021-09-24 stsp free(id_str);
4279 9f98ca05 2021-09-24 stsp id_str = NULL;
4283 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4285 5943eee2 2019-08-13 stsp goto done;
4286 fe621944 2020-11-10 stsp s = logmsg;
4287 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4288 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4289 fe621944 2020-11-10 stsp if (n < 0) {
4290 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4291 fe621944 2020-11-10 stsp goto done;
4293 fe621944 2020-11-10 stsp outoff += n;
4294 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4295 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4297 fe621944 2020-11-10 stsp goto done;
4300 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4302 0208f208 2020-05-05 stsp goto done;
4303 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4304 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4305 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4306 fe621944 2020-11-10 stsp if (n < 0) {
4307 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4308 fe621944 2020-11-10 stsp goto done;
4310 fe621944 2020-11-10 stsp outoff += n;
4311 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4312 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4314 fe621944 2020-11-10 stsp goto done;
4315 0208f208 2020-05-05 stsp free((char *)pe->path);
4316 0208f208 2020-05-05 stsp free(pe->data);
4319 0208f208 2020-05-05 stsp fputc('\n', outfile);
4321 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4323 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4324 abd2672a 2018-12-23 stsp free(id_str);
4325 5943eee2 2019-08-13 stsp free(logmsg);
4326 8b473291 2019-02-21 stsp free(refs_str);
4327 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4328 7510f233 2020-08-09 stsp if (err) {
4329 c7d5c43c 2022-08-04 mark free(*lines);
4330 c7d5c43c 2022-08-04 mark *lines = NULL;
4331 ae6a6978 2020-08-09 stsp *nlines = 0;
4333 fe621944 2020-11-10 stsp return err;
4336 abd2672a 2018-12-23 stsp static const struct got_error *
4337 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4339 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4340 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4341 15a94983 2018-12-23 stsp int obj_type;
4343 c7d5c43c 2022-08-04 mark free(s->lines);
4344 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4345 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4346 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4347 fe621944 2020-11-10 stsp s->nlines = 0;
4349 511a516b 2018-05-19 stsp f = got_opentemp();
4350 48ae06ee 2018-10-18 stsp if (f == NULL) {
4351 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4352 48ae06ee 2018-10-18 stsp goto done;
4354 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4355 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4356 fb43ecf1 2019-02-11 stsp goto done;
4360 15a94983 2018-12-23 stsp if (s->id1)
4361 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4363 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4365 15a94983 2018-12-23 stsp goto done;
4367 15a94983 2018-12-23 stsp switch (obj_type) {
4368 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4369 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4370 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4371 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4372 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4374 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4375 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4376 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4377 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4378 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4380 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4381 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4382 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4383 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4384 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4386 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4388 3ffacbe1 2020-02-02 tracey goto done;
4389 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4390 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4391 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4392 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4393 c7d5c43c 2022-08-04 mark refs, s->repo, s->f);
4394 f44b1f58 2020-02-02 tracey if (err)
4395 f44b1f58 2020-02-02 tracey goto done;
4396 f44b1f58 2020-02-02 tracey } else {
4397 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4398 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4399 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4400 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4401 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4403 f44b1f58 2020-02-02 tracey if (err)
4404 f44b1f58 2020-02-02 tracey goto done;
4409 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4411 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4412 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4413 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4414 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4418 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4422 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4423 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4424 48ae06ee 2018-10-18 stsp return err;
4427 f5215bb9 2019-02-22 stsp static void
4428 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4430 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4431 f5215bb9 2019-02-22 stsp update_panels();
4432 f5215bb9 2019-02-22 stsp doupdate();
4435 f44b1f58 2020-02-02 tracey static const struct got_error *
4436 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4438 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4440 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4441 f44b1f58 2020-02-02 tracey return NULL;
4444 f44b1f58 2020-02-02 tracey static const struct got_error *
4445 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4447 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4448 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4449 f44b1f58 2020-02-02 tracey int lineno;
4450 cb713507 2022-06-16 stsp char *line = NULL;
4451 826082fe 2020-12-10 stsp size_t linesize = 0;
4452 826082fe 2020-12-10 stsp ssize_t linelen;
4454 f44b1f58 2020-02-02 tracey if (!view->searching) {
4455 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4456 f44b1f58 2020-02-02 tracey return NULL;
4459 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4460 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4461 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4463 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4465 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4467 f44b1f58 2020-02-02 tracey while (1) {
4468 f44b1f58 2020-02-02 tracey off_t offset;
4470 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4471 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4472 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4476 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4477 f44b1f58 2020-02-02 tracey lineno = 1;
4479 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4482 c7d5c43c 2022-08-04 mark offset = s->lines[lineno - 1].offset;
4483 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4484 f44b1f58 2020-02-02 tracey free(line);
4485 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4487 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4488 cb713507 2022-06-16 stsp if (linelen != -1) {
4489 cb713507 2022-06-16 stsp char *exstr;
4490 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4493 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4494 cb713507 2022-06-16 stsp &view->regmatch)) {
4495 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4496 cb713507 2022-06-16 stsp s->matched_line = lineno;
4497 cb713507 2022-06-16 stsp free(exstr);
4500 cb713507 2022-06-16 stsp free(exstr);
4502 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4503 f44b1f58 2020-02-02 tracey lineno++;
4505 f44b1f58 2020-02-02 tracey lineno--;
4507 826082fe 2020-12-10 stsp free(line);
4509 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4510 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4511 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4514 145b6838 2022-06-16 stsp return err;
4517 48ae06ee 2018-10-18 stsp static const struct got_error *
4518 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4520 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4521 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4523 b72706c3 2022-06-01 stsp free(s->id1);
4524 b72706c3 2022-06-01 stsp s->id1 = NULL;
4525 b72706c3 2022-06-01 stsp free(s->id2);
4526 b72706c3 2022-06-01 stsp s->id2 = NULL;
4527 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4528 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4529 b72706c3 2022-06-01 stsp s->f = NULL;
4530 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4531 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4532 b72706c3 2022-06-01 stsp s->f1 = NULL;
4533 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4534 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4535 b72706c3 2022-06-01 stsp s->f2 = NULL;
4536 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4537 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4538 f9d37699 2022-06-28 stsp s->fd1 = -1;
4539 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4540 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4541 f9d37699 2022-06-28 stsp s->fd2 = -1;
4542 c7d5c43c 2022-08-04 mark free(s->lines);
4543 c7d5c43c 2022-08-04 mark s->lines = NULL;
4544 b72706c3 2022-06-01 stsp s->nlines = 0;
4545 b72706c3 2022-06-01 stsp return err;
4548 b72706c3 2022-06-01 stsp static const struct got_error *
4549 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4550 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4551 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4552 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4554 48ae06ee 2018-10-18 stsp const struct got_error *err;
4555 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4557 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4558 f9d37699 2022-06-28 stsp s->fd1 = -1;
4559 f9d37699 2022-06-28 stsp s->fd2 = -1;
4561 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4562 15a94983 2018-12-23 stsp int type1, type2;
4563 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4565 15a94983 2018-12-23 stsp return err;
4566 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4568 15a94983 2018-12-23 stsp return err;
4570 15a94983 2018-12-23 stsp if (type1 != type2)
4571 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4573 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4574 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4575 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4576 f44b1f58 2020-02-02 tracey s->repo = repo;
4577 f44b1f58 2020-02-02 tracey s->id1 = id1;
4578 f44b1f58 2020-02-02 tracey s->id2 = id2;
4579 3dbaef42 2020-11-24 stsp s->label1 = label1;
4580 3dbaef42 2020-11-24 stsp s->label2 = label2;
4582 15a94983 2018-12-23 stsp if (id1) {
4583 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4584 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4585 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4587 5465d566 2020-02-01 tracey s->id1 = NULL;
4589 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4590 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4591 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4592 b72706c3 2022-06-01 stsp goto done;
4595 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4596 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4597 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4598 a00719e9 2022-06-17 stsp goto done;
4601 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4602 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4603 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4604 b72706c3 2022-06-01 stsp goto done;
4607 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4608 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4609 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4610 f9d37699 2022-06-28 stsp goto done;
4613 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4614 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4615 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4616 f9d37699 2022-06-28 stsp goto done;
4619 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4620 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4621 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4622 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4623 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4624 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4625 5465d566 2020-02-01 tracey s->repo = repo;
4627 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4630 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
4631 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4632 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4633 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
4634 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4635 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4636 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
4637 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4638 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4639 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
4640 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4641 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4642 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
4643 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4644 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4645 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4646 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4647 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4648 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4649 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4650 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4651 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4652 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
4653 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4654 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
4655 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
4656 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
4657 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
4658 b72706c3 2022-06-01 stsp goto done;
4662 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4663 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4664 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4665 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4667 5465d566 2020-02-01 tracey err = create_diff(s);
4669 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4670 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4671 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4672 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4673 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4674 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4677 b72706c3 2022-06-01 stsp close_diff_view(view);
4678 e5a0f69f 2018-08-18 stsp return err;
4681 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4682 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4684 a3404814 2018-09-02 stsp const struct got_error *err;
4685 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4686 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4687 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4689 a3404814 2018-09-02 stsp if (s->id1) {
4690 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4692 a3404814 2018-09-02 stsp return err;
4693 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4695 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4697 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4699 a3404814 2018-09-02 stsp return err;
4700 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4702 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4703 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4704 a3404814 2018-09-02 stsp free(id_str1);
4705 a3404814 2018-09-02 stsp free(id_str2);
4706 a3404814 2018-09-02 stsp return err;
4708 a3404814 2018-09-02 stsp free(id_str1);
4709 a3404814 2018-09-02 stsp free(id_str2);
4711 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4712 267bb3b8 2021-08-01 stsp free(header);
4713 267bb3b8 2021-08-01 stsp return err;
4716 15a087fe 2019-02-21 stsp static const struct got_error *
4717 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4718 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4720 d7a04538 2019-02-21 stsp const struct got_error *err;
4721 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4722 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4723 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4725 15a087fe 2019-02-21 stsp free(s->id2);
4726 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4727 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4728 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4730 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4732 d7a04538 2019-02-21 stsp return err;
4733 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4734 15a087fe 2019-02-21 stsp free(s->id1);
4735 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4736 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4737 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4738 15a087fe 2019-02-21 stsp return NULL;
4741 0cf4efb1 2018-09-29 stsp static const struct got_error *
4742 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4744 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4746 917d79a7 2022-07-01 stsp view->count = 0;
4747 917d79a7 2022-07-01 stsp wclear(view->window);
4748 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4749 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4750 917d79a7 2022-07-01 stsp s->matched_line = 0;
4751 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4752 917d79a7 2022-07-01 stsp return create_diff(s);
4755 c7d5c43c 2022-08-04 mark static void
4756 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4758 c7d5c43c 2022-08-04 mark int start, i;
4760 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
4762 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
4763 c7d5c43c 2022-08-04 mark if (i == 0)
4764 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
4765 c7d5c43c 2022-08-04 mark if (--i == start)
4766 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
4769 c7d5c43c 2022-08-04 mark s->selected_line = 1;
4770 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
4773 c7d5c43c 2022-08-04 mark static void
4774 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4776 c7d5c43c 2022-08-04 mark int start, i;
4778 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
4780 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
4781 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
4783 c7d5c43c 2022-08-04 mark if (++i == start)
4784 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
4787 c7d5c43c 2022-08-04 mark s->selected_line = 1;
4788 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
4791 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4792 c0f61fa4 2022-07-11 mark int, int, int);
4793 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4794 c0f61fa4 2022-07-11 mark int, int);
4796 917d79a7 2022-07-01 stsp static const struct got_error *
4797 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4799 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4800 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4801 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4802 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4803 826082fe 2020-12-10 stsp char *line = NULL;
4804 826082fe 2020-12-10 stsp size_t linesize = 0;
4805 826082fe 2020-12-10 stsp ssize_t linelen;
4806 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4808 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
4810 e5a0f69f 2018-08-18 stsp switch (ch) {
4812 145b6838 2022-06-16 stsp view->x = 0;
4815 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4816 640cd7ff 2022-06-22 mark view->count = 0;
4818 145b6838 2022-06-16 stsp case KEY_RIGHT:
4820 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4821 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4823 640cd7ff 2022-06-22 mark view->count = 0;
4825 145b6838 2022-06-16 stsp case KEY_LEFT:
4827 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4828 640cd7ff 2022-06-22 mark if (view->x <= 0)
4829 640cd7ff 2022-06-22 mark view->count = 0;
4833 3dbaef42 2020-11-24 stsp if (ch == 'a')
4834 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4835 3dbaef42 2020-11-24 stsp if (ch == 'w')
4836 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4837 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4840 912a3f79 2021-08-30 j case KEY_HOME:
4841 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4842 640cd7ff 2022-06-22 mark view->count = 0;
4845 912a3f79 2021-08-30 j case KEY_END:
4846 640cd7ff 2022-06-22 mark view->count = 0;
4850 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4854 1e37a5c2 2019-05-12 jcs case KEY_UP:
4855 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4856 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4857 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4859 640cd7ff 2022-06-22 mark view->count = 0;
4861 83cc4199 2022-06-13 stsp case CTRL('u'):
4863 83cc4199 2022-06-13 stsp nscroll /= 2;
4864 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4865 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4866 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4868 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4869 640cd7ff 2022-06-22 mark view->count = 0;
4873 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4874 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4877 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4878 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4879 1e37a5c2 2019-05-12 jcs if (!s->eof)
4880 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4882 640cd7ff 2022-06-22 mark view->count = 0;
4884 83cc4199 2022-06-13 stsp case CTRL('d'):
4886 83cc4199 2022-06-13 stsp nscroll /= 2;
4887 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4888 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4889 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4892 640cd7ff 2022-06-22 mark if (s->eof) {
4893 640cd7ff 2022-06-22 mark view->count = 0;
4897 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4898 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4899 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4900 826082fe 2020-12-10 stsp if (linelen == -1) {
4901 826082fe 2020-12-10 stsp if (feof(s->f)) {
4902 826082fe 2020-12-10 stsp s->eof = 1;
4904 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4908 826082fe 2020-12-10 stsp free(line);
4911 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4914 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4917 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4920 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
4923 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4924 1e37a5c2 2019-05-12 jcs s->diff_context--;
4925 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4926 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4927 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4928 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4929 27829c9e 2020-11-21 stsp s->nlines) {
4930 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4931 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4934 640cd7ff 2022-06-22 mark view->count = 0;
4937 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4938 1e37a5c2 2019-05-12 jcs s->diff_context++;
4939 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4940 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4941 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4943 640cd7ff 2022-06-22 mark view->count = 0;
4949 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4953 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4954 640cd7ff 2022-06-22 mark view->count = 0;
4957 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4959 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4960 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4961 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4963 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4964 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4967 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4969 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4972 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4975 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4976 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4977 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4979 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4980 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4981 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4983 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4984 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4987 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4989 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4991 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4992 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4993 c0f61fa4 2022-07-11 mark bs->selected_line);
4994 c0f61fa4 2022-07-11 mark if (id == NULL)
4997 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5000 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5004 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5005 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5006 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5007 145b6838 2022-06-16 stsp view->x = 0;
5009 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5010 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5013 640cd7ff 2022-06-22 mark view->count = 0;
5017 bcbd79e2 2018-08-19 stsp return err;
5020 4ed7e80c 2018-05-20 stsp static const struct got_error *
5021 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5023 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5024 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5025 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5026 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5027 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5028 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5029 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5030 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5031 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5032 3dbaef42 2020-11-24 stsp const char *errstr;
5033 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5034 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5036 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5037 26ed57b2 2018-05-19 stsp switch (ch) {
5039 64453f7e 2020-11-21 stsp force_text_diff = 1;
5042 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5044 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5045 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5046 5a20d08d 2022-02-09 op errstr, errstr);
5048 09b5bff8 2020-02-23 naddy case 'r':
5049 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5050 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5051 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5053 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5056 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5059 17020d27 2019-03-07 stsp usage_diff();
5060 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5064 26ed57b2 2018-05-19 stsp argc -= optind;
5065 26ed57b2 2018-05-19 stsp argv += optind;
5067 26ed57b2 2018-05-19 stsp if (argc == 0) {
5068 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5069 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5070 15a94983 2018-12-23 stsp id_str1 = argv[0];
5071 15a94983 2018-12-23 stsp id_str2 = argv[1];
5073 26ed57b2 2018-05-19 stsp usage_diff();
5075 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5076 0ae84acc 2022-06-15 tracey if (error)
5077 0ae84acc 2022-06-15 tracey goto done;
5079 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5080 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5081 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5082 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5083 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5084 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5085 c156c7a4 2020-12-18 stsp goto done;
5086 a273ac94 2020-02-23 naddy if (worktree)
5087 a273ac94 2020-02-23 naddy repo_path =
5088 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5090 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5091 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5092 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5093 c156c7a4 2020-12-18 stsp goto done;
5097 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5098 eb6600df 2019-01-04 stsp if (error)
5099 eb6600df 2019-01-04 stsp goto done;
5101 a273ac94 2020-02-23 naddy init_curses();
5103 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5104 51a10b52 2020-12-26 stsp if (error)
5105 51a10b52 2020-12-26 stsp goto done;
5107 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5108 26ed57b2 2018-05-19 stsp if (error)
5109 26ed57b2 2018-05-19 stsp goto done;
5111 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5112 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5113 26ed57b2 2018-05-19 stsp if (error)
5114 26ed57b2 2018-05-19 stsp goto done;
5116 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5117 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5118 26ed57b2 2018-05-19 stsp if (error)
5119 26ed57b2 2018-05-19 stsp goto done;
5121 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5122 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5123 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5124 ea5e7bb5 2018-08-01 stsp goto done;
5126 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5127 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5128 5dc9f4bc 2018-08-04 stsp if (error)
5129 5dc9f4bc 2018-08-04 stsp goto done;
5130 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5132 3dbaef42 2020-11-24 stsp free(label1);
5133 3dbaef42 2020-11-24 stsp free(label2);
5134 c02c541e 2019-03-29 stsp free(repo_path);
5135 a273ac94 2020-02-23 naddy free(cwd);
5136 1d0f4054 2021-06-17 stsp if (repo) {
5137 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5138 1d0f4054 2021-06-17 stsp if (error == NULL)
5139 1d0f4054 2021-06-17 stsp error = close_err;
5141 a273ac94 2020-02-23 naddy if (worktree)
5142 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5143 0ae84acc 2022-06-15 tracey if (pack_fds) {
5144 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5145 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5146 0ae84acc 2022-06-15 tracey if (error == NULL)
5147 0ae84acc 2022-06-15 tracey error = pack_err;
5149 51a10b52 2020-12-26 stsp tog_free_refs();
5150 26ed57b2 2018-05-19 stsp return error;
5153 4ed7e80c 2018-05-20 stsp __dead static void
5154 9f7d7167 2018-04-29 stsp usage_blame(void)
5157 87411fa9 2022-06-16 stsp fprintf(stderr,
5158 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5159 9f7d7167 2018-04-29 stsp getprogname());
5163 84451b3e 2018-07-10 stsp struct tog_blame_line {
5164 84451b3e 2018-07-10 stsp int annotated;
5165 84451b3e 2018-07-10 stsp struct got_object_id *id;
5168 4ed7e80c 2018-05-20 stsp static const struct got_error *
5169 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5171 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5172 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5173 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5174 84451b3e 2018-07-10 stsp const struct got_error *err;
5175 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5176 826082fe 2020-12-10 stsp char *line = NULL;
5177 826082fe 2020-12-10 stsp size_t linesize = 0;
5178 826082fe 2020-12-10 stsp ssize_t linelen;
5179 84451b3e 2018-07-10 stsp wchar_t *wline;
5180 27a741e5 2019-09-11 stsp int width;
5181 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5182 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5183 ab089a2a 2018-07-12 stsp char *id_str;
5184 11b20872 2019-11-08 stsp struct tog_color *tc;
5186 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5188 ab089a2a 2018-07-12 stsp return err;
5190 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5191 f7d12f7e 2018-08-01 stsp werase(view->window);
5193 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5194 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5195 ab089a2a 2018-07-12 stsp free(id_str);
5196 ab089a2a 2018-07-12 stsp return err;
5199 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5200 ab089a2a 2018-07-12 stsp free(line);
5201 2550e4c3 2018-07-13 stsp line = NULL;
5203 1cae65b4 2019-09-22 stsp return err;
5204 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5205 a3404814 2018-09-02 stsp wstandout(view->window);
5206 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5208 11b20872 2019-11-08 stsp wattr_on(view->window,
5209 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5210 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5212 11b20872 2019-11-08 stsp wattr_off(view->window,
5213 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5214 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5215 a3404814 2018-09-02 stsp wstandend(view->window);
5216 2550e4c3 2018-07-13 stsp free(wline);
5217 2550e4c3 2018-07-13 stsp wline = NULL;
5218 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5219 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5221 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5222 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5224 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5225 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5226 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5227 ab089a2a 2018-07-12 stsp free(id_str);
5228 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5230 ab089a2a 2018-07-12 stsp free(id_str);
5231 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5232 3f60a8ef 2018-07-10 stsp free(line);
5233 2550e4c3 2018-07-13 stsp line = NULL;
5235 3f60a8ef 2018-07-10 stsp return err;
5236 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5237 2550e4c3 2018-07-13 stsp free(wline);
5238 2550e4c3 2018-07-13 stsp wline = NULL;
5239 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5240 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5242 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5243 145b6838 2022-06-16 stsp view->maxx = 0;
5244 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5245 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5246 826082fe 2020-12-10 stsp if (linelen == -1) {
5247 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5248 826082fe 2020-12-10 stsp s->eof = 1;
5251 84451b3e 2018-07-10 stsp free(line);
5252 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5254 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5256 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5259 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5260 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5261 1853e0f4 2022-06-16 stsp if (err) {
5262 1853e0f4 2022-06-16 stsp free(line);
5263 1853e0f4 2022-06-16 stsp return err;
5265 1853e0f4 2022-06-16 stsp free(wline);
5266 1853e0f4 2022-06-16 stsp wline = NULL;
5267 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5269 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5270 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5272 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5273 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5274 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5275 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5276 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5277 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5278 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5279 8d0fe45a 2019-08-12 stsp char *id_str;
5280 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5281 87411fa9 2022-06-16 stsp blame_line->id);
5282 8d0fe45a 2019-08-12 stsp if (err) {
5283 8d0fe45a 2019-08-12 stsp free(line);
5284 8d0fe45a 2019-08-12 stsp return err;
5286 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5288 11b20872 2019-11-08 stsp wattr_on(view->window,
5289 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5290 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5292 11b20872 2019-11-08 stsp wattr_off(view->window,
5293 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5294 8d0fe45a 2019-08-12 stsp free(id_str);
5295 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5297 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5298 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5301 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5302 ee41ec32 2018-07-10 stsp prev_id = NULL;
5305 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5306 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5307 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5309 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5310 41605754 2020-11-12 stsp width = 9;
5311 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5312 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5313 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5314 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5315 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5316 41605754 2020-11-12 stsp if (err) {
5317 41605754 2020-11-12 stsp free(line);
5318 41605754 2020-11-12 stsp return err;
5320 41605754 2020-11-12 stsp width += 9;
5323 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5324 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5325 4cb9d869 2022-06-16 stsp if (err) {
5326 4cb9d869 2022-06-16 stsp free(line);
5327 4cb9d869 2022-06-16 stsp return err;
5329 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5330 a75b3e2d 2022-06-16 stsp width += 9;
5331 41605754 2020-11-12 stsp free(wline);
5332 41605754 2020-11-12 stsp wline = NULL;
5335 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5336 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5337 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5338 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5340 826082fe 2020-12-10 stsp free(line);
5341 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5343 9b058f45 2022-06-30 mark view_border(view);
5345 84451b3e 2018-07-10 stsp return NULL;
5348 84451b3e 2018-07-10 stsp static const struct got_error *
5349 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5350 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5352 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5353 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5354 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5355 1a76625f 2018-10-22 stsp int errcode;
5357 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5358 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5359 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5361 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5362 1a76625f 2018-10-22 stsp if (errcode)
5363 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5365 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5366 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5367 d68a0a7d 2018-07-10 stsp goto done;
5370 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5371 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5373 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5374 d68a0a7d 2018-07-10 stsp if (line->annotated)
5375 d68a0a7d 2018-07-10 stsp goto done;
5377 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5378 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5379 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5380 84451b3e 2018-07-10 stsp goto done;
5382 84451b3e 2018-07-10 stsp line->annotated = 1;
5384 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5385 1a76625f 2018-10-22 stsp if (errcode)
5386 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5387 84451b3e 2018-07-10 stsp return err;
5390 84451b3e 2018-07-10 stsp static void *
5391 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5393 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5394 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5395 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5396 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5397 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5399 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5400 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5401 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5403 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5404 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5405 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5406 e6e73e55 2022-06-30 tracey goto done;
5409 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5410 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5411 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5412 e6e73e55 2022-06-30 tracey goto done;
5414 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5415 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5416 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5417 e6e73e55 2022-06-30 tracey goto done;
5420 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5422 e6e73e55 2022-06-30 tracey goto done;
5424 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5425 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5426 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5427 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5428 fc06ba56 2019-08-22 stsp err = NULL;
5430 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5431 e6e73e55 2022-06-30 tracey if (errcode) {
5432 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5433 e6e73e55 2022-06-30 tracey goto done;
5436 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5437 1d0f4054 2021-06-17 stsp if (err == NULL)
5438 1d0f4054 2021-06-17 stsp err = close_err;
5439 c9beca56 2018-07-22 stsp ta->repo = NULL;
5440 c9beca56 2018-07-22 stsp *ta->complete = 1;
5442 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5443 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5444 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5447 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5448 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5449 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5450 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5451 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5452 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5453 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5454 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5456 18430de3 2018-07-10 stsp return (void *)err;
5459 245d91c1 2018-07-12 stsp static struct got_object_id *
5460 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5461 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5463 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5465 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5466 8d0fe45a 2019-08-12 stsp return NULL;
5468 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5469 c0f61fa4 2022-07-11 mark if (!line->annotated)
5470 c0f61fa4 2022-07-11 mark return NULL;
5472 c0f61fa4 2022-07-11 mark return line->id;
5475 c0f61fa4 2022-07-11 mark static struct got_object_id *
5476 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5477 c0f61fa4 2022-07-11 mark int lineno)
5479 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5481 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5482 c0f61fa4 2022-07-11 mark return NULL;
5484 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5485 245d91c1 2018-07-12 stsp if (!line->annotated)
5486 245d91c1 2018-07-12 stsp return NULL;
5488 245d91c1 2018-07-12 stsp return line->id;
5491 b880a918 2018-07-10 stsp static const struct got_error *
5492 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5494 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5497 245d91c1 2018-07-12 stsp if (blame->thread) {
5498 1a76625f 2018-10-22 stsp int errcode;
5499 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5500 1a76625f 2018-10-22 stsp if (errcode)
5501 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5502 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5503 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5504 1a76625f 2018-10-22 stsp if (errcode)
5505 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5506 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5507 1a76625f 2018-10-22 stsp if (errcode)
5508 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5509 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5510 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5511 245d91c1 2018-07-12 stsp err = NULL;
5512 245d91c1 2018-07-12 stsp blame->thread = NULL;
5514 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5515 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5516 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5517 1d0f4054 2021-06-17 stsp if (err == NULL)
5518 1d0f4054 2021-06-17 stsp err = close_err;
5519 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5521 245d91c1 2018-07-12 stsp if (blame->f) {
5522 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5523 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5524 245d91c1 2018-07-12 stsp blame->f = NULL;
5526 57670559 2018-12-23 stsp if (blame->lines) {
5527 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5528 57670559 2018-12-23 stsp free(blame->lines[i].id);
5529 57670559 2018-12-23 stsp free(blame->lines);
5530 57670559 2018-12-23 stsp blame->lines = NULL;
5532 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5533 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5534 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5535 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5536 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5537 0ae84acc 2022-06-15 tracey if (err == NULL)
5538 0ae84acc 2022-06-15 tracey err = pack_err;
5539 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5541 245d91c1 2018-07-12 stsp return err;
5544 245d91c1 2018-07-12 stsp static const struct got_error *
5545 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5547 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5548 fc06ba56 2019-08-22 stsp int *done = arg;
5549 fc06ba56 2019-08-22 stsp int errcode;
5551 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5552 fc06ba56 2019-08-22 stsp if (errcode)
5553 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5554 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5556 fc06ba56 2019-08-22 stsp if (*done)
5557 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5559 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5560 fc06ba56 2019-08-22 stsp if (errcode)
5561 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5562 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5564 fc06ba56 2019-08-22 stsp return err;
5567 fc06ba56 2019-08-22 stsp static const struct got_error *
5568 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5570 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5571 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5572 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5573 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5574 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5575 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5576 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5577 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5578 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5580 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5581 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5583 15a94983 2018-12-23 stsp return err;
5585 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5586 eb81bc23 2022-06-28 tracey if (fd == -1) {
5587 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5588 eb81bc23 2022-06-28 tracey goto done;
5591 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5593 a44927cc 2022-04-07 stsp goto done;
5595 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5597 84451b3e 2018-07-10 stsp goto done;
5599 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5600 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5601 84451b3e 2018-07-10 stsp goto done;
5604 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5606 a70480e0 2018-06-23 stsp goto done;
5607 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5608 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5609 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5610 84451b3e 2018-07-10 stsp goto done;
5612 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5613 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5615 1fddf795 2021-01-20 stsp goto done;
5616 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5617 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5618 84451b3e 2018-07-10 stsp goto done;
5621 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5622 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5623 b02560ec 2019-08-19 stsp blame->nlines--;
5625 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5626 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5627 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5628 84451b3e 2018-07-10 stsp goto done;
5631 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5633 bd24772e 2018-07-11 stsp goto done;
5634 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5635 0ae84acc 2022-06-15 tracey pack_fds);
5636 0ae84acc 2022-06-15 tracey if (err)
5637 0ae84acc 2022-06-15 tracey goto done;
5639 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5640 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5641 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5642 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5643 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5644 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5645 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5646 245d91c1 2018-07-12 stsp goto done;
5648 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5650 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5651 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5652 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5653 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5654 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5655 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5656 a5388363 2020-12-01 naddy s->blame_complete = 0;
5658 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5659 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5660 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5661 f5a09613 2020-12-13 naddy s->selected_line = 1;
5663 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5666 a44927cc 2022-04-07 stsp if (commit)
5667 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5668 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5669 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5671 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5672 27d434c2 2018-09-15 stsp free(obj_id);
5674 245d91c1 2018-07-12 stsp stop_blame(blame);
5675 245d91c1 2018-07-12 stsp return err;
5678 245d91c1 2018-07-12 stsp static const struct got_error *
5679 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5680 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5682 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5683 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5685 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5687 c4843652 2019-08-12 stsp s->path = strdup(path);
5688 c4843652 2019-08-12 stsp if (s->path == NULL)
5689 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5691 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5692 c4843652 2019-08-12 stsp if (err) {
5693 c4843652 2019-08-12 stsp free(s->path);
5694 7cbe629d 2018-08-04 stsp return err;
5697 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5698 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5699 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5700 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5701 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5702 fb2756b9 2018-08-04 stsp s->repo = repo;
5703 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5704 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5706 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5707 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5708 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5709 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5711 11b20872 2019-11-08 stsp return err;
5714 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5715 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5716 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5717 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5718 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5719 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5721 a5388363 2020-12-01 naddy return run_blame(view);
5724 e5a0f69f 2018-08-18 stsp static const struct got_error *
5725 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5727 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5728 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5730 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5731 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5733 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5734 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5735 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5736 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5737 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5740 e5a0f69f 2018-08-18 stsp free(s->path);
5741 11b20872 2019-11-08 stsp free_colors(&s->colors);
5742 e5a0f69f 2018-08-18 stsp return err;
5745 7cbe629d 2018-08-04 stsp static const struct got_error *
5746 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5748 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5750 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5751 6c4c42e0 2019-06-24 stsp return NULL;
5754 6c4c42e0 2019-06-24 stsp static const struct got_error *
5755 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5757 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5758 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5759 6c4c42e0 2019-06-24 stsp int lineno;
5760 cb713507 2022-06-16 stsp char *line = NULL;
5761 826082fe 2020-12-10 stsp size_t linesize = 0;
5762 826082fe 2020-12-10 stsp ssize_t linelen;
5764 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5765 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5766 6c4c42e0 2019-06-24 stsp return NULL;
5769 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5770 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5771 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5773 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5775 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5777 6c4c42e0 2019-06-24 stsp while (1) {
5778 6c4c42e0 2019-06-24 stsp off_t offset;
5780 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5781 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5782 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5786 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5787 6c4c42e0 2019-06-24 stsp lineno = 1;
5789 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5792 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5793 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5794 6c4c42e0 2019-06-24 stsp free(line);
5795 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5797 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5798 cb713507 2022-06-16 stsp if (linelen != -1) {
5799 cb713507 2022-06-16 stsp char *exstr;
5800 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5803 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5804 cb713507 2022-06-16 stsp &view->regmatch)) {
5805 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5806 cb713507 2022-06-16 stsp s->matched_line = lineno;
5807 cb713507 2022-06-16 stsp free(exstr);
5810 cb713507 2022-06-16 stsp free(exstr);
5812 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5817 826082fe 2020-12-10 stsp free(line);
5819 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5820 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5821 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5824 145b6838 2022-06-16 stsp return err;
5827 6c4c42e0 2019-06-24 stsp static const struct got_error *
5828 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5830 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5831 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5832 2b380cc8 2018-10-24 stsp int errcode;
5834 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5835 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5836 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5837 2b380cc8 2018-10-24 stsp if (errcode)
5838 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5840 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5843 51fe7530 2019-08-19 stsp if (s->blame_complete)
5844 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5846 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5848 9b058f45 2022-06-30 mark view_border(view);
5849 e5a0f69f 2018-08-18 stsp return err;
5852 e5a0f69f 2018-08-18 stsp static const struct got_error *
5853 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5854 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5856 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5857 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5859 05f04cdf 2022-07-20 mark *new_view = NULL;
5861 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5862 05f04cdf 2022-07-20 mark if (log_view == NULL)
5863 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5865 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5867 05f04cdf 2022-07-20 mark view_close(log_view);
5869 05f04cdf 2022-07-20 mark *new_view = log_view;
5871 05f04cdf 2022-07-20 mark return err;
5874 05f04cdf 2022-07-20 mark static const struct got_error *
5875 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5877 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5878 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
5879 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5880 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5882 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5883 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5884 9b058f45 2022-06-30 mark --eos; /* border */
5886 e5a0f69f 2018-08-18 stsp switch (ch) {
5888 145b6838 2022-06-16 stsp view->x = 0;
5891 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5892 640cd7ff 2022-06-22 mark view->count = 0;
5894 145b6838 2022-06-16 stsp case KEY_RIGHT:
5896 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5897 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5899 640cd7ff 2022-06-22 mark view->count = 0;
5901 145b6838 2022-06-16 stsp case KEY_LEFT:
5903 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5904 640cd7ff 2022-06-22 mark if (view->x <= 0)
5905 640cd7ff 2022-06-22 mark view->count = 0;
5908 1e37a5c2 2019-05-12 jcs s->done = 1;
5910 4deef56f 2021-09-02 naddy case 'g':
5911 4deef56f 2021-09-02 naddy case KEY_HOME:
5912 4deef56f 2021-09-02 naddy s->selected_line = 1;
5913 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5914 640cd7ff 2022-06-22 mark view->count = 0;
5916 4deef56f 2021-09-02 naddy case 'G':
5917 4deef56f 2021-09-02 naddy case KEY_END:
5918 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5919 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5920 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5922 9b058f45 2022-06-30 mark s->selected_line = eos;
5923 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5925 640cd7ff 2022-06-22 mark view->count = 0;
5928 1e37a5c2 2019-05-12 jcs case KEY_UP:
5929 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5930 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5931 1e37a5c2 2019-05-12 jcs s->selected_line--;
5932 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5933 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5934 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5936 640cd7ff 2022-06-22 mark view->count = 0;
5938 83cc4199 2022-06-13 stsp case CTRL('u'):
5940 83cc4199 2022-06-13 stsp nscroll /= 2;
5941 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5942 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5943 ea025d1d 2020-02-22 naddy case CTRL('b'):
5945 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5946 640cd7ff 2022-06-22 mark if (view->count > 1)
5947 640cd7ff 2022-06-22 mark nscroll += nscroll;
5948 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5949 640cd7ff 2022-06-22 mark view->count = 0;
5952 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5953 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5955 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5958 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5959 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5960 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5961 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5962 1e37a5c2 2019-05-12 jcs s->selected_line++;
5963 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5964 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5966 640cd7ff 2022-06-22 mark view->count = 0;
5969 1e37a5c2 2019-05-12 jcs case 'p': {
5970 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5972 640cd7ff 2022-06-22 mark view->count = 0;
5973 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5974 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5975 1e37a5c2 2019-05-12 jcs if (id == NULL)
5977 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5978 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5979 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5980 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5981 1e37a5c2 2019-05-12 jcs int obj_type;
5982 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5983 1e37a5c2 2019-05-12 jcs s->repo, id);
5986 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5987 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5988 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5989 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5992 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5993 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5994 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5997 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5998 a44927cc 2022-04-07 stsp pcommit, s->path);
5999 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6000 e5a0f69f 2018-08-18 stsp if (err) {
6001 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6002 1e37a5c2 2019-05-12 jcs err = NULL;
6003 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6006 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6008 1e37a5c2 2019-05-12 jcs free(blob_id);
6009 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6010 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6011 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6014 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6015 d7b5a0e8 2022-04-20 stsp &pid->id);
6016 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6018 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6019 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6021 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6026 1e37a5c2 2019-05-12 jcs s->done = 1;
6027 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6028 1e37a5c2 2019-05-12 jcs s->done = 0;
6029 1e37a5c2 2019-05-12 jcs if (thread_err)
6031 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6032 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6033 a5388363 2020-12-01 naddy err = run_blame(view);
6038 61417565 2022-06-20 mark case 'C': {
6039 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6041 640cd7ff 2022-06-22 mark view->count = 0;
6042 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6043 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6045 1e37a5c2 2019-05-12 jcs s->done = 1;
6046 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6047 1e37a5c2 2019-05-12 jcs s->done = 0;
6048 1e37a5c2 2019-05-12 jcs if (thread_err)
6050 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6051 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6052 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6053 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6054 a5388363 2020-12-01 naddy err = run_blame(view);
6060 05f04cdf 2022-07-20 mark view->count = 0;
6061 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6062 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6063 136e2bd4 2022-07-23 mark if (s->id_to_log)
6064 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6066 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6067 1e37a5c2 2019-05-12 jcs case '\r': {
6068 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6069 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6070 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6072 640cd7ff 2022-06-22 mark view->count = 0;
6073 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6074 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6075 1e37a5c2 2019-05-12 jcs if (id == NULL)
6077 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6080 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6081 c0f61fa4 2022-07-11 mark if (*new_view) {
6082 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6083 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6086 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6088 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6089 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6091 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6092 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6093 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6094 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6095 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6099 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6100 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6101 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6103 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6106 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6107 c0f61fa4 2022-07-11 mark s->selected_line;
6108 c0f61fa4 2022-07-11 mark if (*new_view)
6109 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6110 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6111 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6112 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6117 e78dc838 2020-12-04 stsp view->focussed = 0;
6118 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6119 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6120 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6121 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6122 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6123 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6126 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6129 e78dc838 2020-12-04 stsp view->focus_child = 1;
6131 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6136 83cc4199 2022-06-13 stsp case CTRL('d'):
6138 83cc4199 2022-06-13 stsp nscroll /= 2;
6139 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6140 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6141 ea025d1d 2020-02-22 naddy case CTRL('f'):
6144 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6145 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6146 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6147 640cd7ff 2022-06-22 mark view->count = 0;
6150 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6151 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6152 83cc4199 2022-06-13 stsp s->selected_line +=
6153 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6154 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6156 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6157 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6159 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6160 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6162 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6163 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6164 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6165 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6169 640cd7ff 2022-06-22 mark view->count = 0;
6172 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6175 917d79a7 2022-07-01 stsp static const struct got_error *
6176 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6178 917d79a7 2022-07-01 stsp const struct got_error *err;
6179 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6181 917d79a7 2022-07-01 stsp view->count = 0;
6182 917d79a7 2022-07-01 stsp s->done = 1;
6183 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6184 917d79a7 2022-07-01 stsp s->done = 0;
6186 917d79a7 2022-07-01 stsp return err;
6187 917d79a7 2022-07-01 stsp return run_blame(view);
6190 a70480e0 2018-06-23 stsp static const struct got_error *
6191 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6193 a70480e0 2018-06-23 stsp const struct got_error *error;
6194 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6195 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6196 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6197 0587e10c 2020-07-23 stsp char *link_target = NULL;
6198 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6199 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6200 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6202 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6203 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6205 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6206 a70480e0 2018-06-23 stsp switch (ch) {
6208 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6211 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6212 69069811 2018-08-02 stsp if (repo_path == NULL)
6213 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6217 17020d27 2019-03-07 stsp usage_blame();
6218 a70480e0 2018-06-23 stsp /* NOTREACHED */
6222 a70480e0 2018-06-23 stsp argc -= optind;
6223 a70480e0 2018-06-23 stsp argv += optind;
6225 f135c941 2020-02-20 stsp if (argc != 1)
6226 a70480e0 2018-06-23 stsp usage_blame();
6228 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6229 0ae84acc 2022-06-15 tracey if (error != NULL)
6230 0ae84acc 2022-06-15 tracey goto done;
6232 69069811 2018-08-02 stsp if (repo_path == NULL) {
6233 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6234 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6235 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6236 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6237 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6238 c156c7a4 2020-12-18 stsp goto done;
6239 f135c941 2020-02-20 stsp if (worktree)
6240 eb41ed75 2019-02-05 stsp repo_path =
6241 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6243 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6244 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6245 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6246 c156c7a4 2020-12-18 stsp goto done;
6250 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6251 c02c541e 2019-03-29 stsp if (error != NULL)
6252 8e94dd5b 2019-01-04 stsp goto done;
6254 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6255 f135c941 2020-02-20 stsp worktree);
6256 c02c541e 2019-03-29 stsp if (error)
6257 92205607 2019-01-04 stsp goto done;
6259 f135c941 2020-02-20 stsp init_curses();
6261 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6262 51a10b52 2020-12-26 stsp if (error)
6263 51a10b52 2020-12-26 stsp goto done;
6265 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6266 eb41ed75 2019-02-05 stsp if (error)
6267 69069811 2018-08-02 stsp goto done;
6269 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6270 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6271 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6272 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6273 a70480e0 2018-06-23 stsp if (error != NULL)
6274 66b4983c 2018-06-23 stsp goto done;
6275 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6276 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6278 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6279 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6281 a19e88aa 2018-06-23 stsp if (error != NULL)
6282 8b473291 2019-02-21 stsp goto done;
6284 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6285 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6286 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6287 e1cd8fed 2018-08-01 stsp goto done;
6290 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6291 a44927cc 2022-04-07 stsp if (error)
6292 a44927cc 2022-04-07 stsp goto done;
6294 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6295 a44927cc 2022-04-07 stsp commit, repo);
6296 7cbe629d 2018-08-04 stsp if (error)
6297 7cbe629d 2018-08-04 stsp goto done;
6299 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6300 78756c87 2020-11-24 stsp commit_id, repo);
6301 0587e10c 2020-07-23 stsp if (error)
6302 0587e10c 2020-07-23 stsp goto done;
6303 12314ad4 2019-08-31 stsp if (worktree) {
6304 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6305 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6306 12314ad4 2019-08-31 stsp worktree = NULL;
6308 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6310 69069811 2018-08-02 stsp free(repo_path);
6311 f135c941 2020-02-20 stsp free(in_repo_path);
6312 0587e10c 2020-07-23 stsp free(link_target);
6313 69069811 2018-08-02 stsp free(cwd);
6314 a70480e0 2018-06-23 stsp free(commit_id);
6315 a44927cc 2022-04-07 stsp if (commit)
6316 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6317 eb41ed75 2019-02-05 stsp if (worktree)
6318 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6319 1d0f4054 2021-06-17 stsp if (repo) {
6320 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6321 1d0f4054 2021-06-17 stsp if (error == NULL)
6322 1d0f4054 2021-06-17 stsp error = close_err;
6324 0ae84acc 2022-06-15 tracey if (pack_fds) {
6325 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6326 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6327 0ae84acc 2022-06-15 tracey if (error == NULL)
6328 0ae84acc 2022-06-15 tracey error = pack_err;
6330 51a10b52 2020-12-26 stsp tog_free_refs();
6331 a70480e0 2018-06-23 stsp return error;
6334 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6335 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6337 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6338 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6339 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6340 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6341 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6342 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6343 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6345 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6346 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6347 9b058f45 2022-06-30 mark --limit; /* border */
6349 f7d12f7e 2018-08-01 stsp werase(view->window);
6351 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6352 ffd1d5e5 2018-06-23 stsp return NULL;
6354 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6357 ffd1d5e5 2018-06-23 stsp return err;
6358 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6359 a3404814 2018-09-02 stsp wstandout(view->window);
6360 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6362 11b20872 2019-11-08 stsp wattr_on(view->window,
6363 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6364 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6366 11b20872 2019-11-08 stsp wattr_off(view->window,
6367 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6368 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6369 a3404814 2018-09-02 stsp wstandend(view->window);
6370 2550e4c3 2018-07-13 stsp free(wline);
6371 2550e4c3 2018-07-13 stsp wline = NULL;
6373 94b80cfa 2022-08-01 mark if (s->selected_entry) {
6374 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(s->selected_entry);
6375 94b80cfa 2022-08-01 mark i += s->tree == s->root ? 1 : 2; /* account for ".." entry */
6377 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6378 94b80cfa 2022-08-01 mark wprintw(view->window, " [%d/%d]", i,
6379 94b80cfa 2022-08-01 mark nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6381 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6382 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6383 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6384 ffd1d5e5 2018-06-23 stsp return NULL;
6385 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6388 ce52c690 2018-06-23 stsp return err;
6389 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6390 2550e4c3 2018-07-13 stsp free(wline);
6391 2550e4c3 2018-07-13 stsp wline = NULL;
6392 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6393 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6394 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6395 ffd1d5e5 2018-06-23 stsp return NULL;
6396 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6397 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6398 a1eca9bb 2018-06-23 stsp return NULL;
6400 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6401 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6402 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6403 0cf4efb1 2018-09-29 stsp if (view->focussed)
6404 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6405 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6407 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6408 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6409 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6410 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6411 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6412 ffd1d5e5 2018-06-23 stsp return NULL;
6416 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6419 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6420 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6421 848d6979 2019-08-12 stsp const char *modestr = "";
6422 56e0773d 2019-11-28 stsp mode_t mode;
6424 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6425 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6427 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6428 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6429 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6431 638f9024 2019-05-13 stsp return got_error_from_errno(
6432 230a42bd 2019-05-11 jcs "got_object_id_str");
6434 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6435 63c5ca5d 2019-08-24 stsp modestr = "$";
6436 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6439 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6440 d86d3b18 2020-12-01 naddy te, s->repo);
6441 0d6c6ee3 2020-05-20 stsp if (err) {
6442 0d6c6ee3 2020-05-20 stsp free(id_str);
6443 0d6c6ee3 2020-05-20 stsp return err;
6445 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6446 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6447 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6449 848d6979 2019-08-12 stsp modestr = "@";
6451 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6452 848d6979 2019-08-12 stsp modestr = "/";
6453 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6454 848d6979 2019-08-12 stsp modestr = "*";
6455 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6456 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6457 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6458 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6459 1d13200f 2018-07-12 stsp free(id_str);
6460 0d6c6ee3 2020-05-20 stsp free(link_target);
6461 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6463 1d13200f 2018-07-12 stsp free(id_str);
6464 0d6c6ee3 2020-05-20 stsp free(link_target);
6465 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6467 ffd1d5e5 2018-06-23 stsp if (err) {
6468 ffd1d5e5 2018-06-23 stsp free(line);
6471 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6472 0cf4efb1 2018-09-29 stsp if (view->focussed)
6473 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6474 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6476 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6478 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6479 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6480 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6482 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6483 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6484 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6485 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6486 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6487 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6488 ffd1d5e5 2018-06-23 stsp free(line);
6489 2550e4c3 2018-07-13 stsp free(wline);
6490 2550e4c3 2018-07-13 stsp wline = NULL;
6492 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6493 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6494 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6498 ffd1d5e5 2018-06-23 stsp return err;
6501 ffd1d5e5 2018-06-23 stsp static void
6502 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6504 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6505 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6506 fa86c4bf 2020-11-29 stsp int i = 0;
6508 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6511 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6512 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6513 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6514 fa86c4bf 2020-11-29 stsp if (!isroot)
6515 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6518 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6519 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6523 9b058f45 2022-06-30 mark static const struct got_error *
6524 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6526 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6527 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6528 ffd1d5e5 2018-06-23 stsp int n = 0;
6530 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6531 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6532 694d3271 2020-12-01 naddy s->first_displayed_entry);
6534 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6536 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6537 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6538 94b80cfa 2022-08-01 mark if (last) {
6539 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6540 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6542 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6543 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6544 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6548 9b058f45 2022-06-30 mark return NULL;
6551 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6552 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6553 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6555 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6556 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6557 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6559 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6560 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6561 56e0773d 2019-11-28 stsp + 1 /* slash */;
6563 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6565 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6566 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6567 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6569 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6570 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6571 d9765a41 2018-06-23 stsp while (pt) {
6572 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6573 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6574 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6575 cb2ebc8a 2018-06-23 stsp goto done;
6577 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6578 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6579 cb2ebc8a 2018-06-23 stsp goto done;
6581 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6584 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6585 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6586 ce52c690 2018-06-23 stsp goto done;
6590 ce52c690 2018-06-23 stsp if (err) {
6591 ce52c690 2018-06-23 stsp free(*path);
6592 ce52c690 2018-06-23 stsp *path = NULL;
6594 ce52c690 2018-06-23 stsp return err;
6597 ce52c690 2018-06-23 stsp static const struct got_error *
6598 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6599 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6600 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6602 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6603 ce52c690 2018-06-23 stsp char *path;
6604 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6606 a0de39f3 2019-08-09 stsp *new_view = NULL;
6608 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6610 ce52c690 2018-06-23 stsp return err;
6612 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6613 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6614 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6615 83ce39e3 2019-08-12 stsp goto done;
6618 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6619 e5a0f69f 2018-08-18 stsp if (err) {
6620 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6621 fc06ba56 2019-08-22 stsp err = NULL;
6622 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6624 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6626 83ce39e3 2019-08-12 stsp free(path);
6627 69efd4c4 2018-07-18 stsp return err;
6630 69efd4c4 2018-07-18 stsp static const struct got_error *
6631 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6632 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6634 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6635 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6636 69efd4c4 2018-07-18 stsp char *path;
6638 a0de39f3 2019-08-09 stsp *new_view = NULL;
6640 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6641 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6642 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6644 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6646 69efd4c4 2018-07-18 stsp return err;
6648 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6651 e5a0f69f 2018-08-18 stsp view_close(log_view);
6653 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6654 cb2ebc8a 2018-06-23 stsp free(path);
6655 cb2ebc8a 2018-06-23 stsp return err;
6658 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6659 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6660 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6662 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6663 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6664 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6665 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6667 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6668 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6670 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6671 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6672 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6674 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6676 bc573f3b 2021-07-10 stsp goto done;
6679 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6680 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6681 bc573f3b 2021-07-10 stsp * closed on demand.
6683 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6684 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6686 bc573f3b 2021-07-10 stsp goto done;
6687 bc573f3b 2021-07-10 stsp s->tree = s->root;
6689 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6690 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6691 ffd1d5e5 2018-06-23 stsp goto done;
6693 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6694 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6695 ffd1d5e5 2018-06-23 stsp goto done;
6698 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6699 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6700 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6701 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6702 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6703 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6704 9cd7cbd1 2020-12-07 stsp goto done;
6707 fb2756b9 2018-08-04 stsp s->repo = repo;
6709 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6710 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6711 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6712 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6714 c0b01bdb 2019-11-08 stsp goto done;
6715 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6716 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6718 c0b01bdb 2019-11-08 stsp goto done;
6719 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6720 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6721 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6723 c0b01bdb 2019-11-08 stsp goto done;
6725 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6726 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6727 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6729 11b20872 2019-11-08 stsp goto done;
6731 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6732 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6734 c0b01bdb 2019-11-08 stsp goto done;
6737 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6738 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6739 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6740 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6741 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6743 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6744 bc573f3b 2021-07-10 stsp if (commit)
6745 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6747 bc573f3b 2021-07-10 stsp close_tree_view(view);
6748 ad80ab7b 2018-08-04 stsp return err;
6751 e5a0f69f 2018-08-18 stsp static const struct got_error *
6752 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6754 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6756 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6757 fb2756b9 2018-08-04 stsp free(s->tree_label);
6758 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6759 6484ec90 2018-09-29 stsp free(s->commit_id);
6760 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6761 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6762 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6763 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6764 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6765 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6766 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6767 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6768 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6769 ad80ab7b 2018-08-04 stsp free(parent);
6772 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6773 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6774 bc573f3b 2021-07-10 stsp if (s->root)
6775 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6776 7c32bd05 2019-06-22 stsp return NULL;
6779 7c32bd05 2019-06-22 stsp static const struct got_error *
6780 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6782 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6784 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6785 7c32bd05 2019-06-22 stsp return NULL;
6788 7c32bd05 2019-06-22 stsp static int
6789 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6791 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6793 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6797 7c32bd05 2019-06-22 stsp static const struct got_error *
6798 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6800 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6801 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6803 7c32bd05 2019-06-22 stsp if (!view->searching) {
6804 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6805 7c32bd05 2019-06-22 stsp return NULL;
6808 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6809 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6810 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6811 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6812 56e0773d 2019-11-28 stsp s->selected_entry);
6814 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6816 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6817 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6819 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6820 56e0773d 2019-11-28 stsp s->selected_entry);
6823 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6824 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6825 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6826 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6828 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6831 7c32bd05 2019-06-22 stsp while (1) {
6832 56e0773d 2019-11-28 stsp if (te == NULL) {
6833 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6834 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6835 ac66afb8 2019-06-24 stsp return NULL;
6837 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6838 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6840 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6843 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6844 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6845 56e0773d 2019-11-28 stsp s->matched_entry = te;
6849 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6850 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6852 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6855 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6856 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6857 7c32bd05 2019-06-22 stsp s->selected = 0;
6860 e5a0f69f 2018-08-18 stsp return NULL;
6863 ad80ab7b 2018-08-04 stsp static const struct got_error *
6864 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6866 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6867 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6868 e5a0f69f 2018-08-18 stsp char *parent_path;
6870 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6872 e5a0f69f 2018-08-18 stsp return err;
6874 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6875 e5a0f69f 2018-08-18 stsp free(parent_path);
6877 9b058f45 2022-06-30 mark view_border(view);
6878 e5a0f69f 2018-08-18 stsp return err;
6881 94b80cfa 2022-08-01 mark static const struct got_error *
6882 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
6884 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
6885 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
6886 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
6887 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
6888 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
6889 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
6891 94b80cfa 2022-08-01 mark g = view->gline;
6892 94b80cfa 2022-08-01 mark view->gline = 0;
6894 94b80cfa 2022-08-01 mark if (g == 0)
6896 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
6897 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6899 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
6900 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
6901 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
6903 94b80cfa 2022-08-01 mark if (*fte != NULL) {
6904 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
6905 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
6907 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
6908 94b80cfa 2022-08-01 mark last += off;
6910 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
6911 94b80cfa 2022-08-01 mark s->selected = g - first;
6912 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
6915 94b80cfa 2022-08-01 mark if (*ste != NULL) {
6916 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
6920 94b80cfa 2022-08-01 mark if (i < g) {
6921 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
6923 94b80cfa 2022-08-01 mark return err;
6924 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
6925 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
6926 94b80cfa 2022-08-01 mark first + s->selected < g &&
6927 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
6928 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
6929 94b80cfa 2022-08-01 mark first += off;
6930 94b80cfa 2022-08-01 mark s->selected = g - first;
6932 94b80cfa 2022-08-01 mark } else if (i > g)
6933 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
6935 94b80cfa 2022-08-01 mark if (g < nlines &&
6936 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6937 94b80cfa 2022-08-01 mark s->selected = g - 1;
6939 94b80cfa 2022-08-01 mark return NULL;
6942 e5a0f69f 2018-08-18 stsp static const struct got_error *
6943 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6945 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6946 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6947 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6948 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
6950 94b80cfa 2022-08-01 mark if (view->gline)
6951 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
6953 e5a0f69f 2018-08-18 stsp switch (ch) {
6955 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6956 640cd7ff 2022-06-22 mark view->count = 0;
6959 640cd7ff 2022-06-22 mark view->count = 0;
6960 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6962 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6965 640cd7ff 2022-06-22 mark view->count = 0;
6966 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
6968 e4526bf5 2021-09-03 naddy case 'g':
6969 e4526bf5 2021-09-03 naddy case KEY_HOME:
6970 e4526bf5 2021-09-03 naddy s->selected = 0;
6971 640cd7ff 2022-06-22 mark view->count = 0;
6972 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6973 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6974 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6976 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6978 e4526bf5 2021-09-03 naddy case 'G':
6979 9b058f45 2022-06-30 mark case KEY_END: {
6980 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6982 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6983 9b058f45 2022-06-30 mark --eos; /* border */
6984 e4526bf5 2021-09-03 naddy s->selected = 0;
6985 640cd7ff 2022-06-22 mark view->count = 0;
6986 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6987 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6988 e4526bf5 2021-09-03 naddy if (te == NULL) {
6989 ad055527 2022-07-16 mark if (s->tree != s->root) {
6990 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6995 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6996 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6998 e4526bf5 2021-09-03 naddy if (n > 0)
6999 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7003 1e37a5c2 2019-05-12 jcs case KEY_UP:
7004 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7005 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7006 1e37a5c2 2019-05-12 jcs s->selected--;
7009 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7010 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7011 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7012 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7013 640cd7ff 2022-06-22 mark view->count = 0;
7015 83cc4199 2022-06-13 stsp case CTRL('u'):
7017 83cc4199 2022-06-13 stsp nscroll /= 2;
7018 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7019 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7020 ea025d1d 2020-02-22 naddy case CTRL('b'):
7022 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7023 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7024 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7025 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7027 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7028 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7030 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7031 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7032 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7033 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7034 640cd7ff 2022-06-22 mark view->count = 0;
7037 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7038 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7039 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7040 1e37a5c2 2019-05-12 jcs s->selected++;
7043 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7044 640cd7ff 2022-06-22 mark == NULL) {
7045 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7046 640cd7ff 2022-06-22 mark view->count = 0;
7049 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7051 83cc4199 2022-06-13 stsp case CTRL('d'):
7053 83cc4199 2022-06-13 stsp nscroll /= 2;
7054 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7055 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7056 ea025d1d 2020-02-22 naddy case CTRL('f'):
7058 48bb96f0 2022-06-20 naddy case ' ':
7059 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7061 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7062 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7063 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7064 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7066 640cd7ff 2022-06-22 mark view->count = 0;
7069 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7071 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7073 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7074 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7075 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7076 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7077 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7078 640cd7ff 2022-06-22 mark view->count = 0;
7081 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7082 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7084 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7085 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7086 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7087 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7088 1e37a5c2 2019-05-12 jcs s->selected_entry =
7089 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7090 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7091 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7092 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7096 1e37a5c2 2019-05-12 jcs free(parent);
7097 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7098 56e0773d 2019-11-28 stsp s->selected_entry))) {
7099 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7100 640cd7ff 2022-06-22 mark view->count = 0;
7101 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7102 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7105 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7106 941e9f74 2019-05-21 stsp if (err) {
7107 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7110 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7111 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7113 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7114 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7115 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7116 640cd7ff 2022-06-22 mark view->count = 0;
7119 640cd7ff 2022-06-22 mark view->count = 0;
7123 ffd1d5e5 2018-06-23 stsp return err;
7126 ffd1d5e5 2018-06-23 stsp __dead static void
7127 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7130 87411fa9 2022-06-16 stsp fprintf(stderr,
7131 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7132 ffd1d5e5 2018-06-23 stsp getprogname());
7136 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7137 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7139 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7140 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7141 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7142 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7143 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7144 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7145 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7146 4e97c21c 2020-12-06 stsp char *label = NULL;
7147 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7148 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7150 5221c383 2018-08-01 stsp struct tog_view *view;
7151 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7153 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7154 ffd1d5e5 2018-06-23 stsp switch (ch) {
7156 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7159 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7160 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7161 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7165 e99e2d15 2020-11-24 naddy usage_tree();
7166 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7170 ffd1d5e5 2018-06-23 stsp argc -= optind;
7171 ffd1d5e5 2018-06-23 stsp argv += optind;
7173 55cccc34 2020-02-20 stsp if (argc > 1)
7174 e99e2d15 2020-11-24 naddy usage_tree();
7176 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7177 0ae84acc 2022-06-15 tracey if (error != NULL)
7178 0ae84acc 2022-06-15 tracey goto done;
7180 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7181 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7182 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7183 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7184 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7185 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7186 c156c7a4 2020-12-18 stsp goto done;
7187 55cccc34 2020-02-20 stsp if (worktree)
7188 52185f70 2019-02-05 stsp repo_path =
7189 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7191 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7192 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7193 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7194 c156c7a4 2020-12-18 stsp goto done;
7198 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7199 c02c541e 2019-03-29 stsp if (error != NULL)
7200 52185f70 2019-02-05 stsp goto done;
7202 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7203 55cccc34 2020-02-20 stsp repo, worktree);
7204 55cccc34 2020-02-20 stsp if (error)
7205 55cccc34 2020-02-20 stsp goto done;
7207 55cccc34 2020-02-20 stsp init_curses();
7209 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7210 c02c541e 2019-03-29 stsp if (error)
7211 52185f70 2019-02-05 stsp goto done;
7213 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7214 51a10b52 2020-12-26 stsp if (error)
7215 51a10b52 2020-12-26 stsp goto done;
7217 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7218 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7219 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7220 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7221 4e97c21c 2020-12-06 stsp if (error)
7222 4e97c21c 2020-12-06 stsp goto done;
7223 4e97c21c 2020-12-06 stsp head_ref_name = label;
7225 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7226 4e97c21c 2020-12-06 stsp if (error == NULL)
7227 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7228 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7229 4e97c21c 2020-12-06 stsp goto done;
7230 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7231 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7232 4e97c21c 2020-12-06 stsp if (error)
7233 4e97c21c 2020-12-06 stsp goto done;
7236 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7237 a44927cc 2022-04-07 stsp if (error)
7238 a44927cc 2022-04-07 stsp goto done;
7240 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7241 5221c383 2018-08-01 stsp if (view == NULL) {
7242 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7243 5221c383 2018-08-01 stsp goto done;
7245 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7246 ad80ab7b 2018-08-04 stsp if (error)
7247 ad80ab7b 2018-08-04 stsp goto done;
7248 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7249 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7250 d91faf3b 2020-12-01 naddy in_repo_path);
7251 55cccc34 2020-02-20 stsp if (error)
7252 55cccc34 2020-02-20 stsp goto done;
7255 55cccc34 2020-02-20 stsp if (worktree) {
7256 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7257 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7258 55cccc34 2020-02-20 stsp worktree = NULL;
7260 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7262 52185f70 2019-02-05 stsp free(repo_path);
7263 e4a0e26d 2020-02-20 stsp free(cwd);
7264 ffd1d5e5 2018-06-23 stsp free(commit_id);
7265 4e97c21c 2020-12-06 stsp free(label);
7267 486cd271 2020-12-06 stsp got_ref_close(ref);
7268 1d0f4054 2021-06-17 stsp if (repo) {
7269 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7270 1d0f4054 2021-06-17 stsp if (error == NULL)
7271 1d0f4054 2021-06-17 stsp error = close_err;
7273 0ae84acc 2022-06-15 tracey if (pack_fds) {
7274 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7275 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7276 0ae84acc 2022-06-15 tracey if (error == NULL)
7277 0ae84acc 2022-06-15 tracey error = pack_err;
7279 51a10b52 2020-12-26 stsp tog_free_refs();
7280 ffd1d5e5 2018-06-23 stsp return error;
7283 6458efa5 2020-11-24 stsp static const struct got_error *
7284 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7286 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7287 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7289 6458efa5 2020-11-24 stsp s->nrefs = 0;
7290 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7291 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7292 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7293 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7294 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7297 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7298 6458efa5 2020-11-24 stsp if (re == NULL)
7299 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7301 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7302 8924d611 2020-12-26 stsp if (re->ref == NULL)
7303 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7304 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7305 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7308 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7309 6458efa5 2020-11-24 stsp return NULL;
7313 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7315 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7317 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7318 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7319 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7320 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7325 6458efa5 2020-11-24 stsp static const struct got_error *
7326 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7328 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7329 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7331 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7332 6458efa5 2020-11-24 stsp s->repo = repo;
7334 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7335 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7337 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7339 6458efa5 2020-11-24 stsp return err;
7341 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7342 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7343 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7344 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7346 6458efa5 2020-11-24 stsp goto done;
7348 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7349 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7350 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7352 6458efa5 2020-11-24 stsp goto done;
7354 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7355 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7356 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7358 cc488aa7 2022-01-23 stsp goto done;
7360 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7361 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7362 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7364 6458efa5 2020-11-24 stsp goto done;
7367 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7368 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7369 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7370 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7371 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7374 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7375 6458efa5 2020-11-24 stsp return err;
7378 6458efa5 2020-11-24 stsp static const struct got_error *
7379 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7381 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7383 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7384 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7386 6458efa5 2020-11-24 stsp return NULL;
7389 6458efa5 2020-11-24 stsp static const struct got_error *
7390 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7391 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7393 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7394 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7395 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7396 6458efa5 2020-11-24 stsp int obj_type;
7398 c42c9805 2020-11-24 stsp *commit_id = NULL;
7400 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7402 6458efa5 2020-11-24 stsp return err;
7404 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7406 6458efa5 2020-11-24 stsp goto done;
7408 6458efa5 2020-11-24 stsp switch (obj_type) {
7409 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7410 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7412 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7413 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7415 6458efa5 2020-11-24 stsp goto done;
7416 c42c9805 2020-11-24 stsp free(obj_id);
7417 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7418 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7420 6458efa5 2020-11-24 stsp goto done;
7421 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7422 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7423 c42c9805 2020-11-24 stsp goto done;
7425 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7426 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7427 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7428 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7429 c42c9805 2020-11-24 stsp goto done;
7433 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7439 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7440 c42c9805 2020-11-24 stsp if (err) {
7441 c42c9805 2020-11-24 stsp free(*commit_id);
7442 c42c9805 2020-11-24 stsp *commit_id = NULL;
7444 c42c9805 2020-11-24 stsp return err;
7447 c42c9805 2020-11-24 stsp static const struct got_error *
7448 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7449 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7451 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7452 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7453 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7455 c42c9805 2020-11-24 stsp *new_view = NULL;
7457 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7458 c42c9805 2020-11-24 stsp if (err) {
7459 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7460 c42c9805 2020-11-24 stsp return err;
7462 c42c9805 2020-11-24 stsp return NULL;
7465 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7466 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7467 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7468 6458efa5 2020-11-24 stsp goto done;
7471 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7472 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7475 6458efa5 2020-11-24 stsp view_close(log_view);
7477 6458efa5 2020-11-24 stsp *new_view = log_view;
7478 c42c9805 2020-11-24 stsp free(commit_id);
7479 6458efa5 2020-11-24 stsp return err;
7482 ce5b7c56 2019-07-09 stsp static void
7483 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7485 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7486 34ba6917 2020-11-30 stsp int i = 0;
7488 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7491 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7492 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7493 34ba6917 2020-11-30 stsp if (re == NULL)
7495 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7496 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7500 9b058f45 2022-06-30 mark static const struct got_error *
7501 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7503 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7504 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7505 6458efa5 2020-11-24 stsp int n = 0;
7507 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7508 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7510 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7512 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7513 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7514 94b80cfa 2022-08-01 mark if (last) {
7515 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7516 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7518 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7519 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7520 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7524 9b058f45 2022-06-30 mark return NULL;
7527 6458efa5 2020-11-24 stsp static const struct got_error *
7528 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7530 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7532 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7533 6458efa5 2020-11-24 stsp return NULL;
7536 6458efa5 2020-11-24 stsp static int
7537 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7539 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7541 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7545 6458efa5 2020-11-24 stsp static const struct got_error *
7546 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7548 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7549 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7551 6458efa5 2020-11-24 stsp if (!view->searching) {
7552 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7553 6458efa5 2020-11-24 stsp return NULL;
7556 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7557 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7558 6458efa5 2020-11-24 stsp if (s->selected_entry)
7559 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7561 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7562 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7564 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7565 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7567 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7568 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7571 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7572 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7573 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7574 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7576 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7579 6458efa5 2020-11-24 stsp while (1) {
7580 6458efa5 2020-11-24 stsp if (re == NULL) {
7581 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7582 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7583 6458efa5 2020-11-24 stsp return NULL;
7585 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7586 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7588 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7591 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7592 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7593 6458efa5 2020-11-24 stsp s->matched_entry = re;
7597 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7598 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7600 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7603 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7604 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7605 6458efa5 2020-11-24 stsp s->selected = 0;
7608 6458efa5 2020-11-24 stsp return NULL;
7611 6458efa5 2020-11-24 stsp static const struct got_error *
7612 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7614 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7615 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7616 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7617 6458efa5 2020-11-24 stsp char *line = NULL;
7618 6458efa5 2020-11-24 stsp wchar_t *wline;
7619 6458efa5 2020-11-24 stsp struct tog_color *tc;
7620 6458efa5 2020-11-24 stsp int width, n;
7621 6458efa5 2020-11-24 stsp int limit = view->nlines;
7623 6458efa5 2020-11-24 stsp werase(view->window);
7625 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7626 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7627 9b058f45 2022-06-30 mark --limit; /* border */
7629 6458efa5 2020-11-24 stsp if (limit == 0)
7630 6458efa5 2020-11-24 stsp return NULL;
7632 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7634 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7635 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7636 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7638 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7639 6458efa5 2020-11-24 stsp if (err) {
7640 6458efa5 2020-11-24 stsp free(line);
7641 6458efa5 2020-11-24 stsp return err;
7643 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7644 6458efa5 2020-11-24 stsp wstandout(view->window);
7645 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7646 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7647 6458efa5 2020-11-24 stsp wstandend(view->window);
7648 6458efa5 2020-11-24 stsp free(wline);
7649 6458efa5 2020-11-24 stsp wline = NULL;
7650 6458efa5 2020-11-24 stsp free(line);
7651 6458efa5 2020-11-24 stsp line = NULL;
7652 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7653 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7654 6458efa5 2020-11-24 stsp if (--limit <= 0)
7655 6458efa5 2020-11-24 stsp return NULL;
7658 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7659 6458efa5 2020-11-24 stsp char *line = NULL;
7660 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7662 b4996bee 2022-06-16 stsp if (s->show_date) {
7663 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7664 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7665 b4996bee 2022-06-16 stsp struct got_object_id *id;
7666 b4996bee 2022-06-16 stsp struct tm tm;
7669 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7671 b4996bee 2022-06-16 stsp return err;
7672 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7673 b4996bee 2022-06-16 stsp if (err) {
7674 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7676 b4996bee 2022-06-16 stsp return err;
7678 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7680 b4996bee 2022-06-16 stsp if (err) {
7682 b4996bee 2022-06-16 stsp return err;
7684 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7685 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7687 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7688 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7691 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7692 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7693 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7694 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7696 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7697 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7698 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7699 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7700 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7701 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7702 6458efa5 2020-11-24 stsp struct got_object_id *id;
7703 6458efa5 2020-11-24 stsp char *id_str;
7704 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7706 6458efa5 2020-11-24 stsp return err;
7707 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7708 6458efa5 2020-11-24 stsp if (err) {
7710 6458efa5 2020-11-24 stsp return err;
7712 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7713 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7714 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7716 6458efa5 2020-11-24 stsp free(id_str);
7717 6458efa5 2020-11-24 stsp return err;
7720 6458efa5 2020-11-24 stsp free(id_str);
7721 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7722 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7723 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7725 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7727 6458efa5 2020-11-24 stsp if (err) {
7728 6458efa5 2020-11-24 stsp free(line);
7729 6458efa5 2020-11-24 stsp return err;
7731 6458efa5 2020-11-24 stsp if (n == s->selected) {
7732 6458efa5 2020-11-24 stsp if (view->focussed)
7733 6458efa5 2020-11-24 stsp wstandout(view->window);
7734 6458efa5 2020-11-24 stsp s->selected_entry = re;
7736 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7738 6458efa5 2020-11-24 stsp wattr_on(view->window,
7739 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7740 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7742 6458efa5 2020-11-24 stsp wattr_off(view->window,
7743 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7744 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7745 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7746 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7747 6458efa5 2020-11-24 stsp wstandend(view->window);
7748 6458efa5 2020-11-24 stsp free(line);
7749 6458efa5 2020-11-24 stsp free(wline);
7750 6458efa5 2020-11-24 stsp wline = NULL;
7752 6458efa5 2020-11-24 stsp s->ndisplayed++;
7753 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7756 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7759 9b058f45 2022-06-30 mark view_border(view);
7760 6458efa5 2020-11-24 stsp return err;
7763 6458efa5 2020-11-24 stsp static const struct got_error *
7764 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7765 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7767 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7768 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7769 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7771 c42c9805 2020-11-24 stsp *new_view = NULL;
7773 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7774 c42c9805 2020-11-24 stsp if (err) {
7775 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7776 c42c9805 2020-11-24 stsp return err;
7778 c42c9805 2020-11-24 stsp return NULL;
7782 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7783 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7784 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7785 c42c9805 2020-11-24 stsp goto done;
7788 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7789 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7791 c42c9805 2020-11-24 stsp goto done;
7793 c42c9805 2020-11-24 stsp *new_view = tree_view;
7795 c42c9805 2020-11-24 stsp free(commit_id);
7796 c42c9805 2020-11-24 stsp return err;
7799 94b80cfa 2022-08-01 mark static const struct got_error *
7800 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
7802 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7803 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
7804 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
7806 94b80cfa 2022-08-01 mark g = view->gline;
7807 94b80cfa 2022-08-01 mark view->gline = 0;
7809 94b80cfa 2022-08-01 mark if (g == 0)
7811 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
7812 94b80cfa 2022-08-01 mark g = s->nrefs;
7814 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
7815 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
7816 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
7817 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
7818 94b80cfa 2022-08-01 mark return NULL;
7821 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
7822 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
7824 94b80cfa 2022-08-01 mark return err;
7825 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7826 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
7827 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
7828 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
7829 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
7830 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
7832 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
7833 94b80cfa 2022-08-01 mark s->selected = g - 1;
7835 94b80cfa 2022-08-01 mark return NULL;
7839 c42c9805 2020-11-24 stsp static const struct got_error *
7840 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7842 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7843 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7844 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7845 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
7847 94b80cfa 2022-08-01 mark if (view->gline)
7848 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
7850 6458efa5 2020-11-24 stsp switch (ch) {
7852 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7853 640cd7ff 2022-06-22 mark view->count = 0;
7856 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7857 640cd7ff 2022-06-22 mark view->count = 0;
7860 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7861 640cd7ff 2022-06-22 mark view->count = 0;
7862 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7863 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7864 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7867 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7868 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7869 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7872 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7873 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7875 6458efa5 2020-11-24 stsp case KEY_ENTER:
7876 6458efa5 2020-11-24 stsp case '\r':
7877 640cd7ff 2022-06-22 mark view->count = 0;
7878 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7880 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7883 640cd7ff 2022-06-22 mark view->count = 0;
7884 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7886 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
7888 e4526bf5 2021-09-03 naddy case 'g':
7889 e4526bf5 2021-09-03 naddy case KEY_HOME:
7890 e4526bf5 2021-09-03 naddy s->selected = 0;
7891 640cd7ff 2022-06-22 mark view->count = 0;
7892 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7894 e4526bf5 2021-09-03 naddy case 'G':
7895 9b058f45 2022-06-30 mark case KEY_END: {
7896 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7898 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7899 9b058f45 2022-06-30 mark --eos; /* border */
7900 e4526bf5 2021-09-03 naddy s->selected = 0;
7901 640cd7ff 2022-06-22 mark view->count = 0;
7902 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7903 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7904 e4526bf5 2021-09-03 naddy if (re == NULL)
7906 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7907 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7909 e4526bf5 2021-09-03 naddy if (n > 0)
7910 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7914 6458efa5 2020-11-24 stsp case KEY_UP:
7915 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7916 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7917 6458efa5 2020-11-24 stsp s->selected--;
7920 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7921 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7922 640cd7ff 2022-06-22 mark view->count = 0;
7924 83cc4199 2022-06-13 stsp case CTRL('u'):
7926 83cc4199 2022-06-13 stsp nscroll /= 2;
7927 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7928 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7929 6458efa5 2020-11-24 stsp case CTRL('b'):
7931 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7932 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7933 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7934 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7935 640cd7ff 2022-06-22 mark view->count = 0;
7938 6458efa5 2020-11-24 stsp case KEY_DOWN:
7939 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7940 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7941 6458efa5 2020-11-24 stsp s->selected++;
7944 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7945 6458efa5 2020-11-24 stsp /* can't scroll any further */
7946 640cd7ff 2022-06-22 mark view->count = 0;
7949 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7951 83cc4199 2022-06-13 stsp case CTRL('d'):
7953 83cc4199 2022-06-13 stsp nscroll /= 2;
7954 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7955 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7956 6458efa5 2020-11-24 stsp case CTRL('f'):
7958 48bb96f0 2022-06-20 naddy case ' ':
7959 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7960 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7961 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7962 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7963 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7964 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7965 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7966 640cd7ff 2022-06-22 mark view->count = 0;
7969 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7971 6458efa5 2020-11-24 stsp case CTRL('l'):
7972 640cd7ff 2022-06-22 mark view->count = 0;
7973 8924d611 2020-12-26 stsp tog_free_refs();
7974 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7977 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7978 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7980 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7981 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7982 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7985 640cd7ff 2022-06-22 mark view->count = 0;
7989 6458efa5 2020-11-24 stsp return err;
7992 6458efa5 2020-11-24 stsp __dead static void
7993 6458efa5 2020-11-24 stsp usage_ref(void)
7996 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7997 6458efa5 2020-11-24 stsp getprogname());
8001 6458efa5 2020-11-24 stsp static const struct got_error *
8002 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8004 6458efa5 2020-11-24 stsp const struct got_error *error;
8005 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8006 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8007 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8009 6458efa5 2020-11-24 stsp struct tog_view *view;
8010 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8012 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8013 6458efa5 2020-11-24 stsp switch (ch) {
8015 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8016 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8017 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8021 e99e2d15 2020-11-24 naddy usage_ref();
8022 6458efa5 2020-11-24 stsp /* NOTREACHED */
8026 6458efa5 2020-11-24 stsp argc -= optind;
8027 6458efa5 2020-11-24 stsp argv += optind;
8029 6458efa5 2020-11-24 stsp if (argc > 1)
8030 e99e2d15 2020-11-24 naddy usage_ref();
8032 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8033 0ae84acc 2022-06-15 tracey if (error != NULL)
8034 0ae84acc 2022-06-15 tracey goto done;
8036 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8037 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8038 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8039 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8040 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8041 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8042 c156c7a4 2020-12-18 stsp goto done;
8043 6458efa5 2020-11-24 stsp if (worktree)
8044 6458efa5 2020-11-24 stsp repo_path =
8045 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8047 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8048 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8049 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8050 c156c7a4 2020-12-18 stsp goto done;
8054 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8055 6458efa5 2020-11-24 stsp if (error != NULL)
8056 6458efa5 2020-11-24 stsp goto done;
8058 6458efa5 2020-11-24 stsp init_curses();
8060 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8061 51a10b52 2020-12-26 stsp if (error)
8062 51a10b52 2020-12-26 stsp goto done;
8064 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8065 6458efa5 2020-11-24 stsp if (error)
8066 6458efa5 2020-11-24 stsp goto done;
8068 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8069 6458efa5 2020-11-24 stsp if (view == NULL) {
8070 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8071 6458efa5 2020-11-24 stsp goto done;
8074 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8075 6458efa5 2020-11-24 stsp if (error)
8076 6458efa5 2020-11-24 stsp goto done;
8078 6458efa5 2020-11-24 stsp if (worktree) {
8079 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8080 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8081 6458efa5 2020-11-24 stsp worktree = NULL;
8083 6458efa5 2020-11-24 stsp error = view_loop(view);
8085 6458efa5 2020-11-24 stsp free(repo_path);
8086 6458efa5 2020-11-24 stsp free(cwd);
8087 1d0f4054 2021-06-17 stsp if (repo) {
8088 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8089 1d0f4054 2021-06-17 stsp if (close_err)
8090 1d0f4054 2021-06-17 stsp error = close_err;
8092 0ae84acc 2022-06-15 tracey if (pack_fds) {
8093 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8094 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8095 0ae84acc 2022-06-15 tracey if (error == NULL)
8096 0ae84acc 2022-06-15 tracey error = pack_err;
8098 51a10b52 2020-12-26 stsp tog_free_refs();
8099 6458efa5 2020-11-24 stsp return error;
8102 136e2bd4 2022-07-23 mark static const struct got_error *
8103 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8104 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
8106 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
8108 136e2bd4 2022-07-23 mark *new_view = NULL;
8110 136e2bd4 2022-07-23 mark switch (request) {
8111 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
8112 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
8113 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
8115 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
8116 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
8117 136e2bd4 2022-07-23 mark view, s->repo);
8119 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8120 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8122 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
8123 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
8124 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
8126 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
8127 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
8130 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8131 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8133 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
8134 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
8135 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
8136 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
8137 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8138 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
8139 136e2bd4 2022-07-23 mark &view->state.tree);
8140 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8141 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
8142 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8143 136e2bd4 2022-07-23 mark view->state.ref.repo);
8145 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8146 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8148 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
8149 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8150 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
8151 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
8152 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
8153 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
8154 136e2bd4 2022-07-23 mark view->state.log.repo);
8155 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8156 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
8157 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8158 136e2bd4 2022-07-23 mark view->state.ref.repo);
8160 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8161 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8163 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
8164 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8165 136e2bd4 2022-07-23 mark if (*new_view == NULL)
8166 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
8167 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8168 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
8169 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8170 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
8172 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
8173 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8175 136e2bd4 2022-07-23 mark view_close(*new_view);
8178 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8181 136e2bd4 2022-07-23 mark return err;
8185 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
8186 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
8188 6458efa5 2020-11-24 stsp static void
8189 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
8191 9b058f45 2022-06-30 mark switch (view->type) {
8192 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8193 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8194 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
8195 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
8199 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
8200 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
8202 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
8203 9b058f45 2022-06-30 mark s->selected_line += view->offset;
8206 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
8207 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
8208 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
8210 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8211 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8212 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8214 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8215 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8216 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8222 9b058f45 2022-06-30 mark view->offset = 0;
8226 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8227 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8229 9b058f45 2022-06-30 mark static const struct got_error *
8230 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8232 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8233 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8234 9b058f45 2022-06-30 mark int *selected = NULL;
8235 9b058f45 2022-06-30 mark int header, offset;
8237 9b058f45 2022-06-30 mark switch (view->type) {
8238 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8239 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8240 9b058f45 2022-06-30 mark header = 3;
8241 9b058f45 2022-06-30 mark scrolld = NULL;
8242 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8243 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8244 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8245 9b058f45 2022-06-30 mark s->selected_line -= offset;
8246 9b058f45 2022-06-30 mark view->offset = offset;
8250 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8251 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8252 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8253 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8254 9b058f45 2022-06-30 mark selected = &s->selected;
8257 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8258 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8259 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8260 9b058f45 2022-06-30 mark header = 3;
8261 9b058f45 2022-06-30 mark selected = &s->selected;
8264 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8265 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8266 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8267 9b058f45 2022-06-30 mark header = 5;
8268 9b058f45 2022-06-30 mark selected = &s->selected;
8272 9b058f45 2022-06-30 mark selected = NULL;
8273 9b058f45 2022-06-30 mark scrolld = NULL;
8274 9b058f45 2022-06-30 mark header = 0;
8278 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8279 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8280 9b058f45 2022-06-30 mark view->offset = offset;
8281 9b058f45 2022-06-30 mark if (scrolld && offset) {
8282 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8283 9b058f45 2022-06-30 mark *selected -= offset;
8287 9b058f45 2022-06-30 mark return err;
8290 9b058f45 2022-06-30 mark static void
8291 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8295 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8296 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8297 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8298 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8300 6879ba42 2020-10-01 naddy fputc('\n', fp);
8303 4ed7e80c 2018-05-20 stsp __dead static void
8304 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8306 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8308 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8309 6879ba42 2020-10-01 naddy getprogname());
8310 6879ba42 2020-10-01 naddy if (hflag) {
8311 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8312 6879ba42 2020-10-01 naddy list_commands(fp);
8314 6879ba42 2020-10-01 naddy exit(status);
8317 c2301be8 2018-04-30 stsp static char **
8318 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8320 ee85c5e8 2020-02-29 stsp va_list ap;
8321 c2301be8 2018-04-30 stsp char **argv;
8324 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8326 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8327 c2301be8 2018-04-30 stsp if (argv == NULL)
8328 c2301be8 2018-04-30 stsp err(1, "calloc");
8329 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8330 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8331 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8332 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8335 ee85c5e8 2020-02-29 stsp va_end(ap);
8336 c2301be8 2018-04-30 stsp return argv;
8340 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8341 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8342 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8343 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8344 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8346 ee85c5e8 2020-02-29 stsp static const struct got_error *
8347 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8349 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8350 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8351 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8352 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8353 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8354 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8355 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8356 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8357 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8359 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8360 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8361 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8363 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8364 0ae84acc 2022-06-15 tracey if (error != NULL)
8365 0ae84acc 2022-06-15 tracey goto done;
8367 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8368 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8369 ee85c5e8 2020-02-29 stsp goto done;
8371 ee85c5e8 2020-02-29 stsp if (worktree)
8372 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8374 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8375 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8376 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8377 ee85c5e8 2020-02-29 stsp goto done;
8380 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8381 ee85c5e8 2020-02-29 stsp if (error != NULL)
8382 ee85c5e8 2020-02-29 stsp goto done;
8384 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8385 ee85c5e8 2020-02-29 stsp repo, worktree);
8386 ee85c5e8 2020-02-29 stsp if (error)
8387 ee85c5e8 2020-02-29 stsp goto done;
8389 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8390 84de9106 2020-12-26 stsp if (error)
8391 84de9106 2020-12-26 stsp goto done;
8392 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8393 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8394 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8395 ee85c5e8 2020-02-29 stsp if (error)
8396 ee85c5e8 2020-02-29 stsp goto done;
8398 ee85c5e8 2020-02-29 stsp if (worktree) {
8399 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8400 ee85c5e8 2020-02-29 stsp worktree = NULL;
8403 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8404 a44927cc 2022-04-07 stsp if (error)
8405 a44927cc 2022-04-07 stsp goto done;
8407 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8408 ee85c5e8 2020-02-29 stsp if (error) {
8409 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8410 ee85c5e8 2020-02-29 stsp goto done;
8411 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8412 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8413 6879ba42 2020-10-01 naddy usage(1, 1);
8414 ee85c5e8 2020-02-29 stsp /* not reached */
8417 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8418 ee85c5e8 2020-02-29 stsp if (error)
8419 ee85c5e8 2020-02-29 stsp goto done;
8421 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8423 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8424 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8426 1d0f4054 2021-06-17 stsp if (repo) {
8427 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8428 1d0f4054 2021-06-17 stsp if (error == NULL)
8429 1d0f4054 2021-06-17 stsp error = close_err;
8431 a44927cc 2022-04-07 stsp if (commit)
8432 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8433 ee85c5e8 2020-02-29 stsp if (worktree)
8434 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8435 0ae84acc 2022-06-15 tracey if (pack_fds) {
8436 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8437 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8438 0ae84acc 2022-06-15 tracey if (error == NULL)
8439 0ae84acc 2022-06-15 tracey error = pack_err;
8442 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8443 ee85c5e8 2020-02-29 stsp free(commit_id);
8444 ee85c5e8 2020-02-29 stsp free(cwd);
8445 ee85c5e8 2020-02-29 stsp free(repo_path);
8446 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8447 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8449 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8450 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8451 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8453 87670572 2020-12-26 naddy tog_free_refs();
8454 ee85c5e8 2020-02-29 stsp return error;
8458 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8460 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8461 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8462 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8463 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8464 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8465 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8466 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8468 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8470 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8472 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8473 9f7d7167 2018-04-29 stsp switch (ch) {
8475 9f7d7167 2018-04-29 stsp hflag = 1;
8478 53ccebc2 2019-07-30 stsp Vflag = 1;
8481 6879ba42 2020-10-01 naddy usage(hflag, 1);
8482 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8486 9f7d7167 2018-04-29 stsp argc -= optind;
8487 9f7d7167 2018-04-29 stsp argv += optind;
8488 9814e6a3 2020-09-27 naddy optind = 1;
8489 c2301be8 2018-04-30 stsp optreset = 1;
8491 53ccebc2 2019-07-30 stsp if (Vflag) {
8492 53ccebc2 2019-07-30 stsp got_version_print_str();
8493 6879ba42 2020-10-01 naddy return 0;
8496 4010e238 2020-12-04 stsp #ifndef PROFILE
8497 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8498 4010e238 2020-12-04 stsp NULL) == -1)
8499 4010e238 2020-12-04 stsp err(1, "pledge");
8502 c2301be8 2018-04-30 stsp if (argc == 0) {
8503 f29d3e89 2018-06-23 stsp if (hflag)
8504 6879ba42 2020-10-01 naddy usage(hflag, 0);
8505 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8506 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8508 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8512 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8513 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8514 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8515 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8516 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8522 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8523 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8524 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8525 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8526 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8527 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8530 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8531 ee85c5e8 2020-02-29 stsp if (argc != 1)
8532 6879ba42 2020-10-01 naddy usage(0, 1);
8533 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8534 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8536 ee85c5e8 2020-02-29 stsp if (hflag)
8537 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8539 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8543 b46c1e04 2020-09-20 naddy putchar('\n');
8544 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8546 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8547 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8548 a2f4a359 2020-02-28 stsp free(cmd_argv);
8551 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8552 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);