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;
140 cc488aa7 2022-01-23 stsp static const struct got_error *
141 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
142 cc488aa7 2022-01-23 stsp struct got_reference* re2)
144 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
145 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
146 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
148 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
149 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
150 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
153 cc488aa7 2022-01-23 stsp return NULL;
154 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
156 cc488aa7 2022-01-23 stsp return NULL;
159 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
160 cc488aa7 2022-01-23 stsp return NULL;
163 11b20872 2019-11-08 stsp static const struct got_error *
164 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
166 51a10b52 2020-12-26 stsp const struct got_error *err;
168 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
169 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
172 51a10b52 2020-12-26 stsp return err;
174 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
178 51a10b52 2020-12-26 stsp static void
179 51a10b52 2020-12-26 stsp tog_free_refs(void)
181 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
182 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
183 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
185 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
188 51a10b52 2020-12-26 stsp static const struct got_error *
189 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
190 11b20872 2019-11-08 stsp int idx, short color)
192 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
193 11b20872 2019-11-08 stsp struct tog_color *tc;
194 11b20872 2019-11-08 stsp int regerr = 0;
196 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
197 11b20872 2019-11-08 stsp return NULL;
199 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
201 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
202 11b20872 2019-11-08 stsp if (tc == NULL)
203 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
204 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
205 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
206 11b20872 2019-11-08 stsp if (regerr) {
207 11b20872 2019-11-08 stsp static char regerr_msg[512];
208 11b20872 2019-11-08 stsp static char err_msg[512];
209 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
210 11b20872 2019-11-08 stsp sizeof(regerr_msg));
211 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
212 11b20872 2019-11-08 stsp regerr_msg);
213 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 11b20872 2019-11-08 stsp return err;
217 11b20872 2019-11-08 stsp tc->colorpair = idx;
218 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
219 11b20872 2019-11-08 stsp return NULL;
222 11b20872 2019-11-08 stsp static void
223 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
225 11b20872 2019-11-08 stsp struct tog_color *tc;
227 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
228 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
229 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
230 11b20872 2019-11-08 stsp regfree(&tc->regex);
235 336075a4 2022-06-25 op static struct tog_color *
236 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
238 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
240 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
241 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
245 11b20872 2019-11-08 stsp return NULL;
249 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
251 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
252 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
253 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
254 11b20872 2019-11-08 stsp return COLOR_CYAN;
255 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
256 11b20872 2019-11-08 stsp return COLOR_YELLOW;
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
258 11b20872 2019-11-08 stsp return COLOR_GREEN;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
260 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
262 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
264 91b8c405 2020-01-25 stsp return COLOR_CYAN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_GREEN;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
268 11b20872 2019-11-08 stsp return COLOR_GREEN;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
270 11b20872 2019-11-08 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_YELLOW;
273 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
274 6458efa5 2020-11-24 stsp return COLOR_GREEN;
275 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
276 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
277 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
278 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
279 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
280 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
286 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
288 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
290 11b20872 2019-11-08 stsp if (val == NULL)
291 11b20872 2019-11-08 stsp return default_color_value(envvar);
293 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
294 11b20872 2019-11-08 stsp return COLOR_BLACK;
295 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
296 11b20872 2019-11-08 stsp return COLOR_RED;
297 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
298 11b20872 2019-11-08 stsp return COLOR_GREEN;
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
300 11b20872 2019-11-08 stsp return COLOR_YELLOW;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
302 11b20872 2019-11-08 stsp return COLOR_BLUE;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
304 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
306 11b20872 2019-11-08 stsp return COLOR_CYAN;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
308 11b20872 2019-11-08 stsp return COLOR_WHITE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
312 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 15a087fe 2019-02-21 stsp int first_displayed_line;
322 15a087fe 2019-02-21 stsp int last_displayed_line;
324 15a087fe 2019-02-21 stsp int diff_context;
325 3dbaef42 2020-11-24 stsp int ignore_whitespace;
326 64453f7e 2020-11-21 stsp int force_text_diff;
327 15a087fe 2019-02-21 stsp struct got_repository *repo;
328 bddb1296 2019-11-08 stsp struct tog_colors colors;
329 fe621944 2020-11-10 stsp size_t nlines;
330 f44b1f58 2020-02-02 tracey off_t *line_offsets;
331 f44b1f58 2020-02-02 tracey int matched_line;
332 f44b1f58 2020-02-02 tracey int selected_line;
334 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
335 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
338 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
341 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
342 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
343 1a76625f 2018-10-22 stsp int commits_needed;
344 fb280deb 2021-08-30 stsp int load_all;
345 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
346 1a76625f 2018-10-22 stsp struct commit_queue *commits;
347 1a76625f 2018-10-22 stsp const char *in_repo_path;
348 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
349 1a76625f 2018-10-22 stsp struct got_repository *repo;
350 74467cc8 2022-06-15 stsp int *pack_fds;
351 1a76625f 2018-10-22 stsp int log_complete;
352 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
353 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
354 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
355 13add988 2019-10-15 stsp int *searching;
356 13add988 2019-10-15 stsp int *search_next_done;
357 13add988 2019-10-15 stsp regex_t *regex;
360 1a76625f 2018-10-22 stsp struct tog_log_view_state {
361 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
362 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
363 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
365 b01e7d3b 2018-08-04 stsp int selected;
366 b01e7d3b 2018-08-04 stsp char *in_repo_path;
367 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
368 b672a97a 2020-01-27 stsp int log_branches;
369 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
370 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
371 1a76625f 2018-10-22 stsp sig_atomic_t quit;
372 1a76625f 2018-10-22 stsp pthread_t thread;
373 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
374 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
375 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
376 11b20872 2019-11-08 stsp struct tog_colors colors;
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
387 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
388 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
389 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
390 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
391 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
393 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
395 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
396 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
397 e9424729 2018-08-04 stsp int nlines;
399 e9424729 2018-08-04 stsp struct tog_view *view;
400 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
404 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
405 e9424729 2018-08-04 stsp const char *path;
406 e9424729 2018-08-04 stsp struct got_repository *repo;
407 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
408 e9424729 2018-08-04 stsp int *complete;
409 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
410 fc06ba56 2019-08-22 stsp void *cancel_arg;
413 e9424729 2018-08-04 stsp struct tog_blame {
415 be659d10 2020-11-18 stsp off_t filesize;
416 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
417 6fcac457 2018-11-19 stsp int nlines;
418 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
419 e9424729 2018-08-04 stsp pthread_t thread;
420 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
421 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
422 e9424729 2018-08-04 stsp const char *path;
423 0ae84acc 2022-06-15 tracey int *pack_fds;
426 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
427 7cbe629d 2018-08-04 stsp int first_displayed_line;
428 7cbe629d 2018-08-04 stsp int last_displayed_line;
429 7cbe629d 2018-08-04 stsp int selected_line;
430 7cbe629d 2018-08-04 stsp int blame_complete;
433 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
434 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
435 e5a0f69f 2018-08-18 stsp char *path;
436 7cbe629d 2018-08-04 stsp struct got_repository *repo;
437 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
438 e9424729 2018-08-04 stsp struct tog_blame blame;
439 6c4c42e0 2019-06-24 stsp int matched_line;
440 11b20872 2019-11-08 stsp struct tog_colors colors;
443 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
444 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
445 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
446 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
447 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
448 ad80ab7b 2018-08-04 stsp int selected;
451 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
453 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
454 ad80ab7b 2018-08-04 stsp char *tree_label;
455 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
456 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
457 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
458 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
459 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
460 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
461 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
462 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
463 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
464 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
465 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
466 6458efa5 2020-11-24 stsp struct tog_colors colors;
469 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
470 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
471 6458efa5 2020-11-24 stsp struct got_reference *ref;
475 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
477 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
478 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
479 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
481 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
482 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
483 6458efa5 2020-11-24 stsp struct got_repository *repo;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
485 bddb1296 2019-11-08 stsp struct tog_colors colors;
489 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
491 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
492 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
493 669b5ffa 2018-10-07 stsp * there is enough screen estate.
495 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
496 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
498 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
499 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
500 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
502 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
503 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
505 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
507 cc3c9aac 2018-08-01 stsp struct tog_view {
508 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
509 26ed57b2 2018-05-19 stsp WINDOW *window;
510 26ed57b2 2018-05-19 stsp PANEL *panel;
511 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
512 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
513 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
514 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
515 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
516 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
518 669b5ffa 2018-10-07 stsp struct tog_view *parent;
519 669b5ffa 2018-10-07 stsp struct tog_view *child;
522 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
523 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
524 e78dc838 2020-12-04 stsp * between parent and child.
525 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
526 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
527 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
528 e78dc838 2020-12-04 stsp * situations.
530 e78dc838 2020-12-04 stsp int focus_child;
532 9b058f45 2022-06-30 mark enum tog_view_mode mode;
533 5dc9f4bc 2018-08-04 stsp /* type-specific state */
534 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
536 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
537 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
538 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
539 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
540 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
543 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
544 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
545 e78dc838 2020-12-04 stsp struct tog_view *, int);
546 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
548 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
549 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
550 c0c4acc8 2021-01-24 stsp int search_started;
551 60493ae3 2019-06-20 stsp int searching;
552 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
553 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
554 60493ae3 2019-06-20 stsp int search_next_done;
555 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
556 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
557 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
558 1803e47f 2019-06-22 stsp regex_t regex;
559 41605754 2020-11-12 stsp regmatch_t regmatch;
562 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
563 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
564 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
565 78756c87 2020-11-24 stsp struct got_repository *);
566 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
567 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
568 e78dc838 2020-12-04 stsp struct tog_view *, int);
569 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
570 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
571 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
573 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
574 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
575 78756c87 2020-11-24 stsp const char *, const char *, int);
576 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
577 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
578 e78dc838 2020-12-04 stsp struct tog_view *, int);
579 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
580 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
581 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
584 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
585 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
587 e78dc838 2020-12-04 stsp struct tog_view *, int);
588 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
589 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
590 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
592 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
593 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
594 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
596 e78dc838 2020-12-04 stsp struct tog_view *, int);
597 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
598 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
599 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
601 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
602 6458efa5 2020-11-24 stsp struct got_repository *);
603 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
604 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
605 e78dc838 2020-12-04 stsp struct tog_view *, int);
606 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
607 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
608 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
610 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
611 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
612 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
613 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
614 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
616 25791caa 2018-10-24 stsp static void
617 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
619 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
622 83baff54 2019-08-12 stsp static void
623 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
625 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
628 61266923 2020-01-14 stsp static void
629 61266923 2020-01-14 stsp tog_sigcont(int signo)
631 61266923 2020-01-14 stsp tog_sigcont_received = 1;
634 2497f032 2022-05-31 stsp static void
635 2497f032 2022-05-31 stsp tog_sigint(int signo)
637 2497f032 2022-05-31 stsp tog_sigint_received = 1;
640 2497f032 2022-05-31 stsp static void
641 2497f032 2022-05-31 stsp tog_sigterm(int signo)
643 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
647 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
649 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
650 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
654 e5a0f69f 2018-08-18 stsp static const struct got_error *
655 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
657 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
659 669b5ffa 2018-10-07 stsp if (view->child) {
660 669b5ffa 2018-10-07 stsp view_close(view->child);
661 669b5ffa 2018-10-07 stsp view->child = NULL;
663 e5a0f69f 2018-08-18 stsp if (view->close)
664 e5a0f69f 2018-08-18 stsp err = view->close(view);
665 ea5e7bb5 2018-08-01 stsp if (view->panel)
666 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
667 ea5e7bb5 2018-08-01 stsp if (view->window)
668 ea5e7bb5 2018-08-01 stsp delwin(view->window);
669 ea5e7bb5 2018-08-01 stsp free(view);
670 e5a0f69f 2018-08-18 stsp return err;
673 ea5e7bb5 2018-08-01 stsp static struct tog_view *
674 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
675 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
677 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
679 ea5e7bb5 2018-08-01 stsp if (view == NULL)
680 ea5e7bb5 2018-08-01 stsp return NULL;
682 d6b05b5b 2018-08-04 stsp view->type = type;
683 f7d12f7e 2018-08-01 stsp view->lines = LINES;
684 f7d12f7e 2018-08-01 stsp view->cols = COLS;
685 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
686 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
687 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
688 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
689 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
690 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
691 96a765a8 2018-08-04 stsp view_close(view);
692 ea5e7bb5 2018-08-01 stsp return NULL;
694 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
695 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
696 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
697 96a765a8 2018-08-04 stsp view_close(view);
698 ea5e7bb5 2018-08-01 stsp return NULL;
701 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
702 ea5e7bb5 2018-08-01 stsp return view;
706 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
708 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
710 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
713 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
715 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
717 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
720 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
722 5c60c32a 2018-10-18 stsp static const struct got_error *
723 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
725 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
727 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
728 9b058f45 2022-06-30 mark view->begin_y = view_split_begin_y(view->nlines);
729 9b058f45 2022-06-30 mark view->begin_x = 0;
731 9b058f45 2022-06-30 mark view->begin_x = view_split_begin_x(0);
732 9b058f45 2022-06-30 mark view->begin_y = 0;
734 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
735 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
736 5c60c32a 2018-10-18 stsp view->lines = LINES;
737 5c60c32a 2018-10-18 stsp view->cols = COLS;
738 5c60c32a 2018-10-18 stsp err = view_resize(view);
740 5c60c32a 2018-10-18 stsp return err;
742 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
743 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
745 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
746 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
748 5c60c32a 2018-10-18 stsp return NULL;
751 5c60c32a 2018-10-18 stsp static const struct got_error *
752 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
754 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
756 5c60c32a 2018-10-18 stsp view->begin_x = 0;
757 5c60c32a 2018-10-18 stsp view->begin_y = 0;
758 5c60c32a 2018-10-18 stsp view->nlines = LINES;
759 5c60c32a 2018-10-18 stsp view->ncols = COLS;
760 5c60c32a 2018-10-18 stsp view->lines = LINES;
761 5c60c32a 2018-10-18 stsp view->cols = COLS;
762 5c60c32a 2018-10-18 stsp err = view_resize(view);
764 5c60c32a 2018-10-18 stsp return err;
766 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
767 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
769 5c60c32a 2018-10-18 stsp return NULL;
773 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
775 5c60c32a 2018-10-18 stsp return view->parent == NULL;
779 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
781 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
784 9b058f45 2022-06-30 mark static void
785 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
787 9b058f45 2022-06-30 mark PANEL *panel;
788 9b058f45 2022-06-30 mark const struct tog_view *view_above;
790 9b058f45 2022-06-30 mark if (view->parent)
791 9b058f45 2022-06-30 mark return view_border(view->parent);
793 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
794 9b058f45 2022-06-30 mark if (panel == NULL)
797 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
798 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
799 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
800 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
801 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
803 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
804 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
807 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
808 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
809 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
811 4d8c2215 2018-08-19 stsp static const struct got_error *
812 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
814 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
815 9b058f45 2022-06-30 mark int dif, nlines, ncols;
817 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
819 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
820 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
822 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
823 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
824 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
826 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
828 4dd27a72 2022-06-29 stsp if (view->child) {
829 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
831 c71ed39a 2022-06-29 stsp if (view->child->focussed)
832 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
833 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
834 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
835 0dbbbe90 2022-06-17 op ncols = COLS;
837 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
838 5c60c32a 2018-10-18 stsp if (view->child->focussed)
839 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
841 5c60c32a 2018-10-18 stsp show_panel(view->panel);
843 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
845 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
846 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
849 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
850 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
852 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
853 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
855 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
856 9b058f45 2022-06-30 mark view->lines - ts->selected) {
857 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
858 9b058f45 2022-06-30 mark view->lines - ts->selected -
859 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
860 9b058f45 2022-06-30 mark err = request_log_commits(view);
862 9b058f45 2022-06-30 mark return err;
867 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
868 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
869 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
872 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
874 9b058f45 2022-06-30 mark return err;
875 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
876 9b058f45 2022-06-30 mark err = offset_selection_down(view);
878 9b058f45 2022-06-30 mark return err;
880 9b058f45 2022-06-30 mark view_border(view);
881 9b058f45 2022-06-30 mark update_panels();
882 9b058f45 2022-06-30 mark doupdate();
883 9b058f45 2022-06-30 mark show_panel(view->child->panel);
884 9b058f45 2022-06-30 mark nlines = view->nlines;
886 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
887 0dbbbe90 2022-06-17 op ncols = COLS;
889 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
890 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
891 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
892 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
893 0dbbbe90 2022-06-17 op wclear(view->window);
895 0dbbbe90 2022-06-17 op view->nlines = nlines;
896 0dbbbe90 2022-06-17 op view->ncols = ncols;
897 0dbbbe90 2022-06-17 op view->lines = LINES;
898 0dbbbe90 2022-06-17 op view->cols = COLS;
900 5c60c32a 2018-10-18 stsp return NULL;
903 669b5ffa 2018-10-07 stsp static const struct got_error *
904 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
906 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
908 669b5ffa 2018-10-07 stsp if (view->child == NULL)
909 669b5ffa 2018-10-07 stsp return NULL;
911 669b5ffa 2018-10-07 stsp err = view_close(view->child);
912 669b5ffa 2018-10-07 stsp view->child = NULL;
913 669b5ffa 2018-10-07 stsp return err;
916 0dbbbe90 2022-06-17 op static const struct got_error *
917 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
919 669b5ffa 2018-10-07 stsp view->child = child;
920 669b5ffa 2018-10-07 stsp child->parent = view;
922 0dbbbe90 2022-06-17 op return view_resize(view);
925 34bc9ec9 2019-02-22 stsp static void
926 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
928 25791caa 2018-10-24 stsp int cols, lines;
929 25791caa 2018-10-24 stsp struct winsize size;
931 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
932 25791caa 2018-10-24 stsp cols = 80; /* Default */
933 25791caa 2018-10-24 stsp lines = 24;
935 25791caa 2018-10-24 stsp cols = size.ws_col;
936 25791caa 2018-10-24 stsp lines = size.ws_row;
938 25791caa 2018-10-24 stsp resize_term(lines, cols);
941 2b49a8ae 2019-06-22 stsp static const struct got_error *
942 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
944 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
945 9b058f45 2022-06-30 mark struct tog_view *v = view;
946 2b49a8ae 2019-06-22 stsp char pattern[1024];
949 c0c4acc8 2021-01-24 stsp if (view->search_started) {
950 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
951 c0c4acc8 2021-01-24 stsp view->searching = 0;
952 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
954 c0c4acc8 2021-01-24 stsp view->search_started = 0;
956 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
957 2b49a8ae 2019-06-22 stsp return NULL;
959 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
960 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
961 9b058f45 2022-06-30 mark v = view->child;
963 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
964 9b058f45 2022-06-30 mark wclrtoeol(v->window);
966 2b49a8ae 2019-06-22 stsp nocbreak();
968 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
969 9b058f45 2022-06-30 mark wrefresh(v->window);
972 2b49a8ae 2019-06-22 stsp if (ret == ERR)
973 2b49a8ae 2019-06-22 stsp return NULL;
975 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
976 7c32bd05 2019-06-22 stsp err = view->search_start(view);
978 7c32bd05 2019-06-22 stsp regfree(&view->regex);
979 7c32bd05 2019-06-22 stsp return err;
981 c0c4acc8 2021-01-24 stsp view->search_started = 1;
982 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
983 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
984 2b49a8ae 2019-06-22 stsp view->search_next(view);
987 2b49a8ae 2019-06-22 stsp return NULL;
991 640cd7ff 2022-06-22 mark * Compute view->count from numeric user input. User has five-tenths of a
992 640cd7ff 2022-06-22 mark * second to follow each numeric keypress with another number to form count.
993 640cd7ff 2022-06-22 mark * Return first non-numeric input or ERR and assign total to view->count.
994 640cd7ff 2022-06-22 mark * XXX Should we add support for user-defined timeout?
997 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
999 9b058f45 2022-06-30 mark struct tog_view *v = view;
1000 9b058f45 2022-06-30 mark int x, n = 0;
1002 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1003 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
1004 9b058f45 2022-06-30 mark v = view->child;
1005 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1006 9b058f45 2022-06-30 mark v = view->parent;
1008 640cd7ff 2022-06-22 mark view->count = 0;
1009 640cd7ff 2022-06-22 mark halfdelay(5); /* block for half a second */
1010 9b058f45 2022-06-30 mark wattron(v->window, A_BOLD);
1011 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1012 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1013 9b058f45 2022-06-30 mark waddch(v->window, ':');
1016 9b058f45 2022-06-30 mark x = getcurx(v->window);
1017 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1018 9b058f45 2022-06-30 mark waddch(v->window, c);
1019 9b058f45 2022-06-30 mark wrefresh(v->window);
1023 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1024 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1026 640cd7ff 2022-06-22 mark if (n >= 9999999)
1027 640cd7ff 2022-06-22 mark n = 9999999;
1029 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1030 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1032 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1033 640cd7ff 2022-06-22 mark view->count = n;
1035 9b058f45 2022-06-30 mark wattroff(v->window, A_BOLD);
1036 640cd7ff 2022-06-22 mark cbreak(); /* return to blocking */
1040 0cf4efb1 2018-09-29 stsp static const struct got_error *
1041 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1042 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1044 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1045 669b5ffa 2018-10-07 stsp struct tog_view *v;
1046 1a76625f 2018-10-22 stsp int ch, errcode;
1048 e5a0f69f 2018-08-18 stsp *new = NULL;
1050 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1051 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1052 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1053 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1054 640cd7ff 2022-06-22 mark view->count = 0;
1057 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1058 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1059 82954512 2020-02-03 stsp if (errcode)
1060 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1061 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1062 3da8ef85 2021-09-21 stsp sched_yield();
1063 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1064 82954512 2020-02-03 stsp if (errcode)
1065 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1066 82954512 2020-02-03 stsp "pthread_mutex_lock");
1067 60493ae3 2019-06-20 stsp view->search_next(view);
1068 60493ae3 2019-06-20 stsp return NULL;
1071 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
1072 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1073 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1074 1a76625f 2018-10-22 stsp if (errcode)
1075 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1076 640cd7ff 2022-06-22 mark /* If we have an unfinished count, don't get a new key map. */
1077 640cd7ff 2022-06-22 mark ch = view->ch;
1078 640cd7ff 2022-06-22 mark if ((view->count && --view->count == 0) || !view->count) {
1079 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1080 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1081 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1083 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1084 1a76625f 2018-10-22 stsp if (errcode)
1085 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1086 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
1088 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1089 25791caa 2018-10-24 stsp tog_resizeterm();
1090 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1091 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1092 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1093 25791caa 2018-10-24 stsp err = view_resize(v);
1095 25791caa 2018-10-24 stsp return err;
1096 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1098 25791caa 2018-10-24 stsp return err;
1099 cdfcfb03 2020-12-06 stsp if (v->child) {
1100 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1102 cdfcfb03 2020-12-06 stsp return err;
1103 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1104 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1106 cdfcfb03 2020-12-06 stsp return err;
1111 e5a0f69f 2018-08-18 stsp switch (ch) {
1113 640cd7ff 2022-06-22 mark view->count = 0;
1114 1e37a5c2 2019-05-12 jcs if (view->child) {
1115 e78dc838 2020-12-04 stsp view->focussed = 0;
1116 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1117 e78dc838 2020-12-04 stsp view->focus_child = 1;
1118 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1119 e78dc838 2020-12-04 stsp view->focussed = 0;
1120 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1121 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1122 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1123 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1124 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1125 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1127 9b058f45 2022-06-30 mark return err;
1129 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1130 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1132 9b058f45 2022-06-30 mark return err;
1137 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1138 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1139 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1140 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1144 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1145 9b058f45 2022-06-30 mark view->parent->mode = TOG_VIEW_SPLIT_NONE;
1147 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1148 9970f7fc 2020-12-03 stsp view->dying = 1;
1154 640cd7ff 2022-06-22 mark view->count = 0;
1155 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1156 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1158 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1159 e78dc838 2020-12-04 stsp view->focussed = 0;
1160 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1161 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1163 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1166 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1167 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1169 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1170 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1171 e78dc838 2020-12-04 stsp view->focussed = 1;
1172 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1174 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1175 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1176 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1180 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1184 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1185 9b058f45 2022-06-30 mark err = request_log_commits(view);
1189 9b058f45 2022-06-30 mark if (view->parent)
1190 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1192 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1194 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1197 640cd7ff 2022-06-22 mark view->count = 0;
1198 60493ae3 2019-06-20 stsp if (view->search_start)
1199 2b49a8ae 2019-06-22 stsp view_search_start(view);
1201 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1205 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1206 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1207 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1208 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1209 60493ae3 2019-06-20 stsp view->search_next(view);
1211 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1214 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1218 e5a0f69f 2018-08-18 stsp return err;
1222 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1224 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1225 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1227 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1229 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1232 669b5ffa 2018-10-07 stsp return view->focussed;
1235 bcbd79e2 2018-08-19 stsp static const struct got_error *
1236 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1238 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1239 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1240 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1241 fd823528 2018-10-22 stsp int fast_refresh = 10;
1242 1a76625f 2018-10-22 stsp int done = 0, errcode;
1244 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1245 1a76625f 2018-10-22 stsp if (errcode)
1246 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1248 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1249 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1251 1004088d 2018-09-29 stsp view->focussed = 1;
1252 878940b7 2018-09-29 stsp err = view->show(view);
1254 0cf4efb1 2018-09-29 stsp return err;
1255 0cf4efb1 2018-09-29 stsp update_panels();
1256 0cf4efb1 2018-09-29 stsp doupdate();
1257 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1258 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1259 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1260 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1262 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1265 9970f7fc 2020-12-03 stsp if (view->dying) {
1266 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1268 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1269 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1271 e78dc838 2020-12-04 stsp else if (view->parent)
1272 669b5ffa 2018-10-07 stsp prev = view->parent;
1274 e78dc838 2020-12-04 stsp if (view->parent) {
1275 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1276 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1277 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1278 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1279 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1283 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1285 9970f7fc 2020-12-03 stsp err = view_close(view);
1287 e5a0f69f 2018-08-18 stsp goto done;
1289 e78dc838 2020-12-04 stsp view = NULL;
1290 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1291 e78dc838 2020-12-04 stsp if (v->focussed)
1294 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1295 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1297 e78dc838 2020-12-04 stsp view = prev;
1298 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1299 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1300 e78dc838 2020-12-04 stsp tog_view_list_head);
1302 e78dc838 2020-12-04 stsp if (view) {
1303 e78dc838 2020-12-04 stsp if (view->focus_child) {
1304 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1305 e78dc838 2020-12-04 stsp view = view->child;
1307 e78dc838 2020-12-04 stsp view->focussed = 1;
1311 bcbd79e2 2018-08-19 stsp if (new_view) {
1312 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1313 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1314 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1315 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1317 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1318 86c66b02 2018-10-18 stsp err = view_close(v);
1320 86c66b02 2018-10-18 stsp goto done;
1323 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1324 fed7eaa8 2018-10-24 stsp view = new_view;
1326 669b5ffa 2018-10-07 stsp if (view) {
1327 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1328 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1329 e78dc838 2020-12-04 stsp view = view->child;
1331 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1332 e78dc838 2020-12-04 stsp view = view->parent;
1334 e78dc838 2020-12-04 stsp show_panel(view->panel);
1335 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1336 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1337 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1338 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1340 1a76625f 2018-10-22 stsp goto done;
1342 669b5ffa 2018-10-07 stsp err = view->show(view);
1344 1a76625f 2018-10-22 stsp goto done;
1345 669b5ffa 2018-10-07 stsp if (view->child) {
1346 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1348 1a76625f 2018-10-22 stsp goto done;
1350 1a76625f 2018-10-22 stsp update_panels();
1351 1a76625f 2018-10-22 stsp doupdate();
1355 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1356 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1357 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1358 e5a0f69f 2018-08-18 stsp view_close(view);
1361 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1362 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1363 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1365 e5a0f69f 2018-08-18 stsp return err;
1368 4ed7e80c 2018-05-20 stsp __dead static void
1369 9f7d7167 2018-04-29 stsp usage_log(void)
1372 c70c5802 2018-08-01 stsp fprintf(stderr,
1373 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1374 9f7d7167 2018-04-29 stsp getprogname());
1378 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1379 80ddbec8 2018-04-29 stsp static const struct got_error *
1380 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1382 00dfcb92 2018-06-11 stsp char *vis = NULL;
1383 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1385 963b370f 2018-05-20 stsp *ws = NULL;
1386 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1387 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1388 00dfcb92 2018-06-11 stsp int vislen;
1389 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1390 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1392 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1393 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1395 00dfcb92 2018-06-11 stsp return err;
1396 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1397 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1398 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1399 a7f50699 2018-06-11 stsp goto done;
1403 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1404 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1405 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1406 a7f50699 2018-06-11 stsp goto done;
1409 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1410 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1412 00dfcb92 2018-06-11 stsp free(vis);
1413 963b370f 2018-05-20 stsp if (err) {
1414 963b370f 2018-05-20 stsp free(*ws);
1415 963b370f 2018-05-20 stsp *ws = NULL;
1416 963b370f 2018-05-20 stsp *wlen = 0;
1418 963b370f 2018-05-20 stsp return err;
1421 145b6838 2022-06-16 stsp static const struct got_error *
1422 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1424 145b6838 2022-06-16 stsp char *dst;
1425 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1427 145b6838 2022-06-16 stsp *ptr = NULL;
1428 145b6838 2022-06-16 stsp n = len = strlen(src);
1429 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1430 145b6838 2022-06-16 stsp if (dst == NULL)
1431 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1433 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1434 145b6838 2022-06-16 stsp const char c = src[idx];
1436 145b6838 2022-06-16 stsp if (c == '\t') {
1437 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1440 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1441 6e1c41ad 2022-06-16 mark if (p == NULL) {
1442 6e1c41ad 2022-06-16 mark free(dst);
1443 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1448 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1451 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1455 145b6838 2022-06-16 stsp dst[sz] = '\0';
1456 145b6838 2022-06-16 stsp *ptr = dst;
1457 145b6838 2022-06-16 stsp return NULL;
1461 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1462 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1463 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1467 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1469 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1471 4e4a9ac8 2022-06-17 op if (n == 0) {
1472 4e4a9ac8 2022-06-17 op *rcol = cols;
1476 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1477 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1478 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1480 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1482 4e4a9ac8 2022-06-17 op if (width == -1) {
1484 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1487 4e4a9ac8 2022-06-17 op if (cols + width > n)
1489 4e4a9ac8 2022-06-17 op cols += width;
1492 4e4a9ac8 2022-06-17 op *rcol = cols;
1497 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1498 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1499 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1501 ccda2f4d 2022-06-16 stsp static const struct got_error *
1502 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1503 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1505 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1507 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1508 145b6838 2022-06-16 stsp char *exstr = NULL;
1509 963b370f 2018-05-20 stsp size_t wlen;
1510 4e4a9ac8 2022-06-17 op int i, scrollx;
1512 963b370f 2018-05-20 stsp *wlinep = NULL;
1513 b700b5d6 2018-07-10 stsp *widthp = 0;
1515 145b6838 2022-06-16 stsp if (expand) {
1516 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1518 145b6838 2022-06-16 stsp return err;
1521 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1522 145b6838 2022-06-16 stsp free(exstr);
1524 963b370f 2018-05-20 stsp return err;
1526 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1528 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1529 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1532 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1533 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1537 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1538 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1540 b700b5d6 2018-07-10 stsp if (widthp)
1541 b700b5d6 2018-07-10 stsp *widthp = cols;
1542 ccda2f4d 2022-06-16 stsp if (scrollxp)
1543 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1545 963b370f 2018-05-20 stsp free(wline);
1547 963b370f 2018-05-20 stsp *wlinep = wline;
1548 963b370f 2018-05-20 stsp return err;
1551 8b473291 2019-02-21 stsp static const struct got_error*
1552 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1553 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1555 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1556 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1558 8b473291 2019-02-21 stsp const char *name;
1560 8b473291 2019-02-21 stsp *refs_str = NULL;
1562 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1563 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1564 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1567 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1568 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1570 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1571 8b473291 2019-02-21 stsp name += 5;
1572 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1573 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1575 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1576 8b473291 2019-02-21 stsp name += 6;
1577 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1578 8b473291 2019-02-21 stsp name += 8;
1579 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1580 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1583 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1586 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1587 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1588 5d844a1e 2019-08-13 stsp if (err) {
1589 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1590 48cae60d 2020-09-22 stsp free(ref_id);
1593 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1594 5d844a1e 2019-08-13 stsp err = NULL;
1595 5d844a1e 2019-08-13 stsp tag = NULL;
1598 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1599 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1600 48cae60d 2020-09-22 stsp free(ref_id);
1602 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1603 52b5abe1 2019-08-13 stsp if (cmp != 0)
1605 8b473291 2019-02-21 stsp s = *refs_str;
1606 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1607 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1608 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1610 8b473291 2019-02-21 stsp *refs_str = NULL;
1616 8b473291 2019-02-21 stsp return err;
1619 963b370f 2018-05-20 stsp static const struct got_error *
1620 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1621 27a741e5 2019-09-11 stsp int col_tab_align)
1623 e6b8b890 2020-12-29 naddy char *smallerthan;
1625 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1626 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1627 5813d178 2019-03-09 stsp author = smallerthan + 1;
1628 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1629 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1630 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1633 5813d178 2019-03-09 stsp static const struct got_error *
1634 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1635 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1636 8fdc79fe 2020-12-01 naddy int author_display_cols)
1638 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1639 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1640 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1641 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1642 5813d178 2019-03-09 stsp char *author = NULL;
1643 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1644 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1645 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1646 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1647 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1648 ccb26ccd 2018-11-05 stsp struct tm tm;
1649 45d799e2 2018-12-23 stsp time_t committer_time;
1650 11b20872 2019-11-08 stsp struct tog_color *tc;
1652 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1653 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1654 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1655 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1656 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1658 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1659 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1661 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1662 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1664 11b20872 2019-11-08 stsp wattr_on(view->window,
1665 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1666 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1668 11b20872 2019-11-08 stsp wattr_off(view->window,
1669 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1670 27a741e5 2019-09-11 stsp col = limit;
1671 b39d25c7 2018-07-10 stsp if (col > avail)
1672 b39d25c7 2018-07-10 stsp goto done;
1674 6570a66d 2019-11-08 stsp if (avail >= 120) {
1675 6570a66d 2019-11-08 stsp char *id_str;
1676 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1678 6570a66d 2019-11-08 stsp goto done;
1679 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1681 11b20872 2019-11-08 stsp wattr_on(view->window,
1682 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1683 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1685 11b20872 2019-11-08 stsp wattr_off(view->window,
1686 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1687 6570a66d 2019-11-08 stsp free(id_str);
1689 6570a66d 2019-11-08 stsp if (col > avail)
1690 6570a66d 2019-11-08 stsp goto done;
1693 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1694 5813d178 2019-03-09 stsp if (author == NULL) {
1695 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1696 80ddbec8 2018-04-29 stsp goto done;
1698 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1700 bb737323 2018-05-20 stsp goto done;
1701 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1703 11b20872 2019-11-08 stsp wattr_on(view->window,
1704 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1705 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1707 11b20872 2019-11-08 stsp wattr_off(view->window,
1708 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1709 bb737323 2018-05-20 stsp col += author_width;
1710 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1711 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1713 bb737323 2018-05-20 stsp author_width++;
1715 9c2eaf34 2018-05-20 stsp if (col > avail)
1716 9c2eaf34 2018-05-20 stsp goto done;
1718 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1720 6d9fbc00 2018-04-29 stsp goto done;
1721 bb737323 2018-05-20 stsp logmsg = logmsg0;
1722 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1724 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1725 bb737323 2018-05-20 stsp if (newline)
1726 bb737323 2018-05-20 stsp *newline = '\0';
1727 ccda2f4d 2022-06-16 stsp limit = avail - col;
1728 63ba1a3a 2022-06-21 op if (view->child && view_is_splitscreen(view->child) && limit > 0)
1729 4d1f6af3 2022-06-17 op limit--; /* for the border */
1730 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1731 ccda2f4d 2022-06-16 stsp limit, col, 1);
1733 29688b02 2022-06-16 stsp goto done;
1734 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1735 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1736 27a741e5 2019-09-11 stsp while (col < avail) {
1737 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1741 80ddbec8 2018-04-29 stsp free(logmsg0);
1742 bb737323 2018-05-20 stsp free(wlogmsg);
1743 5813d178 2019-03-09 stsp free(author);
1744 bb737323 2018-05-20 stsp free(wauthor);
1745 80ddbec8 2018-04-29 stsp free(line);
1746 80ddbec8 2018-04-29 stsp return err;
1749 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1750 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1751 899d86c2 2018-05-10 stsp struct got_object_id *id)
1753 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1755 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1756 80ddbec8 2018-04-29 stsp if (entry == NULL)
1757 899d86c2 2018-05-10 stsp return NULL;
1759 899d86c2 2018-05-10 stsp entry->id = id;
1760 99db9666 2018-05-07 stsp entry->commit = commit;
1761 899d86c2 2018-05-10 stsp return entry;
1764 99db9666 2018-05-07 stsp static void
1765 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1767 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1769 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1770 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1771 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1772 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1773 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1774 99db9666 2018-05-07 stsp free(entry);
1777 99db9666 2018-05-07 stsp static void
1778 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1780 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1781 99db9666 2018-05-07 stsp pop_commit(commits);
1784 c4972b91 2018-05-07 stsp static const struct got_error *
1785 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1786 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1788 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1789 13add988 2019-10-15 stsp regmatch_t regmatch;
1790 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1792 13add988 2019-10-15 stsp *have_match = 0;
1794 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1796 13add988 2019-10-15 stsp return err;
1798 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1800 13add988 2019-10-15 stsp goto done;
1802 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1803 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1804 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1805 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1806 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1807 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1808 13add988 2019-10-15 stsp *have_match = 1;
1810 13add988 2019-10-15 stsp free(id_str);
1811 13add988 2019-10-15 stsp free(logmsg);
1812 13add988 2019-10-15 stsp return err;
1815 13add988 2019-10-15 stsp static const struct got_error *
1816 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1818 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1821 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1822 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1823 1a76625f 2018-10-22 stsp * while updating the display.
1826 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1827 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1828 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1829 1a76625f 2018-10-22 stsp int errcode;
1831 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1832 4e0d2870 2020-12-07 naddy NULL, NULL);
1833 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1836 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1839 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1840 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1841 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1845 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1846 1a76625f 2018-10-22 stsp if (errcode) {
1847 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1848 13add988 2019-10-15 stsp "pthread_mutex_lock");
1852 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1853 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1854 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1856 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1857 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1858 7c1452c1 2020-03-26 stsp int have_match;
1859 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1862 7c1452c1 2020-03-26 stsp if (have_match)
1863 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1866 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1867 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1868 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1869 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1872 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1874 9ba79e04 2018-06-11 stsp return err;
1877 2b779855 2020-12-05 naddy static void
1878 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1880 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1881 2b779855 2020-12-05 naddy int ncommits = 0;
1883 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1884 2b779855 2020-12-05 naddy while (entry) {
1885 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1886 2b779855 2020-12-05 naddy s->selected_entry = entry;
1889 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1890 2b779855 2020-12-05 naddy ncommits++;
1894 0553a4e3 2018-04-30 stsp static const struct got_error *
1895 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1897 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1898 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1899 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1900 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1901 60493ae3 2019-06-20 stsp int width;
1902 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1903 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1904 8b473291 2019-02-21 stsp char *refs_str = NULL;
1905 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1906 11b20872 2019-11-08 stsp struct tog_color *tc;
1907 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1909 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1910 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1911 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1912 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1914 ecb28ae0 2018-07-16 stsp return err;
1915 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1916 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1917 d2075bf3 2020-12-25 stsp if (refs) {
1918 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1919 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1921 d2075bf3 2020-12-25 stsp goto done;
1925 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1926 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1928 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1929 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1930 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1931 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1932 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1933 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1934 8f4ed634 2020-03-26 stsp goto done;
1937 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1939 f9686aa5 2020-03-27 stsp if (view->searching) {
1940 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1941 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1942 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1943 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1944 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1945 f9686aa5 2020-03-27 stsp search_str = "searching...";
1948 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1949 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1950 f9686aa5 2020-03-27 stsp search_str ? search_str :
1951 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1952 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1953 8f4ed634 2020-03-26 stsp goto done;
1957 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1958 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1959 87411fa9 2022-06-16 stsp "........................................",
1960 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1961 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1962 1a76625f 2018-10-22 stsp header = NULL;
1963 1a76625f 2018-10-22 stsp goto done;
1965 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1966 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1967 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1968 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1969 1a76625f 2018-10-22 stsp header = NULL;
1970 1a76625f 2018-10-22 stsp goto done;
1972 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1974 1a76625f 2018-10-22 stsp goto done;
1976 2814baeb 2018-08-01 stsp werase(view->window);
1978 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1979 a3404814 2018-09-02 stsp wstandout(view->window);
1980 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1982 11b20872 2019-11-08 stsp wattr_on(view->window,
1983 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1984 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1986 11b20872 2019-11-08 stsp wattr_off(view->window,
1987 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1988 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1989 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1992 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1993 a3404814 2018-09-02 stsp wstandend(view->window);
1994 ecb28ae0 2018-07-16 stsp free(wline);
1995 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1996 1a76625f 2018-10-22 stsp goto done;
1998 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
1999 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2000 5813d178 2019-03-09 stsp ncommits = 0;
2001 145b6838 2022-06-16 stsp view->maxx = 0;
2002 5813d178 2019-03-09 stsp while (entry) {
2003 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2004 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2005 5813d178 2019-03-09 stsp int width;
2006 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2008 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2009 5813d178 2019-03-09 stsp if (author == NULL) {
2010 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2011 5813d178 2019-03-09 stsp goto done;
2013 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2014 27a741e5 2019-09-11 stsp date_display_cols);
2015 5813d178 2019-03-09 stsp if (author_cols < width)
2016 5813d178 2019-03-09 stsp author_cols = width;
2017 5813d178 2019-03-09 stsp free(wauthor);
2018 5813d178 2019-03-09 stsp free(author);
2019 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2021 145b6838 2022-06-16 stsp goto done;
2022 145b6838 2022-06-16 stsp msg = msg0;
2023 145b6838 2022-06-16 stsp while (*msg == '\n')
2025 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2026 29688b02 2022-06-16 stsp *eol = '\0';
2027 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2028 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2030 29688b02 2022-06-16 stsp goto done;
2031 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2032 145b6838 2022-06-16 stsp free(msg0);
2033 29688b02 2022-06-16 stsp free(wmsg);
2034 7ca04879 2019-10-19 stsp ncommits++;
2035 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2038 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2039 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2040 867c6645 2018-07-10 stsp ncommits = 0;
2041 899d86c2 2018-05-10 stsp while (entry) {
2042 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2044 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2045 2814baeb 2018-08-01 stsp wstandout(view->window);
2046 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2047 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2048 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2049 2814baeb 2018-08-01 stsp wstandend(view->window);
2051 60493ae3 2019-06-20 stsp goto done;
2052 0553a4e3 2018-04-30 stsp ncommits++;
2053 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2054 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2057 9b058f45 2022-06-30 mark view_border(view);
2059 1a76625f 2018-10-22 stsp free(id_str);
2060 8b473291 2019-02-21 stsp free(refs_str);
2061 1a76625f 2018-10-22 stsp free(ncommits_str);
2062 1a76625f 2018-10-22 stsp free(header);
2063 80ddbec8 2018-04-29 stsp return err;
2066 07b55e75 2018-05-10 stsp static void
2067 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2069 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2070 07b55e75 2018-05-10 stsp int nscrolled = 0;
2072 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2073 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2076 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2077 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2078 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2079 07b55e75 2018-05-10 stsp if (entry) {
2080 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2081 07b55e75 2018-05-10 stsp nscrolled++;
2086 aa075928 2018-05-10 stsp static const struct got_error *
2087 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2089 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2090 5e224a3e 2019-02-22 stsp int errcode;
2092 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2094 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2095 ffe38506 2020-12-01 naddy if (ta->log_complete)
2098 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2099 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2100 7aafa0d1 2019-02-22 stsp if (errcode)
2101 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2102 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2105 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2106 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2108 7c1452c1 2020-03-26 stsp if (!wait)
2111 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2112 ffe38506 2020-12-01 naddy show_log_view(view);
2113 7c1452c1 2020-03-26 stsp update_panels();
2114 7c1452c1 2020-03-26 stsp doupdate();
2116 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2117 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2118 82954512 2020-02-03 stsp if (errcode)
2119 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2120 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2122 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2123 ffe38506 2020-12-01 naddy show_log_view(view);
2124 7c1452c1 2020-03-26 stsp update_panels();
2125 7c1452c1 2020-03-26 stsp doupdate();
2128 5e224a3e 2019-02-22 stsp return NULL;
2131 5e224a3e 2019-02-22 stsp static const struct got_error *
2132 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2134 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2135 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2137 9b058f45 2022-06-30 mark state->thread_args.commits_needed = view->nscrolled;
2138 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2139 9b058f45 2022-06-30 mark view->nscrolled = 0;
2141 9b058f45 2022-06-30 mark return err;
2144 9b058f45 2022-06-30 mark static const struct got_error *
2145 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2147 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2148 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2149 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2150 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2152 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2153 5e224a3e 2019-02-22 stsp return NULL;
2155 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2156 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2157 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2159 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2161 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2162 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2164 5e224a3e 2019-02-22 stsp return err;
2168 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2169 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2172 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2173 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2175 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2176 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2178 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2179 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2181 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2182 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2184 9b058f45 2022-06-30 mark view->nscrolled = 0;
2186 dd0a52c1 2018-05-20 stsp return err;
2189 cd0acaa7 2018-05-20 stsp static const struct got_error *
2190 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2191 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2192 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2194 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2195 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2196 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2198 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2199 15a94983 2018-12-23 stsp if (diff_view == NULL)
2200 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2202 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2203 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2204 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2205 e5a0f69f 2018-08-18 stsp if (err == NULL)
2206 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2207 cd0acaa7 2018-05-20 stsp return err;
2210 80ddbec8 2018-04-29 stsp static const struct got_error *
2211 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2212 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2214 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2216 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2217 941e9f74 2019-05-21 stsp if (parent == NULL)
2218 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2220 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2221 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2222 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2223 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2224 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2225 941e9f74 2019-05-21 stsp s->tree = subtree;
2226 941e9f74 2019-05-21 stsp s->selected = 0;
2227 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2228 941e9f74 2019-05-21 stsp return NULL;
2231 941e9f74 2019-05-21 stsp static const struct got_error *
2232 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2233 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2235 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2236 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2237 941e9f74 2019-05-21 stsp const char *p;
2238 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2240 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2242 941e9f74 2019-05-21 stsp while (*p) {
2243 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2244 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2245 56e0773d 2019-11-28 stsp char *te_name;
2247 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2250 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2251 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2252 941e9f74 2019-05-21 stsp if (slash == NULL)
2253 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2255 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2256 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2257 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2260 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2261 56e0773d 2019-11-28 stsp if (te == NULL) {
2262 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2263 56e0773d 2019-11-28 stsp free(te_name);
2266 56e0773d 2019-11-28 stsp free(te_name);
2267 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2269 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2270 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2272 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2273 941e9f74 2019-05-21 stsp if (slash)
2274 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2276 941e9f74 2019-05-21 stsp subpath = strdup(path);
2277 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2278 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2282 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2287 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2288 941e9f74 2019-05-21 stsp free(tree_id);
2292 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2293 941e9f74 2019-05-21 stsp if (err) {
2294 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2297 941e9f74 2019-05-21 stsp if (slash == NULL)
2299 941e9f74 2019-05-21 stsp free(subpath);
2300 941e9f74 2019-05-21 stsp subpath = NULL;
2301 941e9f74 2019-05-21 stsp p = slash;
2304 941e9f74 2019-05-21 stsp free(subpath);
2305 1a76625f 2018-10-22 stsp return err;
2308 61266923 2020-01-14 stsp static const struct got_error *
2309 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2310 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2311 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2313 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2314 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2315 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2317 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2318 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2319 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2321 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2323 55cccc34 2020-02-20 stsp return err;
2324 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2326 55cccc34 2020-02-20 stsp *new_view = tree_view;
2328 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2329 55cccc34 2020-02-20 stsp return NULL;
2331 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2334 55cccc34 2020-02-20 stsp static const struct got_error *
2335 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2337 61266923 2020-01-14 stsp sigset_t sigset;
2338 61266923 2020-01-14 stsp int errcode;
2340 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2341 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2343 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2344 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2345 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2346 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2347 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2348 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2349 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2350 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2351 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2353 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2354 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2355 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2357 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2358 61266923 2020-01-14 stsp if (errcode)
2359 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2361 61266923 2020-01-14 stsp return NULL;
2364 1a76625f 2018-10-22 stsp static void *
2365 1a76625f 2018-10-22 stsp log_thread(void *arg)
2367 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2368 1a76625f 2018-10-22 stsp int errcode = 0;
2369 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2370 1a76625f 2018-10-22 stsp int done = 0;
2372 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2374 61266923 2020-01-14 stsp return (void *)err;
2376 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2377 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2378 1a76625f 2018-10-22 stsp if (err) {
2379 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2380 1a76625f 2018-10-22 stsp return (void *)err;
2381 1a76625f 2018-10-22 stsp err = NULL;
2383 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2384 1a76625f 2018-10-22 stsp a->commits_needed--;
2386 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2387 3abe8080 2019-04-10 stsp if (errcode) {
2388 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2389 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2391 3abe8080 2019-04-10 stsp } else if (*a->quit)
2393 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2394 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2395 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2396 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2399 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2400 7c1452c1 2020-03-26 stsp if (errcode) {
2401 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2402 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2403 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2408 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2410 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2411 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2412 7c1452c1 2020-03-26 stsp &tog_mutex);
2413 7c1452c1 2020-03-26 stsp if (errcode)
2414 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2415 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2416 21355643 2020-12-06 stsp if (*a->quit)
2421 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2422 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2423 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2424 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2426 3abe8080 2019-04-10 stsp a->log_complete = 1;
2427 1a76625f 2018-10-22 stsp return (void *)err;
2430 1a76625f 2018-10-22 stsp static const struct got_error *
2431 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2433 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2434 1a76625f 2018-10-22 stsp int errcode;
2436 1a76625f 2018-10-22 stsp if (s->thread) {
2437 1a76625f 2018-10-22 stsp s->quit = 1;
2438 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2439 1a76625f 2018-10-22 stsp if (errcode)
2440 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2441 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2442 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2443 1a76625f 2018-10-22 stsp if (errcode)
2444 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2445 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2446 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2447 1a76625f 2018-10-22 stsp if (errcode)
2448 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2449 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2450 1a76625f 2018-10-22 stsp if (errcode)
2451 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2452 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2453 1a76625f 2018-10-22 stsp s->thread = NULL;
2456 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2457 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2458 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2461 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2462 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2463 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2464 74467cc8 2022-06-15 stsp if (err == NULL)
2465 74467cc8 2022-06-15 stsp err = pack_err;
2466 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2469 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2470 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2471 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2474 9343a5fb 2018-06-23 stsp return err;
2477 9343a5fb 2018-06-23 stsp static const struct got_error *
2478 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2480 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2481 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2482 276b94a1 2020-11-13 naddy int errcode;
2484 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2486 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2487 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2488 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2490 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2491 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2492 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2494 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2495 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2496 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2497 1a76625f 2018-10-22 stsp free(s->start_id);
2498 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2499 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2500 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2501 1a76625f 2018-10-22 stsp return err;
2504 1a76625f 2018-10-22 stsp static const struct got_error *
2505 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2507 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2509 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2510 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2511 60493ae3 2019-06-20 stsp return NULL;
2514 60493ae3 2019-06-20 stsp static const struct got_error *
2515 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2517 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2518 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2519 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2521 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2522 f9686aa5 2020-03-27 stsp show_log_view(view);
2523 f9686aa5 2020-03-27 stsp update_panels();
2524 f9686aa5 2020-03-27 stsp doupdate();
2526 96e2b566 2019-07-08 stsp if (s->search_entry) {
2527 13add988 2019-10-15 stsp int errcode, ch;
2528 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2529 13add988 2019-10-15 stsp if (errcode)
2530 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2531 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2532 13add988 2019-10-15 stsp ch = wgetch(view->window);
2533 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2534 13add988 2019-10-15 stsp if (errcode)
2535 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2536 13add988 2019-10-15 stsp "pthread_mutex_lock");
2537 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2538 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2539 678cbce5 2019-07-28 stsp return NULL;
2541 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2542 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2544 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2545 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2546 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2547 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2548 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2551 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2552 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2553 f704b35c 2022-06-18 stsp * might have changed.
2555 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2556 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2557 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2559 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2561 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2562 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2563 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2565 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2566 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2569 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2572 60493ae3 2019-06-20 stsp while (1) {
2573 13add988 2019-10-15 stsp int have_match = 0;
2575 60493ae3 2019-06-20 stsp if (entry == NULL) {
2576 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2577 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2578 f9967bca 2020-03-27 stsp view->search_next_done =
2579 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2580 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2581 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2582 f801134a 2019-06-25 stsp return NULL;
2585 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2586 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2587 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2589 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2590 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2593 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2594 13add988 2019-10-15 stsp &view->regex);
2597 13add988 2019-10-15 stsp if (have_match) {
2598 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2599 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2603 96e2b566 2019-07-08 stsp s->search_entry = entry;
2604 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2605 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2607 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2610 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2611 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2612 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2613 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2615 60493ae3 2019-06-20 stsp return err;
2618 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2619 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2621 60493ae3 2019-06-20 stsp return err;
2626 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2628 60493ae3 2019-06-20 stsp return NULL;
2631 60493ae3 2019-06-20 stsp static const struct got_error *
2632 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2633 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2634 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2636 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2637 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2638 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2639 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2640 1a76625f 2018-10-22 stsp int errcode;
2642 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2643 f135c941 2020-02-20 stsp free(s->in_repo_path);
2644 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2645 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2646 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2649 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2650 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2651 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2653 fb2756b9 2018-08-04 stsp s->repo = repo;
2654 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2655 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2656 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2657 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2658 9cd7cbd1 2020-12-07 stsp goto done;
2661 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2662 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2663 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2664 5036bf37 2018-09-24 stsp goto done;
2666 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2668 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2669 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2670 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2671 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2673 11b20872 2019-11-08 stsp goto done;
2674 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2675 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2676 11b20872 2019-11-08 stsp if (err) {
2677 11b20872 2019-11-08 stsp free_colors(&s->colors);
2678 11b20872 2019-11-08 stsp goto done;
2680 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2681 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2682 11b20872 2019-11-08 stsp if (err) {
2683 11b20872 2019-11-08 stsp free_colors(&s->colors);
2684 11b20872 2019-11-08 stsp goto done;
2688 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2689 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2690 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2691 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2692 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2694 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2695 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2697 74467cc8 2022-06-15 stsp goto done;
2699 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2700 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2701 0ae84acc 2022-06-15 tracey if (err)
2702 0ae84acc 2022-06-15 tracey goto done;
2703 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2704 b672a97a 2020-01-27 stsp !s->log_branches);
2706 1a76625f 2018-10-22 stsp goto done;
2707 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2708 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2710 c5b78334 2020-01-12 stsp goto done;
2712 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2713 1a76625f 2018-10-22 stsp if (errcode) {
2714 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2715 1a76625f 2018-10-22 stsp goto done;
2717 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2718 7c1452c1 2020-03-26 stsp if (errcode) {
2719 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2720 7c1452c1 2020-03-26 stsp goto done;
2723 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2724 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2725 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2726 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2727 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2728 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2729 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2730 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2731 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2732 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2733 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2734 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2735 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2738 1a76625f 2018-10-22 stsp close_log_view(view);
2739 ba4f502b 2018-08-04 stsp return err;
2742 e5a0f69f 2018-08-18 stsp static const struct got_error *
2743 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2745 f2f6d207 2020-11-24 stsp const struct got_error *err;
2746 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2748 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2749 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2750 2b380cc8 2018-10-24 stsp &s->thread_args);
2751 2b380cc8 2018-10-24 stsp if (errcode)
2752 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2753 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2754 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2756 f2f6d207 2020-11-24 stsp return err;
2760 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2763 b880cc75 2022-06-30 mark static void
2764 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
2766 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2768 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
2769 b880cc75 2022-06-30 mark view->count = 0;
2770 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
2773 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2775 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2777 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
2778 b880cc75 2022-06-30 mark --s->selected;
2780 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2782 b880cc75 2022-06-30 mark select_commit(s);
2786 b880cc75 2022-06-30 mark static const struct got_error *
2787 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
2789 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2790 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
2791 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
2793 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
2794 b880cc75 2022-06-30 mark if (first == NULL) {
2795 b880cc75 2022-06-30 mark view->count = 0;
2796 b880cc75 2022-06-30 mark return NULL;
2799 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2800 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
2801 b880cc75 2022-06-30 mark return NULL;
2803 b880cc75 2022-06-30 mark if (!page) {
2804 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
2806 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2807 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
2808 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
2809 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
2810 b880cc75 2022-06-30 mark ++s->selected;
2812 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
2813 b880cc75 2022-06-30 mark } else if (s->thread_args.log_complete) {
2814 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2815 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
2816 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
2818 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
2819 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
2820 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2822 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
2824 b880cc75 2022-06-30 mark return err;
2825 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
2826 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2827 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
2831 b880cc75 2022-06-30 mark return err;
2834 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
2835 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
2837 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
2838 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2840 b880cc75 2022-06-30 mark select_commit(s);
2842 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2843 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
2844 b880cc75 2022-06-30 mark view->count = 0;
2846 b880cc75 2022-06-30 mark return NULL;
2850 9b058f45 2022-06-30 mark * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2851 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2852 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_HRZN horizontal split
2853 9b058f45 2022-06-30 mark * Assign start column and line of the new split to *x and *y, respectively,
2854 9b058f45 2022-06-30 mark * and assign view mode to view->mode.
2856 9b058f45 2022-06-30 mark static void
2857 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
2859 9b058f45 2022-06-30 mark char *mode;
2861 9b058f45 2022-06-30 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
2863 9b058f45 2022-06-30 mark if (!mode || mode[0] != 'h') {
2864 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_VERT;
2865 9b058f45 2022-06-30 mark *x = view_split_begin_x(view->begin_x);
2866 9b058f45 2022-06-30 mark } else if (mode && mode[0] == 'h') {
2867 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_HRZN;
2868 9b058f45 2022-06-30 mark *y = view_split_begin_y(view->lines);
2872 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
2873 e5a0f69f 2018-08-18 stsp static const struct got_error *
2874 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
2876 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2878 9b058f45 2022-06-30 mark view->nlines = y;
2879 9b058f45 2022-06-30 mark err = view_resize(view);
2881 9b058f45 2022-06-30 mark return err;
2883 9b058f45 2022-06-30 mark err = offset_selection_down(view);
2885 9b058f45 2022-06-30 mark return err;
2888 9b058f45 2022-06-30 mark static const struct got_error *
2889 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2891 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2892 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2893 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2894 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2895 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2896 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0, n, nscroll = view->nlines - 1;
2898 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2899 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2900 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2901 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2902 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2903 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2905 b880cc75 2022-06-30 mark return err;
2908 528dedf3 2021-08-30 stsp switch (ch) {
2910 1e37a5c2 2019-05-12 jcs s->quit = 1;
2913 145b6838 2022-06-16 stsp view->x = 0;
2916 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2917 640cd7ff 2022-06-22 mark view->count = 0;
2919 145b6838 2022-06-16 stsp case KEY_RIGHT:
2921 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2922 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2924 640cd7ff 2022-06-22 mark view->count = 0;
2926 145b6838 2022-06-16 stsp case KEY_LEFT:
2928 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2929 640cd7ff 2022-06-22 mark if (view->x <= 0)
2930 640cd7ff 2022-06-22 mark view->count = 0;
2933 1e37a5c2 2019-05-12 jcs case KEY_UP:
2936 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2937 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2940 912a3f79 2021-08-30 j case KEY_HOME:
2941 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2942 640cd7ff 2022-06-22 mark view->count = 0;
2944 83cc4199 2022-06-13 stsp case CTRL('u'):
2946 83cc4199 2022-06-13 stsp nscroll /= 2;
2947 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2948 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2949 a4292ac5 2019-05-12 jcs case CTRL('b'):
2951 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2954 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2957 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2958 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
2961 912a3f79 2021-08-30 j case KEY_END: {
2962 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2963 912a3f79 2021-08-30 j * traverse them all. */
2964 640cd7ff 2022-06-22 mark view->count = 0;
2965 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2966 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2967 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2970 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2971 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2972 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2973 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2975 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2976 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2978 f3bc9f1d 2021-09-05 naddy if (n > 0)
2979 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2980 2b779855 2020-12-05 naddy select_commit(s);
2983 80b7e8da 2022-06-11 stsp case CTRL('d'):
2985 83cc4199 2022-06-13 stsp nscroll /= 2;
2986 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2987 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2988 61417565 2022-06-20 mark case CTRL('f'):
2989 48bb96f0 2022-06-20 naddy case 'f':
2991 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
2993 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2994 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2995 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2996 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2997 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2998 2b779855 2020-12-05 naddy select_commit(s);
2999 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3000 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3001 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3002 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3003 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3006 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3007 9b058f45 2022-06-30 mark case '\r': {
3008 640cd7ff 2022-06-22 mark view->count = 0;
3009 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3012 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3013 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3014 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3016 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3017 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3018 78756c87 2020-11-24 stsp view, s->repo);
3022 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3023 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3028 e78dc838 2020-12-04 stsp view->focussed = 0;
3029 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3030 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3031 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3033 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3034 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3036 1e37a5c2 2019-05-12 jcs return err;
3037 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3040 e78dc838 2020-12-04 stsp view->focus_child = 1;
3042 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3046 640cd7ff 2022-06-22 mark view->count = 0;
3047 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3049 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3050 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3051 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
3052 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3056 e78dc838 2020-12-04 stsp view->focussed = 0;
3057 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3058 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3059 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3061 1e37a5c2 2019-05-12 jcs return err;
3062 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3065 e78dc838 2020-12-04 stsp view->focus_child = 1;
3067 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3069 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3070 21355643 2020-12-06 stsp case CTRL('l'):
3072 640cd7ff 2022-06-22 mark view->count = 0;
3073 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3074 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3076 21355643 2020-12-06 stsp err = stop_log_thread(s);
3078 74cfe85e 2020-10-20 stsp return err;
3079 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3080 21355643 2020-12-06 stsp char *parent_path;
3081 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3083 21355643 2020-12-06 stsp return err;
3084 21355643 2020-12-06 stsp free(s->in_repo_path);
3085 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3086 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3087 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3088 21355643 2020-12-06 stsp struct got_object_id *start_id;
3089 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3090 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3091 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3093 21355643 2020-12-06 stsp return err;
3094 21355643 2020-12-06 stsp free(s->start_id);
3095 21355643 2020-12-06 stsp s->start_id = start_id;
3096 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3097 21355643 2020-12-06 stsp } else /* 'B' */
3098 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3100 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3101 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3103 b0dd8d27 2022-06-16 stsp return err;
3105 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3106 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3107 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3109 21355643 2020-12-06 stsp return err;
3110 51a10b52 2020-12-26 stsp tog_free_refs();
3111 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3113 ca51c541 2020-12-07 stsp return err;
3114 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3115 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3117 d01904d4 2019-06-25 stsp return err;
3118 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3119 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3121 b672a97a 2020-01-27 stsp return err;
3122 21355643 2020-12-06 stsp free_commits(&s->commits);
3123 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3124 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3125 21355643 2020-12-06 stsp s->selected_entry = NULL;
3126 21355643 2020-12-06 stsp s->selected = 0;
3127 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3128 21355643 2020-12-06 stsp s->quit = 0;
3129 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3130 dfee752e 2022-06-18 op s->matched_entry = NULL;
3131 dfee752e 2022-06-18 op s->search_entry = NULL;
3134 640cd7ff 2022-06-22 mark view->count = 0;
3135 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3136 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
3137 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
3138 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
3139 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3140 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3141 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3142 6458efa5 2020-11-24 stsp if (err) {
3143 6458efa5 2020-11-24 stsp view_close(ref_view);
3144 6458efa5 2020-11-24 stsp return err;
3146 e78dc838 2020-12-04 stsp view->focussed = 0;
3147 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3148 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3149 6458efa5 2020-11-24 stsp err = view_close_child(view);
3151 6458efa5 2020-11-24 stsp return err;
3152 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3155 e78dc838 2020-12-04 stsp view->focus_child = 1;
3157 6458efa5 2020-11-24 stsp *new_view = ref_view;
3160 640cd7ff 2022-06-22 mark view->count = 0;
3164 80ddbec8 2018-04-29 stsp return err;
3167 4ed7e80c 2018-05-20 stsp static const struct got_error *
3168 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3170 c2db6724 2019-01-04 stsp const struct got_error *error;
3172 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3173 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3174 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3176 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3177 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3179 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3180 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3182 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3183 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3185 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3186 c2db6724 2019-01-04 stsp if (error != NULL)
3187 c2db6724 2019-01-04 stsp return error;
3189 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3190 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3192 c2db6724 2019-01-04 stsp return NULL;
3195 a915003a 2019-02-05 stsp static void
3196 a915003a 2019-02-05 stsp init_curses(void)
3199 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3200 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3201 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3203 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3204 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3205 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3206 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3207 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3209 a915003a 2019-02-05 stsp initscr();
3211 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3214 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3215 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3216 a915003a 2019-02-05 stsp curs_set(0);
3217 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3218 6d17833f 2019-11-08 stsp start_color();
3219 6d17833f 2019-11-08 stsp use_default_colors();
3223 c2db6724 2019-01-04 stsp static const struct got_error *
3224 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3225 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3227 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3229 f135c941 2020-02-20 stsp if (argc == 0) {
3230 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3231 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3232 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3233 f135c941 2020-02-20 stsp return NULL;
3236 f135c941 2020-02-20 stsp if (worktree) {
3237 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3240 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3242 f135c941 2020-02-20 stsp return err;
3243 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3244 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3245 bfd61697 2020-11-14 stsp p) == -1) {
3246 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3247 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3251 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3253 f135c941 2020-02-20 stsp return err;
3256 f135c941 2020-02-20 stsp static const struct got_error *
3257 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3259 80ddbec8 2018-04-29 stsp const struct got_error *error;
3260 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3261 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3262 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3263 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3264 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3265 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3266 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3267 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3268 04cc582a 2018-08-01 stsp struct tog_view *view;
3269 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3271 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3272 80ddbec8 2018-04-29 stsp switch (ch) {
3274 b672a97a 2020-01-27 stsp log_branches = 1;
3277 80ddbec8 2018-04-29 stsp start_commit = optarg;
3280 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3281 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3282 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3286 17020d27 2019-03-07 stsp usage_log();
3287 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3291 80ddbec8 2018-04-29 stsp argc -= optind;
3292 80ddbec8 2018-04-29 stsp argv += optind;
3294 f135c941 2020-02-20 stsp if (argc > 1)
3295 f135c941 2020-02-20 stsp usage_log();
3297 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3298 0ae84acc 2022-06-15 tracey if (error != NULL)
3299 0ae84acc 2022-06-15 tracey goto done;
3301 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3302 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3303 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3304 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3305 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3306 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3307 c156c7a4 2020-12-18 stsp goto done;
3308 a1fbf39a 2019-08-11 stsp if (worktree)
3309 6962eb72 2020-02-20 stsp repo_path =
3310 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3312 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3313 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3314 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3315 c156c7a4 2020-12-18 stsp goto done;
3319 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3320 80ddbec8 2018-04-29 stsp if (error != NULL)
3321 ecb28ae0 2018-07-16 stsp goto done;
3323 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3324 f135c941 2020-02-20 stsp repo, worktree);
3325 f135c941 2020-02-20 stsp if (error)
3326 f135c941 2020-02-20 stsp goto done;
3328 f135c941 2020-02-20 stsp init_curses();
3330 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3331 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3332 c02c541e 2019-03-29 stsp if (error)
3333 c02c541e 2019-03-29 stsp goto done;
3335 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3336 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3337 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3338 87670572 2020-12-26 naddy if (error)
3339 87670572 2020-12-26 naddy goto done;
3342 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3343 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3344 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3345 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3346 d8f38dc4 2020-12-05 stsp if (error)
3347 d8f38dc4 2020-12-05 stsp goto done;
3348 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3350 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3351 d8f38dc4 2020-12-05 stsp if (error == NULL)
3352 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3353 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3354 d8f38dc4 2020-12-05 stsp goto done;
3355 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3356 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3357 d8f38dc4 2020-12-05 stsp if (error)
3358 d8f38dc4 2020-12-05 stsp goto done;
3361 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3362 04cc582a 2018-08-01 stsp if (view == NULL) {
3363 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3364 04cc582a 2018-08-01 stsp goto done;
3366 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3367 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3368 ba4f502b 2018-08-04 stsp if (error)
3369 ba4f502b 2018-08-04 stsp goto done;
3370 2fc00ff4 2019-08-31 stsp if (worktree) {
3371 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3372 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3373 2fc00ff4 2019-08-31 stsp worktree = NULL;
3375 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3377 f135c941 2020-02-20 stsp free(in_repo_path);
3378 ecb28ae0 2018-07-16 stsp free(repo_path);
3379 ecb28ae0 2018-07-16 stsp free(cwd);
3380 899d86c2 2018-05-10 stsp free(start_id);
3381 d8f38dc4 2020-12-05 stsp free(label);
3383 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3384 1d0f4054 2021-06-17 stsp if (repo) {
3385 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3386 1d0f4054 2021-06-17 stsp if (error == NULL)
3387 1d0f4054 2021-06-17 stsp error = close_err;
3389 ec142235 2019-03-07 stsp if (worktree)
3390 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3391 0ae84acc 2022-06-15 tracey if (pack_fds) {
3392 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3393 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3394 0ae84acc 2022-06-15 tracey if (error == NULL)
3395 0ae84acc 2022-06-15 tracey error = pack_err;
3397 51a10b52 2020-12-26 stsp tog_free_refs();
3398 80ddbec8 2018-04-29 stsp return error;
3401 4ed7e80c 2018-05-20 stsp __dead static void
3402 9f7d7167 2018-04-29 stsp usage_diff(void)
3405 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3406 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3410 6d17833f 2019-11-08 stsp static int
3411 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3412 41605754 2020-11-12 stsp regmatch_t *regmatch)
3414 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3417 336075a4 2022-06-25 op static struct tog_color *
3418 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3420 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3422 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3423 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3424 6d17833f 2019-11-08 stsp return tc;
3427 6d17833f 2019-11-08 stsp return NULL;
3430 4ed7e80c 2018-05-20 stsp static const struct got_error *
3431 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3432 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3434 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3435 44a87665 2022-06-16 stsp char *exstr = NULL;
3436 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3437 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3438 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3439 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3441 41605754 2020-11-12 stsp *wtotal = 0;
3443 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3444 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3446 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3448 44a87665 2022-06-16 stsp return err;
3450 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3451 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3452 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3453 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3454 44a87665 2022-06-16 stsp goto done;
3456 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3457 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3458 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3459 1853e0f4 2022-06-16 stsp goto done;
3461 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3462 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3463 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3464 1853e0f4 2022-06-16 stsp goto done;
3467 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3468 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3469 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3471 1853e0f4 2022-06-16 stsp goto done;
3472 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3474 1853e0f4 2022-06-16 stsp free(wline);
3475 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3476 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3478 1853e0f4 2022-06-16 stsp goto done;
3479 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3480 1853e0f4 2022-06-16 stsp wlimit -= width;
3481 1853e0f4 2022-06-16 stsp *wtotal += width;
3484 41605754 2020-11-12 stsp if (wlimit > 0) {
3485 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3486 1853e0f4 2022-06-16 stsp size_t wlen;
3488 1853e0f4 2022-06-16 stsp free(wline);
3489 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3490 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3492 1853e0f4 2022-06-16 stsp goto done;
3493 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3494 1853e0f4 2022-06-16 stsp while (i < wlen) {
3495 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3496 1853e0f4 2022-06-16 stsp if (width == -1) {
3497 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3498 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3499 1853e0f4 2022-06-16 stsp goto done;
3501 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3503 61417565 2022-06-20 mark w += width;
3506 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3507 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3508 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3509 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3510 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3511 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3512 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3516 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3517 1853e0f4 2022-06-16 stsp free(wline);
3518 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3519 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3520 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3521 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3523 1853e0f4 2022-06-16 stsp goto done;
3524 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3526 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3527 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3529 1853e0f4 2022-06-16 stsp goto done;
3530 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3532 1853e0f4 2022-06-16 stsp *wtotal += width2;
3535 145b6838 2022-06-16 stsp free(wline);
3536 44a87665 2022-06-16 stsp free(exstr);
3537 1853e0f4 2022-06-16 stsp free(seg0);
3538 1853e0f4 2022-06-16 stsp free(seg1);
3539 1853e0f4 2022-06-16 stsp free(seg2);
3540 1853e0f4 2022-06-16 stsp return err;
3543 41605754 2020-11-12 stsp static const struct got_error *
3544 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3546 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3547 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3548 61e69b96 2018-05-20 stsp const struct got_error *err;
3549 fe621944 2020-11-10 stsp int nprinted = 0;
3550 b304db33 2018-05-20 stsp char *line;
3551 826082fe 2020-12-10 stsp size_t linesize = 0;
3552 826082fe 2020-12-10 stsp ssize_t linelen;
3553 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3554 61e69b96 2018-05-20 stsp wchar_t *wline;
3555 e0b650dd 2018-05-20 stsp int width;
3556 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3557 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3558 fe621944 2020-11-10 stsp off_t line_offset;
3560 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3561 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3562 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3564 f7d12f7e 2018-08-01 stsp werase(view->window);
3566 a3404814 2018-09-02 stsp if (header) {
3567 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3568 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3569 135a2da0 2020-11-11 stsp header) == -1)
3570 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3571 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3573 135a2da0 2020-11-11 stsp free(line);
3575 a3404814 2018-09-02 stsp return err;
3577 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3578 a3404814 2018-09-02 stsp wstandout(view->window);
3579 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3580 e54cc94a 2020-11-11 stsp free(wline);
3581 e54cc94a 2020-11-11 stsp wline = NULL;
3582 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3583 a3404814 2018-09-02 stsp wstandend(view->window);
3584 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3585 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3587 a3404814 2018-09-02 stsp if (max_lines <= 1)
3588 a3404814 2018-09-02 stsp return NULL;
3589 a3404814 2018-09-02 stsp max_lines--;
3592 89f1a395 2020-12-01 naddy s->eof = 0;
3593 145b6838 2022-06-16 stsp view->maxx = 0;
3594 826082fe 2020-12-10 stsp line = NULL;
3595 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3596 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3597 826082fe 2020-12-10 stsp if (linelen == -1) {
3598 826082fe 2020-12-10 stsp if (feof(s->f)) {
3599 826082fe 2020-12-10 stsp s->eof = 1;
3602 826082fe 2020-12-10 stsp free(line);
3603 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3606 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3607 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3608 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3609 1853e0f4 2022-06-16 stsp if (err) {
3610 1853e0f4 2022-06-16 stsp free(line);
3611 1853e0f4 2022-06-16 stsp return err;
3613 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3614 1853e0f4 2022-06-16 stsp free(wline);
3615 1853e0f4 2022-06-16 stsp wline = NULL;
3617 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3619 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3620 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3621 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3622 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3623 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3624 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3625 41605754 2020-11-12 stsp if (err) {
3626 41605754 2020-11-12 stsp free(line);
3627 41605754 2020-11-12 stsp return err;
3631 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3632 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3633 969c159c 2022-06-16 stsp if (err) {
3634 969c159c 2022-06-16 stsp free(line);
3635 969c159c 2022-06-16 stsp return err;
3637 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3638 969c159c 2022-06-16 stsp free(wline);
3639 41605754 2020-11-12 stsp wline = NULL;
3642 6d17833f 2019-11-08 stsp wattr_off(view->window,
3643 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3644 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3645 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3646 fe621944 2020-11-10 stsp nprinted++;
3648 826082fe 2020-12-10 stsp free(line);
3649 fe621944 2020-11-10 stsp if (nprinted >= 1)
3650 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3651 89f1a395 2020-12-01 naddy (nprinted - 1);
3653 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3655 9b058f45 2022-06-30 mark view_border(view);
3657 89f1a395 2020-12-01 naddy if (s->eof) {
3658 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3659 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3660 c3e9aa98 2019-05-13 jcs nprinted++;
3663 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3664 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3666 c3e9aa98 2019-05-13 jcs return err;
3669 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3670 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3671 e54cc94a 2020-11-11 stsp free(wline);
3672 e54cc94a 2020-11-11 stsp wline = NULL;
3673 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3676 26ed57b2 2018-05-19 stsp return NULL;
3679 abd2672a 2018-12-23 stsp static char *
3680 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3682 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3683 09867e48 2019-08-13 stsp char *p, *s;
3685 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3686 09867e48 2019-08-13 stsp if (tm == NULL)
3687 09867e48 2019-08-13 stsp return NULL;
3688 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3689 09867e48 2019-08-13 stsp if (s == NULL)
3690 09867e48 2019-08-13 stsp return NULL;
3691 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3693 abd2672a 2018-12-23 stsp *p = '\0';
3697 4ed7e80c 2018-05-20 stsp static const struct got_error *
3698 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3699 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3701 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3702 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3703 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3704 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3706 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3707 0208f208 2020-05-05 stsp if (qid != NULL) {
3708 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3709 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3710 d7b5a0e8 2022-04-20 stsp &qid->id);
3712 0208f208 2020-05-05 stsp return err;
3714 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3715 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3716 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3717 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3718 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3720 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3724 0208f208 2020-05-05 stsp if (tree_id1) {
3725 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3727 0208f208 2020-05-05 stsp goto done;
3730 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3731 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3733 0208f208 2020-05-05 stsp goto done;
3735 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3736 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3738 0208f208 2020-05-05 stsp if (tree1)
3739 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3740 0208f208 2020-05-05 stsp if (tree2)
3741 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3742 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3743 0208f208 2020-05-05 stsp return err;
3746 0208f208 2020-05-05 stsp static const struct got_error *
3747 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3751 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3752 fe621944 2020-11-10 stsp if (p == NULL)
3753 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3754 fe621944 2020-11-10 stsp *line_offsets = p;
3755 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3756 fe621944 2020-11-10 stsp (*nlines)++;
3757 fe621944 2020-11-10 stsp return NULL;
3760 fe621944 2020-11-10 stsp static const struct got_error *
3761 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3762 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3763 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3765 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3766 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3767 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3768 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3769 45d799e2 2018-12-23 stsp time_t committer_time;
3770 45d799e2 2018-12-23 stsp const char *author, *committer;
3771 8b473291 2019-02-21 stsp char *refs_str = NULL;
3772 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3773 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3774 fe621944 2020-11-10 stsp off_t outoff = 0;
3777 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3779 8b473291 2019-02-21 stsp if (refs) {
3780 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3782 8b473291 2019-02-21 stsp return err;
3785 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3787 abd2672a 2018-12-23 stsp return err;
3789 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3790 15a94983 2018-12-23 stsp if (err) {
3791 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3792 15a94983 2018-12-23 stsp goto done;
3795 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3797 fe621944 2020-11-10 stsp goto done;
3799 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3800 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3801 fe621944 2020-11-10 stsp if (n < 0) {
3802 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3803 abd2672a 2018-12-23 stsp goto done;
3805 fe621944 2020-11-10 stsp outoff += n;
3806 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3808 fe621944 2020-11-10 stsp goto done;
3810 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3811 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3812 fe621944 2020-11-10 stsp if (n < 0) {
3813 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3814 abd2672a 2018-12-23 stsp goto done;
3816 fe621944 2020-11-10 stsp outoff += n;
3817 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3819 fe621944 2020-11-10 stsp goto done;
3821 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3822 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3823 fe621944 2020-11-10 stsp if (datestr) {
3824 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3825 fe621944 2020-11-10 stsp if (n < 0) {
3826 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3827 fe621944 2020-11-10 stsp goto done;
3829 fe621944 2020-11-10 stsp outoff += n;
3830 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3832 fe621944 2020-11-10 stsp goto done;
3834 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3835 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3836 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3837 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3838 fe621944 2020-11-10 stsp if (n < 0) {
3839 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3840 fe621944 2020-11-10 stsp goto done;
3842 fe621944 2020-11-10 stsp outoff += n;
3843 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3845 fe621944 2020-11-10 stsp goto done;
3847 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3848 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3849 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3850 9f98ca05 2021-09-24 stsp int pn = 1;
3851 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3852 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3853 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3855 9f98ca05 2021-09-24 stsp goto done;
3856 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3857 9f98ca05 2021-09-24 stsp if (n < 0) {
3858 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3859 9f98ca05 2021-09-24 stsp goto done;
3861 9f98ca05 2021-09-24 stsp outoff += n;
3862 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3864 9f98ca05 2021-09-24 stsp goto done;
3865 9f98ca05 2021-09-24 stsp free(id_str);
3866 9f98ca05 2021-09-24 stsp id_str = NULL;
3870 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3872 5943eee2 2019-08-13 stsp goto done;
3873 fe621944 2020-11-10 stsp s = logmsg;
3874 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3875 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3876 fe621944 2020-11-10 stsp if (n < 0) {
3877 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3878 fe621944 2020-11-10 stsp goto done;
3880 fe621944 2020-11-10 stsp outoff += n;
3881 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3883 fe621944 2020-11-10 stsp goto done;
3886 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3888 0208f208 2020-05-05 stsp goto done;
3889 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3890 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3891 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3892 fe621944 2020-11-10 stsp if (n < 0) {
3893 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3894 fe621944 2020-11-10 stsp goto done;
3896 fe621944 2020-11-10 stsp outoff += n;
3897 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3899 fe621944 2020-11-10 stsp goto done;
3900 0208f208 2020-05-05 stsp free((char *)pe->path);
3901 0208f208 2020-05-05 stsp free(pe->data);
3904 0208f208 2020-05-05 stsp fputc('\n', outfile);
3906 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3908 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3909 abd2672a 2018-12-23 stsp free(id_str);
3910 5943eee2 2019-08-13 stsp free(logmsg);
3911 8b473291 2019-02-21 stsp free(refs_str);
3912 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3913 7510f233 2020-08-09 stsp if (err) {
3914 ae6a6978 2020-08-09 stsp free(*line_offsets);
3915 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3916 ae6a6978 2020-08-09 stsp *nlines = 0;
3918 fe621944 2020-11-10 stsp return err;
3921 abd2672a 2018-12-23 stsp static const struct got_error *
3922 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3924 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3925 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3926 15a94983 2018-12-23 stsp int obj_type;
3928 fe621944 2020-11-10 stsp free(s->line_offsets);
3929 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3930 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3931 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3932 fe621944 2020-11-10 stsp s->nlines = 0;
3934 511a516b 2018-05-19 stsp f = got_opentemp();
3935 48ae06ee 2018-10-18 stsp if (f == NULL) {
3936 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3937 48ae06ee 2018-10-18 stsp goto done;
3939 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3940 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3941 fb43ecf1 2019-02-11 stsp goto done;
3945 15a94983 2018-12-23 stsp if (s->id1)
3946 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3948 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3950 15a94983 2018-12-23 stsp goto done;
3952 15a94983 2018-12-23 stsp switch (obj_type) {
3953 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3954 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3955 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3956 f9d37699 2022-06-28 stsp s->label1, s->label2, s->diff_context,
3957 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3959 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3960 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3961 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3962 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3963 f9d37699 2022-06-28 stsp s->repo, s->f);
3965 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3966 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3967 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3968 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3969 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3971 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3973 3ffacbe1 2020-02-02 tracey goto done;
3974 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3975 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3976 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3977 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3978 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3979 f44b1f58 2020-02-02 tracey if (err)
3980 f44b1f58 2020-02-02 tracey goto done;
3981 f44b1f58 2020-02-02 tracey } else {
3982 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3983 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3984 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3985 fe621944 2020-11-10 stsp err = write_commit_info(
3986 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3987 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3988 f44b1f58 2020-02-02 tracey if (err)
3989 f44b1f58 2020-02-02 tracey goto done;
3994 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3996 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3997 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
3998 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3999 f9d37699 2022-06-28 stsp s->repo, s->f);
4003 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4006 f44b1f58 2020-02-02 tracey if (err)
4007 f44b1f58 2020-02-02 tracey goto done;
4009 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4010 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4011 48ae06ee 2018-10-18 stsp return err;
4014 f5215bb9 2019-02-22 stsp static void
4015 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4017 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4018 f5215bb9 2019-02-22 stsp update_panels();
4019 f5215bb9 2019-02-22 stsp doupdate();
4022 f44b1f58 2020-02-02 tracey static const struct got_error *
4023 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4025 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4027 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4028 f44b1f58 2020-02-02 tracey return NULL;
4031 f44b1f58 2020-02-02 tracey static const struct got_error *
4032 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4034 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4035 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4036 f44b1f58 2020-02-02 tracey int lineno;
4037 cb713507 2022-06-16 stsp char *line = NULL;
4038 826082fe 2020-12-10 stsp size_t linesize = 0;
4039 826082fe 2020-12-10 stsp ssize_t linelen;
4041 f44b1f58 2020-02-02 tracey if (!view->searching) {
4042 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4043 f44b1f58 2020-02-02 tracey return NULL;
4046 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4047 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4048 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4050 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4052 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4054 f44b1f58 2020-02-02 tracey while (1) {
4055 f44b1f58 2020-02-02 tracey off_t offset;
4057 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4058 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4059 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4063 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4064 f44b1f58 2020-02-02 tracey lineno = 1;
4066 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4069 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4070 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4071 f44b1f58 2020-02-02 tracey free(line);
4072 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4074 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4075 cb713507 2022-06-16 stsp if (linelen != -1) {
4076 cb713507 2022-06-16 stsp char *exstr;
4077 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4080 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4081 cb713507 2022-06-16 stsp &view->regmatch)) {
4082 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4083 cb713507 2022-06-16 stsp s->matched_line = lineno;
4084 cb713507 2022-06-16 stsp free(exstr);
4087 cb713507 2022-06-16 stsp free(exstr);
4089 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4090 f44b1f58 2020-02-02 tracey lineno++;
4092 f44b1f58 2020-02-02 tracey lineno--;
4094 826082fe 2020-12-10 stsp free(line);
4096 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4097 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4098 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4101 145b6838 2022-06-16 stsp return err;
4104 48ae06ee 2018-10-18 stsp static const struct got_error *
4105 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4107 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4108 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4110 b72706c3 2022-06-01 stsp free(s->id1);
4111 b72706c3 2022-06-01 stsp s->id1 = NULL;
4112 b72706c3 2022-06-01 stsp free(s->id2);
4113 b72706c3 2022-06-01 stsp s->id2 = NULL;
4114 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4115 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4116 b72706c3 2022-06-01 stsp s->f = NULL;
4117 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4118 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4119 b72706c3 2022-06-01 stsp s->f1 = NULL;
4120 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4121 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4122 b72706c3 2022-06-01 stsp s->f2 = NULL;
4123 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4124 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4125 f9d37699 2022-06-28 stsp s->fd1 = -1;
4126 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4127 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4128 f9d37699 2022-06-28 stsp s->fd2 = -1;
4129 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4130 b72706c3 2022-06-01 stsp free(s->line_offsets);
4131 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4132 b72706c3 2022-06-01 stsp s->nlines = 0;
4133 b72706c3 2022-06-01 stsp return err;
4136 b72706c3 2022-06-01 stsp static const struct got_error *
4137 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4138 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4139 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4140 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4142 48ae06ee 2018-10-18 stsp const struct got_error *err;
4143 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4145 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4146 f9d37699 2022-06-28 stsp s->fd1 = -1;
4147 f9d37699 2022-06-28 stsp s->fd2 = -1;
4149 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4150 15a94983 2018-12-23 stsp int type1, type2;
4151 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4153 15a94983 2018-12-23 stsp return err;
4154 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4156 15a94983 2018-12-23 stsp return err;
4158 15a94983 2018-12-23 stsp if (type1 != type2)
4159 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4161 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4162 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4163 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4164 f44b1f58 2020-02-02 tracey s->repo = repo;
4165 f44b1f58 2020-02-02 tracey s->id1 = id1;
4166 f44b1f58 2020-02-02 tracey s->id2 = id2;
4167 3dbaef42 2020-11-24 stsp s->label1 = label1;
4168 3dbaef42 2020-11-24 stsp s->label2 = label2;
4170 15a94983 2018-12-23 stsp if (id1) {
4171 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4172 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4173 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4175 5465d566 2020-02-01 tracey s->id1 = NULL;
4177 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4178 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4179 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4180 b72706c3 2022-06-01 stsp goto done;
4183 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4184 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4185 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4186 a00719e9 2022-06-17 stsp goto done;
4189 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4190 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4191 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4192 b72706c3 2022-06-01 stsp goto done;
4195 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4196 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4197 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4198 f9d37699 2022-06-28 stsp goto done;
4201 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4202 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4203 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4204 f9d37699 2022-06-28 stsp goto done;
4207 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4208 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4209 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4210 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4211 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4212 5465d566 2020-02-01 tracey s->log_view = log_view;
4213 5465d566 2020-02-01 tracey s->repo = repo;
4215 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4216 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4217 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4218 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4219 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4221 b72706c3 2022-06-01 stsp goto done;
4222 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4223 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4224 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4226 b72706c3 2022-06-01 stsp goto done;
4227 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4228 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4229 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4231 b72706c3 2022-06-01 stsp goto done;
4233 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4234 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4235 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4236 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4237 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4239 b72706c3 2022-06-01 stsp goto done;
4241 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4242 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4243 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4245 b72706c3 2022-06-01 stsp goto done;
4247 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4248 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4249 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4251 b72706c3 2022-06-01 stsp goto done;
4254 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4255 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4256 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4258 5465d566 2020-02-01 tracey err = create_diff(s);
4260 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4261 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4262 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4263 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4264 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4267 b72706c3 2022-06-01 stsp close_diff_view(view);
4268 e5a0f69f 2018-08-18 stsp return err;
4271 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4272 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4274 a3404814 2018-09-02 stsp const struct got_error *err;
4275 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4276 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4277 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4279 a3404814 2018-09-02 stsp if (s->id1) {
4280 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4282 a3404814 2018-09-02 stsp return err;
4283 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4285 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4287 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4289 a3404814 2018-09-02 stsp return err;
4290 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4292 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4293 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4294 a3404814 2018-09-02 stsp free(id_str1);
4295 a3404814 2018-09-02 stsp free(id_str2);
4296 a3404814 2018-09-02 stsp return err;
4298 a3404814 2018-09-02 stsp free(id_str1);
4299 a3404814 2018-09-02 stsp free(id_str2);
4301 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4302 267bb3b8 2021-08-01 stsp free(header);
4303 267bb3b8 2021-08-01 stsp return err;
4306 15a087fe 2019-02-21 stsp static const struct got_error *
4307 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4308 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4310 d7a04538 2019-02-21 stsp const struct got_error *err;
4311 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4312 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4313 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4315 15a087fe 2019-02-21 stsp free(s->id2);
4316 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4317 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4318 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4320 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4322 d7a04538 2019-02-21 stsp return err;
4323 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4324 15a087fe 2019-02-21 stsp free(s->id1);
4325 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4326 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4327 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4328 15a087fe 2019-02-21 stsp return NULL;
4331 0cf4efb1 2018-09-29 stsp static const struct got_error *
4332 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4334 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4335 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4336 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4337 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4338 826082fe 2020-12-10 stsp char *line = NULL;
4339 826082fe 2020-12-10 stsp size_t linesize = 0;
4340 826082fe 2020-12-10 stsp ssize_t linelen;
4341 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4343 e5a0f69f 2018-08-18 stsp switch (ch) {
4345 145b6838 2022-06-16 stsp view->x = 0;
4348 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4349 640cd7ff 2022-06-22 mark view->count = 0;
4351 145b6838 2022-06-16 stsp case KEY_RIGHT:
4353 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4354 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4356 640cd7ff 2022-06-22 mark view->count = 0;
4358 145b6838 2022-06-16 stsp case KEY_LEFT:
4360 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4361 640cd7ff 2022-06-22 mark if (view->x <= 0)
4362 640cd7ff 2022-06-22 mark view->count = 0;
4366 3dbaef42 2020-11-24 stsp if (ch == 'a')
4367 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4368 3dbaef42 2020-11-24 stsp if (ch == 'w')
4369 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4370 64453f7e 2020-11-21 stsp wclear(view->window);
4371 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
4372 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4373 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4374 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
4375 64453f7e 2020-11-21 stsp err = create_diff(s);
4376 640cd7ff 2022-06-22 mark view->count = 0;
4379 912a3f79 2021-08-30 j case KEY_HOME:
4380 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4381 640cd7ff 2022-06-22 mark view->count = 0;
4384 912a3f79 2021-08-30 j case KEY_END:
4385 640cd7ff 2022-06-22 mark view->count = 0;
4389 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4393 1e37a5c2 2019-05-12 jcs case KEY_UP:
4394 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4395 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4396 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4398 640cd7ff 2022-06-22 mark view->count = 0;
4400 83cc4199 2022-06-13 stsp case CTRL('u'):
4402 83cc4199 2022-06-13 stsp nscroll /= 2;
4403 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4404 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4405 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4407 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4408 640cd7ff 2022-06-22 mark view->count = 0;
4412 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4413 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4416 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4417 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4418 1e37a5c2 2019-05-12 jcs if (!s->eof)
4419 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4421 640cd7ff 2022-06-22 mark view->count = 0;
4423 83cc4199 2022-06-13 stsp case CTRL('d'):
4425 83cc4199 2022-06-13 stsp nscroll /= 2;
4426 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4427 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4428 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4431 640cd7ff 2022-06-22 mark if (s->eof) {
4432 640cd7ff 2022-06-22 mark view->count = 0;
4436 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4437 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4438 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4439 826082fe 2020-12-10 stsp if (linelen == -1) {
4440 826082fe 2020-12-10 stsp if (feof(s->f)) {
4441 826082fe 2020-12-10 stsp s->eof = 1;
4443 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4447 826082fe 2020-12-10 stsp free(line);
4450 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4451 1e37a5c2 2019-05-12 jcs s->diff_context--;
4452 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4453 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4454 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4455 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4456 27829c9e 2020-11-21 stsp s->nlines) {
4457 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4458 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4461 640cd7ff 2022-06-22 mark view->count = 0;
4464 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4465 1e37a5c2 2019-05-12 jcs s->diff_context++;
4466 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4467 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4468 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4470 640cd7ff 2022-06-22 mark view->count = 0;
4474 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4475 640cd7ff 2022-06-22 mark view->count = 0;
4478 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4479 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4481 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4482 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4486 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4489 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4493 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4494 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4495 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4496 145b6838 2022-06-16 stsp view->x = 0;
4498 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4499 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4503 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4504 640cd7ff 2022-06-22 mark view->count = 0;
4507 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4508 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4510 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4511 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4515 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4518 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4522 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4523 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4524 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4525 145b6838 2022-06-16 stsp view->x = 0;
4527 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4528 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4531 640cd7ff 2022-06-22 mark view->count = 0;
4535 bcbd79e2 2018-08-19 stsp return err;
4538 4ed7e80c 2018-05-20 stsp static const struct got_error *
4539 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4541 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4542 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4543 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4544 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4545 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4546 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4547 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4548 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4549 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4550 3dbaef42 2020-11-24 stsp const char *errstr;
4551 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4552 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4554 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4555 26ed57b2 2018-05-19 stsp switch (ch) {
4557 64453f7e 2020-11-21 stsp force_text_diff = 1;
4560 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4562 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4563 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4564 5a20d08d 2022-02-09 op errstr, errstr);
4566 09b5bff8 2020-02-23 naddy case 'r':
4567 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4568 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4569 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4571 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4574 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4577 17020d27 2019-03-07 stsp usage_diff();
4578 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4582 26ed57b2 2018-05-19 stsp argc -= optind;
4583 26ed57b2 2018-05-19 stsp argv += optind;
4585 26ed57b2 2018-05-19 stsp if (argc == 0) {
4586 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4587 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4588 15a94983 2018-12-23 stsp id_str1 = argv[0];
4589 15a94983 2018-12-23 stsp id_str2 = argv[1];
4591 26ed57b2 2018-05-19 stsp usage_diff();
4593 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4594 0ae84acc 2022-06-15 tracey if (error)
4595 0ae84acc 2022-06-15 tracey goto done;
4597 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4598 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4599 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4600 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4601 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4602 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4603 c156c7a4 2020-12-18 stsp goto done;
4604 a273ac94 2020-02-23 naddy if (worktree)
4605 a273ac94 2020-02-23 naddy repo_path =
4606 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4608 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4609 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4610 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4611 c156c7a4 2020-12-18 stsp goto done;
4615 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4616 eb6600df 2019-01-04 stsp if (error)
4617 eb6600df 2019-01-04 stsp goto done;
4619 a273ac94 2020-02-23 naddy init_curses();
4621 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4622 51a10b52 2020-12-26 stsp if (error)
4623 51a10b52 2020-12-26 stsp goto done;
4625 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4626 26ed57b2 2018-05-19 stsp if (error)
4627 26ed57b2 2018-05-19 stsp goto done;
4629 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4630 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4631 26ed57b2 2018-05-19 stsp if (error)
4632 26ed57b2 2018-05-19 stsp goto done;
4634 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4635 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4636 26ed57b2 2018-05-19 stsp if (error)
4637 26ed57b2 2018-05-19 stsp goto done;
4639 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4640 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4641 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4642 ea5e7bb5 2018-08-01 stsp goto done;
4644 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4645 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4646 5dc9f4bc 2018-08-04 stsp if (error)
4647 5dc9f4bc 2018-08-04 stsp goto done;
4648 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4650 3dbaef42 2020-11-24 stsp free(label1);
4651 3dbaef42 2020-11-24 stsp free(label2);
4652 c02c541e 2019-03-29 stsp free(repo_path);
4653 a273ac94 2020-02-23 naddy free(cwd);
4654 1d0f4054 2021-06-17 stsp if (repo) {
4655 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4656 1d0f4054 2021-06-17 stsp if (error == NULL)
4657 1d0f4054 2021-06-17 stsp error = close_err;
4659 a273ac94 2020-02-23 naddy if (worktree)
4660 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4661 0ae84acc 2022-06-15 tracey if (pack_fds) {
4662 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4663 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4664 0ae84acc 2022-06-15 tracey if (error == NULL)
4665 0ae84acc 2022-06-15 tracey error = pack_err;
4667 51a10b52 2020-12-26 stsp tog_free_refs();
4668 26ed57b2 2018-05-19 stsp return error;
4671 4ed7e80c 2018-05-20 stsp __dead static void
4672 9f7d7167 2018-04-29 stsp usage_blame(void)
4675 87411fa9 2022-06-16 stsp fprintf(stderr,
4676 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4677 9f7d7167 2018-04-29 stsp getprogname());
4681 84451b3e 2018-07-10 stsp struct tog_blame_line {
4682 84451b3e 2018-07-10 stsp int annotated;
4683 84451b3e 2018-07-10 stsp struct got_object_id *id;
4686 4ed7e80c 2018-05-20 stsp static const struct got_error *
4687 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4689 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4690 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4691 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4692 84451b3e 2018-07-10 stsp const struct got_error *err;
4693 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4694 826082fe 2020-12-10 stsp char *line = NULL;
4695 826082fe 2020-12-10 stsp size_t linesize = 0;
4696 826082fe 2020-12-10 stsp ssize_t linelen;
4697 84451b3e 2018-07-10 stsp wchar_t *wline;
4698 27a741e5 2019-09-11 stsp int width;
4699 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4700 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4701 ab089a2a 2018-07-12 stsp char *id_str;
4702 11b20872 2019-11-08 stsp struct tog_color *tc;
4704 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4706 ab089a2a 2018-07-12 stsp return err;
4708 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4709 f7d12f7e 2018-08-01 stsp werase(view->window);
4711 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4712 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4713 ab089a2a 2018-07-12 stsp free(id_str);
4714 ab089a2a 2018-07-12 stsp return err;
4717 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4718 ab089a2a 2018-07-12 stsp free(line);
4719 2550e4c3 2018-07-13 stsp line = NULL;
4721 1cae65b4 2019-09-22 stsp return err;
4722 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4723 a3404814 2018-09-02 stsp wstandout(view->window);
4724 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4726 11b20872 2019-11-08 stsp wattr_on(view->window,
4727 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4728 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4730 11b20872 2019-11-08 stsp wattr_off(view->window,
4731 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4732 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4733 a3404814 2018-09-02 stsp wstandend(view->window);
4734 2550e4c3 2018-07-13 stsp free(wline);
4735 2550e4c3 2018-07-13 stsp wline = NULL;
4736 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4737 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4739 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4740 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4741 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4742 ab089a2a 2018-07-12 stsp free(id_str);
4743 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4745 ab089a2a 2018-07-12 stsp free(id_str);
4746 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4747 3f60a8ef 2018-07-10 stsp free(line);
4748 2550e4c3 2018-07-13 stsp line = NULL;
4750 3f60a8ef 2018-07-10 stsp return err;
4751 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4752 2550e4c3 2018-07-13 stsp free(wline);
4753 2550e4c3 2018-07-13 stsp wline = NULL;
4754 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4755 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4757 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4758 145b6838 2022-06-16 stsp view->maxx = 0;
4759 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4760 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4761 826082fe 2020-12-10 stsp if (linelen == -1) {
4762 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4763 826082fe 2020-12-10 stsp s->eof = 1;
4766 84451b3e 2018-07-10 stsp free(line);
4767 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4769 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4772 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4773 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4774 1853e0f4 2022-06-16 stsp if (err) {
4775 1853e0f4 2022-06-16 stsp free(line);
4776 1853e0f4 2022-06-16 stsp return err;
4778 1853e0f4 2022-06-16 stsp free(wline);
4779 1853e0f4 2022-06-16 stsp wline = NULL;
4780 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4782 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4783 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4785 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4786 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4787 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4788 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4789 27a741e5 2019-09-11 stsp !(view->focussed &&
4790 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4791 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4792 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4793 8d0fe45a 2019-08-12 stsp char *id_str;
4794 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4795 87411fa9 2022-06-16 stsp blame_line->id);
4796 8d0fe45a 2019-08-12 stsp if (err) {
4797 8d0fe45a 2019-08-12 stsp free(line);
4798 8d0fe45a 2019-08-12 stsp return err;
4800 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4802 11b20872 2019-11-08 stsp wattr_on(view->window,
4803 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4804 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4806 11b20872 2019-11-08 stsp wattr_off(view->window,
4807 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4808 8d0fe45a 2019-08-12 stsp free(id_str);
4809 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4811 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4812 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4815 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4816 ee41ec32 2018-07-10 stsp prev_id = NULL;
4819 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4820 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4821 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4823 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4824 41605754 2020-11-12 stsp width = 9;
4825 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4826 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4827 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4828 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4829 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4830 41605754 2020-11-12 stsp if (err) {
4831 41605754 2020-11-12 stsp free(line);
4832 41605754 2020-11-12 stsp return err;
4834 41605754 2020-11-12 stsp width += 9;
4837 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4838 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4839 4cb9d869 2022-06-16 stsp if (err) {
4840 4cb9d869 2022-06-16 stsp free(line);
4841 4cb9d869 2022-06-16 stsp return err;
4843 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4844 a75b3e2d 2022-06-16 stsp width += 9;
4845 41605754 2020-11-12 stsp free(wline);
4846 41605754 2020-11-12 stsp wline = NULL;
4849 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4850 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4851 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4852 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4854 826082fe 2020-12-10 stsp free(line);
4855 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4857 9b058f45 2022-06-30 mark view_border(view);
4859 84451b3e 2018-07-10 stsp return NULL;
4862 84451b3e 2018-07-10 stsp static const struct got_error *
4863 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4864 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4866 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4867 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4868 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4869 1a76625f 2018-10-22 stsp int errcode;
4871 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4872 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4873 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4875 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4876 1a76625f 2018-10-22 stsp if (errcode)
4877 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4879 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4880 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4881 d68a0a7d 2018-07-10 stsp goto done;
4884 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4885 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4887 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4888 d68a0a7d 2018-07-10 stsp if (line->annotated)
4889 d68a0a7d 2018-07-10 stsp goto done;
4891 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4892 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4893 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4894 84451b3e 2018-07-10 stsp goto done;
4896 84451b3e 2018-07-10 stsp line->annotated = 1;
4898 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4899 1a76625f 2018-10-22 stsp if (errcode)
4900 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4901 84451b3e 2018-07-10 stsp return err;
4904 84451b3e 2018-07-10 stsp static void *
4905 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4907 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4908 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4909 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4910 1b484788 2022-06-28 tracey int errcode, fd = -1;
4912 1b484788 2022-06-28 tracey fd = got_opentempfd();
4913 1b484788 2022-06-28 tracey if (fd == -1)
4914 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4916 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4918 61266923 2020-01-14 stsp return (void *)err;
4920 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4921 1b484788 2022-06-28 tracey blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd);
4922 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4923 fc06ba56 2019-08-22 stsp err = NULL;
4925 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4926 1a76625f 2018-10-22 stsp if (errcode)
4927 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4928 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4930 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4931 1d0f4054 2021-06-17 stsp if (err == NULL)
4932 1d0f4054 2021-06-17 stsp err = close_err;
4933 c9beca56 2018-07-22 stsp ta->repo = NULL;
4934 c9beca56 2018-07-22 stsp *ta->complete = 1;
4936 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4937 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4938 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4940 1b484788 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4941 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
4943 18430de3 2018-07-10 stsp return (void *)err;
4946 245d91c1 2018-07-12 stsp static struct got_object_id *
4947 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4948 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4950 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4952 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4953 8d0fe45a 2019-08-12 stsp return NULL;
4955 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4956 245d91c1 2018-07-12 stsp if (!line->annotated)
4957 245d91c1 2018-07-12 stsp return NULL;
4959 245d91c1 2018-07-12 stsp return line->id;
4962 b880a918 2018-07-10 stsp static const struct got_error *
4963 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4965 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4968 245d91c1 2018-07-12 stsp if (blame->thread) {
4969 1a76625f 2018-10-22 stsp int errcode;
4970 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4971 1a76625f 2018-10-22 stsp if (errcode)
4972 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4973 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4974 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4975 1a76625f 2018-10-22 stsp if (errcode)
4976 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4977 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4978 1a76625f 2018-10-22 stsp if (errcode)
4979 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4980 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4981 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4982 245d91c1 2018-07-12 stsp err = NULL;
4983 245d91c1 2018-07-12 stsp blame->thread = NULL;
4985 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4986 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4987 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4988 1d0f4054 2021-06-17 stsp if (err == NULL)
4989 1d0f4054 2021-06-17 stsp err = close_err;
4990 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4992 245d91c1 2018-07-12 stsp if (blame->f) {
4993 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4994 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4995 245d91c1 2018-07-12 stsp blame->f = NULL;
4997 57670559 2018-12-23 stsp if (blame->lines) {
4998 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4999 57670559 2018-12-23 stsp free(blame->lines[i].id);
5000 57670559 2018-12-23 stsp free(blame->lines);
5001 57670559 2018-12-23 stsp blame->lines = NULL;
5003 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5004 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5005 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5006 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5007 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5008 0ae84acc 2022-06-15 tracey if (err == NULL)
5009 0ae84acc 2022-06-15 tracey err = pack_err;
5010 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5012 245d91c1 2018-07-12 stsp return err;
5015 245d91c1 2018-07-12 stsp static const struct got_error *
5016 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5018 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5019 fc06ba56 2019-08-22 stsp int *done = arg;
5020 fc06ba56 2019-08-22 stsp int errcode;
5022 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5023 fc06ba56 2019-08-22 stsp if (errcode)
5024 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5025 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5027 fc06ba56 2019-08-22 stsp if (*done)
5028 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5030 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5031 fc06ba56 2019-08-22 stsp if (errcode)
5032 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5033 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5035 fc06ba56 2019-08-22 stsp return err;
5038 fc06ba56 2019-08-22 stsp static const struct got_error *
5039 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5041 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5042 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5043 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5044 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5045 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5046 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5047 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5048 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5049 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5051 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5052 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5054 15a94983 2018-12-23 stsp return err;
5056 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5057 eb81bc23 2022-06-28 tracey if (fd == -1) {
5058 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5059 eb81bc23 2022-06-28 tracey goto done;
5062 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5064 a44927cc 2022-04-07 stsp goto done;
5066 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5068 84451b3e 2018-07-10 stsp goto done;
5070 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5071 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5072 84451b3e 2018-07-10 stsp goto done;
5075 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5077 a70480e0 2018-06-23 stsp goto done;
5078 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5079 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5080 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5081 84451b3e 2018-07-10 stsp goto done;
5083 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5084 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5086 1fddf795 2021-01-20 stsp goto done;
5087 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5088 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5089 84451b3e 2018-07-10 stsp goto done;
5092 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5093 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5094 b02560ec 2019-08-19 stsp blame->nlines--;
5096 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5097 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5098 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5099 84451b3e 2018-07-10 stsp goto done;
5102 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5104 bd24772e 2018-07-11 stsp goto done;
5105 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5106 0ae84acc 2022-06-15 tracey pack_fds);
5107 0ae84acc 2022-06-15 tracey if (err)
5108 0ae84acc 2022-06-15 tracey goto done;
5110 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5111 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5112 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5113 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5114 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5115 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5116 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5117 245d91c1 2018-07-12 stsp goto done;
5119 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5121 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5122 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5123 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5124 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5125 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5126 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5127 a5388363 2020-12-01 naddy s->blame_complete = 0;
5129 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5130 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5131 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5132 f5a09613 2020-12-13 naddy s->selected_line = 1;
5134 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5137 a44927cc 2022-04-07 stsp if (commit)
5138 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5139 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5140 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5142 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5143 27d434c2 2018-09-15 stsp free(obj_id);
5145 245d91c1 2018-07-12 stsp stop_blame(blame);
5146 245d91c1 2018-07-12 stsp return err;
5149 245d91c1 2018-07-12 stsp static const struct got_error *
5150 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5151 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5153 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5154 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5156 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5158 c4843652 2019-08-12 stsp s->path = strdup(path);
5159 c4843652 2019-08-12 stsp if (s->path == NULL)
5160 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5162 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5163 c4843652 2019-08-12 stsp if (err) {
5164 c4843652 2019-08-12 stsp free(s->path);
5165 7cbe629d 2018-08-04 stsp return err;
5168 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5169 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5170 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5171 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5172 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5173 fb2756b9 2018-08-04 stsp s->repo = repo;
5174 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5175 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5177 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5178 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5179 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5180 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5182 11b20872 2019-11-08 stsp return err;
5185 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5186 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5187 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5188 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5189 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5191 a5388363 2020-12-01 naddy return run_blame(view);
5194 e5a0f69f 2018-08-18 stsp static const struct got_error *
5195 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5197 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5198 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5200 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5201 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5203 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5204 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5205 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5206 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5207 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5210 e5a0f69f 2018-08-18 stsp free(s->path);
5211 11b20872 2019-11-08 stsp free_colors(&s->colors);
5212 e5a0f69f 2018-08-18 stsp return err;
5215 7cbe629d 2018-08-04 stsp static const struct got_error *
5216 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5218 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5220 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5221 6c4c42e0 2019-06-24 stsp return NULL;
5224 6c4c42e0 2019-06-24 stsp static const struct got_error *
5225 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5227 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5228 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5229 6c4c42e0 2019-06-24 stsp int lineno;
5230 cb713507 2022-06-16 stsp char *line = NULL;
5231 826082fe 2020-12-10 stsp size_t linesize = 0;
5232 826082fe 2020-12-10 stsp ssize_t linelen;
5234 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5235 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5236 6c4c42e0 2019-06-24 stsp return NULL;
5239 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5240 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5241 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5243 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5245 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5247 6c4c42e0 2019-06-24 stsp while (1) {
5248 6c4c42e0 2019-06-24 stsp off_t offset;
5250 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5251 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5252 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5256 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5257 6c4c42e0 2019-06-24 stsp lineno = 1;
5259 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5262 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5263 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5264 6c4c42e0 2019-06-24 stsp free(line);
5265 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5267 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5268 cb713507 2022-06-16 stsp if (linelen != -1) {
5269 cb713507 2022-06-16 stsp char *exstr;
5270 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5273 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5274 cb713507 2022-06-16 stsp &view->regmatch)) {
5275 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5276 cb713507 2022-06-16 stsp s->matched_line = lineno;
5277 cb713507 2022-06-16 stsp free(exstr);
5280 cb713507 2022-06-16 stsp free(exstr);
5282 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5287 826082fe 2020-12-10 stsp free(line);
5289 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5290 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5291 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5294 145b6838 2022-06-16 stsp return err;
5297 6c4c42e0 2019-06-24 stsp static const struct got_error *
5298 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5300 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5301 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5302 2b380cc8 2018-10-24 stsp int errcode;
5304 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5305 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5306 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5307 2b380cc8 2018-10-24 stsp if (errcode)
5308 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5310 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5313 51fe7530 2019-08-19 stsp if (s->blame_complete)
5314 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5316 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5318 9b058f45 2022-06-30 mark view_border(view);
5319 e5a0f69f 2018-08-18 stsp return err;
5322 e5a0f69f 2018-08-18 stsp static const struct got_error *
5323 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5325 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5326 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5327 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5328 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5330 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5331 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5332 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5333 9b058f45 2022-06-30 mark --eos; /* border */
5335 e5a0f69f 2018-08-18 stsp switch (ch) {
5337 145b6838 2022-06-16 stsp view->x = 0;
5340 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5341 640cd7ff 2022-06-22 mark view->count = 0;
5343 145b6838 2022-06-16 stsp case KEY_RIGHT:
5345 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5346 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5348 640cd7ff 2022-06-22 mark view->count = 0;
5350 145b6838 2022-06-16 stsp case KEY_LEFT:
5352 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5353 640cd7ff 2022-06-22 mark if (view->x <= 0)
5354 640cd7ff 2022-06-22 mark view->count = 0;
5357 1e37a5c2 2019-05-12 jcs s->done = 1;
5359 4deef56f 2021-09-02 naddy case 'g':
5360 4deef56f 2021-09-02 naddy case KEY_HOME:
5361 4deef56f 2021-09-02 naddy s->selected_line = 1;
5362 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5363 640cd7ff 2022-06-22 mark view->count = 0;
5365 4deef56f 2021-09-02 naddy case 'G':
5366 4deef56f 2021-09-02 naddy case KEY_END:
5367 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5368 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5369 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5371 9b058f45 2022-06-30 mark s->selected_line = eos;
5372 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5374 640cd7ff 2022-06-22 mark view->count = 0;
5377 1e37a5c2 2019-05-12 jcs case KEY_UP:
5378 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5379 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5380 1e37a5c2 2019-05-12 jcs s->selected_line--;
5381 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5382 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5383 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5385 640cd7ff 2022-06-22 mark view->count = 0;
5387 83cc4199 2022-06-13 stsp case CTRL('u'):
5389 83cc4199 2022-06-13 stsp nscroll /= 2;
5390 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5391 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5392 ea025d1d 2020-02-22 naddy case CTRL('b'):
5394 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5395 640cd7ff 2022-06-22 mark if (view->count > 1)
5396 640cd7ff 2022-06-22 mark nscroll += nscroll;
5397 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5398 640cd7ff 2022-06-22 mark view->count = 0;
5401 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5402 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5404 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5407 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5408 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5409 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5410 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5411 1e37a5c2 2019-05-12 jcs s->selected_line++;
5412 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5413 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5415 640cd7ff 2022-06-22 mark view->count = 0;
5418 1e37a5c2 2019-05-12 jcs case 'p': {
5419 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5421 640cd7ff 2022-06-22 mark view->count = 0;
5422 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5423 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5424 1e37a5c2 2019-05-12 jcs if (id == NULL)
5426 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5427 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5428 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5429 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5430 1e37a5c2 2019-05-12 jcs int obj_type;
5431 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5432 1e37a5c2 2019-05-12 jcs s->repo, id);
5435 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5436 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5437 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5438 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5441 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5442 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5443 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5446 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5447 a44927cc 2022-04-07 stsp pcommit, s->path);
5448 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5449 e5a0f69f 2018-08-18 stsp if (err) {
5450 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5451 1e37a5c2 2019-05-12 jcs err = NULL;
5452 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5455 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5457 1e37a5c2 2019-05-12 jcs free(blob_id);
5458 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5459 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5460 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5463 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5464 d7b5a0e8 2022-04-20 stsp &pid->id);
5465 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5467 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5468 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5470 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5475 1e37a5c2 2019-05-12 jcs s->done = 1;
5476 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5477 1e37a5c2 2019-05-12 jcs s->done = 0;
5478 1e37a5c2 2019-05-12 jcs if (thread_err)
5480 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5481 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5482 a5388363 2020-12-01 naddy err = run_blame(view);
5487 61417565 2022-06-20 mark case 'C': {
5488 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5490 640cd7ff 2022-06-22 mark view->count = 0;
5491 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5492 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5494 1e37a5c2 2019-05-12 jcs s->done = 1;
5495 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5496 1e37a5c2 2019-05-12 jcs s->done = 0;
5497 1e37a5c2 2019-05-12 jcs if (thread_err)
5499 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5500 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5501 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5502 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5503 a5388363 2020-12-01 naddy err = run_blame(view);
5508 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5509 1e37a5c2 2019-05-12 jcs case '\r': {
5510 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5511 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5512 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5514 640cd7ff 2022-06-22 mark view->count = 0;
5515 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5516 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5517 1e37a5c2 2019-05-12 jcs if (id == NULL)
5519 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5522 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5523 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5524 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5526 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5527 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5528 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5529 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5532 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5533 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5534 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5536 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5539 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5540 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5545 e78dc838 2020-12-04 stsp view->focussed = 0;
5546 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5547 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5548 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5549 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5550 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5553 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5556 e78dc838 2020-12-04 stsp view->focus_child = 1;
5558 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5563 83cc4199 2022-06-13 stsp case CTRL('d'):
5565 83cc4199 2022-06-13 stsp nscroll /= 2;
5566 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5567 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5568 ea025d1d 2020-02-22 naddy case CTRL('f'):
5571 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5572 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5573 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5574 640cd7ff 2022-06-22 mark view->count = 0;
5577 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5578 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5579 83cc4199 2022-06-13 stsp s->selected_line +=
5580 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5581 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5583 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5584 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5586 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5587 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5589 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5590 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5591 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5592 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5596 640cd7ff 2022-06-22 mark view->count = 0;
5599 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5602 a70480e0 2018-06-23 stsp static const struct got_error *
5603 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5605 a70480e0 2018-06-23 stsp const struct got_error *error;
5606 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5607 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5608 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5609 0587e10c 2020-07-23 stsp char *link_target = NULL;
5610 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5611 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5612 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5614 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5615 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5617 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5618 a70480e0 2018-06-23 stsp switch (ch) {
5620 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5623 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5624 69069811 2018-08-02 stsp if (repo_path == NULL)
5625 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5629 17020d27 2019-03-07 stsp usage_blame();
5630 a70480e0 2018-06-23 stsp /* NOTREACHED */
5634 a70480e0 2018-06-23 stsp argc -= optind;
5635 a70480e0 2018-06-23 stsp argv += optind;
5637 f135c941 2020-02-20 stsp if (argc != 1)
5638 a70480e0 2018-06-23 stsp usage_blame();
5640 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5641 0ae84acc 2022-06-15 tracey if (error != NULL)
5642 0ae84acc 2022-06-15 tracey goto done;
5644 69069811 2018-08-02 stsp if (repo_path == NULL) {
5645 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5646 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5647 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5648 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5649 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5650 c156c7a4 2020-12-18 stsp goto done;
5651 f135c941 2020-02-20 stsp if (worktree)
5652 eb41ed75 2019-02-05 stsp repo_path =
5653 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5655 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5656 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5657 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5658 c156c7a4 2020-12-18 stsp goto done;
5662 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5663 c02c541e 2019-03-29 stsp if (error != NULL)
5664 8e94dd5b 2019-01-04 stsp goto done;
5666 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5667 f135c941 2020-02-20 stsp worktree);
5668 c02c541e 2019-03-29 stsp if (error)
5669 92205607 2019-01-04 stsp goto done;
5671 f135c941 2020-02-20 stsp init_curses();
5673 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5674 51a10b52 2020-12-26 stsp if (error)
5675 51a10b52 2020-12-26 stsp goto done;
5677 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5678 eb41ed75 2019-02-05 stsp if (error)
5679 69069811 2018-08-02 stsp goto done;
5681 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5682 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5683 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5684 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5685 a70480e0 2018-06-23 stsp if (error != NULL)
5686 66b4983c 2018-06-23 stsp goto done;
5687 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5688 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5690 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5691 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5693 a19e88aa 2018-06-23 stsp if (error != NULL)
5694 8b473291 2019-02-21 stsp goto done;
5696 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5697 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5698 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5699 e1cd8fed 2018-08-01 stsp goto done;
5702 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5703 a44927cc 2022-04-07 stsp if (error)
5704 a44927cc 2022-04-07 stsp goto done;
5706 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5707 a44927cc 2022-04-07 stsp commit, repo);
5708 7cbe629d 2018-08-04 stsp if (error)
5709 7cbe629d 2018-08-04 stsp goto done;
5711 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5712 78756c87 2020-11-24 stsp commit_id, repo);
5713 0587e10c 2020-07-23 stsp if (error)
5714 0587e10c 2020-07-23 stsp goto done;
5715 12314ad4 2019-08-31 stsp if (worktree) {
5716 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5717 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5718 12314ad4 2019-08-31 stsp worktree = NULL;
5720 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5722 69069811 2018-08-02 stsp free(repo_path);
5723 f135c941 2020-02-20 stsp free(in_repo_path);
5724 0587e10c 2020-07-23 stsp free(link_target);
5725 69069811 2018-08-02 stsp free(cwd);
5726 a70480e0 2018-06-23 stsp free(commit_id);
5727 a44927cc 2022-04-07 stsp if (commit)
5728 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5729 eb41ed75 2019-02-05 stsp if (worktree)
5730 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5731 1d0f4054 2021-06-17 stsp if (repo) {
5732 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5733 1d0f4054 2021-06-17 stsp if (error == NULL)
5734 1d0f4054 2021-06-17 stsp error = close_err;
5736 0ae84acc 2022-06-15 tracey if (pack_fds) {
5737 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5738 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5739 0ae84acc 2022-06-15 tracey if (error == NULL)
5740 0ae84acc 2022-06-15 tracey error = pack_err;
5742 51a10b52 2020-12-26 stsp tog_free_refs();
5743 a70480e0 2018-06-23 stsp return error;
5746 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5747 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5749 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5750 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5751 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5752 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5753 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5754 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5755 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5757 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5758 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5759 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5760 9b058f45 2022-06-30 mark --limit; /* border */
5762 f7d12f7e 2018-08-01 stsp werase(view->window);
5764 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5765 ffd1d5e5 2018-06-23 stsp return NULL;
5767 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5770 ffd1d5e5 2018-06-23 stsp return err;
5771 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5772 a3404814 2018-09-02 stsp wstandout(view->window);
5773 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5775 11b20872 2019-11-08 stsp wattr_on(view->window,
5776 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5777 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5779 11b20872 2019-11-08 stsp wattr_off(view->window,
5780 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5781 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5782 a3404814 2018-09-02 stsp wstandend(view->window);
5783 2550e4c3 2018-07-13 stsp free(wline);
5784 2550e4c3 2018-07-13 stsp wline = NULL;
5785 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5786 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5787 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5788 ffd1d5e5 2018-06-23 stsp return NULL;
5789 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5792 ce52c690 2018-06-23 stsp return err;
5793 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5794 2550e4c3 2018-07-13 stsp free(wline);
5795 2550e4c3 2018-07-13 stsp wline = NULL;
5796 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5797 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5798 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5799 ffd1d5e5 2018-06-23 stsp return NULL;
5800 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5801 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5802 a1eca9bb 2018-06-23 stsp return NULL;
5804 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5805 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5806 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5807 0cf4efb1 2018-09-29 stsp if (view->focussed)
5808 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5809 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5811 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5812 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5813 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5814 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5815 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5816 ffd1d5e5 2018-06-23 stsp return NULL;
5820 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5823 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5824 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5825 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5826 848d6979 2019-08-12 stsp const char *modestr = "";
5827 56e0773d 2019-11-28 stsp mode_t mode;
5829 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5830 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5832 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5833 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5834 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5836 638f9024 2019-05-13 stsp return got_error_from_errno(
5837 230a42bd 2019-05-11 jcs "got_object_id_str");
5839 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5840 63c5ca5d 2019-08-24 stsp modestr = "$";
5841 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5844 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5845 d86d3b18 2020-12-01 naddy te, s->repo);
5846 0d6c6ee3 2020-05-20 stsp if (err) {
5847 0d6c6ee3 2020-05-20 stsp free(id_str);
5848 0d6c6ee3 2020-05-20 stsp return err;
5850 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5851 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5852 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5854 848d6979 2019-08-12 stsp modestr = "@";
5856 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5857 848d6979 2019-08-12 stsp modestr = "/";
5858 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5859 848d6979 2019-08-12 stsp modestr = "*";
5860 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5861 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5862 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5863 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5864 1d13200f 2018-07-12 stsp free(id_str);
5865 0d6c6ee3 2020-05-20 stsp free(link_target);
5866 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5868 1d13200f 2018-07-12 stsp free(id_str);
5869 0d6c6ee3 2020-05-20 stsp free(link_target);
5870 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5872 ffd1d5e5 2018-06-23 stsp if (err) {
5873 ffd1d5e5 2018-06-23 stsp free(line);
5876 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5877 0cf4efb1 2018-09-29 stsp if (view->focussed)
5878 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5879 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5881 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5883 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5884 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5885 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5887 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5888 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5889 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5890 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5891 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5892 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5893 ffd1d5e5 2018-06-23 stsp free(line);
5894 2550e4c3 2018-07-13 stsp free(wline);
5895 2550e4c3 2018-07-13 stsp wline = NULL;
5897 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5898 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5899 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5903 ffd1d5e5 2018-06-23 stsp return err;
5906 ffd1d5e5 2018-06-23 stsp static void
5907 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5909 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5910 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5911 fa86c4bf 2020-11-29 stsp int i = 0;
5913 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5916 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5917 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5918 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5919 fa86c4bf 2020-11-29 stsp if (!isroot)
5920 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5923 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5924 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5928 9b058f45 2022-06-30 mark static const struct got_error *
5929 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
5931 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
5932 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5933 ffd1d5e5 2018-06-23 stsp int n = 0;
5935 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5936 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5937 694d3271 2020-12-01 naddy s->first_displayed_entry);
5939 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5941 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5942 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
5944 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
5945 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
5946 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5947 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5951 9b058f45 2022-06-30 mark return NULL;
5954 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5955 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5956 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5958 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5959 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5960 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5962 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5963 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5964 56e0773d 2019-11-28 stsp + 1 /* slash */;
5966 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5968 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5969 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5970 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5972 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5973 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5974 d9765a41 2018-06-23 stsp while (pt) {
5975 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5976 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5977 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5978 cb2ebc8a 2018-06-23 stsp goto done;
5980 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5981 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5982 cb2ebc8a 2018-06-23 stsp goto done;
5984 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5987 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5988 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5989 ce52c690 2018-06-23 stsp goto done;
5993 ce52c690 2018-06-23 stsp if (err) {
5994 ce52c690 2018-06-23 stsp free(*path);
5995 ce52c690 2018-06-23 stsp *path = NULL;
5997 ce52c690 2018-06-23 stsp return err;
6000 ce52c690 2018-06-23 stsp static const struct got_error *
6001 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6002 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6003 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6005 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6006 ce52c690 2018-06-23 stsp char *path;
6007 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6009 a0de39f3 2019-08-09 stsp *new_view = NULL;
6011 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6013 ce52c690 2018-06-23 stsp return err;
6015 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6016 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6017 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6018 83ce39e3 2019-08-12 stsp goto done;
6021 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6022 e5a0f69f 2018-08-18 stsp if (err) {
6023 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6024 fc06ba56 2019-08-22 stsp err = NULL;
6025 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6027 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6029 83ce39e3 2019-08-12 stsp free(path);
6030 69efd4c4 2018-07-18 stsp return err;
6033 69efd4c4 2018-07-18 stsp static const struct got_error *
6034 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6035 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6037 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6038 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6039 69efd4c4 2018-07-18 stsp char *path;
6041 a0de39f3 2019-08-09 stsp *new_view = NULL;
6043 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6044 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6045 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6047 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6049 69efd4c4 2018-07-18 stsp return err;
6051 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6054 e5a0f69f 2018-08-18 stsp view_close(log_view);
6056 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6057 cb2ebc8a 2018-06-23 stsp free(path);
6058 cb2ebc8a 2018-06-23 stsp return err;
6061 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6062 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6063 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6065 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6066 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6067 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6068 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6070 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6071 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6073 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6074 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6075 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6077 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6079 bc573f3b 2021-07-10 stsp goto done;
6082 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6083 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6084 bc573f3b 2021-07-10 stsp * closed on demand.
6086 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6087 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6089 bc573f3b 2021-07-10 stsp goto done;
6090 bc573f3b 2021-07-10 stsp s->tree = s->root;
6092 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6093 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6094 ffd1d5e5 2018-06-23 stsp goto done;
6096 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6097 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6098 ffd1d5e5 2018-06-23 stsp goto done;
6101 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6102 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6103 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6104 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6105 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6106 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6107 9cd7cbd1 2020-12-07 stsp goto done;
6110 fb2756b9 2018-08-04 stsp s->repo = repo;
6112 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6113 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6114 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6115 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6117 c0b01bdb 2019-11-08 stsp goto done;
6118 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6119 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6121 c0b01bdb 2019-11-08 stsp goto done;
6122 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6123 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6124 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6126 c0b01bdb 2019-11-08 stsp goto done;
6128 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6129 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6130 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6132 11b20872 2019-11-08 stsp goto done;
6134 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6135 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6137 c0b01bdb 2019-11-08 stsp goto done;
6140 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6141 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6142 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6143 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6144 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6146 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6147 bc573f3b 2021-07-10 stsp if (commit)
6148 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6150 bc573f3b 2021-07-10 stsp close_tree_view(view);
6151 ad80ab7b 2018-08-04 stsp return err;
6154 e5a0f69f 2018-08-18 stsp static const struct got_error *
6155 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6157 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6159 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6160 fb2756b9 2018-08-04 stsp free(s->tree_label);
6161 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6162 6484ec90 2018-09-29 stsp free(s->commit_id);
6163 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6164 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6165 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6166 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6167 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6168 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6169 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6170 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6171 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6172 ad80ab7b 2018-08-04 stsp free(parent);
6175 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6176 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6177 bc573f3b 2021-07-10 stsp if (s->root)
6178 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6179 7c32bd05 2019-06-22 stsp return NULL;
6182 7c32bd05 2019-06-22 stsp static const struct got_error *
6183 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6185 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6187 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6188 7c32bd05 2019-06-22 stsp return NULL;
6191 7c32bd05 2019-06-22 stsp static int
6192 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6194 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6196 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6200 7c32bd05 2019-06-22 stsp static const struct got_error *
6201 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6203 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6204 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6206 7c32bd05 2019-06-22 stsp if (!view->searching) {
6207 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6208 7c32bd05 2019-06-22 stsp return NULL;
6211 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6212 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6213 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6214 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6215 56e0773d 2019-11-28 stsp s->selected_entry);
6217 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6219 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6220 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6222 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6223 56e0773d 2019-11-28 stsp s->selected_entry);
6226 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6227 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6228 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6229 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6231 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6234 7c32bd05 2019-06-22 stsp while (1) {
6235 56e0773d 2019-11-28 stsp if (te == NULL) {
6236 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6237 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6238 ac66afb8 2019-06-24 stsp return NULL;
6240 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6241 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6243 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6246 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6247 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6248 56e0773d 2019-11-28 stsp s->matched_entry = te;
6252 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6253 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6255 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6258 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6259 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6260 7c32bd05 2019-06-22 stsp s->selected = 0;
6263 e5a0f69f 2018-08-18 stsp return NULL;
6266 ad80ab7b 2018-08-04 stsp static const struct got_error *
6267 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6269 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6270 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6271 e5a0f69f 2018-08-18 stsp char *parent_path;
6273 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6275 e5a0f69f 2018-08-18 stsp return err;
6277 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6278 e5a0f69f 2018-08-18 stsp free(parent_path);
6280 9b058f45 2022-06-30 mark view_border(view);
6281 e5a0f69f 2018-08-18 stsp return err;
6284 e5a0f69f 2018-08-18 stsp static const struct got_error *
6285 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6287 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6288 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6289 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6290 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6291 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
6293 e5a0f69f 2018-08-18 stsp switch (ch) {
6295 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6296 640cd7ff 2022-06-22 mark view->count = 0;
6299 640cd7ff 2022-06-22 mark view->count = 0;
6300 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6302 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6303 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
6304 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
6305 e78dc838 2020-12-04 stsp view->focussed = 0;
6306 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6307 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6308 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6310 1e37a5c2 2019-05-12 jcs return err;
6311 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6314 e78dc838 2020-12-04 stsp view->focus_child = 1;
6316 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6319 640cd7ff 2022-06-22 mark view->count = 0;
6320 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6321 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
6322 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
6323 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
6324 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6325 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6326 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6327 152c1c93 2020-11-29 stsp if (err) {
6328 152c1c93 2020-11-29 stsp view_close(ref_view);
6329 152c1c93 2020-11-29 stsp return err;
6331 e78dc838 2020-12-04 stsp view->focussed = 0;
6332 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6333 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6334 152c1c93 2020-11-29 stsp err = view_close_child(view);
6336 152c1c93 2020-11-29 stsp return err;
6337 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6340 e78dc838 2020-12-04 stsp view->focus_child = 1;
6342 152c1c93 2020-11-29 stsp *new_view = ref_view;
6344 e4526bf5 2021-09-03 naddy case 'g':
6345 e4526bf5 2021-09-03 naddy case KEY_HOME:
6346 e4526bf5 2021-09-03 naddy s->selected = 0;
6347 640cd7ff 2022-06-22 mark view->count = 0;
6348 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6349 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6350 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6352 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6354 e4526bf5 2021-09-03 naddy case 'G':
6355 9b058f45 2022-06-30 mark case KEY_END: {
6356 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6358 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6359 9b058f45 2022-06-30 mark --eos; /* border */
6360 e4526bf5 2021-09-03 naddy s->selected = 0;
6361 640cd7ff 2022-06-22 mark view->count = 0;
6362 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6363 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6364 e4526bf5 2021-09-03 naddy if (te == NULL) {
6365 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6366 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6371 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6372 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6374 e4526bf5 2021-09-03 naddy if (n > 0)
6375 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6379 1e37a5c2 2019-05-12 jcs case KEY_UP:
6380 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6381 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6382 1e37a5c2 2019-05-12 jcs s->selected--;
6385 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6386 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6387 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6388 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6389 640cd7ff 2022-06-22 mark view->count = 0;
6391 83cc4199 2022-06-13 stsp case CTRL('u'):
6393 83cc4199 2022-06-13 stsp nscroll /= 2;
6394 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6395 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6396 ea025d1d 2020-02-22 naddy case CTRL('b'):
6398 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6399 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6400 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6401 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6403 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6404 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6406 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6407 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6408 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6409 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6410 640cd7ff 2022-06-22 mark view->count = 0;
6413 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6414 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6415 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6416 1e37a5c2 2019-05-12 jcs s->selected++;
6419 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6420 640cd7ff 2022-06-22 mark == NULL) {
6421 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6422 640cd7ff 2022-06-22 mark view->count = 0;
6425 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6427 83cc4199 2022-06-13 stsp case CTRL('d'):
6429 83cc4199 2022-06-13 stsp nscroll /= 2;
6430 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6431 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6432 ea025d1d 2020-02-22 naddy case CTRL('f'):
6434 48bb96f0 2022-06-20 naddy case ' ':
6435 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6437 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6438 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6439 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6440 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6442 640cd7ff 2022-06-22 mark view->count = 0;
6445 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6447 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6449 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6450 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6451 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6452 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6453 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6454 640cd7ff 2022-06-22 mark view->count = 0;
6457 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6458 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6460 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6461 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6462 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6463 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6464 1e37a5c2 2019-05-12 jcs s->selected_entry =
6465 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6466 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6467 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6468 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6472 1e37a5c2 2019-05-12 jcs free(parent);
6473 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6474 56e0773d 2019-11-28 stsp s->selected_entry))) {
6475 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6476 640cd7ff 2022-06-22 mark view->count = 0;
6477 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6478 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6481 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6482 941e9f74 2019-05-21 stsp if (err) {
6483 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6486 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6487 56e0773d 2019-11-28 stsp s->selected_entry))) {
6488 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6489 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6491 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6492 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6494 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6495 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6496 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6500 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6501 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6506 640cd7ff 2022-06-22 mark view->count = 0;
6507 e78dc838 2020-12-04 stsp view->focussed = 0;
6508 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6509 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6510 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6511 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6512 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6514 669b5ffa 2018-10-07 stsp return err;
6515 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6518 e78dc838 2020-12-04 stsp view->focus_child = 1;
6520 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6523 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6524 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6525 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6526 640cd7ff 2022-06-22 mark view->count = 0;
6529 640cd7ff 2022-06-22 mark view->count = 0;
6533 ffd1d5e5 2018-06-23 stsp return err;
6536 ffd1d5e5 2018-06-23 stsp __dead static void
6537 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6540 87411fa9 2022-06-16 stsp fprintf(stderr,
6541 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6542 ffd1d5e5 2018-06-23 stsp getprogname());
6546 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6547 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6549 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6550 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6551 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6552 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6553 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6554 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6555 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6556 4e97c21c 2020-12-06 stsp char *label = NULL;
6557 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6558 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6560 5221c383 2018-08-01 stsp struct tog_view *view;
6561 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6563 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6564 ffd1d5e5 2018-06-23 stsp switch (ch) {
6566 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6569 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6570 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6571 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6575 e99e2d15 2020-11-24 naddy usage_tree();
6576 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6580 ffd1d5e5 2018-06-23 stsp argc -= optind;
6581 ffd1d5e5 2018-06-23 stsp argv += optind;
6583 55cccc34 2020-02-20 stsp if (argc > 1)
6584 e99e2d15 2020-11-24 naddy usage_tree();
6586 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6587 0ae84acc 2022-06-15 tracey if (error != NULL)
6588 0ae84acc 2022-06-15 tracey goto done;
6590 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6591 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6592 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6593 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6594 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6595 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6596 c156c7a4 2020-12-18 stsp goto done;
6597 55cccc34 2020-02-20 stsp if (worktree)
6598 52185f70 2019-02-05 stsp repo_path =
6599 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6601 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6602 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6603 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6604 c156c7a4 2020-12-18 stsp goto done;
6608 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6609 c02c541e 2019-03-29 stsp if (error != NULL)
6610 52185f70 2019-02-05 stsp goto done;
6612 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6613 55cccc34 2020-02-20 stsp repo, worktree);
6614 55cccc34 2020-02-20 stsp if (error)
6615 55cccc34 2020-02-20 stsp goto done;
6617 55cccc34 2020-02-20 stsp init_curses();
6619 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6620 c02c541e 2019-03-29 stsp if (error)
6621 52185f70 2019-02-05 stsp goto done;
6623 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6624 51a10b52 2020-12-26 stsp if (error)
6625 51a10b52 2020-12-26 stsp goto done;
6627 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6628 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6629 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6630 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6631 4e97c21c 2020-12-06 stsp if (error)
6632 4e97c21c 2020-12-06 stsp goto done;
6633 4e97c21c 2020-12-06 stsp head_ref_name = label;
6635 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6636 4e97c21c 2020-12-06 stsp if (error == NULL)
6637 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6638 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6639 4e97c21c 2020-12-06 stsp goto done;
6640 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6641 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6642 4e97c21c 2020-12-06 stsp if (error)
6643 4e97c21c 2020-12-06 stsp goto done;
6646 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6647 a44927cc 2022-04-07 stsp if (error)
6648 a44927cc 2022-04-07 stsp goto done;
6650 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6651 5221c383 2018-08-01 stsp if (view == NULL) {
6652 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6653 5221c383 2018-08-01 stsp goto done;
6655 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6656 ad80ab7b 2018-08-04 stsp if (error)
6657 ad80ab7b 2018-08-04 stsp goto done;
6658 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6659 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6660 d91faf3b 2020-12-01 naddy in_repo_path);
6661 55cccc34 2020-02-20 stsp if (error)
6662 55cccc34 2020-02-20 stsp goto done;
6665 55cccc34 2020-02-20 stsp if (worktree) {
6666 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6667 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6668 55cccc34 2020-02-20 stsp worktree = NULL;
6670 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6672 52185f70 2019-02-05 stsp free(repo_path);
6673 e4a0e26d 2020-02-20 stsp free(cwd);
6674 ffd1d5e5 2018-06-23 stsp free(commit_id);
6675 4e97c21c 2020-12-06 stsp free(label);
6677 486cd271 2020-12-06 stsp got_ref_close(ref);
6678 1d0f4054 2021-06-17 stsp if (repo) {
6679 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6680 1d0f4054 2021-06-17 stsp if (error == NULL)
6681 1d0f4054 2021-06-17 stsp error = close_err;
6683 0ae84acc 2022-06-15 tracey if (pack_fds) {
6684 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6685 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6686 0ae84acc 2022-06-15 tracey if (error == NULL)
6687 0ae84acc 2022-06-15 tracey error = pack_err;
6689 51a10b52 2020-12-26 stsp tog_free_refs();
6690 ffd1d5e5 2018-06-23 stsp return error;
6693 6458efa5 2020-11-24 stsp static const struct got_error *
6694 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6696 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6697 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6699 6458efa5 2020-11-24 stsp s->nrefs = 0;
6700 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6701 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6702 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6703 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6704 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6707 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6708 6458efa5 2020-11-24 stsp if (re == NULL)
6709 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6711 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6712 8924d611 2020-12-26 stsp if (re->ref == NULL)
6713 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6714 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6715 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6718 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6719 6458efa5 2020-11-24 stsp return NULL;
6723 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6725 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6727 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6728 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6729 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6730 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6735 6458efa5 2020-11-24 stsp static const struct got_error *
6736 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6738 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6739 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6741 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6742 6458efa5 2020-11-24 stsp s->repo = repo;
6744 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6745 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6747 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6749 6458efa5 2020-11-24 stsp return err;
6751 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6752 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6753 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6754 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6756 6458efa5 2020-11-24 stsp goto done;
6758 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6759 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6760 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6762 6458efa5 2020-11-24 stsp goto done;
6764 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6765 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6766 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6768 cc488aa7 2022-01-23 stsp goto done;
6770 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6771 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6772 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6774 6458efa5 2020-11-24 stsp goto done;
6777 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6778 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6779 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6780 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6781 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6784 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6785 6458efa5 2020-11-24 stsp return err;
6788 6458efa5 2020-11-24 stsp static const struct got_error *
6789 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6791 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6793 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6794 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6796 6458efa5 2020-11-24 stsp return NULL;
6799 6458efa5 2020-11-24 stsp static const struct got_error *
6800 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6801 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6803 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6804 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6805 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6806 6458efa5 2020-11-24 stsp int obj_type;
6808 c42c9805 2020-11-24 stsp *commit_id = NULL;
6810 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6812 6458efa5 2020-11-24 stsp return err;
6814 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6816 6458efa5 2020-11-24 stsp goto done;
6818 6458efa5 2020-11-24 stsp switch (obj_type) {
6819 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6820 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6822 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6823 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6825 6458efa5 2020-11-24 stsp goto done;
6826 c42c9805 2020-11-24 stsp free(obj_id);
6827 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6828 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6830 6458efa5 2020-11-24 stsp goto done;
6831 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6832 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6833 c42c9805 2020-11-24 stsp goto done;
6835 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6836 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6837 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6838 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6839 c42c9805 2020-11-24 stsp goto done;
6843 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6849 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6850 c42c9805 2020-11-24 stsp if (err) {
6851 c42c9805 2020-11-24 stsp free(*commit_id);
6852 c42c9805 2020-11-24 stsp *commit_id = NULL;
6854 c42c9805 2020-11-24 stsp return err;
6857 c42c9805 2020-11-24 stsp static const struct got_error *
6858 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6859 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6861 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6862 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6863 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6865 c42c9805 2020-11-24 stsp *new_view = NULL;
6867 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6868 c42c9805 2020-11-24 stsp if (err) {
6869 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6870 c42c9805 2020-11-24 stsp return err;
6872 c42c9805 2020-11-24 stsp return NULL;
6875 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6876 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6877 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6878 6458efa5 2020-11-24 stsp goto done;
6881 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6882 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6885 6458efa5 2020-11-24 stsp view_close(log_view);
6887 6458efa5 2020-11-24 stsp *new_view = log_view;
6888 c42c9805 2020-11-24 stsp free(commit_id);
6889 6458efa5 2020-11-24 stsp return err;
6892 ce5b7c56 2019-07-09 stsp static void
6893 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6895 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6896 34ba6917 2020-11-30 stsp int i = 0;
6898 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6901 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6902 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6903 34ba6917 2020-11-30 stsp if (re == NULL)
6905 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6906 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6910 9b058f45 2022-06-30 mark static const struct got_error *
6911 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
6913 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
6914 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6915 6458efa5 2020-11-24 stsp int n = 0;
6917 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6918 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6920 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6922 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6923 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6925 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
6926 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
6927 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6928 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6932 9b058f45 2022-06-30 mark return NULL;
6935 6458efa5 2020-11-24 stsp static const struct got_error *
6936 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6938 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6940 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6941 6458efa5 2020-11-24 stsp return NULL;
6944 6458efa5 2020-11-24 stsp static int
6945 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6947 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6949 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6953 6458efa5 2020-11-24 stsp static const struct got_error *
6954 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6956 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6957 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6959 6458efa5 2020-11-24 stsp if (!view->searching) {
6960 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6961 6458efa5 2020-11-24 stsp return NULL;
6964 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6965 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6966 6458efa5 2020-11-24 stsp if (s->selected_entry)
6967 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6969 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6970 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6972 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6973 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6975 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6976 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6979 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6980 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6981 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6982 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6984 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6987 6458efa5 2020-11-24 stsp while (1) {
6988 6458efa5 2020-11-24 stsp if (re == NULL) {
6989 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6990 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6991 6458efa5 2020-11-24 stsp return NULL;
6993 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6994 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6996 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6999 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7000 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7001 6458efa5 2020-11-24 stsp s->matched_entry = re;
7005 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7006 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7008 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7011 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7012 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7013 6458efa5 2020-11-24 stsp s->selected = 0;
7016 6458efa5 2020-11-24 stsp return NULL;
7019 6458efa5 2020-11-24 stsp static const struct got_error *
7020 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7022 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7023 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7024 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7025 6458efa5 2020-11-24 stsp char *line = NULL;
7026 6458efa5 2020-11-24 stsp wchar_t *wline;
7027 6458efa5 2020-11-24 stsp struct tog_color *tc;
7028 6458efa5 2020-11-24 stsp int width, n;
7029 6458efa5 2020-11-24 stsp int limit = view->nlines;
7031 6458efa5 2020-11-24 stsp werase(view->window);
7033 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7034 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7035 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
7036 9b058f45 2022-06-30 mark --limit; /* border */
7038 6458efa5 2020-11-24 stsp if (limit == 0)
7039 6458efa5 2020-11-24 stsp return NULL;
7041 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7043 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7044 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7045 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7047 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7048 6458efa5 2020-11-24 stsp if (err) {
7049 6458efa5 2020-11-24 stsp free(line);
7050 6458efa5 2020-11-24 stsp return err;
7052 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7053 6458efa5 2020-11-24 stsp wstandout(view->window);
7054 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7055 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7056 6458efa5 2020-11-24 stsp wstandend(view->window);
7057 6458efa5 2020-11-24 stsp free(wline);
7058 6458efa5 2020-11-24 stsp wline = NULL;
7059 6458efa5 2020-11-24 stsp free(line);
7060 6458efa5 2020-11-24 stsp line = NULL;
7061 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7062 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7063 6458efa5 2020-11-24 stsp if (--limit <= 0)
7064 6458efa5 2020-11-24 stsp return NULL;
7067 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7068 6458efa5 2020-11-24 stsp char *line = NULL;
7069 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7071 b4996bee 2022-06-16 stsp if (s->show_date) {
7072 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7073 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7074 b4996bee 2022-06-16 stsp struct got_object_id *id;
7075 b4996bee 2022-06-16 stsp struct tm tm;
7078 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7080 b4996bee 2022-06-16 stsp return err;
7081 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7082 b4996bee 2022-06-16 stsp if (err) {
7083 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7085 b4996bee 2022-06-16 stsp return err;
7087 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7089 b4996bee 2022-06-16 stsp if (err) {
7091 b4996bee 2022-06-16 stsp return err;
7093 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7094 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7096 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7097 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7100 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7101 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7102 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7103 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7105 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7106 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7107 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7108 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7109 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7110 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7111 6458efa5 2020-11-24 stsp struct got_object_id *id;
7112 6458efa5 2020-11-24 stsp char *id_str;
7113 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7115 6458efa5 2020-11-24 stsp return err;
7116 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7117 6458efa5 2020-11-24 stsp if (err) {
7119 6458efa5 2020-11-24 stsp return err;
7121 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7122 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7123 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7125 6458efa5 2020-11-24 stsp free(id_str);
7126 6458efa5 2020-11-24 stsp return err;
7129 6458efa5 2020-11-24 stsp free(id_str);
7130 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7131 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7132 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7134 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7136 6458efa5 2020-11-24 stsp if (err) {
7137 6458efa5 2020-11-24 stsp free(line);
7138 6458efa5 2020-11-24 stsp return err;
7140 6458efa5 2020-11-24 stsp if (n == s->selected) {
7141 6458efa5 2020-11-24 stsp if (view->focussed)
7142 6458efa5 2020-11-24 stsp wstandout(view->window);
7143 6458efa5 2020-11-24 stsp s->selected_entry = re;
7145 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7147 6458efa5 2020-11-24 stsp wattr_on(view->window,
7148 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7149 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7151 6458efa5 2020-11-24 stsp wattr_off(view->window,
7152 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7153 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7154 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7155 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7156 6458efa5 2020-11-24 stsp wstandend(view->window);
7157 6458efa5 2020-11-24 stsp free(line);
7158 6458efa5 2020-11-24 stsp free(wline);
7159 6458efa5 2020-11-24 stsp wline = NULL;
7161 6458efa5 2020-11-24 stsp s->ndisplayed++;
7162 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7165 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7168 9b058f45 2022-06-30 mark view_border(view);
7169 6458efa5 2020-11-24 stsp return err;
7172 6458efa5 2020-11-24 stsp static const struct got_error *
7173 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
7174 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7176 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7177 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7178 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7180 c42c9805 2020-11-24 stsp *new_view = NULL;
7182 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7183 c42c9805 2020-11-24 stsp if (err) {
7184 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7185 c42c9805 2020-11-24 stsp return err;
7187 c42c9805 2020-11-24 stsp return NULL;
7191 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7192 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7193 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7194 c42c9805 2020-11-24 stsp goto done;
7197 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7198 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7200 c42c9805 2020-11-24 stsp goto done;
7202 c42c9805 2020-11-24 stsp *new_view = tree_view;
7204 c42c9805 2020-11-24 stsp free(commit_id);
7205 c42c9805 2020-11-24 stsp return err;
7207 c42c9805 2020-11-24 stsp static const struct got_error *
7208 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7210 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7211 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7212 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7213 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7214 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7216 6458efa5 2020-11-24 stsp switch (ch) {
7218 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7219 640cd7ff 2022-06-22 mark view->count = 0;
7222 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7223 640cd7ff 2022-06-22 mark view->count = 0;
7226 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7227 640cd7ff 2022-06-22 mark view->count = 0;
7228 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7229 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7230 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7233 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7234 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7235 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7238 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7239 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7241 6458efa5 2020-11-24 stsp case KEY_ENTER:
7242 6458efa5 2020-11-24 stsp case '\r':
7243 640cd7ff 2022-06-22 mark view->count = 0;
7244 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7246 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7247 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7249 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7250 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7254 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7255 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7260 e78dc838 2020-12-04 stsp view->focussed = 0;
7261 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7262 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7263 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7264 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7265 6458efa5 2020-11-24 stsp err = view_close_child(view);
7267 6458efa5 2020-11-24 stsp return err;
7268 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7271 e78dc838 2020-12-04 stsp view->focus_child = 1;
7273 6458efa5 2020-11-24 stsp *new_view = log_view;
7276 640cd7ff 2022-06-22 mark view->count = 0;
7277 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7279 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7280 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
7281 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7283 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7285 e78dc838 2020-12-04 stsp view->focussed = 0;
7286 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7287 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7288 c42c9805 2020-11-24 stsp err = view_close_child(view);
7290 c42c9805 2020-11-24 stsp return err;
7291 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7294 e78dc838 2020-12-04 stsp view->focus_child = 1;
7296 c42c9805 2020-11-24 stsp *new_view = tree_view;
7298 e4526bf5 2021-09-03 naddy case 'g':
7299 e4526bf5 2021-09-03 naddy case KEY_HOME:
7300 e4526bf5 2021-09-03 naddy s->selected = 0;
7301 640cd7ff 2022-06-22 mark view->count = 0;
7302 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7304 e4526bf5 2021-09-03 naddy case 'G':
7305 9b058f45 2022-06-30 mark case KEY_END: {
7306 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7308 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7309 9b058f45 2022-06-30 mark --eos; /* border */
7310 e4526bf5 2021-09-03 naddy s->selected = 0;
7311 640cd7ff 2022-06-22 mark view->count = 0;
7312 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7313 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7314 e4526bf5 2021-09-03 naddy if (re == NULL)
7316 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7317 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7319 e4526bf5 2021-09-03 naddy if (n > 0)
7320 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7324 6458efa5 2020-11-24 stsp case KEY_UP:
7325 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7326 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7327 6458efa5 2020-11-24 stsp s->selected--;
7330 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7331 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7332 640cd7ff 2022-06-22 mark view->count = 0;
7334 83cc4199 2022-06-13 stsp case CTRL('u'):
7336 83cc4199 2022-06-13 stsp nscroll /= 2;
7337 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7338 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7339 6458efa5 2020-11-24 stsp case CTRL('b'):
7341 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7342 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7343 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7344 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7345 640cd7ff 2022-06-22 mark view->count = 0;
7348 6458efa5 2020-11-24 stsp case KEY_DOWN:
7349 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7350 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7351 6458efa5 2020-11-24 stsp s->selected++;
7354 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7355 6458efa5 2020-11-24 stsp /* can't scroll any further */
7356 640cd7ff 2022-06-22 mark view->count = 0;
7359 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7361 83cc4199 2022-06-13 stsp case CTRL('d'):
7363 83cc4199 2022-06-13 stsp nscroll /= 2;
7364 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7365 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7366 6458efa5 2020-11-24 stsp case CTRL('f'):
7368 48bb96f0 2022-06-20 naddy case ' ':
7369 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7370 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7371 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7372 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7373 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7374 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7375 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7376 640cd7ff 2022-06-22 mark view->count = 0;
7379 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7381 6458efa5 2020-11-24 stsp case CTRL('l'):
7382 640cd7ff 2022-06-22 mark view->count = 0;
7383 8924d611 2020-12-26 stsp tog_free_refs();
7384 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7387 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7388 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7390 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7391 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7392 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7395 640cd7ff 2022-06-22 mark view->count = 0;
7399 6458efa5 2020-11-24 stsp return err;
7402 6458efa5 2020-11-24 stsp __dead static void
7403 6458efa5 2020-11-24 stsp usage_ref(void)
7406 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7407 6458efa5 2020-11-24 stsp getprogname());
7411 6458efa5 2020-11-24 stsp static const struct got_error *
7412 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7414 6458efa5 2020-11-24 stsp const struct got_error *error;
7415 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7416 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7417 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7419 6458efa5 2020-11-24 stsp struct tog_view *view;
7420 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7422 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7423 6458efa5 2020-11-24 stsp switch (ch) {
7425 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7426 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7427 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7431 e99e2d15 2020-11-24 naddy usage_ref();
7432 6458efa5 2020-11-24 stsp /* NOTREACHED */
7436 6458efa5 2020-11-24 stsp argc -= optind;
7437 6458efa5 2020-11-24 stsp argv += optind;
7439 6458efa5 2020-11-24 stsp if (argc > 1)
7440 e99e2d15 2020-11-24 naddy usage_ref();
7442 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7443 0ae84acc 2022-06-15 tracey if (error != NULL)
7444 0ae84acc 2022-06-15 tracey goto done;
7446 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7447 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7448 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7449 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7450 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7451 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7452 c156c7a4 2020-12-18 stsp goto done;
7453 6458efa5 2020-11-24 stsp if (worktree)
7454 6458efa5 2020-11-24 stsp repo_path =
7455 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7457 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7458 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7459 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7460 c156c7a4 2020-12-18 stsp goto done;
7464 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7465 6458efa5 2020-11-24 stsp if (error != NULL)
7466 6458efa5 2020-11-24 stsp goto done;
7468 6458efa5 2020-11-24 stsp init_curses();
7470 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7471 51a10b52 2020-12-26 stsp if (error)
7472 51a10b52 2020-12-26 stsp goto done;
7474 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7475 6458efa5 2020-11-24 stsp if (error)
7476 6458efa5 2020-11-24 stsp goto done;
7478 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7479 6458efa5 2020-11-24 stsp if (view == NULL) {
7480 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7481 6458efa5 2020-11-24 stsp goto done;
7484 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7485 6458efa5 2020-11-24 stsp if (error)
7486 6458efa5 2020-11-24 stsp goto done;
7488 6458efa5 2020-11-24 stsp if (worktree) {
7489 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7490 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7491 6458efa5 2020-11-24 stsp worktree = NULL;
7493 6458efa5 2020-11-24 stsp error = view_loop(view);
7495 6458efa5 2020-11-24 stsp free(repo_path);
7496 6458efa5 2020-11-24 stsp free(cwd);
7497 1d0f4054 2021-06-17 stsp if (repo) {
7498 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7499 1d0f4054 2021-06-17 stsp if (close_err)
7500 1d0f4054 2021-06-17 stsp error = close_err;
7502 0ae84acc 2022-06-15 tracey if (pack_fds) {
7503 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7504 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7505 0ae84acc 2022-06-15 tracey if (error == NULL)
7506 0ae84acc 2022-06-15 tracey error = pack_err;
7508 51a10b52 2020-12-26 stsp tog_free_refs();
7509 6458efa5 2020-11-24 stsp return error;
7513 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7514 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7516 6458efa5 2020-11-24 stsp static void
7517 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7519 9b058f45 2022-06-30 mark switch (view->type) {
7520 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7521 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7522 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7523 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7527 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7528 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7530 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7531 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7534 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7535 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7536 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7538 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7539 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7540 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7542 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7543 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7544 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7550 9b058f45 2022-06-30 mark view->offset = 0;
7554 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7555 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7557 9b058f45 2022-06-30 mark static const struct got_error *
7558 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7560 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7561 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7562 9b058f45 2022-06-30 mark int *selected = NULL;
7563 9b058f45 2022-06-30 mark int header, offset;
7565 9b058f45 2022-06-30 mark switch (view->type) {
7566 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7567 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7568 9b058f45 2022-06-30 mark header = 3;
7569 9b058f45 2022-06-30 mark scrolld = NULL;
7570 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7571 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7572 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7573 9b058f45 2022-06-30 mark s->selected_line -= offset;
7574 9b058f45 2022-06-30 mark view->offset = offset;
7578 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7579 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7580 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7581 9b058f45 2022-06-30 mark header = 3;
7582 9b058f45 2022-06-30 mark selected = &s->selected;
7585 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7586 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7587 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7588 9b058f45 2022-06-30 mark header = 3;
7589 9b058f45 2022-06-30 mark selected = &s->selected;
7592 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7593 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7594 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7595 9b058f45 2022-06-30 mark header = 5;
7596 9b058f45 2022-06-30 mark selected = &s->selected;
7600 9b058f45 2022-06-30 mark selected = NULL;
7601 9b058f45 2022-06-30 mark scrolld = NULL;
7602 9b058f45 2022-06-30 mark header = 0;
7606 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7607 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7608 9b058f45 2022-06-30 mark view->offset = offset;
7609 9b058f45 2022-06-30 mark if (scrolld && offset) {
7610 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7611 9b058f45 2022-06-30 mark *selected -= offset;
7615 9b058f45 2022-06-30 mark return err;
7618 9b058f45 2022-06-30 mark static void
7619 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7623 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7624 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7625 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7626 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7628 6879ba42 2020-10-01 naddy fputc('\n', fp);
7631 4ed7e80c 2018-05-20 stsp __dead static void
7632 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7634 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7636 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7637 6879ba42 2020-10-01 naddy getprogname());
7638 6879ba42 2020-10-01 naddy if (hflag) {
7639 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7640 6879ba42 2020-10-01 naddy list_commands(fp);
7642 6879ba42 2020-10-01 naddy exit(status);
7645 c2301be8 2018-04-30 stsp static char **
7646 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7648 ee85c5e8 2020-02-29 stsp va_list ap;
7649 c2301be8 2018-04-30 stsp char **argv;
7652 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7654 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7655 c2301be8 2018-04-30 stsp if (argv == NULL)
7656 c2301be8 2018-04-30 stsp err(1, "calloc");
7657 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7658 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7659 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7660 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7663 ee85c5e8 2020-02-29 stsp va_end(ap);
7664 c2301be8 2018-04-30 stsp return argv;
7668 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7669 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7670 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7671 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7672 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7674 ee85c5e8 2020-02-29 stsp static const struct got_error *
7675 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7677 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7678 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7679 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7680 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7681 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7682 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7683 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7684 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7685 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7687 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7688 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7689 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7691 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7692 0ae84acc 2022-06-15 tracey if (error != NULL)
7693 0ae84acc 2022-06-15 tracey goto done;
7695 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7696 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7697 ee85c5e8 2020-02-29 stsp goto done;
7699 ee85c5e8 2020-02-29 stsp if (worktree)
7700 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7702 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7703 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7704 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7705 ee85c5e8 2020-02-29 stsp goto done;
7708 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7709 ee85c5e8 2020-02-29 stsp if (error != NULL)
7710 ee85c5e8 2020-02-29 stsp goto done;
7712 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7713 ee85c5e8 2020-02-29 stsp repo, worktree);
7714 ee85c5e8 2020-02-29 stsp if (error)
7715 ee85c5e8 2020-02-29 stsp goto done;
7717 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7718 84de9106 2020-12-26 stsp if (error)
7719 84de9106 2020-12-26 stsp goto done;
7720 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7721 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7722 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7723 ee85c5e8 2020-02-29 stsp if (error)
7724 ee85c5e8 2020-02-29 stsp goto done;
7726 ee85c5e8 2020-02-29 stsp if (worktree) {
7727 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7728 ee85c5e8 2020-02-29 stsp worktree = NULL;
7731 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7732 a44927cc 2022-04-07 stsp if (error)
7733 a44927cc 2022-04-07 stsp goto done;
7735 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7736 ee85c5e8 2020-02-29 stsp if (error) {
7737 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7738 ee85c5e8 2020-02-29 stsp goto done;
7739 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7740 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7741 6879ba42 2020-10-01 naddy usage(1, 1);
7742 ee85c5e8 2020-02-29 stsp /* not reached */
7745 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7746 1d0f4054 2021-06-17 stsp if (error == NULL)
7747 1d0f4054 2021-06-17 stsp error = close_err;
7748 ee85c5e8 2020-02-29 stsp repo = NULL;
7750 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7751 ee85c5e8 2020-02-29 stsp if (error)
7752 ee85c5e8 2020-02-29 stsp goto done;
7754 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7756 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7757 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7759 1d0f4054 2021-06-17 stsp if (repo) {
7760 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7761 1d0f4054 2021-06-17 stsp if (error == NULL)
7762 1d0f4054 2021-06-17 stsp error = close_err;
7764 a44927cc 2022-04-07 stsp if (commit)
7765 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7766 ee85c5e8 2020-02-29 stsp if (worktree)
7767 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7768 0ae84acc 2022-06-15 tracey if (pack_fds) {
7769 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7770 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7771 0ae84acc 2022-06-15 tracey if (error == NULL)
7772 0ae84acc 2022-06-15 tracey error = pack_err;
7775 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7776 ee85c5e8 2020-02-29 stsp free(commit_id);
7777 ee85c5e8 2020-02-29 stsp free(cwd);
7778 ee85c5e8 2020-02-29 stsp free(repo_path);
7779 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7780 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7782 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7783 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7784 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7786 87670572 2020-12-26 naddy tog_free_refs();
7787 ee85c5e8 2020-02-29 stsp return error;
7791 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7793 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7794 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7795 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7796 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7797 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7798 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7799 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7802 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7804 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7805 9f7d7167 2018-04-29 stsp switch (ch) {
7807 9f7d7167 2018-04-29 stsp hflag = 1;
7810 53ccebc2 2019-07-30 stsp Vflag = 1;
7813 6879ba42 2020-10-01 naddy usage(hflag, 1);
7814 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7818 9f7d7167 2018-04-29 stsp argc -= optind;
7819 9f7d7167 2018-04-29 stsp argv += optind;
7820 9814e6a3 2020-09-27 naddy optind = 1;
7821 c2301be8 2018-04-30 stsp optreset = 1;
7823 53ccebc2 2019-07-30 stsp if (Vflag) {
7824 53ccebc2 2019-07-30 stsp got_version_print_str();
7825 6879ba42 2020-10-01 naddy return 0;
7828 4010e238 2020-12-04 stsp #ifndef PROFILE
7829 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7830 4010e238 2020-12-04 stsp NULL) == -1)
7831 4010e238 2020-12-04 stsp err(1, "pledge");
7834 c2301be8 2018-04-30 stsp if (argc == 0) {
7835 f29d3e89 2018-06-23 stsp if (hflag)
7836 6879ba42 2020-10-01 naddy usage(hflag, 0);
7837 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7838 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7840 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7844 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7845 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7846 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7847 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7848 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7854 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7855 ee85c5e8 2020-02-29 stsp if (argc != 1)
7856 6879ba42 2020-10-01 naddy usage(0, 1);
7857 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7858 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7860 ee85c5e8 2020-02-29 stsp if (hflag)
7861 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7863 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7867 b46c1e04 2020-09-20 naddy putchar('\n');
7868 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7870 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7871 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7872 a2f4a359 2020-02-28 stsp free(cmd_argv);
7875 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7876 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);