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 8b925c6c 2022-07-16 thomas #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 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
172 51a10b52 2020-12-26 stsp const struct got_error *err;
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
178 51a10b52 2020-12-26 stsp return err;
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
221 11b20872 2019-11-08 stsp return err;
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
231 11b20872 2019-11-08 stsp struct tog_color *tc;
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
251 11b20872 2019-11-08 stsp return NULL;
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
322 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
323 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
324 3dbaef42 2020-11-24 stsp const char *label1, *label2;
325 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
326 19a6a6b5 2022-07-01 thomas int fd1, fd2;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 bddb1296 2019-11-08 stsp struct tog_colors colors;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey off_t *line_offsets;
337 f44b1f58 2020-02-02 tracey int matched_line;
338 f44b1f58 2020-02-02 tracey int selected_line;
340 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
341 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
344 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
347 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
348 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
349 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
350 1a76625f 2018-10-22 stsp int commits_needed;
351 fb280deb 2021-08-30 stsp int load_all;
352 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
353 1a76625f 2018-10-22 stsp struct commit_queue *commits;
354 1a76625f 2018-10-22 stsp const char *in_repo_path;
355 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
356 1a76625f 2018-10-22 stsp struct got_repository *repo;
357 1af5eddf 2022-06-23 thomas int *pack_fds;
358 1a76625f 2018-10-22 stsp int log_complete;
359 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
361 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
362 13add988 2019-10-15 stsp int *searching;
363 13add988 2019-10-15 stsp int *search_next_done;
364 13add988 2019-10-15 stsp regex_t *regex;
367 1a76625f 2018-10-22 stsp struct tog_log_view_state {
368 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
371 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
372 b01e7d3b 2018-08-04 stsp int selected;
373 b01e7d3b 2018-08-04 stsp char *in_repo_path;
374 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
375 b672a97a 2020-01-27 stsp int log_branches;
376 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
377 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
378 1a76625f 2018-10-22 stsp sig_atomic_t quit;
379 1a76625f 2018-10-22 stsp pthread_t thread;
380 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
381 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
382 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
383 11b20872 2019-11-08 stsp struct tog_colors colors;
384 f69c5a46 2022-07-19 thomas int use_committer;
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
394 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
395 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
396 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
397 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
400 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
401 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
403 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
404 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
405 e9424729 2018-08-04 stsp int nlines;
407 e9424729 2018-08-04 stsp struct tog_view *view;
408 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
412 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
413 e9424729 2018-08-04 stsp const char *path;
414 e9424729 2018-08-04 stsp struct got_repository *repo;
415 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
416 e9424729 2018-08-04 stsp int *complete;
417 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
418 fc06ba56 2019-08-22 stsp void *cancel_arg;
421 e9424729 2018-08-04 stsp struct tog_blame {
423 be659d10 2020-11-18 stsp off_t filesize;
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
425 6fcac457 2018-11-19 stsp int nlines;
426 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
427 e9424729 2018-08-04 stsp pthread_t thread;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
429 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
430 e9424729 2018-08-04 stsp const char *path;
431 7cd52833 2022-06-23 thomas int *pack_fds;
434 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
435 7cbe629d 2018-08-04 stsp int first_displayed_line;
436 7cbe629d 2018-08-04 stsp int last_displayed_line;
437 7cbe629d 2018-08-04 stsp int selected_line;
438 4fc71f3b 2022-07-12 thomas int last_diffed_line;
439 7cbe629d 2018-08-04 stsp int blame_complete;
442 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
443 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
444 e5a0f69f 2018-08-18 stsp char *path;
445 7cbe629d 2018-08-04 stsp struct got_repository *repo;
446 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
447 e9424729 2018-08-04 stsp struct tog_blame blame;
448 6c4c42e0 2019-06-24 stsp int matched_line;
449 11b20872 2019-11-08 stsp struct tog_colors colors;
452 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
453 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 ad80ab7b 2018-08-04 stsp int selected;
460 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
462 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
463 ad80ab7b 2018-08-04 stsp char *tree_label;
464 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
470 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
471 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
473 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
474 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
475 6458efa5 2020-11-24 stsp struct tog_colors colors;
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
479 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
480 6458efa5 2020-11-24 stsp struct got_reference *ref;
484 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
486 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
487 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
491 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 6458efa5 2020-11-24 stsp struct got_repository *repo;
493 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
494 bddb1296 2019-11-08 stsp struct tog_colors colors;
498 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
500 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
501 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
502 669b5ffa 2018-10-07 stsp * there is enough screen estate.
504 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
505 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
507 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
508 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
509 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
511 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
512 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
514 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
516 cc3c9aac 2018-08-01 stsp struct tog_view {
517 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
518 26ed57b2 2018-05-19 stsp WINDOW *window;
519 26ed57b2 2018-05-19 stsp PANEL *panel;
520 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
522 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
523 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
524 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
526 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
527 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
529 669b5ffa 2018-10-07 stsp struct tog_view *parent;
530 669b5ffa 2018-10-07 stsp struct tog_view *child;
533 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
534 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
535 e78dc838 2020-12-04 stsp * between parent and child.
536 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
537 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
538 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
539 e78dc838 2020-12-04 stsp * situations.
541 e78dc838 2020-12-04 stsp int focus_child;
543 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
544 5dc9f4bc 2018-08-04 stsp /* type-specific state */
545 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
547 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
548 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
549 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
550 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
551 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
554 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
556 e78dc838 2020-12-04 stsp struct tog_view *, int);
557 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
558 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
559 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
561 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
563 c0c4acc8 2021-01-24 stsp int search_started;
564 60493ae3 2019-06-20 stsp int searching;
565 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
567 60493ae3 2019-06-20 stsp int search_next_done;
568 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
570 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
571 1803e47f 2019-06-22 stsp regex_t regex;
572 41605754 2020-11-12 stsp regmatch_t regmatch;
575 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
576 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
577 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
578 78756c87 2020-11-24 stsp struct got_repository *);
579 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
584 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
587 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
588 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
589 78756c87 2020-11-24 stsp const char *, const char *, int);
590 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
591 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
592 e78dc838 2020-12-04 stsp struct tog_view *, int);
593 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
594 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
595 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
598 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
599 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
600 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
601 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
602 e78dc838 2020-12-04 stsp struct tog_view *, int);
603 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
604 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
605 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
608 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
609 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
610 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
611 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
612 e78dc838 2020-12-04 stsp struct tog_view *, int);
613 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
614 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
617 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
618 6458efa5 2020-11-24 stsp struct got_repository *);
619 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
621 e78dc838 2020-12-04 stsp struct tog_view *, int);
622 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
623 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
626 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
627 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
628 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
629 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
632 25791caa 2018-10-24 stsp static void
633 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
635 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
638 83baff54 2019-08-12 stsp static void
639 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
641 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
644 61266923 2020-01-14 stsp static void
645 61266923 2020-01-14 stsp tog_sigcont(int signo)
647 61266923 2020-01-14 stsp tog_sigcont_received = 1;
650 296152d1 2022-05-31 thomas static void
651 296152d1 2022-05-31 thomas tog_sigint(int signo)
653 296152d1 2022-05-31 thomas tog_sigint_received = 1;
656 296152d1 2022-05-31 thomas static void
657 296152d1 2022-05-31 thomas tog_sigterm(int signo)
659 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
662 296152d1 2022-05-31 thomas static int
663 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
665 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
666 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
669 e5a0f69f 2018-08-18 stsp static const struct got_error *
670 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
672 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
674 669b5ffa 2018-10-07 stsp if (view->child) {
675 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
676 669b5ffa 2018-10-07 stsp view->child = NULL;
678 e5a0f69f 2018-08-18 stsp if (view->close)
679 e5a0f69f 2018-08-18 stsp err = view->close(view);
680 ea5e7bb5 2018-08-01 stsp if (view->panel)
681 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
682 ea5e7bb5 2018-08-01 stsp if (view->window)
683 ea5e7bb5 2018-08-01 stsp delwin(view->window);
684 ea5e7bb5 2018-08-01 stsp free(view);
685 f2d749db 2022-07-12 thomas return err ? err : child_err;
688 ea5e7bb5 2018-08-01 stsp static struct tog_view *
689 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
690 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
692 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
694 ea5e7bb5 2018-08-01 stsp if (view == NULL)
695 ea5e7bb5 2018-08-01 stsp return NULL;
697 d6b05b5b 2018-08-04 stsp view->type = type;
698 f7d12f7e 2018-08-01 stsp view->lines = LINES;
699 f7d12f7e 2018-08-01 stsp view->cols = COLS;
700 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
701 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
702 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
703 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
704 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
705 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
706 96a765a8 2018-08-04 stsp view_close(view);
707 ea5e7bb5 2018-08-01 stsp return NULL;
709 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
710 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
711 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
712 96a765a8 2018-08-04 stsp view_close(view);
713 ea5e7bb5 2018-08-01 stsp return NULL;
716 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
717 ea5e7bb5 2018-08-01 stsp return view;
721 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
723 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
725 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
728 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
729 a5d43cac 2022-07-01 thomas static int
730 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
732 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
735 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
737 5c60c32a 2018-10-18 stsp static const struct got_error *
738 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
740 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
742 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
743 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
744 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
746 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
747 a5d43cac 2022-07-01 thomas view->begin_x = 0;
748 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
749 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
750 53d2bdd3 2022-07-10 thomas view->cols > 119)
751 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
753 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
754 a5d43cac 2022-07-01 thomas view->begin_y = 0;
756 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
757 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
758 5c60c32a 2018-10-18 stsp view->lines = LINES;
759 5c60c32a 2018-10-18 stsp view->cols = COLS;
760 5c60c32a 2018-10-18 stsp err = view_resize(view);
762 5c60c32a 2018-10-18 stsp return err;
764 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
765 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
767 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
768 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
770 5c60c32a 2018-10-18 stsp return NULL;
773 5c60c32a 2018-10-18 stsp static const struct got_error *
774 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
776 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
778 5c60c32a 2018-10-18 stsp view->begin_x = 0;
779 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
780 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
781 5c60c32a 2018-10-18 stsp view->ncols = COLS;
782 5c60c32a 2018-10-18 stsp view->lines = LINES;
783 5c60c32a 2018-10-18 stsp view->cols = COLS;
784 5c60c32a 2018-10-18 stsp err = view_resize(view);
786 5c60c32a 2018-10-18 stsp return err;
788 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
789 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
791 5c60c32a 2018-10-18 stsp return NULL;
795 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
797 5c60c32a 2018-10-18 stsp return view->parent == NULL;
800 b65b3ea0 2022-06-23 thomas static int
801 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
803 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
806 e44940c3 2022-07-01 thomas static int
807 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
809 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
812 444d5325 2022-07-03 thomas static int
813 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
815 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
816 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
819 a5d43cac 2022-07-01 thomas static void
820 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
822 a5d43cac 2022-07-01 thomas PANEL *panel;
823 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
825 a5d43cac 2022-07-01 thomas if (view->parent)
826 a5d43cac 2022-07-01 thomas return view_border(view->parent);
828 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
829 a5d43cac 2022-07-01 thomas if (panel == NULL)
832 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
833 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
834 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
835 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
836 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
838 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
839 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
842 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
843 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
844 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
846 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
848 4d8c2215 2018-08-19 stsp static const struct got_error *
849 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
851 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
852 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
854 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
856 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
857 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
859 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
860 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
861 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
863 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
865 4918811f 2022-07-01 thomas if (view->child) {
866 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
868 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
869 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
870 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
871 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
872 40236d76 2022-06-23 thomas ncols = COLS;
874 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
875 5c60c32a 2018-10-18 stsp if (view->child->focussed)
876 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
878 5c60c32a 2018-10-18 stsp show_panel(view->panel);
880 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
882 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
883 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
886 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
887 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
888 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
890 a5d43cac 2022-07-01 thomas if (hs) {
891 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
893 a5d43cac 2022-07-01 thomas return err;
894 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
895 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
897 a5d43cac 2022-07-01 thomas return err;
899 a5d43cac 2022-07-01 thomas view_border(view);
900 a5d43cac 2022-07-01 thomas update_panels();
901 a5d43cac 2022-07-01 thomas doupdate();
902 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
903 a5d43cac 2022-07-01 thomas nlines = view->nlines;
905 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
906 40236d76 2022-06-23 thomas ncols = COLS;
908 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
909 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
911 ea0bff04 2022-07-19 thomas return err;
914 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
915 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
916 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
917 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
918 40236d76 2022-06-23 thomas wclear(view->window);
920 40236d76 2022-06-23 thomas view->nlines = nlines;
921 40236d76 2022-06-23 thomas view->ncols = ncols;
922 40236d76 2022-06-23 thomas view->lines = LINES;
923 40236d76 2022-06-23 thomas view->cols = COLS;
925 5c60c32a 2018-10-18 stsp return NULL;
928 ea0bff04 2022-07-19 thomas static const struct got_error *
929 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
931 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
932 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
933 3a0139e8 2022-07-22 thomas int n = 0;
935 3a0139e8 2022-07-22 thomas if (s->selected_entry)
936 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
939 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
940 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
942 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
943 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
944 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
947 ea0bff04 2022-07-19 thomas return err;
950 97cb21cd 2022-07-12 thomas static void
951 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
953 97cb21cd 2022-07-12 thomas if (n == 0)
956 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
957 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
958 97cb21cd 2022-07-12 thomas view->parent->offset += n;
960 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
961 97cb21cd 2022-07-12 thomas } else if (view->offset) {
962 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
963 97cb21cd 2022-07-12 thomas view->offset -= n;
965 97cb21cd 2022-07-12 thomas view->offset = 0;
969 53d2bdd3 2022-07-10 thomas static const struct got_error *
970 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
972 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
973 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
975 53d2bdd3 2022-07-10 thomas if (view->parent)
976 53d2bdd3 2022-07-10 thomas v = view->parent;
978 53d2bdd3 2022-07-10 thomas v = view;
980 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
981 53d2bdd3 2022-07-10 thomas return NULL;
983 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
985 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
986 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
987 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
988 53d2bdd3 2022-07-10 thomas if (view->parent)
989 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
991 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
992 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
993 53d2bdd3 2022-07-10 thomas view->count = 0;
994 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
995 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
996 53d2bdd3 2022-07-10 thomas view->count = 0;
997 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
999 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1000 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1001 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1002 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1003 53d2bdd3 2022-07-10 thomas if (err)
1004 53d2bdd3 2022-07-10 thomas return err;
1005 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1006 53d2bdd3 2022-07-10 thomas } else {
1007 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1008 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1009 53d2bdd3 2022-07-10 thomas if (view->parent)
1010 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1012 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1013 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1014 53d2bdd3 2022-07-10 thomas view->count = 0;
1015 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1016 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1017 53d2bdd3 2022-07-10 thomas view->count = 0;
1018 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1020 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1023 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1024 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1025 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1026 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1028 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1029 53d2bdd3 2022-07-10 thomas if (err)
1030 53d2bdd3 2022-07-10 thomas return err;
1031 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1032 53d2bdd3 2022-07-10 thomas if (err)
1033 53d2bdd3 2022-07-10 thomas return err;
1035 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1036 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1037 53d2bdd3 2022-07-10 thomas if (err)
1038 53d2bdd3 2022-07-10 thomas return err;
1041 3a0139e8 2022-07-22 thomas if (v->resize)
1042 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1043 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1044 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1046 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1048 53d2bdd3 2022-07-10 thomas return err;
1051 53d2bdd3 2022-07-10 thomas static void
1052 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1054 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1056 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1057 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1060 669b5ffa 2018-10-07 stsp static const struct got_error *
1061 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1063 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1065 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1066 669b5ffa 2018-10-07 stsp return NULL;
1068 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1069 669b5ffa 2018-10-07 stsp view->child = NULL;
1070 669b5ffa 2018-10-07 stsp return err;
1073 40236d76 2022-06-23 thomas static const struct got_error *
1074 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1076 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1078 669b5ffa 2018-10-07 stsp view->child = child;
1079 669b5ffa 2018-10-07 stsp child->parent = view;
1081 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1082 53d2bdd3 2022-07-10 thomas if (err)
1083 53d2bdd3 2022-07-10 thomas return err;
1085 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1086 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1088 53d2bdd3 2022-07-10 thomas return err;
1091 34bc9ec9 2019-02-22 stsp static void
1092 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1094 25791caa 2018-10-24 stsp int cols, lines;
1095 25791caa 2018-10-24 stsp struct winsize size;
1097 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1098 25791caa 2018-10-24 stsp cols = 80; /* Default */
1099 25791caa 2018-10-24 stsp lines = 24;
1101 25791caa 2018-10-24 stsp cols = size.ws_col;
1102 25791caa 2018-10-24 stsp lines = size.ws_row;
1104 25791caa 2018-10-24 stsp resize_term(lines, cols);
1107 2b49a8ae 2019-06-22 stsp static const struct got_error *
1108 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1110 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1111 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1112 2b49a8ae 2019-06-22 stsp char pattern[1024];
1115 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1116 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1117 c0c4acc8 2021-01-24 stsp view->searching = 0;
1118 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1120 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1122 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1123 2b49a8ae 2019-06-22 stsp return NULL;
1125 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1126 a5d43cac 2022-07-01 thomas v = view->child;
1128 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1129 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1131 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1132 2b49a8ae 2019-06-22 stsp nocbreak();
1134 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1135 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1138 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1139 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1140 2b49a8ae 2019-06-22 stsp return NULL;
1142 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1143 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1144 7c32bd05 2019-06-22 stsp if (err) {
1145 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1146 7c32bd05 2019-06-22 stsp return err;
1148 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1149 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1150 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1151 2b49a8ae 2019-06-22 stsp view->search_next(view);
1154 2b49a8ae 2019-06-22 stsp return NULL;
1157 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1158 64486692 2022-07-07 thomas static const struct got_error *
1159 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1161 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1162 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1164 64486692 2022-07-07 thomas if (view->parent)
1165 64486692 2022-07-07 thomas v = view->parent;
1167 64486692 2022-07-07 thomas v = view;
1169 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1170 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1172 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1174 ddbc4d37 2022-07-12 thomas if (!v->child)
1175 ddbc4d37 2022-07-12 thomas return NULL;
1176 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1177 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1179 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1180 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1181 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1182 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1183 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1186 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1187 64486692 2022-07-07 thomas v->ncols = COLS;
1188 64486692 2022-07-07 thomas v->child->ncols = COLS;
1189 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1191 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1192 64486692 2022-07-07 thomas if (err)
1193 64486692 2022-07-07 thomas return err;
1195 64486692 2022-07-07 thomas v->child->mode = v->mode;
1196 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1197 64486692 2022-07-07 thomas v->focus_child = 1;
1199 64486692 2022-07-07 thomas err = view_fullscreen(v);
1200 64486692 2022-07-07 thomas if (err)
1201 64486692 2022-07-07 thomas return err;
1202 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1203 64486692 2022-07-07 thomas if (err)
1204 64486692 2022-07-07 thomas return err;
1206 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1207 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1208 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1209 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1210 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1211 ddbc4d37 2022-07-12 thomas } else {
1212 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1213 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1215 f4e6231a 2022-07-22 thomas if (v->resize)
1216 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1217 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1218 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1220 64486692 2022-07-07 thomas return err;
1224 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1225 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1227 07b0611c 2022-06-23 thomas static int
1228 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1230 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1231 a5d43cac 2022-07-01 thomas int x, n = 0;
1233 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1234 a5d43cac 2022-07-01 thomas v = view->child;
1235 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1236 a5d43cac 2022-07-01 thomas v = view->parent;
1238 07b0611c 2022-06-23 thomas view->count = 0;
1239 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1240 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1241 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1242 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1245 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1246 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1247 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1248 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1252 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1253 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1255 07b0611c 2022-06-23 thomas if (n >= 9999999)
1256 07b0611c 2022-06-23 thomas n = 9999999;
1258 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1259 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1261 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1262 07b0611c 2022-06-23 thomas view->count = n;
1264 07b0611c 2022-06-23 thomas return c;
1267 0cf4efb1 2018-09-29 stsp static const struct got_error *
1268 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1269 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1271 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1272 669b5ffa 2018-10-07 stsp struct tog_view *v;
1273 1a76625f 2018-10-22 stsp int ch, errcode;
1275 e5a0f69f 2018-08-18 stsp *new = NULL;
1277 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1278 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1279 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1280 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1281 07b0611c 2022-06-23 thomas view->count = 0;
1284 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1285 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1286 82954512 2020-02-03 stsp if (errcode)
1287 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1288 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1289 3da8ef85 2021-09-21 stsp sched_yield();
1290 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1291 82954512 2020-02-03 stsp if (errcode)
1292 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1293 82954512 2020-02-03 stsp "pthread_mutex_lock");
1294 60493ae3 2019-06-20 stsp view->search_next(view);
1295 60493ae3 2019-06-20 stsp return NULL;
1298 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1299 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1300 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1301 1a76625f 2018-10-22 stsp if (errcode)
1302 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1303 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1304 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1305 634cb454 2022-07-03 thomas cbreak();
1306 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1307 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1308 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1309 634cb454 2022-07-03 thomas view->count = 0;
1311 634cb454 2022-07-03 thomas ch = view->ch;
1312 634cb454 2022-07-03 thomas } else {
1313 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1314 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1315 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1317 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1318 1a76625f 2018-10-22 stsp if (errcode)
1319 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1320 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1322 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1323 25791caa 2018-10-24 stsp tog_resizeterm();
1324 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1325 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1326 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1327 25791caa 2018-10-24 stsp err = view_resize(v);
1329 25791caa 2018-10-24 stsp return err;
1330 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1332 25791caa 2018-10-24 stsp return err;
1333 cdfcfb03 2020-12-06 stsp if (v->child) {
1334 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1336 cdfcfb03 2020-12-06 stsp return err;
1337 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1338 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1340 cdfcfb03 2020-12-06 stsp return err;
1341 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1342 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1343 53d2bdd3 2022-07-10 thomas if (err)
1344 53d2bdd3 2022-07-10 thomas return err;
1350 e5a0f69f 2018-08-18 stsp switch (ch) {
1352 07b0611c 2022-06-23 thomas view->count = 0;
1353 1e37a5c2 2019-05-12 jcs if (view->child) {
1354 e78dc838 2020-12-04 stsp view->focussed = 0;
1355 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1356 e78dc838 2020-12-04 stsp view->focus_child = 1;
1357 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1358 e78dc838 2020-12-04 stsp view->focussed = 0;
1359 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1360 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1361 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1362 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1363 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1365 a5d43cac 2022-07-01 thomas if (err)
1366 a5d43cac 2022-07-01 thomas return err;
1368 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1369 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1370 a5d43cac 2022-07-01 thomas if (err)
1371 a5d43cac 2022-07-01 thomas return err;
1376 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1377 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1378 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1379 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1380 a5d43cac 2022-07-01 thomas if (err)
1383 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1385 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1386 9970f7fc 2020-12-03 stsp view->dying = 1;
1391 1c5e5faa 2022-06-23 thomas case 'F':
1392 07b0611c 2022-06-23 thomas view->count = 0;
1393 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1394 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1396 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1397 e78dc838 2020-12-04 stsp view->focussed = 0;
1398 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1399 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1400 53d2bdd3 2022-07-10 thomas } else {
1401 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1402 53d2bdd3 2022-07-10 thomas if (!err)
1403 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1407 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1408 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1410 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1411 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1412 e78dc838 2020-12-04 stsp view->focussed = 1;
1413 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1415 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1416 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1417 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1418 53d2bdd3 2022-07-10 thomas if (!err)
1419 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1423 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1425 a5d43cac 2022-07-01 thomas if (err)
1427 3a0139e8 2022-07-22 thomas if (view->resize) {
1428 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1429 a5d43cac 2022-07-01 thomas if (err)
1432 a5d43cac 2022-07-01 thomas if (view->parent)
1433 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1434 a5d43cac 2022-07-01 thomas if (!err)
1435 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1437 64486692 2022-07-07 thomas case 'S':
1438 53d2bdd3 2022-07-10 thomas view->count = 0;
1439 64486692 2022-07-07 thomas err = switch_split(view);
1441 53d2bdd3 2022-07-10 thomas case '-':
1442 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1444 53d2bdd3 2022-07-10 thomas case '+':
1445 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1447 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1450 07b0611c 2022-06-23 thomas view->count = 0;
1451 60493ae3 2019-06-20 stsp if (view->search_start)
1452 2b49a8ae 2019-06-22 stsp view_search_start(view);
1454 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1458 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1459 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1460 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1461 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1462 60493ae3 2019-06-20 stsp view->search_next(view);
1464 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1466 adf4c9e0 2022-07-03 thomas case 'A':
1467 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1468 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1470 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1471 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1472 adf4c9e0 2022-07-03 thomas if (v->reset) {
1473 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1474 adf4c9e0 2022-07-03 thomas if (err)
1475 adf4c9e0 2022-07-03 thomas return err;
1477 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1478 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1479 adf4c9e0 2022-07-03 thomas if (err)
1480 adf4c9e0 2022-07-03 thomas return err;
1485 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1489 e5a0f69f 2018-08-18 stsp return err;
1492 ef20f542 2022-06-26 thomas static int
1493 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1495 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1496 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1498 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1500 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1503 669b5ffa 2018-10-07 stsp return view->focussed;
1506 bcbd79e2 2018-08-19 stsp static const struct got_error *
1507 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1509 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1510 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1511 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1512 64486692 2022-07-07 thomas char *mode;
1513 fd823528 2018-10-22 stsp int fast_refresh = 10;
1514 1a76625f 2018-10-22 stsp int done = 0, errcode;
1516 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1517 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1518 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1520 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1522 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1523 1a76625f 2018-10-22 stsp if (errcode)
1524 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1526 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1527 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1529 1004088d 2018-09-29 stsp view->focussed = 1;
1530 878940b7 2018-09-29 stsp err = view->show(view);
1532 0cf4efb1 2018-09-29 stsp return err;
1533 0cf4efb1 2018-09-29 stsp update_panels();
1534 0cf4efb1 2018-09-29 stsp doupdate();
1535 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1536 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1537 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1538 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1539 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1541 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1544 9970f7fc 2020-12-03 stsp if (view->dying) {
1545 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1547 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1548 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1550 e78dc838 2020-12-04 stsp else if (view->parent)
1551 669b5ffa 2018-10-07 stsp prev = view->parent;
1553 e78dc838 2020-12-04 stsp if (view->parent) {
1554 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1555 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1556 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1557 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1558 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1559 40236d76 2022-06-23 thomas if (err)
1561 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1562 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1564 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1566 9970f7fc 2020-12-03 stsp err = view_close(view);
1568 e5a0f69f 2018-08-18 stsp goto done;
1570 e78dc838 2020-12-04 stsp view = NULL;
1571 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1572 e78dc838 2020-12-04 stsp if (v->focussed)
1575 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1576 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1578 e78dc838 2020-12-04 stsp view = prev;
1579 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1580 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1581 e78dc838 2020-12-04 stsp tog_view_list_head);
1583 e78dc838 2020-12-04 stsp if (view) {
1584 e78dc838 2020-12-04 stsp if (view->focus_child) {
1585 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1586 e78dc838 2020-12-04 stsp view = view->child;
1588 e78dc838 2020-12-04 stsp view->focussed = 1;
1592 bcbd79e2 2018-08-19 stsp if (new_view) {
1593 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1594 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1595 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1596 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1598 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1599 86c66b02 2018-10-18 stsp err = view_close(v);
1601 86c66b02 2018-10-18 stsp goto done;
1604 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1605 fed7eaa8 2018-10-24 stsp view = new_view;
1607 669b5ffa 2018-10-07 stsp if (view) {
1608 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1609 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1610 e78dc838 2020-12-04 stsp view = view->child;
1612 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1613 e78dc838 2020-12-04 stsp view = view->parent;
1615 e78dc838 2020-12-04 stsp show_panel(view->panel);
1616 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1617 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1618 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1619 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1621 1a76625f 2018-10-22 stsp goto done;
1623 669b5ffa 2018-10-07 stsp err = view->show(view);
1625 1a76625f 2018-10-22 stsp goto done;
1626 669b5ffa 2018-10-07 stsp if (view->child) {
1627 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1629 1a76625f 2018-10-22 stsp goto done;
1631 1a76625f 2018-10-22 stsp update_panels();
1632 1a76625f 2018-10-22 stsp doupdate();
1636 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1637 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1638 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1639 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1640 f2d749db 2022-07-12 thomas close_err = view_close(view);
1641 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1642 f2d749db 2022-07-12 thomas err = close_err;
1645 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1646 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1647 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1649 e5a0f69f 2018-08-18 stsp return err;
1652 4ed7e80c 2018-05-20 stsp __dead static void
1653 9f7d7167 2018-04-29 stsp usage_log(void)
1656 c70c5802 2018-08-01 stsp fprintf(stderr,
1657 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1658 9f7d7167 2018-04-29 stsp getprogname());
1662 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1663 80ddbec8 2018-04-29 stsp static const struct got_error *
1664 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1666 00dfcb92 2018-06-11 stsp char *vis = NULL;
1667 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1669 963b370f 2018-05-20 stsp *ws = NULL;
1670 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1671 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1672 00dfcb92 2018-06-11 stsp int vislen;
1673 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1674 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1676 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1677 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1679 00dfcb92 2018-06-11 stsp return err;
1680 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1681 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1682 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1683 a7f50699 2018-06-11 stsp goto done;
1687 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1688 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1689 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1690 a7f50699 2018-06-11 stsp goto done;
1693 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1694 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1696 00dfcb92 2018-06-11 stsp free(vis);
1697 963b370f 2018-05-20 stsp if (err) {
1698 963b370f 2018-05-20 stsp free(*ws);
1699 963b370f 2018-05-20 stsp *ws = NULL;
1700 963b370f 2018-05-20 stsp *wlen = 0;
1702 963b370f 2018-05-20 stsp return err;
1705 05171be4 2022-06-23 thomas static const struct got_error *
1706 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1708 05171be4 2022-06-23 thomas char *dst;
1709 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1711 05171be4 2022-06-23 thomas *ptr = NULL;
1712 05171be4 2022-06-23 thomas n = len = strlen(src);
1713 83316834 2022-06-23 thomas dst = malloc(n + 1);
1714 05171be4 2022-06-23 thomas if (dst == NULL)
1715 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1717 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1718 05171be4 2022-06-23 thomas const char c = src[idx];
1720 05171be4 2022-06-23 thomas if (c == '\t') {
1721 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1722 b235ad5d 2022-06-23 thomas char *p;
1724 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1725 83316834 2022-06-23 thomas if (p == NULL) {
1726 83316834 2022-06-23 thomas free(dst);
1727 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1730 83316834 2022-06-23 thomas dst = p;
1731 05171be4 2022-06-23 thomas n += nb;
1732 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1733 05171be4 2022-06-23 thomas sz += nb;
1735 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1739 05171be4 2022-06-23 thomas dst[sz] = '\0';
1740 05171be4 2022-06-23 thomas *ptr = dst;
1741 05171be4 2022-06-23 thomas return NULL;
1745 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1746 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1747 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1748 8d208d34 2022-06-23 thomas * *rcol.
1750 8d208d34 2022-06-23 thomas static int
1751 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1753 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1755 8d208d34 2022-06-23 thomas if (n == 0) {
1756 8d208d34 2022-06-23 thomas *rcol = cols;
1757 8d208d34 2022-06-23 thomas return off;
1760 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1761 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1762 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1764 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1766 8d208d34 2022-06-23 thomas if (width == -1) {
1767 8d208d34 2022-06-23 thomas width = 1;
1768 8d208d34 2022-06-23 thomas wline[i] = L'.';
1771 8d208d34 2022-06-23 thomas if (cols + width > n)
1773 8d208d34 2022-06-23 thomas cols += width;
1776 8d208d34 2022-06-23 thomas *rcol = cols;
1777 8d208d34 2022-06-23 thomas return i;
1781 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1782 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1783 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1785 f91a2b48 2022-06-23 thomas static const struct got_error *
1786 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1787 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1789 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1790 8d208d34 2022-06-23 thomas int cols;
1791 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1792 05171be4 2022-06-23 thomas char *exstr = NULL;
1793 963b370f 2018-05-20 stsp size_t wlen;
1794 8d208d34 2022-06-23 thomas int i, scrollx;
1796 963b370f 2018-05-20 stsp *wlinep = NULL;
1797 b700b5d6 2018-07-10 stsp *widthp = 0;
1799 05171be4 2022-06-23 thomas if (expand) {
1800 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1801 05171be4 2022-06-23 thomas if (err)
1802 05171be4 2022-06-23 thomas return err;
1805 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1806 05171be4 2022-06-23 thomas free(exstr);
1808 963b370f 2018-05-20 stsp return err;
1810 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1812 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1813 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1816 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1817 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1821 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1822 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1824 b700b5d6 2018-07-10 stsp if (widthp)
1825 b700b5d6 2018-07-10 stsp *widthp = cols;
1826 f91a2b48 2022-06-23 thomas if (scrollxp)
1827 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1829 963b370f 2018-05-20 stsp free(wline);
1831 963b370f 2018-05-20 stsp *wlinep = wline;
1832 963b370f 2018-05-20 stsp return err;
1835 8b473291 2019-02-21 stsp static const struct got_error*
1836 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1837 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1839 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1840 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1842 8b473291 2019-02-21 stsp const char *name;
1844 8b473291 2019-02-21 stsp *refs_str = NULL;
1846 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1847 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1848 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1851 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1852 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1854 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1855 8b473291 2019-02-21 stsp name += 5;
1856 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1857 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1859 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1860 8b473291 2019-02-21 stsp name += 6;
1861 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1862 8b473291 2019-02-21 stsp name += 8;
1863 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1864 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1867 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1870 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1871 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1872 5d844a1e 2019-08-13 stsp if (err) {
1873 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1874 48cae60d 2020-09-22 stsp free(ref_id);
1877 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1878 5d844a1e 2019-08-13 stsp err = NULL;
1879 5d844a1e 2019-08-13 stsp tag = NULL;
1882 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1883 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1884 48cae60d 2020-09-22 stsp free(ref_id);
1886 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1887 52b5abe1 2019-08-13 stsp if (cmp != 0)
1889 8b473291 2019-02-21 stsp s = *refs_str;
1890 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1891 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1892 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1894 8b473291 2019-02-21 stsp *refs_str = NULL;
1900 8b473291 2019-02-21 stsp return err;
1903 963b370f 2018-05-20 stsp static const struct got_error *
1904 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1905 27a741e5 2019-09-11 stsp int col_tab_align)
1907 e6b8b890 2020-12-29 naddy char *smallerthan;
1909 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1910 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1911 5813d178 2019-03-09 stsp author = smallerthan + 1;
1912 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1913 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1914 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1917 5813d178 2019-03-09 stsp static const struct got_error *
1918 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1919 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1920 8fdc79fe 2020-12-01 naddy int author_display_cols)
1922 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1923 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1924 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1925 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1926 5813d178 2019-03-09 stsp char *author = NULL;
1927 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1928 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1929 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1930 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1931 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1932 ccb26ccd 2018-11-05 stsp struct tm tm;
1933 45d799e2 2018-12-23 stsp time_t committer_time;
1934 11b20872 2019-11-08 stsp struct tog_color *tc;
1936 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1937 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1938 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1939 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1940 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1942 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1943 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1945 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1946 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1948 11b20872 2019-11-08 stsp wattr_on(view->window,
1949 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1950 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1952 11b20872 2019-11-08 stsp wattr_off(view->window,
1953 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1954 27a741e5 2019-09-11 stsp col = limit;
1955 b39d25c7 2018-07-10 stsp if (col > avail)
1956 b39d25c7 2018-07-10 stsp goto done;
1958 6570a66d 2019-11-08 stsp if (avail >= 120) {
1959 6570a66d 2019-11-08 stsp char *id_str;
1960 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1962 6570a66d 2019-11-08 stsp goto done;
1963 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1965 11b20872 2019-11-08 stsp wattr_on(view->window,
1966 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1967 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1969 11b20872 2019-11-08 stsp wattr_off(view->window,
1970 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1971 6570a66d 2019-11-08 stsp free(id_str);
1973 6570a66d 2019-11-08 stsp if (col > avail)
1974 6570a66d 2019-11-08 stsp goto done;
1977 f69c5a46 2022-07-19 thomas if (s->use_committer)
1978 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
1980 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
1981 5813d178 2019-03-09 stsp if (author == NULL) {
1982 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1983 80ddbec8 2018-04-29 stsp goto done;
1985 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1987 bb737323 2018-05-20 stsp goto done;
1988 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1990 11b20872 2019-11-08 stsp wattr_on(view->window,
1991 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1992 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1994 11b20872 2019-11-08 stsp wattr_off(view->window,
1995 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1996 bb737323 2018-05-20 stsp col += author_width;
1997 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1998 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2000 bb737323 2018-05-20 stsp author_width++;
2002 9c2eaf34 2018-05-20 stsp if (col > avail)
2003 9c2eaf34 2018-05-20 stsp goto done;
2005 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2007 6d9fbc00 2018-04-29 stsp goto done;
2008 bb737323 2018-05-20 stsp logmsg = logmsg0;
2009 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2011 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2012 bb737323 2018-05-20 stsp if (newline)
2013 bb737323 2018-05-20 stsp *newline = '\0';
2014 f91a2b48 2022-06-23 thomas limit = avail - col;
2015 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2016 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2017 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2018 f91a2b48 2022-06-23 thomas limit, col, 1);
2019 331b1a16 2022-06-23 thomas if (err)
2020 331b1a16 2022-06-23 thomas goto done;
2021 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2022 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2023 27a741e5 2019-09-11 stsp while (col < avail) {
2024 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2028 80ddbec8 2018-04-29 stsp free(logmsg0);
2029 bb737323 2018-05-20 stsp free(wlogmsg);
2030 5813d178 2019-03-09 stsp free(author);
2031 bb737323 2018-05-20 stsp free(wauthor);
2032 80ddbec8 2018-04-29 stsp free(line);
2033 80ddbec8 2018-04-29 stsp return err;
2036 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2037 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2038 899d86c2 2018-05-10 stsp struct got_object_id *id)
2040 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2042 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2043 80ddbec8 2018-04-29 stsp if (entry == NULL)
2044 899d86c2 2018-05-10 stsp return NULL;
2046 899d86c2 2018-05-10 stsp entry->id = id;
2047 99db9666 2018-05-07 stsp entry->commit = commit;
2048 899d86c2 2018-05-10 stsp return entry;
2051 99db9666 2018-05-07 stsp static void
2052 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2054 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2056 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2057 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2058 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2059 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2060 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2061 99db9666 2018-05-07 stsp free(entry);
2064 99db9666 2018-05-07 stsp static void
2065 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2067 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2068 99db9666 2018-05-07 stsp pop_commit(commits);
2071 c4972b91 2018-05-07 stsp static const struct got_error *
2072 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2073 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2075 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2076 13add988 2019-10-15 stsp regmatch_t regmatch;
2077 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2079 13add988 2019-10-15 stsp *have_match = 0;
2081 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2083 13add988 2019-10-15 stsp return err;
2085 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2087 13add988 2019-10-15 stsp goto done;
2089 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2090 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2091 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2092 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2093 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2094 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2095 13add988 2019-10-15 stsp *have_match = 1;
2097 13add988 2019-10-15 stsp free(id_str);
2098 13add988 2019-10-15 stsp free(logmsg);
2099 13add988 2019-10-15 stsp return err;
2102 13add988 2019-10-15 stsp static const struct got_error *
2103 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2105 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2108 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2109 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2110 1a76625f 2018-10-22 stsp * while updating the display.
2113 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2114 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2115 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2116 1a76625f 2018-10-22 stsp int errcode;
2118 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2119 4e0d2870 2020-12-07 naddy NULL, NULL);
2120 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2123 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2126 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2127 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2128 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2132 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2133 1a76625f 2018-10-22 stsp if (errcode) {
2134 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2135 13add988 2019-10-15 stsp "pthread_mutex_lock");
2139 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2140 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2141 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2143 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2144 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2145 7c1452c1 2020-03-26 stsp int have_match;
2146 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2149 7c1452c1 2020-03-26 stsp if (have_match)
2150 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2153 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2154 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2155 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2156 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2159 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2161 9ba79e04 2018-06-11 stsp return err;
2164 2b779855 2020-12-05 naddy static void
2165 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2167 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2168 2b779855 2020-12-05 naddy int ncommits = 0;
2170 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2171 2b779855 2020-12-05 naddy while (entry) {
2172 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2173 2b779855 2020-12-05 naddy s->selected_entry = entry;
2176 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2177 2b779855 2020-12-05 naddy ncommits++;
2181 0553a4e3 2018-04-30 stsp static const struct got_error *
2182 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2184 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2185 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2186 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2187 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2188 60493ae3 2019-06-20 stsp int width;
2189 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2190 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2191 8b473291 2019-02-21 stsp char *refs_str = NULL;
2192 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2193 11b20872 2019-11-08 stsp struct tog_color *tc;
2194 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2196 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2197 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2198 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2199 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2201 ecb28ae0 2018-07-16 stsp return err;
2202 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2203 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2204 d2075bf3 2020-12-25 stsp if (refs) {
2205 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2206 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2208 d2075bf3 2020-12-25 stsp goto done;
2212 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2213 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2215 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2216 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2217 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2218 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2219 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2220 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2221 8f4ed634 2020-03-26 stsp goto done;
2224 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2226 f9686aa5 2020-03-27 stsp if (view->searching) {
2227 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2228 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2229 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2230 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2231 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2232 f9686aa5 2020-03-27 stsp search_str = "searching...";
2235 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2236 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2237 f9686aa5 2020-03-27 stsp search_str ? search_str :
2238 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2239 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2240 8f4ed634 2020-03-26 stsp goto done;
2244 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2245 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2246 91198554 2022-06-23 thomas "........................................",
2247 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2248 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2249 1a76625f 2018-10-22 stsp header = NULL;
2250 1a76625f 2018-10-22 stsp goto done;
2252 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2253 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2254 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2255 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2256 1a76625f 2018-10-22 stsp header = NULL;
2257 1a76625f 2018-10-22 stsp goto done;
2259 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2261 1a76625f 2018-10-22 stsp goto done;
2263 2814baeb 2018-08-01 stsp werase(view->window);
2265 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2266 a3404814 2018-09-02 stsp wstandout(view->window);
2267 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2269 11b20872 2019-11-08 stsp wattr_on(view->window,
2270 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2271 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2273 11b20872 2019-11-08 stsp wattr_off(view->window,
2274 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2275 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2276 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2279 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2280 a3404814 2018-09-02 stsp wstandend(view->window);
2281 ecb28ae0 2018-07-16 stsp free(wline);
2282 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2283 1a76625f 2018-10-22 stsp goto done;
2285 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2286 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2287 5813d178 2019-03-09 stsp ncommits = 0;
2288 05171be4 2022-06-23 thomas view->maxx = 0;
2289 5813d178 2019-03-09 stsp while (entry) {
2290 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2291 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2292 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2293 5813d178 2019-03-09 stsp int width;
2294 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2296 f69c5a46 2022-07-19 thomas if (s->use_committer)
2297 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2299 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2300 5813d178 2019-03-09 stsp if (author == NULL) {
2301 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2302 5813d178 2019-03-09 stsp goto done;
2304 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2305 27a741e5 2019-09-11 stsp date_display_cols);
2306 5813d178 2019-03-09 stsp if (author_cols < width)
2307 5813d178 2019-03-09 stsp author_cols = width;
2308 5813d178 2019-03-09 stsp free(wauthor);
2309 5813d178 2019-03-09 stsp free(author);
2310 ef944b8b 2022-07-21 thomas if (err)
2311 ef944b8b 2022-07-21 thomas goto done;
2312 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2313 05171be4 2022-06-23 thomas if (err)
2314 05171be4 2022-06-23 thomas goto done;
2315 05171be4 2022-06-23 thomas msg = msg0;
2316 05171be4 2022-06-23 thomas while (*msg == '\n')
2318 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2319 331b1a16 2022-06-23 thomas *eol = '\0';
2320 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2321 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2322 331b1a16 2022-06-23 thomas if (err)
2323 331b1a16 2022-06-23 thomas goto done;
2324 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2325 05171be4 2022-06-23 thomas free(msg0);
2326 331b1a16 2022-06-23 thomas free(wmsg);
2327 7ca04879 2019-10-19 stsp ncommits++;
2328 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2331 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2332 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2333 867c6645 2018-07-10 stsp ncommits = 0;
2334 899d86c2 2018-05-10 stsp while (entry) {
2335 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2337 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2338 2814baeb 2018-08-01 stsp wstandout(view->window);
2339 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2340 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2341 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2342 2814baeb 2018-08-01 stsp wstandend(view->window);
2344 60493ae3 2019-06-20 stsp goto done;
2345 0553a4e3 2018-04-30 stsp ncommits++;
2346 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2347 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2350 a5d43cac 2022-07-01 thomas view_border(view);
2352 1a76625f 2018-10-22 stsp free(id_str);
2353 8b473291 2019-02-21 stsp free(refs_str);
2354 1a76625f 2018-10-22 stsp free(ncommits_str);
2355 1a76625f 2018-10-22 stsp free(header);
2356 80ddbec8 2018-04-29 stsp return err;
2359 07b55e75 2018-05-10 stsp static void
2360 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2362 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2363 07b55e75 2018-05-10 stsp int nscrolled = 0;
2365 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2366 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2369 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2370 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2371 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2372 07b55e75 2018-05-10 stsp if (entry) {
2373 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2374 07b55e75 2018-05-10 stsp nscrolled++;
2379 aa075928 2018-05-10 stsp static const struct got_error *
2380 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2382 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2383 5e224a3e 2019-02-22 stsp int errcode;
2385 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2387 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2388 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2389 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2390 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2391 7aafa0d1 2019-02-22 stsp if (errcode)
2392 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2393 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2396 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2397 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2399 7c1452c1 2020-03-26 stsp if (!wait)
2402 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2403 ffe38506 2020-12-01 naddy show_log_view(view);
2404 7c1452c1 2020-03-26 stsp update_panels();
2405 7c1452c1 2020-03-26 stsp doupdate();
2407 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2408 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2409 82954512 2020-02-03 stsp if (errcode)
2410 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2411 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2413 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2414 ffe38506 2020-12-01 naddy show_log_view(view);
2415 7c1452c1 2020-03-26 stsp update_panels();
2416 7c1452c1 2020-03-26 stsp doupdate();
2419 5e224a3e 2019-02-22 stsp return NULL;
2422 a5d43cac 2022-07-01 thomas static const struct got_error *
2423 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2425 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2426 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2428 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2429 fe731b51 2022-07-14 thomas return NULL;
2431 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2432 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2433 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2435 a5d43cac 2022-07-01 thomas return err;
2438 5e224a3e 2019-02-22 stsp static const struct got_error *
2439 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2441 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2442 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2443 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2444 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2446 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2447 5e224a3e 2019-02-22 stsp return NULL;
2449 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2450 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2451 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2453 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2455 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2456 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2458 5e224a3e 2019-02-22 stsp return err;
2462 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2463 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2466 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2467 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2469 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2470 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2472 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2473 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2475 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2476 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2478 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2480 dd0a52c1 2018-05-20 stsp return err;
2483 cd0acaa7 2018-05-20 stsp static const struct got_error *
2484 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2485 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2486 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2488 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2489 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2490 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2492 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2493 15a94983 2018-12-23 stsp if (diff_view == NULL)
2494 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2496 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2497 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2498 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2499 e5a0f69f 2018-08-18 stsp if (err == NULL)
2500 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2501 cd0acaa7 2018-05-20 stsp return err;
2504 80ddbec8 2018-04-29 stsp static const struct got_error *
2505 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2506 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2508 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2510 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2511 941e9f74 2019-05-21 stsp if (parent == NULL)
2512 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2514 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2515 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2516 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2517 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2518 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2519 941e9f74 2019-05-21 stsp s->tree = subtree;
2520 941e9f74 2019-05-21 stsp s->selected = 0;
2521 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2522 941e9f74 2019-05-21 stsp return NULL;
2525 941e9f74 2019-05-21 stsp static const struct got_error *
2526 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2527 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2529 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2530 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2531 941e9f74 2019-05-21 stsp const char *p;
2532 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2534 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2536 941e9f74 2019-05-21 stsp while (*p) {
2537 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2538 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2539 56e0773d 2019-11-28 stsp char *te_name;
2541 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2544 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2545 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2546 941e9f74 2019-05-21 stsp if (slash == NULL)
2547 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2549 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2550 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2551 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2554 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2555 56e0773d 2019-11-28 stsp if (te == NULL) {
2556 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2557 56e0773d 2019-11-28 stsp free(te_name);
2560 56e0773d 2019-11-28 stsp free(te_name);
2561 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2563 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2564 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2566 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2567 941e9f74 2019-05-21 stsp if (slash)
2568 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2570 941e9f74 2019-05-21 stsp subpath = strdup(path);
2571 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2572 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2576 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2581 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2582 941e9f74 2019-05-21 stsp free(tree_id);
2586 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2587 941e9f74 2019-05-21 stsp if (err) {
2588 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2591 941e9f74 2019-05-21 stsp if (slash == NULL)
2593 941e9f74 2019-05-21 stsp free(subpath);
2594 941e9f74 2019-05-21 stsp subpath = NULL;
2595 941e9f74 2019-05-21 stsp p = slash;
2598 941e9f74 2019-05-21 stsp free(subpath);
2599 1a76625f 2018-10-22 stsp return err;
2602 61266923 2020-01-14 stsp static const struct got_error *
2603 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2604 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2605 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2607 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2608 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2609 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2611 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2612 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2613 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2615 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2617 55cccc34 2020-02-20 stsp return err;
2618 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2620 55cccc34 2020-02-20 stsp *new_view = tree_view;
2622 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2623 55cccc34 2020-02-20 stsp return NULL;
2625 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2628 55cccc34 2020-02-20 stsp static const struct got_error *
2629 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2631 61266923 2020-01-14 stsp sigset_t sigset;
2632 61266923 2020-01-14 stsp int errcode;
2634 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2635 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2637 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2638 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2639 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2640 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2641 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2642 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2643 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2644 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2645 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2647 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2648 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2649 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2651 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2652 61266923 2020-01-14 stsp if (errcode)
2653 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2655 61266923 2020-01-14 stsp return NULL;
2658 1a76625f 2018-10-22 stsp static void *
2659 1a76625f 2018-10-22 stsp log_thread(void *arg)
2661 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2662 1a76625f 2018-10-22 stsp int errcode = 0;
2663 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2664 1a76625f 2018-10-22 stsp int done = 0;
2667 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2668 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2670 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2671 f2d749db 2022-07-12 thomas if (errcode) {
2672 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2673 61266923 2020-01-14 stsp return (void *)err;
2676 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2677 f2d749db 2022-07-12 thomas if (err) {
2678 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2679 f2d749db 2022-07-12 thomas goto done;
2682 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2683 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2684 f2d749db 2022-07-12 thomas if (errcode) {
2685 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2686 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2687 f2d749db 2022-07-12 thomas goto done;
2689 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2690 1a76625f 2018-10-22 stsp if (err) {
2691 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2692 f2d749db 2022-07-12 thomas goto done;
2693 1a76625f 2018-10-22 stsp err = NULL;
2695 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2696 1a76625f 2018-10-22 stsp a->commits_needed--;
2698 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2699 3abe8080 2019-04-10 stsp if (errcode) {
2700 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2701 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2702 f2d749db 2022-07-12 thomas goto done;
2703 3abe8080 2019-04-10 stsp } else if (*a->quit)
2705 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2706 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2707 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2708 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2711 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2712 7c1452c1 2020-03-26 stsp if (errcode) {
2713 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2714 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2715 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2716 f2d749db 2022-07-12 thomas goto done;
2720 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2722 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2723 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2724 7c1452c1 2020-03-26 stsp &tog_mutex);
2725 f2d749db 2022-07-12 thomas if (errcode) {
2726 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2727 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2728 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2729 f2d749db 2022-07-12 thomas goto done;
2731 21355643 2020-12-06 stsp if (*a->quit)
2736 3abe8080 2019-04-10 stsp a->log_complete = 1;
2737 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2738 f2d749db 2022-07-12 thomas if (errcode)
2739 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2741 f2d749db 2022-07-12 thomas if (err) {
2742 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2743 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2745 1a76625f 2018-10-22 stsp return (void *)err;
2748 1a76625f 2018-10-22 stsp static const struct got_error *
2749 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2751 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2752 1a76625f 2018-10-22 stsp int errcode;
2754 1a76625f 2018-10-22 stsp if (s->thread) {
2755 1a76625f 2018-10-22 stsp s->quit = 1;
2756 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2757 1a76625f 2018-10-22 stsp if (errcode)
2758 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2759 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2760 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2761 1a76625f 2018-10-22 stsp if (errcode)
2762 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2763 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2764 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2765 1a76625f 2018-10-22 stsp if (errcode)
2766 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2767 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2768 1a76625f 2018-10-22 stsp if (errcode)
2769 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2770 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2771 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2774 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2775 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2776 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2779 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2780 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2781 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2782 1af5eddf 2022-06-23 thomas if (err == NULL)
2783 1af5eddf 2022-06-23 thomas err = pack_err;
2784 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2787 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2788 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2789 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2792 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2795 9343a5fb 2018-06-23 stsp static const struct got_error *
2796 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2798 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2799 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2800 276b94a1 2020-11-13 naddy int errcode;
2802 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2804 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2805 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2806 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2808 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2809 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2810 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2812 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2813 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2814 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2815 1a76625f 2018-10-22 stsp free(s->start_id);
2816 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2817 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2818 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2819 1a76625f 2018-10-22 stsp return err;
2822 1a76625f 2018-10-22 stsp static const struct got_error *
2823 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2825 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2827 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2828 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2829 60493ae3 2019-06-20 stsp return NULL;
2832 60493ae3 2019-06-20 stsp static const struct got_error *
2833 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2835 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2836 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2837 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2839 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2840 f9686aa5 2020-03-27 stsp show_log_view(view);
2841 f9686aa5 2020-03-27 stsp update_panels();
2842 f9686aa5 2020-03-27 stsp doupdate();
2844 96e2b566 2019-07-08 stsp if (s->search_entry) {
2845 13add988 2019-10-15 stsp int errcode, ch;
2846 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2847 13add988 2019-10-15 stsp if (errcode)
2848 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2849 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2850 13add988 2019-10-15 stsp ch = wgetch(view->window);
2851 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2852 13add988 2019-10-15 stsp if (errcode)
2853 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2854 13add988 2019-10-15 stsp "pthread_mutex_lock");
2855 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2856 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2857 678cbce5 2019-07-28 stsp return NULL;
2859 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2860 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2862 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2863 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2864 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2865 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2866 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2869 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2870 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2871 1f4d5162 2022-06-23 thomas * might have changed.
2873 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2874 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2875 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2877 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2878 b74feda6 2022-06-23 thomas } else {
2879 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2880 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2881 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2883 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2884 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2887 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2890 60493ae3 2019-06-20 stsp while (1) {
2891 13add988 2019-10-15 stsp int have_match = 0;
2893 60493ae3 2019-06-20 stsp if (entry == NULL) {
2894 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2895 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2896 f9967bca 2020-03-27 stsp view->search_next_done =
2897 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2898 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2899 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2900 f801134a 2019-06-25 stsp return NULL;
2903 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2904 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2905 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2907 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2908 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2911 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2912 13add988 2019-10-15 stsp &view->regex);
2915 13add988 2019-10-15 stsp if (have_match) {
2916 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2917 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2921 96e2b566 2019-07-08 stsp s->search_entry = entry;
2922 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2923 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2925 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2928 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2929 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2930 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2931 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2933 60493ae3 2019-06-20 stsp return err;
2936 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2937 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2939 60493ae3 2019-06-20 stsp return err;
2944 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2946 60493ae3 2019-06-20 stsp return NULL;
2949 60493ae3 2019-06-20 stsp static const struct got_error *
2950 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2951 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2952 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2954 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2955 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2956 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2957 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2958 1a76625f 2018-10-22 stsp int errcode;
2960 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2961 f135c941 2020-02-20 stsp free(s->in_repo_path);
2962 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2963 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2964 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2967 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2968 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2969 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2971 fb2756b9 2018-08-04 stsp s->repo = repo;
2972 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2973 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2974 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2975 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2976 9cd7cbd1 2020-12-07 stsp goto done;
2979 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2980 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2981 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2982 5036bf37 2018-09-24 stsp goto done;
2984 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2986 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2987 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2988 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2989 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2991 11b20872 2019-11-08 stsp goto done;
2992 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2993 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2994 11b20872 2019-11-08 stsp if (err) {
2995 11b20872 2019-11-08 stsp free_colors(&s->colors);
2996 11b20872 2019-11-08 stsp goto done;
2998 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2999 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3000 11b20872 2019-11-08 stsp if (err) {
3001 11b20872 2019-11-08 stsp free_colors(&s->colors);
3002 11b20872 2019-11-08 stsp goto done;
3006 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3007 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3008 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3009 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3010 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3011 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3013 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3014 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3015 1af5eddf 2022-06-23 thomas if (err)
3016 1af5eddf 2022-06-23 thomas goto done;
3018 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3019 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3020 7cd52833 2022-06-23 thomas if (err)
3021 7cd52833 2022-06-23 thomas goto done;
3022 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3023 b672a97a 2020-01-27 stsp !s->log_branches);
3025 1a76625f 2018-10-22 stsp goto done;
3026 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3027 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3029 c5b78334 2020-01-12 stsp goto done;
3031 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3032 1a76625f 2018-10-22 stsp if (errcode) {
3033 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3034 1a76625f 2018-10-22 stsp goto done;
3036 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3037 7c1452c1 2020-03-26 stsp if (errcode) {
3038 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3039 7c1452c1 2020-03-26 stsp goto done;
3042 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3043 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3044 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3045 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3046 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3047 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3048 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3049 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3050 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3051 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3052 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3053 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3054 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3057 1a76625f 2018-10-22 stsp close_log_view(view);
3058 ba4f502b 2018-08-04 stsp return err;
3061 e5a0f69f 2018-08-18 stsp static const struct got_error *
3062 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3064 f2f6d207 2020-11-24 stsp const struct got_error *err;
3065 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3067 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3068 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3069 2b380cc8 2018-10-24 stsp &s->thread_args);
3070 2b380cc8 2018-10-24 stsp if (errcode)
3071 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3072 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3073 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3075 f2f6d207 2020-11-24 stsp return err;
3079 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3082 b31f89ff 2022-07-01 thomas static void
3083 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3085 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3087 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3088 b31f89ff 2022-07-01 thomas view->count = 0;
3089 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3092 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3093 b31f89ff 2022-07-01 thomas || home)
3094 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3096 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3097 b31f89ff 2022-07-01 thomas --s->selected;
3099 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3101 b31f89ff 2022-07-01 thomas select_commit(s);
3105 b31f89ff 2022-07-01 thomas static const struct got_error *
3106 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3108 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3109 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3110 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3112 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3113 b31f89ff 2022-07-01 thomas if (first == NULL) {
3114 b31f89ff 2022-07-01 thomas view->count = 0;
3115 b31f89ff 2022-07-01 thomas return NULL;
3118 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3119 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3120 b31f89ff 2022-07-01 thomas return NULL;
3122 b31f89ff 2022-07-01 thomas if (!page) {
3123 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3125 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3126 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3127 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3128 b31f89ff 2022-07-01 thomas ++s->selected;
3130 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3131 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3132 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3133 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3134 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3136 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3137 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3138 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3139 b31f89ff 2022-07-01 thomas } else {
3140 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3141 b31f89ff 2022-07-01 thomas if (err)
3142 b31f89ff 2022-07-01 thomas return err;
3143 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3144 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3145 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3148 b31f89ff 2022-07-01 thomas if (err)
3149 b31f89ff 2022-07-01 thomas return err;
3152 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3153 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3155 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3156 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3158 b31f89ff 2022-07-01 thomas select_commit(s);
3160 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3161 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3162 b31f89ff 2022-07-01 thomas view->count = 0;
3164 b31f89ff 2022-07-01 thomas return NULL;
3167 a5d43cac 2022-07-01 thomas static void
3168 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3173 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3174 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3175 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3176 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3177 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3179 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3180 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3181 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3182 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3183 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3184 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3186 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3190 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3191 e5a0f69f 2018-08-18 stsp static const struct got_error *
3192 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3194 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3196 a5d43cac 2022-07-01 thomas view->nlines = y;
3197 64486692 2022-07-07 thomas view->ncols = COLS;
3198 a5d43cac 2022-07-01 thomas err = view_resize(view);
3199 a5d43cac 2022-07-01 thomas if (err)
3200 a5d43cac 2022-07-01 thomas return err;
3202 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3204 a5d43cac 2022-07-01 thomas return err;
3207 a5d43cac 2022-07-01 thomas static const struct got_error *
3208 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3210 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3211 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3212 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3213 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3214 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3215 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3217 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3218 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3219 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3220 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3221 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3222 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3224 b31f89ff 2022-07-01 thomas return err;
3227 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3228 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3229 068ab281 2022-07-01 thomas --eos; /* border */
3231 528dedf3 2021-08-30 stsp switch (ch) {
3233 1e37a5c2 2019-05-12 jcs s->quit = 1;
3235 05171be4 2022-06-23 thomas case '0':
3236 05171be4 2022-06-23 thomas view->x = 0;
3238 05171be4 2022-06-23 thomas case '$':
3239 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3240 07b0611c 2022-06-23 thomas view->count = 0;
3242 05171be4 2022-06-23 thomas case KEY_RIGHT:
3243 05171be4 2022-06-23 thomas case 'l':
3244 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3245 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3247 07b0611c 2022-06-23 thomas view->count = 0;
3249 05171be4 2022-06-23 thomas case KEY_LEFT:
3250 05171be4 2022-06-23 thomas case 'h':
3251 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3252 07b0611c 2022-06-23 thomas if (view->x <= 0)
3253 07b0611c 2022-06-23 thomas view->count = 0;
3256 1e37a5c2 2019-05-12 jcs case KEY_UP:
3259 f7140bf5 2021-10-17 thomas case CTRL('p'):
3260 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3263 912a3f79 2021-08-30 j case KEY_HOME:
3264 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3265 07b0611c 2022-06-23 thomas view->count = 0;
3267 70f17a53 2022-06-13 thomas case CTRL('u'):
3268 23427b14 2022-06-23 thomas case 'u':
3269 70f17a53 2022-06-13 thomas nscroll /= 2;
3270 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3271 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3272 a4292ac5 2019-05-12 jcs case CTRL('b'):
3273 1c5e5faa 2022-06-23 thomas case 'b':
3274 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3277 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3280 f7140bf5 2021-10-17 thomas case CTRL('n'):
3281 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3283 f69c5a46 2022-07-19 thomas case '@':
3284 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3287 912a3f79 2021-08-30 j case KEY_END: {
3288 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3289 912a3f79 2021-08-30 j * traverse them all. */
3290 07b0611c 2022-06-23 thomas view->count = 0;
3291 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3292 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3293 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3296 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3297 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3298 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3299 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3301 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3302 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3304 f3bc9f1d 2021-09-05 naddy if (n > 0)
3305 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3306 2b779855 2020-12-05 naddy select_commit(s);
3309 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3310 23427b14 2022-06-23 thomas case 'd':
3311 70f17a53 2022-06-13 thomas nscroll /= 2;
3312 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3313 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3314 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3315 4c2d69cb 2022-06-23 thomas case 'f':
3316 b31f89ff 2022-07-01 thomas case ' ':
3317 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3319 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3320 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3321 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3322 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3323 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3324 2b779855 2020-12-05 naddy select_commit(s);
3325 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3326 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3327 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3328 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3329 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3332 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3333 444d5325 2022-07-03 thomas case '\r':
3334 07b0611c 2022-06-23 thomas view->count = 0;
3335 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3338 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3339 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3340 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3342 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3343 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3344 78756c87 2020-11-24 stsp view, s->repo);
3348 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3349 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3350 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3351 a5d43cac 2022-07-01 thomas if (err)
3355 e78dc838 2020-12-04 stsp view->focussed = 0;
3356 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3357 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3358 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3360 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3361 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3362 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3364 1e37a5c2 2019-05-12 jcs return err;
3365 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3366 40236d76 2022-06-23 thomas if (err)
3367 40236d76 2022-06-23 thomas return err;
3368 e78dc838 2020-12-04 stsp view->focus_child = 1;
3370 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3373 07b0611c 2022-06-23 thomas view->count = 0;
3374 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3376 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3377 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3378 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3379 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3383 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3384 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3385 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3386 444d5325 2022-07-03 thomas if (err)
3389 e78dc838 2020-12-04 stsp view->focussed = 0;
3390 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3391 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3392 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3393 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3394 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3395 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3397 1e37a5c2 2019-05-12 jcs return err;
3398 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3399 40236d76 2022-06-23 thomas if (err)
3400 40236d76 2022-06-23 thomas return err;
3401 e78dc838 2020-12-04 stsp view->focus_child = 1;
3403 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3405 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3406 21355643 2020-12-06 stsp case CTRL('l'):
3408 07b0611c 2022-06-23 thomas view->count = 0;
3409 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3410 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3412 21355643 2020-12-06 stsp err = stop_log_thread(s);
3414 74cfe85e 2020-10-20 stsp return err;
3415 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3416 21355643 2020-12-06 stsp char *parent_path;
3417 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3419 21355643 2020-12-06 stsp return err;
3420 21355643 2020-12-06 stsp free(s->in_repo_path);
3421 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3422 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3423 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3424 21355643 2020-12-06 stsp struct got_object_id *start_id;
3425 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3426 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3427 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3429 21355643 2020-12-06 stsp return err;
3430 21355643 2020-12-06 stsp free(s->start_id);
3431 21355643 2020-12-06 stsp s->start_id = start_id;
3432 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3433 21355643 2020-12-06 stsp } else /* 'B' */
3434 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3436 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3437 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3438 bf7e79b3 2022-06-23 thomas if (err)
3439 bf7e79b3 2022-06-23 thomas return err;
3441 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3442 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3443 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3445 21355643 2020-12-06 stsp return err;
3446 51a10b52 2020-12-26 stsp tog_free_refs();
3447 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3449 ca51c541 2020-12-07 stsp return err;
3450 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3451 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3453 d01904d4 2019-06-25 stsp return err;
3454 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3455 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3457 b672a97a 2020-01-27 stsp return err;
3458 21355643 2020-12-06 stsp free_commits(&s->commits);
3459 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3460 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3461 21355643 2020-12-06 stsp s->selected_entry = NULL;
3462 21355643 2020-12-06 stsp s->selected = 0;
3463 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3464 21355643 2020-12-06 stsp s->quit = 0;
3465 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3466 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3467 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3470 07b0611c 2022-06-23 thomas view->count = 0;
3471 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3472 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3473 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3474 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3475 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3476 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3477 6458efa5 2020-11-24 stsp if (err) {
3478 6458efa5 2020-11-24 stsp view_close(ref_view);
3479 6458efa5 2020-11-24 stsp return err;
3481 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3482 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3483 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3484 444d5325 2022-07-03 thomas if (err)
3487 e78dc838 2020-12-04 stsp view->focussed = 0;
3488 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3489 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3490 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3491 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3492 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3493 6458efa5 2020-11-24 stsp err = view_close_child(view);
3495 6458efa5 2020-11-24 stsp return err;
3496 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3497 40236d76 2022-06-23 thomas if (err)
3498 40236d76 2022-06-23 thomas return err;
3499 e78dc838 2020-12-04 stsp view->focus_child = 1;
3501 6458efa5 2020-11-24 stsp *new_view = ref_view;
3504 07b0611c 2022-06-23 thomas view->count = 0;
3508 80ddbec8 2018-04-29 stsp return err;
3511 4ed7e80c 2018-05-20 stsp static const struct got_error *
3512 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3514 c2db6724 2019-01-04 stsp const struct got_error *error;
3516 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3517 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3518 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3520 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3521 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3523 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3524 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3526 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3527 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3529 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3530 c2db6724 2019-01-04 stsp if (error != NULL)
3531 c2db6724 2019-01-04 stsp return error;
3533 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3534 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3536 c2db6724 2019-01-04 stsp return NULL;
3539 a915003a 2019-02-05 stsp static void
3540 a915003a 2019-02-05 stsp init_curses(void)
3543 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3544 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3545 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3547 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3548 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3549 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3550 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3551 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3553 a915003a 2019-02-05 stsp initscr();
3555 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3558 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3559 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3560 a915003a 2019-02-05 stsp curs_set(0);
3561 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3562 6d17833f 2019-11-08 stsp start_color();
3563 6d17833f 2019-11-08 stsp use_default_colors();
3567 c2db6724 2019-01-04 stsp static const struct got_error *
3568 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3569 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3571 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3573 f135c941 2020-02-20 stsp if (argc == 0) {
3574 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3575 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3576 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3577 f135c941 2020-02-20 stsp return NULL;
3580 f135c941 2020-02-20 stsp if (worktree) {
3581 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3584 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3586 f135c941 2020-02-20 stsp return err;
3587 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3588 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3589 bfd61697 2020-11-14 stsp p) == -1) {
3590 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3591 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3595 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3597 f135c941 2020-02-20 stsp return err;
3600 f135c941 2020-02-20 stsp static const struct got_error *
3601 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3603 80ddbec8 2018-04-29 stsp const struct got_error *error;
3604 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3605 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3606 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3607 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3608 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3609 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3610 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3611 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3612 04cc582a 2018-08-01 stsp struct tog_view *view;
3613 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3615 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3616 80ddbec8 2018-04-29 stsp switch (ch) {
3618 b672a97a 2020-01-27 stsp log_branches = 1;
3621 80ddbec8 2018-04-29 stsp start_commit = optarg;
3624 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3625 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3626 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3630 17020d27 2019-03-07 stsp usage_log();
3631 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3635 80ddbec8 2018-04-29 stsp argc -= optind;
3636 80ddbec8 2018-04-29 stsp argv += optind;
3638 f135c941 2020-02-20 stsp if (argc > 1)
3639 f135c941 2020-02-20 stsp usage_log();
3641 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3642 7cd52833 2022-06-23 thomas if (error != NULL)
3643 7cd52833 2022-06-23 thomas goto done;
3645 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3646 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3647 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3648 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3649 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3650 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3651 c156c7a4 2020-12-18 stsp goto done;
3652 a1fbf39a 2019-08-11 stsp if (worktree)
3653 6962eb72 2020-02-20 stsp repo_path =
3654 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3656 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3657 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3658 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3659 c156c7a4 2020-12-18 stsp goto done;
3663 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3664 80ddbec8 2018-04-29 stsp if (error != NULL)
3665 ecb28ae0 2018-07-16 stsp goto done;
3667 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3668 f135c941 2020-02-20 stsp repo, worktree);
3669 f135c941 2020-02-20 stsp if (error)
3670 f135c941 2020-02-20 stsp goto done;
3672 f135c941 2020-02-20 stsp init_curses();
3674 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3675 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3676 c02c541e 2019-03-29 stsp if (error)
3677 c02c541e 2019-03-29 stsp goto done;
3679 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3680 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3681 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3682 87670572 2020-12-26 naddy if (error)
3683 87670572 2020-12-26 naddy goto done;
3686 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3687 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3688 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3689 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3690 d8f38dc4 2020-12-05 stsp if (error)
3691 d8f38dc4 2020-12-05 stsp goto done;
3692 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3694 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3695 d8f38dc4 2020-12-05 stsp if (error == NULL)
3696 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3697 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3698 d8f38dc4 2020-12-05 stsp goto done;
3699 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3700 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3701 d8f38dc4 2020-12-05 stsp if (error)
3702 d8f38dc4 2020-12-05 stsp goto done;
3705 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3706 04cc582a 2018-08-01 stsp if (view == NULL) {
3707 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3708 04cc582a 2018-08-01 stsp goto done;
3710 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3711 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3712 ba4f502b 2018-08-04 stsp if (error)
3713 ba4f502b 2018-08-04 stsp goto done;
3714 2fc00ff4 2019-08-31 stsp if (worktree) {
3715 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3716 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3717 2fc00ff4 2019-08-31 stsp worktree = NULL;
3719 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3721 f135c941 2020-02-20 stsp free(in_repo_path);
3722 ecb28ae0 2018-07-16 stsp free(repo_path);
3723 ecb28ae0 2018-07-16 stsp free(cwd);
3724 899d86c2 2018-05-10 stsp free(start_id);
3725 d8f38dc4 2020-12-05 stsp free(label);
3727 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3728 1d0f4054 2021-06-17 stsp if (repo) {
3729 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3730 1d0f4054 2021-06-17 stsp if (error == NULL)
3731 1d0f4054 2021-06-17 stsp error = close_err;
3733 ec142235 2019-03-07 stsp if (worktree)
3734 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3735 7cd52833 2022-06-23 thomas if (pack_fds) {
3736 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3737 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3738 7cd52833 2022-06-23 thomas if (error == NULL)
3739 7cd52833 2022-06-23 thomas error = pack_err;
3741 51a10b52 2020-12-26 stsp tog_free_refs();
3742 80ddbec8 2018-04-29 stsp return error;
3745 4ed7e80c 2018-05-20 stsp __dead static void
3746 9f7d7167 2018-04-29 stsp usage_diff(void)
3749 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3750 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3754 6d17833f 2019-11-08 stsp static int
3755 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3756 41605754 2020-11-12 stsp regmatch_t *regmatch)
3758 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3761 ef20f542 2022-06-26 thomas static struct tog_color *
3762 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3764 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3766 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3767 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3768 6d17833f 2019-11-08 stsp return tc;
3771 6d17833f 2019-11-08 stsp return NULL;
3774 4ed7e80c 2018-05-20 stsp static const struct got_error *
3775 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3776 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3778 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3779 666f7b10 2022-06-23 thomas char *exstr = NULL;
3780 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3781 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3782 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3783 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3785 41605754 2020-11-12 stsp *wtotal = 0;
3787 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3788 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3790 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3791 666f7b10 2022-06-23 thomas if (err)
3792 666f7b10 2022-06-23 thomas return err;
3794 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3795 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3796 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3797 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3798 666f7b10 2022-06-23 thomas goto done;
3800 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3801 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3802 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3803 cbea3800 2022-06-23 thomas goto done;
3805 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3806 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3807 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3808 cbea3800 2022-06-23 thomas goto done;
3811 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3812 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3813 cbea3800 2022-06-23 thomas col_tab_align, 1);
3814 cbea3800 2022-06-23 thomas if (err)
3815 cbea3800 2022-06-23 thomas goto done;
3816 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3817 05171be4 2022-06-23 thomas if (n) {
3818 cbea3800 2022-06-23 thomas free(wline);
3819 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3820 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3821 cbea3800 2022-06-23 thomas if (err)
3822 cbea3800 2022-06-23 thomas goto done;
3823 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3824 cbea3800 2022-06-23 thomas wlimit -= width;
3825 cbea3800 2022-06-23 thomas *wtotal += width;
3828 41605754 2020-11-12 stsp if (wlimit > 0) {
3829 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3830 cbea3800 2022-06-23 thomas size_t wlen;
3832 cbea3800 2022-06-23 thomas free(wline);
3833 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3834 cbea3800 2022-06-23 thomas col_tab_align, 1);
3835 cbea3800 2022-06-23 thomas if (err)
3836 cbea3800 2022-06-23 thomas goto done;
3837 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3838 cbea3800 2022-06-23 thomas while (i < wlen) {
3839 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3840 cbea3800 2022-06-23 thomas if (width == -1) {
3841 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3842 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3843 cbea3800 2022-06-23 thomas goto done;
3845 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3847 1c5e5faa 2022-06-23 thomas w += width;
3850 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3851 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3852 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3853 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3854 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3855 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3856 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3860 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3861 cbea3800 2022-06-23 thomas free(wline);
3862 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3863 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3864 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3865 cbea3800 2022-06-23 thomas col_tab_align, 1);
3866 cbea3800 2022-06-23 thomas if (err)
3867 cbea3800 2022-06-23 thomas goto done;
3868 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3869 cbea3800 2022-06-23 thomas } else {
3870 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3871 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3872 cbea3800 2022-06-23 thomas if (err)
3873 cbea3800 2022-06-23 thomas goto done;
3874 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3876 cbea3800 2022-06-23 thomas *wtotal += width2;
3879 05171be4 2022-06-23 thomas free(wline);
3880 666f7b10 2022-06-23 thomas free(exstr);
3881 cbea3800 2022-06-23 thomas free(seg0);
3882 cbea3800 2022-06-23 thomas free(seg1);
3883 cbea3800 2022-06-23 thomas free(seg2);
3884 cbea3800 2022-06-23 thomas return err;
3887 41605754 2020-11-12 stsp static const struct got_error *
3888 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3890 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3891 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3892 61e69b96 2018-05-20 stsp const struct got_error *err;
3893 fe621944 2020-11-10 stsp int nprinted = 0;
3894 b304db33 2018-05-20 stsp char *line;
3895 826082fe 2020-12-10 stsp size_t linesize = 0;
3896 826082fe 2020-12-10 stsp ssize_t linelen;
3897 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3898 61e69b96 2018-05-20 stsp wchar_t *wline;
3899 e0b650dd 2018-05-20 stsp int width;
3900 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3901 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3902 fe621944 2020-11-10 stsp off_t line_offset;
3904 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3905 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3906 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3908 f7d12f7e 2018-08-01 stsp werase(view->window);
3910 a3404814 2018-09-02 stsp if (header) {
3911 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3912 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3913 135a2da0 2020-11-11 stsp header) == -1)
3914 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3915 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3917 135a2da0 2020-11-11 stsp free(line);
3919 a3404814 2018-09-02 stsp return err;
3921 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3922 a3404814 2018-09-02 stsp wstandout(view->window);
3923 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3924 e54cc94a 2020-11-11 stsp free(wline);
3925 e54cc94a 2020-11-11 stsp wline = NULL;
3926 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3927 a3404814 2018-09-02 stsp wstandend(view->window);
3928 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3929 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3931 a3404814 2018-09-02 stsp if (max_lines <= 1)
3932 a3404814 2018-09-02 stsp return NULL;
3933 a3404814 2018-09-02 stsp max_lines--;
3936 89f1a395 2020-12-01 naddy s->eof = 0;
3937 05171be4 2022-06-23 thomas view->maxx = 0;
3938 826082fe 2020-12-10 stsp line = NULL;
3939 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3940 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3941 826082fe 2020-12-10 stsp if (linelen == -1) {
3942 826082fe 2020-12-10 stsp if (feof(s->f)) {
3943 826082fe 2020-12-10 stsp s->eof = 1;
3946 826082fe 2020-12-10 stsp free(line);
3947 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3950 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3951 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3952 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3953 cbea3800 2022-06-23 thomas if (err) {
3954 cbea3800 2022-06-23 thomas free(line);
3955 cbea3800 2022-06-23 thomas return err;
3957 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3958 cbea3800 2022-06-23 thomas free(wline);
3959 cbea3800 2022-06-23 thomas wline = NULL;
3961 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3963 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3964 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3965 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3966 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3967 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3968 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3969 41605754 2020-11-12 stsp if (err) {
3970 41605754 2020-11-12 stsp free(line);
3971 41605754 2020-11-12 stsp return err;
3974 f1c0ec19 2022-06-23 thomas int skip;
3975 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3976 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3977 f1c0ec19 2022-06-23 thomas if (err) {
3978 f1c0ec19 2022-06-23 thomas free(line);
3979 f1c0ec19 2022-06-23 thomas return err;
3981 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3982 f1c0ec19 2022-06-23 thomas free(wline);
3983 41605754 2020-11-12 stsp wline = NULL;
3986 6d17833f 2019-11-08 stsp wattr_off(view->window,
3987 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3988 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3989 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3990 fe621944 2020-11-10 stsp nprinted++;
3992 826082fe 2020-12-10 stsp free(line);
3993 fe621944 2020-11-10 stsp if (nprinted >= 1)
3994 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3995 89f1a395 2020-12-01 naddy (nprinted - 1);
3997 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3999 a5d43cac 2022-07-01 thomas view_border(view);
4001 89f1a395 2020-12-01 naddy if (s->eof) {
4002 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4003 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4004 c3e9aa98 2019-05-13 jcs nprinted++;
4007 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4008 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4010 c3e9aa98 2019-05-13 jcs return err;
4013 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4014 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4015 e54cc94a 2020-11-11 stsp free(wline);
4016 e54cc94a 2020-11-11 stsp wline = NULL;
4017 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4020 26ed57b2 2018-05-19 stsp return NULL;
4023 abd2672a 2018-12-23 stsp static char *
4024 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4026 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4027 09867e48 2019-08-13 stsp char *p, *s;
4029 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4030 09867e48 2019-08-13 stsp if (tm == NULL)
4031 09867e48 2019-08-13 stsp return NULL;
4032 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4033 09867e48 2019-08-13 stsp if (s == NULL)
4034 09867e48 2019-08-13 stsp return NULL;
4035 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4037 abd2672a 2018-12-23 stsp *p = '\0';
4041 4ed7e80c 2018-05-20 stsp static const struct got_error *
4042 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4043 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4045 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4046 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4047 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4048 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4050 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4051 0208f208 2020-05-05 stsp if (qid != NULL) {
4052 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4053 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4054 ec242592 2022-04-22 thomas &qid->id);
4056 0208f208 2020-05-05 stsp return err;
4058 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4059 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4060 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4061 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4062 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4064 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4068 0208f208 2020-05-05 stsp if (tree_id1) {
4069 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4071 0208f208 2020-05-05 stsp goto done;
4074 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4075 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4077 0208f208 2020-05-05 stsp goto done;
4079 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4080 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4082 0208f208 2020-05-05 stsp if (tree1)
4083 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4084 0208f208 2020-05-05 stsp if (tree2)
4085 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4086 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4087 0208f208 2020-05-05 stsp return err;
4090 0208f208 2020-05-05 stsp static const struct got_error *
4091 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4095 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4096 fe621944 2020-11-10 stsp if (p == NULL)
4097 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4098 fe621944 2020-11-10 stsp *line_offsets = p;
4099 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4100 fe621944 2020-11-10 stsp (*nlines)++;
4101 fe621944 2020-11-10 stsp return NULL;
4104 fe621944 2020-11-10 stsp static const struct got_error *
4105 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4106 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4107 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4109 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4110 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4111 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4112 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4113 45d799e2 2018-12-23 stsp time_t committer_time;
4114 45d799e2 2018-12-23 stsp const char *author, *committer;
4115 8b473291 2019-02-21 stsp char *refs_str = NULL;
4116 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4117 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4118 fe621944 2020-11-10 stsp off_t outoff = 0;
4121 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4123 8b473291 2019-02-21 stsp if (refs) {
4124 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4126 8b473291 2019-02-21 stsp return err;
4129 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4131 abd2672a 2018-12-23 stsp return err;
4133 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4134 15a94983 2018-12-23 stsp if (err) {
4135 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4136 15a94983 2018-12-23 stsp goto done;
4139 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4141 fe621944 2020-11-10 stsp goto done;
4143 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4144 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4145 fe621944 2020-11-10 stsp if (n < 0) {
4146 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4147 abd2672a 2018-12-23 stsp goto done;
4149 fe621944 2020-11-10 stsp outoff += n;
4150 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4152 fe621944 2020-11-10 stsp goto done;
4154 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4155 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4156 fe621944 2020-11-10 stsp if (n < 0) {
4157 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4158 abd2672a 2018-12-23 stsp goto done;
4160 fe621944 2020-11-10 stsp outoff += n;
4161 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4163 fe621944 2020-11-10 stsp goto done;
4165 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4166 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4167 fe621944 2020-11-10 stsp if (datestr) {
4168 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4169 fe621944 2020-11-10 stsp if (n < 0) {
4170 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4171 fe621944 2020-11-10 stsp goto done;
4173 fe621944 2020-11-10 stsp outoff += n;
4174 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4176 fe621944 2020-11-10 stsp goto done;
4178 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4179 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4180 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4181 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4182 fe621944 2020-11-10 stsp if (n < 0) {
4183 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4184 fe621944 2020-11-10 stsp goto done;
4186 fe621944 2020-11-10 stsp outoff += n;
4187 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4189 fe621944 2020-11-10 stsp goto done;
4191 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4192 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4193 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4194 ac4dc263 2021-09-24 thomas int pn = 1;
4195 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4196 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4197 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4198 ac4dc263 2021-09-24 thomas if (err)
4199 ac4dc263 2021-09-24 thomas goto done;
4200 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4201 ac4dc263 2021-09-24 thomas if (n < 0) {
4202 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4203 ac4dc263 2021-09-24 thomas goto done;
4205 ac4dc263 2021-09-24 thomas outoff += n;
4206 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4207 ac4dc263 2021-09-24 thomas if (err)
4208 ac4dc263 2021-09-24 thomas goto done;
4209 ac4dc263 2021-09-24 thomas free(id_str);
4210 ac4dc263 2021-09-24 thomas id_str = NULL;
4214 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4216 5943eee2 2019-08-13 stsp goto done;
4217 fe621944 2020-11-10 stsp s = logmsg;
4218 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4219 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4220 fe621944 2020-11-10 stsp if (n < 0) {
4221 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4222 fe621944 2020-11-10 stsp goto done;
4224 fe621944 2020-11-10 stsp outoff += n;
4225 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4227 fe621944 2020-11-10 stsp goto done;
4230 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4232 0208f208 2020-05-05 stsp goto done;
4233 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4234 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4235 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4236 fe621944 2020-11-10 stsp if (n < 0) {
4237 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4238 fe621944 2020-11-10 stsp goto done;
4240 fe621944 2020-11-10 stsp outoff += n;
4241 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4243 fe621944 2020-11-10 stsp goto done;
4244 0208f208 2020-05-05 stsp free((char *)pe->path);
4245 0208f208 2020-05-05 stsp free(pe->data);
4248 0208f208 2020-05-05 stsp fputc('\n', outfile);
4250 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4252 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4253 abd2672a 2018-12-23 stsp free(id_str);
4254 5943eee2 2019-08-13 stsp free(logmsg);
4255 8b473291 2019-02-21 stsp free(refs_str);
4256 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4257 7510f233 2020-08-09 stsp if (err) {
4258 ae6a6978 2020-08-09 stsp free(*line_offsets);
4259 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4260 ae6a6978 2020-08-09 stsp *nlines = 0;
4262 fe621944 2020-11-10 stsp return err;
4265 abd2672a 2018-12-23 stsp static const struct got_error *
4266 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4268 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4269 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4270 15a94983 2018-12-23 stsp int obj_type;
4272 fe621944 2020-11-10 stsp free(s->line_offsets);
4273 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4274 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4275 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4276 fe621944 2020-11-10 stsp s->nlines = 0;
4278 511a516b 2018-05-19 stsp f = got_opentemp();
4279 48ae06ee 2018-10-18 stsp if (f == NULL) {
4280 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4281 48ae06ee 2018-10-18 stsp goto done;
4283 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4284 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4285 fb43ecf1 2019-02-11 stsp goto done;
4289 15a94983 2018-12-23 stsp if (s->id1)
4290 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4292 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4294 15a94983 2018-12-23 stsp goto done;
4296 15a94983 2018-12-23 stsp switch (obj_type) {
4297 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4298 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4299 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4300 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4301 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4303 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4304 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4305 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4306 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4307 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4309 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4310 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4311 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4312 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4313 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4315 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4317 3ffacbe1 2020-02-02 tracey goto done;
4318 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4319 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4320 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4321 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4322 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4323 f44b1f58 2020-02-02 tracey if (err)
4324 f44b1f58 2020-02-02 tracey goto done;
4325 f44b1f58 2020-02-02 tracey } else {
4326 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4327 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4328 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4329 fe621944 2020-11-10 stsp err = write_commit_info(
4330 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4331 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4332 f44b1f58 2020-02-02 tracey if (err)
4333 f44b1f58 2020-02-02 tracey goto done;
4338 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4340 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4341 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4342 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4343 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4347 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4350 f44b1f58 2020-02-02 tracey if (err)
4351 f44b1f58 2020-02-02 tracey goto done;
4353 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4354 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4355 48ae06ee 2018-10-18 stsp return err;
4358 f5215bb9 2019-02-22 stsp static void
4359 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4361 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4362 f5215bb9 2019-02-22 stsp update_panels();
4363 f5215bb9 2019-02-22 stsp doupdate();
4366 f44b1f58 2020-02-02 tracey static const struct got_error *
4367 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4369 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4371 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4372 f44b1f58 2020-02-02 tracey return NULL;
4375 f44b1f58 2020-02-02 tracey static const struct got_error *
4376 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4378 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4379 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4380 f44b1f58 2020-02-02 tracey int lineno;
4381 78eecffc 2022-06-23 thomas char *line = NULL;
4382 826082fe 2020-12-10 stsp size_t linesize = 0;
4383 826082fe 2020-12-10 stsp ssize_t linelen;
4385 f44b1f58 2020-02-02 tracey if (!view->searching) {
4386 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4387 f44b1f58 2020-02-02 tracey return NULL;
4390 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4391 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4392 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4394 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4396 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4398 f44b1f58 2020-02-02 tracey while (1) {
4399 f44b1f58 2020-02-02 tracey off_t offset;
4401 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4402 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4403 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4407 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4408 f44b1f58 2020-02-02 tracey lineno = 1;
4410 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4413 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4414 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4415 f44b1f58 2020-02-02 tracey free(line);
4416 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4418 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4419 78eecffc 2022-06-23 thomas if (linelen != -1) {
4420 78eecffc 2022-06-23 thomas char *exstr;
4421 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4422 78eecffc 2022-06-23 thomas if (err)
4424 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4425 78eecffc 2022-06-23 thomas &view->regmatch)) {
4426 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4427 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4428 78eecffc 2022-06-23 thomas free(exstr);
4431 78eecffc 2022-06-23 thomas free(exstr);
4433 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4434 f44b1f58 2020-02-02 tracey lineno++;
4436 f44b1f58 2020-02-02 tracey lineno--;
4438 826082fe 2020-12-10 stsp free(line);
4440 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4441 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4442 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4445 05171be4 2022-06-23 thomas return err;
4448 48ae06ee 2018-10-18 stsp static const struct got_error *
4449 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4451 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4452 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4454 a0f32f33 2022-06-13 thomas free(s->id1);
4455 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4456 a0f32f33 2022-06-13 thomas free(s->id2);
4457 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4458 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4459 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4460 a0f32f33 2022-06-13 thomas s->f = NULL;
4461 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4462 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4463 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4464 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4465 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4466 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4467 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4468 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4469 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4470 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4471 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4472 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4473 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4474 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4475 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4476 a0f32f33 2022-06-13 thomas s->nlines = 0;
4477 a0f32f33 2022-06-13 thomas return err;
4480 a0f32f33 2022-06-13 thomas static const struct got_error *
4481 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4482 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4483 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4484 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4486 48ae06ee 2018-10-18 stsp const struct got_error *err;
4487 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4489 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4490 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4491 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4493 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4494 15a94983 2018-12-23 stsp int type1, type2;
4495 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4497 15a94983 2018-12-23 stsp return err;
4498 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4500 15a94983 2018-12-23 stsp return err;
4502 15a94983 2018-12-23 stsp if (type1 != type2)
4503 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4505 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4506 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4507 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4508 f44b1f58 2020-02-02 tracey s->repo = repo;
4509 f44b1f58 2020-02-02 tracey s->id1 = id1;
4510 f44b1f58 2020-02-02 tracey s->id2 = id2;
4511 3dbaef42 2020-11-24 stsp s->label1 = label1;
4512 3dbaef42 2020-11-24 stsp s->label2 = label2;
4514 15a94983 2018-12-23 stsp if (id1) {
4515 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4516 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4517 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4519 5465d566 2020-02-01 tracey s->id1 = NULL;
4521 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4522 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4523 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4524 a0f32f33 2022-06-13 thomas goto done;
4527 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4528 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4529 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4530 9a1d7435 2022-06-23 thomas goto done;
4533 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4534 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4535 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4536 a0f32f33 2022-06-13 thomas goto done;
4539 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4540 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4541 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4542 19a6a6b5 2022-07-01 thomas goto done;
4545 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4546 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4547 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4548 19a6a6b5 2022-07-01 thomas goto done;
4551 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4552 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4553 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4554 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4555 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4556 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4557 5465d566 2020-02-01 tracey s->repo = repo;
4559 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4560 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4561 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4562 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4563 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4565 a0f32f33 2022-06-13 thomas goto done;
4566 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4567 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4568 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4569 a0f32f33 2022-06-13 thomas if (err)
4570 a0f32f33 2022-06-13 thomas goto done;
4571 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4572 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4573 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4574 a0f32f33 2022-06-13 thomas if (err)
4575 a0f32f33 2022-06-13 thomas goto done;
4577 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4578 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4579 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4580 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4581 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4582 a0f32f33 2022-06-13 thomas if (err)
4583 a0f32f33 2022-06-13 thomas goto done;
4585 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4586 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4587 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4588 a0f32f33 2022-06-13 thomas if (err)
4589 a0f32f33 2022-06-13 thomas goto done;
4591 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4592 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4593 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4594 a0f32f33 2022-06-13 thomas if (err)
4595 a0f32f33 2022-06-13 thomas goto done;
4598 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4599 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4600 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4601 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4603 5465d566 2020-02-01 tracey err = create_diff(s);
4605 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4606 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4607 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4608 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4609 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4610 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4612 a0f32f33 2022-06-13 thomas if (err)
4613 a0f32f33 2022-06-13 thomas close_diff_view(view);
4614 e5a0f69f 2018-08-18 stsp return err;
4617 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4618 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4620 a3404814 2018-09-02 stsp const struct got_error *err;
4621 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4622 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4623 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4625 a3404814 2018-09-02 stsp if (s->id1) {
4626 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4628 a3404814 2018-09-02 stsp return err;
4629 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4631 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4633 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4635 a3404814 2018-09-02 stsp return err;
4636 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4638 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4639 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4640 a3404814 2018-09-02 stsp free(id_str1);
4641 a3404814 2018-09-02 stsp free(id_str2);
4642 a3404814 2018-09-02 stsp return err;
4644 a3404814 2018-09-02 stsp free(id_str1);
4645 a3404814 2018-09-02 stsp free(id_str2);
4647 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4648 267bb3b8 2021-08-01 stsp free(header);
4649 267bb3b8 2021-08-01 stsp return err;
4652 15a087fe 2019-02-21 stsp static const struct got_error *
4653 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4654 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4656 d7a04538 2019-02-21 stsp const struct got_error *err;
4657 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4658 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4659 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4661 15a087fe 2019-02-21 stsp free(s->id2);
4662 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4663 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4664 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4666 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4668 d7a04538 2019-02-21 stsp return err;
4669 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4670 15a087fe 2019-02-21 stsp free(s->id1);
4671 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4672 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4673 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4674 15a087fe 2019-02-21 stsp return NULL;
4677 0cf4efb1 2018-09-29 stsp static const struct got_error *
4678 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4680 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4682 adf4c9e0 2022-07-03 thomas view->count = 0;
4683 adf4c9e0 2022-07-03 thomas wclear(view->window);
4684 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4685 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4686 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4687 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4688 adf4c9e0 2022-07-03 thomas return create_diff(s);
4691 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4692 4fc71f3b 2022-07-12 thomas int, int, int);
4693 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4694 4fc71f3b 2022-07-12 thomas int, int);
4696 adf4c9e0 2022-07-03 thomas static const struct got_error *
4697 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4699 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4700 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4701 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4702 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4703 826082fe 2020-12-10 stsp char *line = NULL;
4704 826082fe 2020-12-10 stsp size_t linesize = 0;
4705 826082fe 2020-12-10 stsp ssize_t linelen;
4706 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4708 e5a0f69f 2018-08-18 stsp switch (ch) {
4709 05171be4 2022-06-23 thomas case '0':
4710 05171be4 2022-06-23 thomas view->x = 0;
4712 05171be4 2022-06-23 thomas case '$':
4713 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4714 07b0611c 2022-06-23 thomas view->count = 0;
4716 05171be4 2022-06-23 thomas case KEY_RIGHT:
4717 05171be4 2022-06-23 thomas case 'l':
4718 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4719 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4721 07b0611c 2022-06-23 thomas view->count = 0;
4723 05171be4 2022-06-23 thomas case KEY_LEFT:
4724 05171be4 2022-06-23 thomas case 'h':
4725 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4726 07b0611c 2022-06-23 thomas if (view->x <= 0)
4727 07b0611c 2022-06-23 thomas view->count = 0;
4731 3dbaef42 2020-11-24 stsp if (ch == 'a')
4732 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4733 3dbaef42 2020-11-24 stsp if (ch == 'w')
4734 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4735 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4738 912a3f79 2021-08-30 j case KEY_HOME:
4739 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4740 07b0611c 2022-06-23 thomas view->count = 0;
4743 912a3f79 2021-08-30 j case KEY_END:
4744 07b0611c 2022-06-23 thomas view->count = 0;
4748 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4752 1e37a5c2 2019-05-12 jcs case KEY_UP:
4753 f7140bf5 2021-10-17 thomas case CTRL('p'):
4754 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4755 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4757 07b0611c 2022-06-23 thomas view->count = 0;
4759 70f17a53 2022-06-13 thomas case CTRL('u'):
4760 23427b14 2022-06-23 thomas case 'u':
4761 70f17a53 2022-06-13 thomas nscroll /= 2;
4762 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4763 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4764 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4765 1c5e5faa 2022-06-23 thomas case 'b':
4766 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4767 07b0611c 2022-06-23 thomas view->count = 0;
4771 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4772 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4775 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4776 f7140bf5 2021-10-17 thomas case CTRL('n'):
4777 1e37a5c2 2019-05-12 jcs if (!s->eof)
4778 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4780 07b0611c 2022-06-23 thomas view->count = 0;
4782 70f17a53 2022-06-13 thomas case CTRL('d'):
4783 23427b14 2022-06-23 thomas case 'd':
4784 70f17a53 2022-06-13 thomas nscroll /= 2;
4785 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4786 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4787 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4788 1c5e5faa 2022-06-23 thomas case 'f':
4790 07b0611c 2022-06-23 thomas if (s->eof) {
4791 07b0611c 2022-06-23 thomas view->count = 0;
4795 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4796 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4797 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4798 826082fe 2020-12-10 stsp if (linelen == -1) {
4799 826082fe 2020-12-10 stsp if (feof(s->f)) {
4800 826082fe 2020-12-10 stsp s->eof = 1;
4802 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4806 826082fe 2020-12-10 stsp free(line);
4809 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4810 1e37a5c2 2019-05-12 jcs s->diff_context--;
4811 aa61903a 2021-12-31 thomas s->matched_line = 0;
4812 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4813 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4814 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4815 27829c9e 2020-11-21 stsp s->nlines) {
4816 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4817 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4820 07b0611c 2022-06-23 thomas view->count = 0;
4823 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4824 1e37a5c2 2019-05-12 jcs s->diff_context++;
4825 aa61903a 2021-12-31 thomas s->matched_line = 0;
4826 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4827 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4829 07b0611c 2022-06-23 thomas view->count = 0;
4833 777aae21 2022-07-20 thomas case 'K':
4835 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4836 4fc71f3b 2022-07-12 thomas case '>':
4837 4fc71f3b 2022-07-12 thomas case '.':
4838 777aae21 2022-07-20 thomas case 'J':
4839 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4840 07b0611c 2022-06-23 thomas view->count = 0;
4843 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4845 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4846 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4847 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4849 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4850 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4851 4fc71f3b 2022-07-12 thomas if (err)
4853 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4855 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4858 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4859 4fc71f3b 2022-07-12 thomas if (err)
4861 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4862 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4863 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4865 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4866 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4867 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4869 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4870 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4871 4fc71f3b 2022-07-12 thomas if (err)
4873 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4875 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4877 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4878 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4879 4fc71f3b 2022-07-12 thomas bs->selected_line);
4880 4fc71f3b 2022-07-12 thomas if (id == NULL)
4883 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4886 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4887 4fc71f3b 2022-07-12 thomas if (err)
4890 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4891 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4892 aa61903a 2021-12-31 thomas s->matched_line = 0;
4893 05171be4 2022-06-23 thomas view->x = 0;
4895 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4896 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4899 07b0611c 2022-06-23 thomas view->count = 0;
4903 bcbd79e2 2018-08-19 stsp return err;
4906 4ed7e80c 2018-05-20 stsp static const struct got_error *
4907 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4909 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4910 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4911 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4912 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4913 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4914 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4915 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4916 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4917 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4918 3dbaef42 2020-11-24 stsp const char *errstr;
4919 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4920 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4922 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4923 26ed57b2 2018-05-19 stsp switch (ch) {
4925 64453f7e 2020-11-21 stsp force_text_diff = 1;
4928 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4930 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4931 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4932 8f666e67 2022-02-12 thomas errstr, errstr);
4934 09b5bff8 2020-02-23 naddy case 'r':
4935 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4936 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4937 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4939 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4942 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4945 17020d27 2019-03-07 stsp usage_diff();
4946 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4950 26ed57b2 2018-05-19 stsp argc -= optind;
4951 26ed57b2 2018-05-19 stsp argv += optind;
4953 26ed57b2 2018-05-19 stsp if (argc == 0) {
4954 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4955 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4956 15a94983 2018-12-23 stsp id_str1 = argv[0];
4957 15a94983 2018-12-23 stsp id_str2 = argv[1];
4959 26ed57b2 2018-05-19 stsp usage_diff();
4961 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4962 7cd52833 2022-06-23 thomas if (error)
4963 7cd52833 2022-06-23 thomas goto done;
4965 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4966 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4967 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4968 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4969 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4970 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4971 c156c7a4 2020-12-18 stsp goto done;
4972 a273ac94 2020-02-23 naddy if (worktree)
4973 a273ac94 2020-02-23 naddy repo_path =
4974 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4976 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4977 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4978 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4979 c156c7a4 2020-12-18 stsp goto done;
4983 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4984 eb6600df 2019-01-04 stsp if (error)
4985 eb6600df 2019-01-04 stsp goto done;
4987 a273ac94 2020-02-23 naddy init_curses();
4989 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4990 51a10b52 2020-12-26 stsp if (error)
4991 51a10b52 2020-12-26 stsp goto done;
4993 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4994 26ed57b2 2018-05-19 stsp if (error)
4995 26ed57b2 2018-05-19 stsp goto done;
4997 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4998 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4999 26ed57b2 2018-05-19 stsp if (error)
5000 26ed57b2 2018-05-19 stsp goto done;
5002 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5003 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5004 26ed57b2 2018-05-19 stsp if (error)
5005 26ed57b2 2018-05-19 stsp goto done;
5007 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5008 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5009 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5010 ea5e7bb5 2018-08-01 stsp goto done;
5012 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5013 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5014 5dc9f4bc 2018-08-04 stsp if (error)
5015 5dc9f4bc 2018-08-04 stsp goto done;
5016 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5018 3dbaef42 2020-11-24 stsp free(label1);
5019 3dbaef42 2020-11-24 stsp free(label2);
5020 c02c541e 2019-03-29 stsp free(repo_path);
5021 a273ac94 2020-02-23 naddy free(cwd);
5022 1d0f4054 2021-06-17 stsp if (repo) {
5023 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5024 1d0f4054 2021-06-17 stsp if (error == NULL)
5025 1d0f4054 2021-06-17 stsp error = close_err;
5027 a273ac94 2020-02-23 naddy if (worktree)
5028 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5029 7cd52833 2022-06-23 thomas if (pack_fds) {
5030 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5031 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5032 7cd52833 2022-06-23 thomas if (error == NULL)
5033 7cd52833 2022-06-23 thomas error = pack_err;
5035 51a10b52 2020-12-26 stsp tog_free_refs();
5036 26ed57b2 2018-05-19 stsp return error;
5039 4ed7e80c 2018-05-20 stsp __dead static void
5040 9f7d7167 2018-04-29 stsp usage_blame(void)
5043 91198554 2022-06-23 thomas fprintf(stderr,
5044 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5045 9f7d7167 2018-04-29 stsp getprogname());
5049 84451b3e 2018-07-10 stsp struct tog_blame_line {
5050 84451b3e 2018-07-10 stsp int annotated;
5051 84451b3e 2018-07-10 stsp struct got_object_id *id;
5054 4ed7e80c 2018-05-20 stsp static const struct got_error *
5055 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5057 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5058 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5059 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5060 84451b3e 2018-07-10 stsp const struct got_error *err;
5061 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5062 826082fe 2020-12-10 stsp char *line = NULL;
5063 826082fe 2020-12-10 stsp size_t linesize = 0;
5064 826082fe 2020-12-10 stsp ssize_t linelen;
5065 84451b3e 2018-07-10 stsp wchar_t *wline;
5066 27a741e5 2019-09-11 stsp int width;
5067 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5068 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5069 ab089a2a 2018-07-12 stsp char *id_str;
5070 11b20872 2019-11-08 stsp struct tog_color *tc;
5072 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5074 ab089a2a 2018-07-12 stsp return err;
5076 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5077 f7d12f7e 2018-08-01 stsp werase(view->window);
5079 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5080 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5081 ab089a2a 2018-07-12 stsp free(id_str);
5082 ab089a2a 2018-07-12 stsp return err;
5085 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5086 ab089a2a 2018-07-12 stsp free(line);
5087 2550e4c3 2018-07-13 stsp line = NULL;
5089 1cae65b4 2019-09-22 stsp return err;
5090 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5091 a3404814 2018-09-02 stsp wstandout(view->window);
5092 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5094 11b20872 2019-11-08 stsp wattr_on(view->window,
5095 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5096 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5098 11b20872 2019-11-08 stsp wattr_off(view->window,
5099 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5100 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5101 a3404814 2018-09-02 stsp wstandend(view->window);
5102 2550e4c3 2018-07-13 stsp free(wline);
5103 2550e4c3 2018-07-13 stsp wline = NULL;
5104 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5105 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5107 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5108 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5109 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5110 ab089a2a 2018-07-12 stsp free(id_str);
5111 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5113 ab089a2a 2018-07-12 stsp free(id_str);
5114 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5115 3f60a8ef 2018-07-10 stsp free(line);
5116 2550e4c3 2018-07-13 stsp line = NULL;
5118 3f60a8ef 2018-07-10 stsp return err;
5119 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5120 2550e4c3 2018-07-13 stsp free(wline);
5121 2550e4c3 2018-07-13 stsp wline = NULL;
5122 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5123 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5125 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5126 05171be4 2022-06-23 thomas view->maxx = 0;
5127 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5128 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5129 826082fe 2020-12-10 stsp if (linelen == -1) {
5130 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5131 826082fe 2020-12-10 stsp s->eof = 1;
5134 84451b3e 2018-07-10 stsp free(line);
5135 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5137 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5140 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5141 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5142 cbea3800 2022-06-23 thomas if (err) {
5143 cbea3800 2022-06-23 thomas free(line);
5144 cbea3800 2022-06-23 thomas return err;
5146 cbea3800 2022-06-23 thomas free(wline);
5147 cbea3800 2022-06-23 thomas wline = NULL;
5148 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5150 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5151 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5153 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5154 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5155 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5156 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5157 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5158 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5159 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5160 8d0fe45a 2019-08-12 stsp char *id_str;
5161 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5162 91198554 2022-06-23 thomas blame_line->id);
5163 8d0fe45a 2019-08-12 stsp if (err) {
5164 8d0fe45a 2019-08-12 stsp free(line);
5165 8d0fe45a 2019-08-12 stsp return err;
5167 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5169 11b20872 2019-11-08 stsp wattr_on(view->window,
5170 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5171 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5173 11b20872 2019-11-08 stsp wattr_off(view->window,
5174 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5175 8d0fe45a 2019-08-12 stsp free(id_str);
5176 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5178 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5179 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5182 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5183 ee41ec32 2018-07-10 stsp prev_id = NULL;
5186 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5187 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5188 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5190 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5191 41605754 2020-11-12 stsp width = 9;
5192 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5193 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5194 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5195 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5196 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5197 41605754 2020-11-12 stsp if (err) {
5198 41605754 2020-11-12 stsp free(line);
5199 41605754 2020-11-12 stsp return err;
5201 41605754 2020-11-12 stsp width += 9;
5203 923086fd 2022-06-23 thomas int skip;
5204 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5205 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5206 923086fd 2022-06-23 thomas if (err) {
5207 923086fd 2022-06-23 thomas free(line);
5208 923086fd 2022-06-23 thomas return err;
5210 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5211 02bce7e2 2022-06-23 thomas width += 9;
5212 41605754 2020-11-12 stsp free(wline);
5213 41605754 2020-11-12 stsp wline = NULL;
5216 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5217 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5218 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5219 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5221 826082fe 2020-12-10 stsp free(line);
5222 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5224 a5d43cac 2022-07-01 thomas view_border(view);
5226 84451b3e 2018-07-10 stsp return NULL;
5229 84451b3e 2018-07-10 stsp static const struct got_error *
5230 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5231 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5233 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5234 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5235 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5236 1a76625f 2018-10-22 stsp int errcode;
5238 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5239 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5240 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5242 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5243 1a76625f 2018-10-22 stsp if (errcode)
5244 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5246 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5247 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5248 d68a0a7d 2018-07-10 stsp goto done;
5251 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5252 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5254 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5255 d68a0a7d 2018-07-10 stsp if (line->annotated)
5256 d68a0a7d 2018-07-10 stsp goto done;
5258 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5259 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5260 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5261 84451b3e 2018-07-10 stsp goto done;
5263 84451b3e 2018-07-10 stsp line->annotated = 1;
5265 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5266 1a76625f 2018-10-22 stsp if (errcode)
5267 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5268 84451b3e 2018-07-10 stsp return err;
5271 84451b3e 2018-07-10 stsp static void *
5272 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5274 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5275 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5276 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5277 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5278 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5280 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5281 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5282 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5284 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5285 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5286 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5287 9117a7b7 2022-07-01 thomas goto done;
5290 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5291 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5292 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5293 9117a7b7 2022-07-01 thomas goto done;
5295 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5296 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5297 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5298 9117a7b7 2022-07-01 thomas goto done;
5301 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5303 9117a7b7 2022-07-01 thomas goto done;
5305 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5306 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5307 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5308 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5309 fc06ba56 2019-08-22 stsp err = NULL;
5311 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5312 9117a7b7 2022-07-01 thomas if (errcode) {
5313 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5314 9117a7b7 2022-07-01 thomas goto done;
5317 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5318 1d0f4054 2021-06-17 stsp if (err == NULL)
5319 1d0f4054 2021-06-17 stsp err = close_err;
5320 c9beca56 2018-07-22 stsp ta->repo = NULL;
5321 c9beca56 2018-07-22 stsp *ta->complete = 1;
5323 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5324 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5325 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5328 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5329 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5330 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5331 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5332 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5333 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5334 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5335 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5337 18430de3 2018-07-10 stsp return (void *)err;
5340 245d91c1 2018-07-12 stsp static struct got_object_id *
5341 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5342 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5344 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5346 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5347 8d0fe45a 2019-08-12 stsp return NULL;
5349 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5350 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5351 4fc71f3b 2022-07-12 thomas return NULL;
5353 4fc71f3b 2022-07-12 thomas return line->id;
5356 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5357 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5358 4fc71f3b 2022-07-12 thomas int lineno)
5360 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5362 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5363 4fc71f3b 2022-07-12 thomas return NULL;
5365 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5366 245d91c1 2018-07-12 stsp if (!line->annotated)
5367 245d91c1 2018-07-12 stsp return NULL;
5369 245d91c1 2018-07-12 stsp return line->id;
5372 b880a918 2018-07-10 stsp static const struct got_error *
5373 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5375 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5378 245d91c1 2018-07-12 stsp if (blame->thread) {
5379 1a76625f 2018-10-22 stsp int errcode;
5380 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5381 1a76625f 2018-10-22 stsp if (errcode)
5382 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5383 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5384 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5385 1a76625f 2018-10-22 stsp if (errcode)
5386 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5387 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5388 1a76625f 2018-10-22 stsp if (errcode)
5389 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5390 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5391 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5392 245d91c1 2018-07-12 stsp err = NULL;
5393 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5395 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5396 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5397 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5398 1d0f4054 2021-06-17 stsp if (err == NULL)
5399 1d0f4054 2021-06-17 stsp err = close_err;
5400 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5402 245d91c1 2018-07-12 stsp if (blame->f) {
5403 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5404 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5405 245d91c1 2018-07-12 stsp blame->f = NULL;
5407 57670559 2018-12-23 stsp if (blame->lines) {
5408 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5409 57670559 2018-12-23 stsp free(blame->lines[i].id);
5410 57670559 2018-12-23 stsp free(blame->lines);
5411 57670559 2018-12-23 stsp blame->lines = NULL;
5413 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5414 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5415 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5416 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5417 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5418 7cd52833 2022-06-23 thomas if (err == NULL)
5419 7cd52833 2022-06-23 thomas err = pack_err;
5420 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5422 245d91c1 2018-07-12 stsp return err;
5425 245d91c1 2018-07-12 stsp static const struct got_error *
5426 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5428 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5429 fc06ba56 2019-08-22 stsp int *done = arg;
5430 fc06ba56 2019-08-22 stsp int errcode;
5432 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5433 fc06ba56 2019-08-22 stsp if (errcode)
5434 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5435 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5437 fc06ba56 2019-08-22 stsp if (*done)
5438 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5440 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5441 fc06ba56 2019-08-22 stsp if (errcode)
5442 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5443 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5445 fc06ba56 2019-08-22 stsp return err;
5448 fc06ba56 2019-08-22 stsp static const struct got_error *
5449 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5451 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5452 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5453 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5454 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5455 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5456 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5457 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5458 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5459 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5461 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5462 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5464 15a94983 2018-12-23 stsp return err;
5466 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5467 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5468 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5469 f4ae6ddb 2022-07-01 thomas goto done;
5472 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5473 945f9229 2022-04-16 thomas if (err)
5474 945f9229 2022-04-16 thomas goto done;
5476 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5478 84451b3e 2018-07-10 stsp goto done;
5480 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5481 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5482 84451b3e 2018-07-10 stsp goto done;
5485 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5487 a70480e0 2018-06-23 stsp goto done;
5488 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5489 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5490 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5491 84451b3e 2018-07-10 stsp goto done;
5493 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5494 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5496 1fddf795 2021-01-20 stsp goto done;
5497 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5498 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5499 84451b3e 2018-07-10 stsp goto done;
5502 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5503 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5504 b02560ec 2019-08-19 stsp blame->nlines--;
5506 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5507 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5508 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5509 84451b3e 2018-07-10 stsp goto done;
5512 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5514 bd24772e 2018-07-11 stsp goto done;
5515 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5516 7cd52833 2022-06-23 thomas pack_fds);
5517 7cd52833 2022-06-23 thomas if (err)
5518 7cd52833 2022-06-23 thomas goto done;
5520 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5521 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5522 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5523 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5524 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5525 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5526 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5527 245d91c1 2018-07-12 stsp goto done;
5529 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5531 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5532 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5533 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5534 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5535 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5536 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5537 a5388363 2020-12-01 naddy s->blame_complete = 0;
5539 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5540 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5541 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5542 f5a09613 2020-12-13 naddy s->selected_line = 1;
5544 aa61903a 2021-12-31 thomas s->matched_line = 0;
5547 945f9229 2022-04-16 thomas if (commit)
5548 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5549 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5550 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5552 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5553 27d434c2 2018-09-15 stsp free(obj_id);
5555 245d91c1 2018-07-12 stsp stop_blame(blame);
5556 245d91c1 2018-07-12 stsp return err;
5559 245d91c1 2018-07-12 stsp static const struct got_error *
5560 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5561 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5563 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5564 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5566 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5568 c4843652 2019-08-12 stsp s->path = strdup(path);
5569 c4843652 2019-08-12 stsp if (s->path == NULL)
5570 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5572 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5573 c4843652 2019-08-12 stsp if (err) {
5574 c4843652 2019-08-12 stsp free(s->path);
5575 7cbe629d 2018-08-04 stsp return err;
5578 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5579 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5580 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5581 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5582 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5583 fb2756b9 2018-08-04 stsp s->repo = repo;
5584 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5585 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5587 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5588 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5589 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5590 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5592 11b20872 2019-11-08 stsp return err;
5595 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5596 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5597 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5598 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5599 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5600 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5602 a5388363 2020-12-01 naddy return run_blame(view);
5605 e5a0f69f 2018-08-18 stsp static const struct got_error *
5606 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5608 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5609 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5611 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5612 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5614 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5615 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5616 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5617 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5618 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5621 e5a0f69f 2018-08-18 stsp free(s->path);
5622 11b20872 2019-11-08 stsp free_colors(&s->colors);
5623 e5a0f69f 2018-08-18 stsp return err;
5626 7cbe629d 2018-08-04 stsp static const struct got_error *
5627 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5629 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5631 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5632 6c4c42e0 2019-06-24 stsp return NULL;
5635 6c4c42e0 2019-06-24 stsp static const struct got_error *
5636 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5638 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5639 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5640 6c4c42e0 2019-06-24 stsp int lineno;
5641 78eecffc 2022-06-23 thomas char *line = NULL;
5642 826082fe 2020-12-10 stsp size_t linesize = 0;
5643 826082fe 2020-12-10 stsp ssize_t linelen;
5645 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5646 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5647 6c4c42e0 2019-06-24 stsp return NULL;
5650 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5651 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5652 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5654 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5656 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5658 6c4c42e0 2019-06-24 stsp while (1) {
5659 6c4c42e0 2019-06-24 stsp off_t offset;
5661 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5662 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5663 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5667 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5668 6c4c42e0 2019-06-24 stsp lineno = 1;
5670 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5673 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5674 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5675 6c4c42e0 2019-06-24 stsp free(line);
5676 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5678 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5679 78eecffc 2022-06-23 thomas if (linelen != -1) {
5680 78eecffc 2022-06-23 thomas char *exstr;
5681 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5682 78eecffc 2022-06-23 thomas if (err)
5684 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5685 78eecffc 2022-06-23 thomas &view->regmatch)) {
5686 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5687 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5688 78eecffc 2022-06-23 thomas free(exstr);
5691 78eecffc 2022-06-23 thomas free(exstr);
5693 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5698 826082fe 2020-12-10 stsp free(line);
5700 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5701 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5702 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5705 05171be4 2022-06-23 thomas return err;
5708 6c4c42e0 2019-06-24 stsp static const struct got_error *
5709 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5711 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5712 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5713 2b380cc8 2018-10-24 stsp int errcode;
5715 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5716 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5717 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5718 2b380cc8 2018-10-24 stsp if (errcode)
5719 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5721 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5724 51fe7530 2019-08-19 stsp if (s->blame_complete)
5725 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5727 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5729 a5d43cac 2022-07-01 thomas view_border(view);
5730 e5a0f69f 2018-08-18 stsp return err;
5733 e5a0f69f 2018-08-18 stsp static const struct got_error *
5734 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5735 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5737 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5738 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5740 eaeaa612 2022-07-20 thomas *new_view = NULL;
5742 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5743 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5744 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5746 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5747 eaeaa612 2022-07-20 thomas if (err)
5748 eaeaa612 2022-07-20 thomas view_close(log_view);
5750 eaeaa612 2022-07-20 thomas *new_view = log_view;
5752 eaeaa612 2022-07-20 thomas return err;
5755 eaeaa612 2022-07-20 thomas static const struct got_error *
5756 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5758 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5759 eaeaa612 2022-07-20 thomas struct tog_view *diff_view, *log_view;
5760 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5761 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5763 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5764 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5765 a5d43cac 2022-07-01 thomas --eos; /* border */
5767 e5a0f69f 2018-08-18 stsp switch (ch) {
5768 05171be4 2022-06-23 thomas case '0':
5769 05171be4 2022-06-23 thomas view->x = 0;
5771 05171be4 2022-06-23 thomas case '$':
5772 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5773 07b0611c 2022-06-23 thomas view->count = 0;
5775 05171be4 2022-06-23 thomas case KEY_RIGHT:
5776 05171be4 2022-06-23 thomas case 'l':
5777 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5778 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5780 07b0611c 2022-06-23 thomas view->count = 0;
5782 05171be4 2022-06-23 thomas case KEY_LEFT:
5783 05171be4 2022-06-23 thomas case 'h':
5784 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5785 07b0611c 2022-06-23 thomas if (view->x <= 0)
5786 07b0611c 2022-06-23 thomas view->count = 0;
5789 1e37a5c2 2019-05-12 jcs s->done = 1;
5791 4deef56f 2021-09-02 naddy case 'g':
5792 4deef56f 2021-09-02 naddy case KEY_HOME:
5793 4deef56f 2021-09-02 naddy s->selected_line = 1;
5794 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5795 07b0611c 2022-06-23 thomas view->count = 0;
5797 4deef56f 2021-09-02 naddy case 'G':
5798 4deef56f 2021-09-02 naddy case KEY_END:
5799 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5800 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5801 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5803 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5804 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5806 07b0611c 2022-06-23 thomas view->count = 0;
5809 1e37a5c2 2019-05-12 jcs case KEY_UP:
5810 f7140bf5 2021-10-17 thomas case CTRL('p'):
5811 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5812 1e37a5c2 2019-05-12 jcs s->selected_line--;
5813 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5814 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5815 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5817 07b0611c 2022-06-23 thomas view->count = 0;
5819 70f17a53 2022-06-13 thomas case CTRL('u'):
5820 23427b14 2022-06-23 thomas case 'u':
5821 70f17a53 2022-06-13 thomas nscroll /= 2;
5822 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5823 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5824 ea025d1d 2020-02-22 naddy case CTRL('b'):
5825 1c5e5faa 2022-06-23 thomas case 'b':
5826 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5827 07b0611c 2022-06-23 thomas if (view->count > 1)
5828 07b0611c 2022-06-23 thomas nscroll += nscroll;
5829 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5830 07b0611c 2022-06-23 thomas view->count = 0;
5833 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5834 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5836 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5839 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5840 f7140bf5 2021-10-17 thomas case CTRL('n'):
5841 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5842 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5843 1e37a5c2 2019-05-12 jcs s->selected_line++;
5844 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5845 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5847 07b0611c 2022-06-23 thomas view->count = 0;
5849 1c5e5faa 2022-06-23 thomas case 'c':
5850 1e37a5c2 2019-05-12 jcs case 'p': {
5851 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5853 07b0611c 2022-06-23 thomas view->count = 0;
5854 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5855 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5856 1e37a5c2 2019-05-12 jcs if (id == NULL)
5858 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5859 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5860 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5861 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5862 1e37a5c2 2019-05-12 jcs int obj_type;
5863 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5864 1e37a5c2 2019-05-12 jcs s->repo, id);
5867 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5868 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5869 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5870 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5873 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5874 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5875 ec242592 2022-04-22 thomas s->repo, &pid->id);
5876 945f9229 2022-04-16 thomas if (err)
5878 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5879 945f9229 2022-04-16 thomas pcommit, s->path);
5880 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5881 e5a0f69f 2018-08-18 stsp if (err) {
5882 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5883 1e37a5c2 2019-05-12 jcs err = NULL;
5884 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5887 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5889 1e37a5c2 2019-05-12 jcs free(blob_id);
5890 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5891 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5892 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5895 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5896 ec242592 2022-04-22 thomas &pid->id);
5897 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5899 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5900 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5902 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5907 1e37a5c2 2019-05-12 jcs s->done = 1;
5908 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5909 1e37a5c2 2019-05-12 jcs s->done = 0;
5910 1e37a5c2 2019-05-12 jcs if (thread_err)
5912 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5913 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5914 a5388363 2020-12-01 naddy err = run_blame(view);
5919 1c5e5faa 2022-06-23 thomas case 'C': {
5920 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5922 07b0611c 2022-06-23 thomas view->count = 0;
5923 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5924 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5926 1e37a5c2 2019-05-12 jcs s->done = 1;
5927 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5928 1e37a5c2 2019-05-12 jcs s->done = 0;
5929 1e37a5c2 2019-05-12 jcs if (thread_err)
5931 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5932 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5933 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5934 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5935 a5388363 2020-12-01 naddy err = run_blame(view);
5940 eaeaa612 2022-07-20 thomas case 'L': {
5941 eaeaa612 2022-07-20 thomas struct got_object_id *id = NULL;
5943 eaeaa612 2022-07-20 thomas view->count = 0;
5944 eaeaa612 2022-07-20 thomas id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5945 eaeaa612 2022-07-20 thomas s->first_displayed_line, s->selected_line);
5946 eaeaa612 2022-07-20 thomas if (id == NULL)
5949 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view))
5950 eaeaa612 2022-07-20 thomas view_get_split(view, &begin_y, &begin_x);
5951 eaeaa612 2022-07-20 thomas err = log_annotated_line(&log_view, begin_y, begin_x,
5952 eaeaa612 2022-07-20 thomas s->repo, id);
5953 eaeaa612 2022-07-20 thomas if (err)
5955 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view) &&
5956 eaeaa612 2022-07-20 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5957 eaeaa612 2022-07-20 thomas err = view_init_hsplit(view, begin_y);
5958 eaeaa612 2022-07-20 thomas if (err)
5962 eaeaa612 2022-07-20 thomas view->focussed = 0;
5963 eaeaa612 2022-07-20 thomas log_view->focussed = 1;
5964 eaeaa612 2022-07-20 thomas log_view->mode = view->mode;
5965 eaeaa612 2022-07-20 thomas log_view->nlines = view->lines - begin_y;
5966 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view)) {
5967 eaeaa612 2022-07-20 thomas view_transfer_size(log_view, view);
5968 eaeaa612 2022-07-20 thomas err = view_close_child(view);
5969 eaeaa612 2022-07-20 thomas if (err)
5970 eaeaa612 2022-07-20 thomas return err;
5971 eaeaa612 2022-07-20 thomas err = view_set_child(view, log_view);
5972 eaeaa612 2022-07-20 thomas if (err)
5973 eaeaa612 2022-07-20 thomas return err;
5974 eaeaa612 2022-07-20 thomas view->focus_child = 1;
5976 eaeaa612 2022-07-20 thomas *new_view = log_view;
5979 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5980 1e37a5c2 2019-05-12 jcs case '\r': {
5981 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5982 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5983 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5985 07b0611c 2022-06-23 thomas view->count = 0;
5986 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5987 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5988 1e37a5c2 2019-05-12 jcs if (id == NULL)
5990 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5993 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5994 4fc71f3b 2022-07-12 thomas if (*new_view) {
5995 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5996 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5997 4fc71f3b 2022-07-12 thomas if (err)
5999 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6000 4fc71f3b 2022-07-12 thomas } else {
6001 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6002 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6004 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6005 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6006 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6007 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6008 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6012 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6013 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6014 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6016 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6019 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6020 4fc71f3b 2022-07-12 thomas s->selected_line;
6021 4fc71f3b 2022-07-12 thomas if (*new_view)
6022 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6023 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6024 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6025 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6026 a5d43cac 2022-07-01 thomas if (err)
6030 e78dc838 2020-12-04 stsp view->focussed = 0;
6031 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6032 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6033 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6034 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6035 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6036 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6039 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6040 40236d76 2022-06-23 thomas if (err)
6042 e78dc838 2020-12-04 stsp view->focus_child = 1;
6044 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6049 70f17a53 2022-06-13 thomas case CTRL('d'):
6050 23427b14 2022-06-23 thomas case 'd':
6051 70f17a53 2022-06-13 thomas nscroll /= 2;
6052 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6053 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6054 ea025d1d 2020-02-22 naddy case CTRL('f'):
6055 1c5e5faa 2022-06-23 thomas case 'f':
6057 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6058 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6059 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6060 07b0611c 2022-06-23 thomas view->count = 0;
6063 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6064 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6065 70f17a53 2022-06-13 thomas s->selected_line +=
6066 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6067 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6069 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6070 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6072 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6073 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6075 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6076 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6077 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6078 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6082 07b0611c 2022-06-23 thomas view->count = 0;
6085 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6088 adf4c9e0 2022-07-03 thomas static const struct got_error *
6089 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6091 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6092 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6094 adf4c9e0 2022-07-03 thomas view->count = 0;
6095 adf4c9e0 2022-07-03 thomas s->done = 1;
6096 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6097 adf4c9e0 2022-07-03 thomas s->done = 0;
6098 adf4c9e0 2022-07-03 thomas if (err)
6099 adf4c9e0 2022-07-03 thomas return err;
6100 adf4c9e0 2022-07-03 thomas return run_blame(view);
6103 a70480e0 2018-06-23 stsp static const struct got_error *
6104 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6106 a70480e0 2018-06-23 stsp const struct got_error *error;
6107 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6108 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6109 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6110 0587e10c 2020-07-23 stsp char *link_target = NULL;
6111 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6112 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6113 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6115 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6116 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6118 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6119 a70480e0 2018-06-23 stsp switch (ch) {
6121 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6124 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6125 69069811 2018-08-02 stsp if (repo_path == NULL)
6126 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6130 17020d27 2019-03-07 stsp usage_blame();
6131 a70480e0 2018-06-23 stsp /* NOTREACHED */
6135 a70480e0 2018-06-23 stsp argc -= optind;
6136 a70480e0 2018-06-23 stsp argv += optind;
6138 f135c941 2020-02-20 stsp if (argc != 1)
6139 a70480e0 2018-06-23 stsp usage_blame();
6141 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6142 7cd52833 2022-06-23 thomas if (error != NULL)
6143 7cd52833 2022-06-23 thomas goto done;
6145 69069811 2018-08-02 stsp if (repo_path == NULL) {
6146 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6147 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6148 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6149 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6150 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6151 c156c7a4 2020-12-18 stsp goto done;
6152 f135c941 2020-02-20 stsp if (worktree)
6153 eb41ed75 2019-02-05 stsp repo_path =
6154 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6156 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6157 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6158 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6159 c156c7a4 2020-12-18 stsp goto done;
6163 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6164 c02c541e 2019-03-29 stsp if (error != NULL)
6165 8e94dd5b 2019-01-04 stsp goto done;
6167 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6168 f135c941 2020-02-20 stsp worktree);
6169 c02c541e 2019-03-29 stsp if (error)
6170 92205607 2019-01-04 stsp goto done;
6172 f135c941 2020-02-20 stsp init_curses();
6174 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6175 51a10b52 2020-12-26 stsp if (error)
6176 51a10b52 2020-12-26 stsp goto done;
6178 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6179 eb41ed75 2019-02-05 stsp if (error)
6180 69069811 2018-08-02 stsp goto done;
6182 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6183 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6184 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6185 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6186 a70480e0 2018-06-23 stsp if (error != NULL)
6187 66b4983c 2018-06-23 stsp goto done;
6188 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6189 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6191 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6192 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6194 a19e88aa 2018-06-23 stsp if (error != NULL)
6195 8b473291 2019-02-21 stsp goto done;
6197 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6198 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6199 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6200 e1cd8fed 2018-08-01 stsp goto done;
6203 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6204 945f9229 2022-04-16 thomas if (error)
6205 945f9229 2022-04-16 thomas goto done;
6207 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6208 945f9229 2022-04-16 thomas commit, repo);
6209 7cbe629d 2018-08-04 stsp if (error)
6210 7cbe629d 2018-08-04 stsp goto done;
6212 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6213 78756c87 2020-11-24 stsp commit_id, repo);
6214 0587e10c 2020-07-23 stsp if (error)
6215 0587e10c 2020-07-23 stsp goto done;
6216 12314ad4 2019-08-31 stsp if (worktree) {
6217 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6218 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6219 12314ad4 2019-08-31 stsp worktree = NULL;
6221 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6223 69069811 2018-08-02 stsp free(repo_path);
6224 f135c941 2020-02-20 stsp free(in_repo_path);
6225 0587e10c 2020-07-23 stsp free(link_target);
6226 69069811 2018-08-02 stsp free(cwd);
6227 a70480e0 2018-06-23 stsp free(commit_id);
6228 945f9229 2022-04-16 thomas if (commit)
6229 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6230 eb41ed75 2019-02-05 stsp if (worktree)
6231 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6232 1d0f4054 2021-06-17 stsp if (repo) {
6233 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6234 1d0f4054 2021-06-17 stsp if (error == NULL)
6235 1d0f4054 2021-06-17 stsp error = close_err;
6237 7cd52833 2022-06-23 thomas if (pack_fds) {
6238 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6239 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6240 7cd52833 2022-06-23 thomas if (error == NULL)
6241 7cd52833 2022-06-23 thomas error = pack_err;
6243 51a10b52 2020-12-26 stsp tog_free_refs();
6244 a70480e0 2018-06-23 stsp return error;
6247 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6248 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6250 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6251 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6252 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6253 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6254 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6255 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6256 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6258 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6259 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6260 a5d43cac 2022-07-01 thomas --limit; /* border */
6262 f7d12f7e 2018-08-01 stsp werase(view->window);
6264 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6265 ffd1d5e5 2018-06-23 stsp return NULL;
6267 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6270 ffd1d5e5 2018-06-23 stsp return err;
6271 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6272 a3404814 2018-09-02 stsp wstandout(view->window);
6273 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6275 11b20872 2019-11-08 stsp wattr_on(view->window,
6276 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6277 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6279 11b20872 2019-11-08 stsp wattr_off(view->window,
6280 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6281 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6282 a3404814 2018-09-02 stsp wstandend(view->window);
6283 2550e4c3 2018-07-13 stsp free(wline);
6284 2550e4c3 2018-07-13 stsp wline = NULL;
6285 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6286 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6287 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6288 ffd1d5e5 2018-06-23 stsp return NULL;
6289 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6292 ce52c690 2018-06-23 stsp return err;
6293 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6294 2550e4c3 2018-07-13 stsp free(wline);
6295 2550e4c3 2018-07-13 stsp wline = NULL;
6296 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6297 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6298 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6299 ffd1d5e5 2018-06-23 stsp return NULL;
6300 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6301 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6302 a1eca9bb 2018-06-23 stsp return NULL;
6304 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6305 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6306 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6307 0cf4efb1 2018-09-29 stsp if (view->focussed)
6308 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6309 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6311 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6312 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6313 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6314 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6315 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6316 ffd1d5e5 2018-06-23 stsp return NULL;
6320 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6323 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6324 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6325 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6326 848d6979 2019-08-12 stsp const char *modestr = "";
6327 56e0773d 2019-11-28 stsp mode_t mode;
6329 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6330 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6332 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6333 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6334 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6336 638f9024 2019-05-13 stsp return got_error_from_errno(
6337 230a42bd 2019-05-11 jcs "got_object_id_str");
6339 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6340 63c5ca5d 2019-08-24 stsp modestr = "$";
6341 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6344 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6345 d86d3b18 2020-12-01 naddy te, s->repo);
6346 0d6c6ee3 2020-05-20 stsp if (err) {
6347 0d6c6ee3 2020-05-20 stsp free(id_str);
6348 0d6c6ee3 2020-05-20 stsp return err;
6350 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6351 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6352 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6354 848d6979 2019-08-12 stsp modestr = "@";
6356 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6357 848d6979 2019-08-12 stsp modestr = "/";
6358 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6359 848d6979 2019-08-12 stsp modestr = "*";
6360 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6361 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6362 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6363 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6364 1d13200f 2018-07-12 stsp free(id_str);
6365 0d6c6ee3 2020-05-20 stsp free(link_target);
6366 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6368 1d13200f 2018-07-12 stsp free(id_str);
6369 0d6c6ee3 2020-05-20 stsp free(link_target);
6370 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6372 ffd1d5e5 2018-06-23 stsp if (err) {
6373 ffd1d5e5 2018-06-23 stsp free(line);
6376 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6377 0cf4efb1 2018-09-29 stsp if (view->focussed)
6378 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6379 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6381 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6383 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6384 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6385 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6387 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6388 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6389 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6390 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6391 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6392 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6393 ffd1d5e5 2018-06-23 stsp free(line);
6394 2550e4c3 2018-07-13 stsp free(wline);
6395 2550e4c3 2018-07-13 stsp wline = NULL;
6397 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6398 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6399 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6403 ffd1d5e5 2018-06-23 stsp return err;
6406 ffd1d5e5 2018-06-23 stsp static void
6407 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6409 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6410 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6411 fa86c4bf 2020-11-29 stsp int i = 0;
6413 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6416 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6417 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6418 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6419 fa86c4bf 2020-11-29 stsp if (!isroot)
6420 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6423 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6424 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6428 a5d43cac 2022-07-01 thomas static const struct got_error *
6429 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6431 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6432 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6433 ffd1d5e5 2018-06-23 stsp int n = 0;
6435 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6436 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6437 694d3271 2020-12-01 naddy s->first_displayed_entry);
6439 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6441 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6442 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6443 a5d43cac 2022-07-01 thomas if (last)
6444 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6445 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6446 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6447 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6451 a5d43cac 2022-07-01 thomas return NULL;
6454 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6455 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6456 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6458 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6459 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6460 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6462 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6463 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6464 56e0773d 2019-11-28 stsp + 1 /* slash */;
6466 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6468 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6469 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6470 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6472 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6473 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6474 d9765a41 2018-06-23 stsp while (pt) {
6475 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6476 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6477 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6478 cb2ebc8a 2018-06-23 stsp goto done;
6480 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6481 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6482 cb2ebc8a 2018-06-23 stsp goto done;
6484 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6487 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6488 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6489 ce52c690 2018-06-23 stsp goto done;
6493 ce52c690 2018-06-23 stsp if (err) {
6494 ce52c690 2018-06-23 stsp free(*path);
6495 ce52c690 2018-06-23 stsp *path = NULL;
6497 ce52c690 2018-06-23 stsp return err;
6500 ce52c690 2018-06-23 stsp static const struct got_error *
6501 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6502 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6503 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6505 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6506 ce52c690 2018-06-23 stsp char *path;
6507 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6509 a0de39f3 2019-08-09 stsp *new_view = NULL;
6511 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6513 ce52c690 2018-06-23 stsp return err;
6515 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6516 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6517 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6518 83ce39e3 2019-08-12 stsp goto done;
6521 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6522 e5a0f69f 2018-08-18 stsp if (err) {
6523 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6524 fc06ba56 2019-08-22 stsp err = NULL;
6525 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6527 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6529 83ce39e3 2019-08-12 stsp free(path);
6530 69efd4c4 2018-07-18 stsp return err;
6533 69efd4c4 2018-07-18 stsp static const struct got_error *
6534 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6535 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6537 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6538 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6539 69efd4c4 2018-07-18 stsp char *path;
6541 a0de39f3 2019-08-09 stsp *new_view = NULL;
6543 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6544 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6545 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6547 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6549 69efd4c4 2018-07-18 stsp return err;
6551 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6554 e5a0f69f 2018-08-18 stsp view_close(log_view);
6556 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6557 cb2ebc8a 2018-06-23 stsp free(path);
6558 cb2ebc8a 2018-06-23 stsp return err;
6561 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6562 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6563 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6565 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6566 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6567 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6568 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6570 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6571 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6573 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6574 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6575 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6577 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6579 bc573f3b 2021-07-10 stsp goto done;
6582 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6583 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6584 bc573f3b 2021-07-10 stsp * closed on demand.
6586 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6587 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6589 bc573f3b 2021-07-10 stsp goto done;
6590 bc573f3b 2021-07-10 stsp s->tree = s->root;
6592 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6593 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6594 ffd1d5e5 2018-06-23 stsp goto done;
6596 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6597 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6598 ffd1d5e5 2018-06-23 stsp goto done;
6601 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6602 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6603 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6604 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6605 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6606 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6607 9cd7cbd1 2020-12-07 stsp goto done;
6610 fb2756b9 2018-08-04 stsp s->repo = repo;
6612 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6613 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6614 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6615 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6617 c0b01bdb 2019-11-08 stsp goto done;
6618 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6619 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6621 c0b01bdb 2019-11-08 stsp goto done;
6622 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6623 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6624 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6626 c0b01bdb 2019-11-08 stsp goto done;
6628 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6629 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6630 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6632 11b20872 2019-11-08 stsp goto done;
6634 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6635 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6637 c0b01bdb 2019-11-08 stsp goto done;
6640 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6641 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6642 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6643 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6644 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6646 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6647 bc573f3b 2021-07-10 stsp if (commit)
6648 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6650 bc573f3b 2021-07-10 stsp close_tree_view(view);
6651 ad80ab7b 2018-08-04 stsp return err;
6654 e5a0f69f 2018-08-18 stsp static const struct got_error *
6655 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6657 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6659 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6660 fb2756b9 2018-08-04 stsp free(s->tree_label);
6661 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6662 6484ec90 2018-09-29 stsp free(s->commit_id);
6663 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6664 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6665 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6666 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6667 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6668 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6669 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6670 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6671 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6672 ad80ab7b 2018-08-04 stsp free(parent);
6675 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6676 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6677 bc573f3b 2021-07-10 stsp if (s->root)
6678 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6679 7c32bd05 2019-06-22 stsp return NULL;
6682 7c32bd05 2019-06-22 stsp static const struct got_error *
6683 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6685 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6687 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6688 7c32bd05 2019-06-22 stsp return NULL;
6691 7c32bd05 2019-06-22 stsp static int
6692 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6694 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6696 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6700 7c32bd05 2019-06-22 stsp static const struct got_error *
6701 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6703 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6704 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6706 7c32bd05 2019-06-22 stsp if (!view->searching) {
6707 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6708 7c32bd05 2019-06-22 stsp return NULL;
6711 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6712 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6713 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6714 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6715 56e0773d 2019-11-28 stsp s->selected_entry);
6717 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6719 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6720 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6722 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6723 56e0773d 2019-11-28 stsp s->selected_entry);
6726 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6727 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6728 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6729 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6731 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6734 7c32bd05 2019-06-22 stsp while (1) {
6735 56e0773d 2019-11-28 stsp if (te == NULL) {
6736 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6737 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6738 ac66afb8 2019-06-24 stsp return NULL;
6740 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6741 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6743 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6746 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6747 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6748 56e0773d 2019-11-28 stsp s->matched_entry = te;
6752 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6753 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6755 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6758 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6759 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6760 7c32bd05 2019-06-22 stsp s->selected = 0;
6763 e5a0f69f 2018-08-18 stsp return NULL;
6766 ad80ab7b 2018-08-04 stsp static const struct got_error *
6767 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6769 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6770 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6771 e5a0f69f 2018-08-18 stsp char *parent_path;
6773 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6775 e5a0f69f 2018-08-18 stsp return err;
6777 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6778 e5a0f69f 2018-08-18 stsp free(parent_path);
6780 a5d43cac 2022-07-01 thomas view_border(view);
6781 e5a0f69f 2018-08-18 stsp return err;
6784 e5a0f69f 2018-08-18 stsp static const struct got_error *
6785 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6787 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6788 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6789 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6790 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6791 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6793 e5a0f69f 2018-08-18 stsp switch (ch) {
6795 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6796 07b0611c 2022-06-23 thomas view->count = 0;
6799 07b0611c 2022-06-23 thomas view->count = 0;
6800 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6802 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6803 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6804 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6805 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6806 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6807 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6808 444d5325 2022-07-03 thomas if (err)
6811 e78dc838 2020-12-04 stsp view->focussed = 0;
6812 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6813 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6814 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6815 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6816 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6817 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6819 1e37a5c2 2019-05-12 jcs return err;
6820 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6821 40236d76 2022-06-23 thomas if (err)
6822 40236d76 2022-06-23 thomas return err;
6823 e78dc838 2020-12-04 stsp view->focus_child = 1;
6825 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6828 07b0611c 2022-06-23 thomas view->count = 0;
6829 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6830 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6831 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6832 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6833 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6834 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6835 152c1c93 2020-11-29 stsp if (err) {
6836 152c1c93 2020-11-29 stsp view_close(ref_view);
6837 152c1c93 2020-11-29 stsp return err;
6839 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6840 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6841 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6842 444d5325 2022-07-03 thomas if (err)
6845 e78dc838 2020-12-04 stsp view->focussed = 0;
6846 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6847 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6848 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6849 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6850 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6851 152c1c93 2020-11-29 stsp err = view_close_child(view);
6853 152c1c93 2020-11-29 stsp return err;
6854 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6855 40236d76 2022-06-23 thomas if (err)
6856 40236d76 2022-06-23 thomas return err;
6857 e78dc838 2020-12-04 stsp view->focus_child = 1;
6859 152c1c93 2020-11-29 stsp *new_view = ref_view;
6861 e4526bf5 2021-09-03 naddy case 'g':
6862 e4526bf5 2021-09-03 naddy case KEY_HOME:
6863 e4526bf5 2021-09-03 naddy s->selected = 0;
6864 07b0611c 2022-06-23 thomas view->count = 0;
6865 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6866 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6867 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6869 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6871 e4526bf5 2021-09-03 naddy case 'G':
6872 a5d43cac 2022-07-01 thomas case KEY_END: {
6873 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6875 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6876 a5d43cac 2022-07-01 thomas --eos; /* border */
6877 e4526bf5 2021-09-03 naddy s->selected = 0;
6878 07b0611c 2022-06-23 thomas view->count = 0;
6879 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6880 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6881 e4526bf5 2021-09-03 naddy if (te == NULL) {
6882 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6883 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6888 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6889 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6891 e4526bf5 2021-09-03 naddy if (n > 0)
6892 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6896 1e37a5c2 2019-05-12 jcs case KEY_UP:
6897 f7140bf5 2021-10-17 thomas case CTRL('p'):
6898 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6899 1e37a5c2 2019-05-12 jcs s->selected--;
6902 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6903 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6904 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6905 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6906 07b0611c 2022-06-23 thomas view->count = 0;
6908 70f17a53 2022-06-13 thomas case CTRL('u'):
6909 23427b14 2022-06-23 thomas case 'u':
6910 70f17a53 2022-06-13 thomas nscroll /= 2;
6911 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6912 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6913 ea025d1d 2020-02-22 naddy case CTRL('b'):
6914 1c5e5faa 2022-06-23 thomas case 'b':
6915 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6916 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6917 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6918 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6920 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6921 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6923 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6924 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6925 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6926 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6927 07b0611c 2022-06-23 thomas view->count = 0;
6930 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6931 f7140bf5 2021-10-17 thomas case CTRL('n'):
6932 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6933 1e37a5c2 2019-05-12 jcs s->selected++;
6936 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6937 07b0611c 2022-06-23 thomas == NULL) {
6938 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6939 07b0611c 2022-06-23 thomas view->count = 0;
6942 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6944 70f17a53 2022-06-13 thomas case CTRL('d'):
6945 23427b14 2022-06-23 thomas case 'd':
6946 70f17a53 2022-06-13 thomas nscroll /= 2;
6947 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6948 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6949 ea025d1d 2020-02-22 naddy case CTRL('f'):
6950 1c5e5faa 2022-06-23 thomas case 'f':
6951 4c2d69cb 2022-06-23 thomas case ' ':
6952 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6954 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6955 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6956 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6957 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6959 07b0611c 2022-06-23 thomas view->count = 0;
6962 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6964 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6966 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6967 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6968 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6969 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6970 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6971 07b0611c 2022-06-23 thomas view->count = 0;
6974 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6975 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6977 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6978 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6979 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6980 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6981 1e37a5c2 2019-05-12 jcs s->selected_entry =
6982 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6983 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6984 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6985 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6986 a5d43cac 2022-07-01 thomas if (err)
6989 1e37a5c2 2019-05-12 jcs free(parent);
6990 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6991 56e0773d 2019-11-28 stsp s->selected_entry))) {
6992 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6993 07b0611c 2022-06-23 thomas view->count = 0;
6994 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6995 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6998 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6999 941e9f74 2019-05-21 stsp if (err) {
7000 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7003 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7004 56e0773d 2019-11-28 stsp s->selected_entry))) {
7005 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7006 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
7008 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
7009 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7011 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
7012 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7013 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7017 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7018 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7019 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7020 a5d43cac 2022-07-01 thomas if (err)
7024 07b0611c 2022-06-23 thomas view->count = 0;
7025 e78dc838 2020-12-04 stsp view->focussed = 0;
7026 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7027 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
7028 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
7029 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7030 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
7031 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7033 669b5ffa 2018-10-07 stsp return err;
7034 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
7035 40236d76 2022-06-23 thomas if (err)
7036 40236d76 2022-06-23 thomas return err;
7037 e78dc838 2020-12-04 stsp view->focus_child = 1;
7039 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7042 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7043 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7044 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7045 07b0611c 2022-06-23 thomas view->count = 0;
7048 07b0611c 2022-06-23 thomas view->count = 0;
7052 ffd1d5e5 2018-06-23 stsp return err;
7055 ffd1d5e5 2018-06-23 stsp __dead static void
7056 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7059 91198554 2022-06-23 thomas fprintf(stderr,
7060 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7061 ffd1d5e5 2018-06-23 stsp getprogname());
7065 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7066 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7068 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7069 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7070 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7071 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7072 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7073 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7074 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7075 4e97c21c 2020-12-06 stsp char *label = NULL;
7076 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7077 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7079 5221c383 2018-08-01 stsp struct tog_view *view;
7080 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7082 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7083 ffd1d5e5 2018-06-23 stsp switch (ch) {
7085 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7088 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7089 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7090 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7094 e99e2d15 2020-11-24 naddy usage_tree();
7095 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7099 ffd1d5e5 2018-06-23 stsp argc -= optind;
7100 ffd1d5e5 2018-06-23 stsp argv += optind;
7102 55cccc34 2020-02-20 stsp if (argc > 1)
7103 e99e2d15 2020-11-24 naddy usage_tree();
7105 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7106 7cd52833 2022-06-23 thomas if (error != NULL)
7107 7cd52833 2022-06-23 thomas goto done;
7109 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7110 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7111 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7112 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7113 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7114 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7115 c156c7a4 2020-12-18 stsp goto done;
7116 55cccc34 2020-02-20 stsp if (worktree)
7117 52185f70 2019-02-05 stsp repo_path =
7118 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7120 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7121 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7122 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7123 c156c7a4 2020-12-18 stsp goto done;
7127 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7128 c02c541e 2019-03-29 stsp if (error != NULL)
7129 52185f70 2019-02-05 stsp goto done;
7131 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7132 55cccc34 2020-02-20 stsp repo, worktree);
7133 55cccc34 2020-02-20 stsp if (error)
7134 55cccc34 2020-02-20 stsp goto done;
7136 55cccc34 2020-02-20 stsp init_curses();
7138 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7139 c02c541e 2019-03-29 stsp if (error)
7140 52185f70 2019-02-05 stsp goto done;
7142 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7143 51a10b52 2020-12-26 stsp if (error)
7144 51a10b52 2020-12-26 stsp goto done;
7146 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7147 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7148 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7149 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7150 4e97c21c 2020-12-06 stsp if (error)
7151 4e97c21c 2020-12-06 stsp goto done;
7152 4e97c21c 2020-12-06 stsp head_ref_name = label;
7154 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7155 4e97c21c 2020-12-06 stsp if (error == NULL)
7156 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7157 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7158 4e97c21c 2020-12-06 stsp goto done;
7159 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7160 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7161 4e97c21c 2020-12-06 stsp if (error)
7162 4e97c21c 2020-12-06 stsp goto done;
7165 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7166 945f9229 2022-04-16 thomas if (error)
7167 945f9229 2022-04-16 thomas goto done;
7169 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7170 5221c383 2018-08-01 stsp if (view == NULL) {
7171 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7172 5221c383 2018-08-01 stsp goto done;
7174 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7175 ad80ab7b 2018-08-04 stsp if (error)
7176 ad80ab7b 2018-08-04 stsp goto done;
7177 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7178 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7179 d91faf3b 2020-12-01 naddy in_repo_path);
7180 55cccc34 2020-02-20 stsp if (error)
7181 55cccc34 2020-02-20 stsp goto done;
7184 55cccc34 2020-02-20 stsp if (worktree) {
7185 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7186 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7187 55cccc34 2020-02-20 stsp worktree = NULL;
7189 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7191 52185f70 2019-02-05 stsp free(repo_path);
7192 e4a0e26d 2020-02-20 stsp free(cwd);
7193 ffd1d5e5 2018-06-23 stsp free(commit_id);
7194 4e97c21c 2020-12-06 stsp free(label);
7196 486cd271 2020-12-06 stsp got_ref_close(ref);
7197 1d0f4054 2021-06-17 stsp if (repo) {
7198 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7199 1d0f4054 2021-06-17 stsp if (error == NULL)
7200 1d0f4054 2021-06-17 stsp error = close_err;
7202 7cd52833 2022-06-23 thomas if (pack_fds) {
7203 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7204 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7205 7cd52833 2022-06-23 thomas if (error == NULL)
7206 7cd52833 2022-06-23 thomas error = pack_err;
7208 51a10b52 2020-12-26 stsp tog_free_refs();
7209 ffd1d5e5 2018-06-23 stsp return error;
7212 6458efa5 2020-11-24 stsp static const struct got_error *
7213 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7215 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7216 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7218 6458efa5 2020-11-24 stsp s->nrefs = 0;
7219 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7220 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7221 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7222 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7223 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7226 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7227 6458efa5 2020-11-24 stsp if (re == NULL)
7228 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7230 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7231 8924d611 2020-12-26 stsp if (re->ref == NULL)
7232 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7233 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7234 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7237 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7238 6458efa5 2020-11-24 stsp return NULL;
7241 ef20f542 2022-06-26 thomas static void
7242 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7244 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7246 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7247 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7248 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7249 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7254 6458efa5 2020-11-24 stsp static const struct got_error *
7255 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7257 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7258 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7260 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7261 6458efa5 2020-11-24 stsp s->repo = repo;
7263 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7264 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7266 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7268 6458efa5 2020-11-24 stsp return err;
7270 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7271 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7272 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7273 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7275 6458efa5 2020-11-24 stsp goto done;
7277 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7278 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7279 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7281 6458efa5 2020-11-24 stsp goto done;
7283 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7284 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7285 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7286 2183bbf6 2022-01-23 thomas if (err)
7287 2183bbf6 2022-01-23 thomas goto done;
7289 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7290 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7291 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7293 6458efa5 2020-11-24 stsp goto done;
7296 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7297 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7298 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7299 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7300 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7303 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7304 6458efa5 2020-11-24 stsp return err;
7307 6458efa5 2020-11-24 stsp static const struct got_error *
7308 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7310 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7312 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7313 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7315 6458efa5 2020-11-24 stsp return NULL;
7318 6458efa5 2020-11-24 stsp static const struct got_error *
7319 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7320 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7322 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7323 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7324 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7325 6458efa5 2020-11-24 stsp int obj_type;
7327 c42c9805 2020-11-24 stsp *commit_id = NULL;
7329 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7331 6458efa5 2020-11-24 stsp return err;
7333 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7335 6458efa5 2020-11-24 stsp goto done;
7337 6458efa5 2020-11-24 stsp switch (obj_type) {
7338 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7339 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7341 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7342 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7344 6458efa5 2020-11-24 stsp goto done;
7345 c42c9805 2020-11-24 stsp free(obj_id);
7346 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7347 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7349 6458efa5 2020-11-24 stsp goto done;
7350 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7351 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7352 c42c9805 2020-11-24 stsp goto done;
7354 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7355 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7356 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7357 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7358 c42c9805 2020-11-24 stsp goto done;
7362 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7368 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7369 c42c9805 2020-11-24 stsp if (err) {
7370 c42c9805 2020-11-24 stsp free(*commit_id);
7371 c42c9805 2020-11-24 stsp *commit_id = NULL;
7373 c42c9805 2020-11-24 stsp return err;
7376 c42c9805 2020-11-24 stsp static const struct got_error *
7377 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7378 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7380 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7381 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7382 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7384 c42c9805 2020-11-24 stsp *new_view = NULL;
7386 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7387 c42c9805 2020-11-24 stsp if (err) {
7388 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7389 c42c9805 2020-11-24 stsp return err;
7391 c42c9805 2020-11-24 stsp return NULL;
7394 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7395 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7396 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7397 6458efa5 2020-11-24 stsp goto done;
7400 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7401 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7404 6458efa5 2020-11-24 stsp view_close(log_view);
7406 6458efa5 2020-11-24 stsp *new_view = log_view;
7407 c42c9805 2020-11-24 stsp free(commit_id);
7408 6458efa5 2020-11-24 stsp return err;
7411 ce5b7c56 2019-07-09 stsp static void
7412 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7414 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7415 34ba6917 2020-11-30 stsp int i = 0;
7417 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7420 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7421 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7422 34ba6917 2020-11-30 stsp if (re == NULL)
7424 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7425 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7429 a5d43cac 2022-07-01 thomas static const struct got_error *
7430 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7432 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7433 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7434 6458efa5 2020-11-24 stsp int n = 0;
7436 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7437 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7439 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7441 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7442 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7443 a5d43cac 2022-07-01 thomas if (last)
7444 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7445 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7446 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7447 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7451 a5d43cac 2022-07-01 thomas return NULL;
7454 6458efa5 2020-11-24 stsp static const struct got_error *
7455 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7457 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7459 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7460 6458efa5 2020-11-24 stsp return NULL;
7463 6458efa5 2020-11-24 stsp static int
7464 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7466 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7468 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7472 6458efa5 2020-11-24 stsp static const struct got_error *
7473 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7475 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7476 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7478 6458efa5 2020-11-24 stsp if (!view->searching) {
7479 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7480 6458efa5 2020-11-24 stsp return NULL;
7483 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7484 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7485 6458efa5 2020-11-24 stsp if (s->selected_entry)
7486 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7488 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7489 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7491 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7492 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7494 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7495 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7498 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7499 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7500 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7501 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7503 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7506 6458efa5 2020-11-24 stsp while (1) {
7507 6458efa5 2020-11-24 stsp if (re == NULL) {
7508 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7509 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7510 6458efa5 2020-11-24 stsp return NULL;
7512 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7513 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7515 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7518 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7519 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7520 6458efa5 2020-11-24 stsp s->matched_entry = re;
7524 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7525 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7527 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7530 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7531 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7532 6458efa5 2020-11-24 stsp s->selected = 0;
7535 6458efa5 2020-11-24 stsp return NULL;
7538 6458efa5 2020-11-24 stsp static const struct got_error *
7539 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7541 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7542 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7543 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7544 6458efa5 2020-11-24 stsp char *line = NULL;
7545 6458efa5 2020-11-24 stsp wchar_t *wline;
7546 6458efa5 2020-11-24 stsp struct tog_color *tc;
7547 6458efa5 2020-11-24 stsp int width, n;
7548 6458efa5 2020-11-24 stsp int limit = view->nlines;
7550 6458efa5 2020-11-24 stsp werase(view->window);
7552 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7553 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7554 a5d43cac 2022-07-01 thomas --limit; /* border */
7556 6458efa5 2020-11-24 stsp if (limit == 0)
7557 6458efa5 2020-11-24 stsp return NULL;
7559 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7561 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7562 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7563 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7565 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7566 6458efa5 2020-11-24 stsp if (err) {
7567 6458efa5 2020-11-24 stsp free(line);
7568 6458efa5 2020-11-24 stsp return err;
7570 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7571 6458efa5 2020-11-24 stsp wstandout(view->window);
7572 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7573 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7574 6458efa5 2020-11-24 stsp wstandend(view->window);
7575 6458efa5 2020-11-24 stsp free(wline);
7576 6458efa5 2020-11-24 stsp wline = NULL;
7577 6458efa5 2020-11-24 stsp free(line);
7578 6458efa5 2020-11-24 stsp line = NULL;
7579 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7580 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7581 6458efa5 2020-11-24 stsp if (--limit <= 0)
7582 6458efa5 2020-11-24 stsp return NULL;
7585 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7586 6458efa5 2020-11-24 stsp char *line = NULL;
7587 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7589 84227eb1 2022-06-23 thomas if (s->show_date) {
7590 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7591 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7592 84227eb1 2022-06-23 thomas struct got_object_id *id;
7593 84227eb1 2022-06-23 thomas struct tm tm;
7594 84227eb1 2022-06-23 thomas time_t t;
7596 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7597 84227eb1 2022-06-23 thomas if (err)
7598 84227eb1 2022-06-23 thomas return err;
7599 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7600 84227eb1 2022-06-23 thomas if (err) {
7601 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7602 84227eb1 2022-06-23 thomas free(id);
7603 84227eb1 2022-06-23 thomas return err;
7605 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7607 84227eb1 2022-06-23 thomas if (err) {
7608 84227eb1 2022-06-23 thomas free(id);
7609 84227eb1 2022-06-23 thomas return err;
7611 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7612 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7613 84227eb1 2022-06-23 thomas } else {
7614 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7615 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7617 84227eb1 2022-06-23 thomas free(id);
7618 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7619 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7620 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7621 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7623 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7624 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7625 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7626 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7627 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7628 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7629 6458efa5 2020-11-24 stsp struct got_object_id *id;
7630 6458efa5 2020-11-24 stsp char *id_str;
7631 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7633 6458efa5 2020-11-24 stsp return err;
7634 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7635 6458efa5 2020-11-24 stsp if (err) {
7637 6458efa5 2020-11-24 stsp return err;
7639 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7640 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7641 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7643 6458efa5 2020-11-24 stsp free(id_str);
7644 6458efa5 2020-11-24 stsp return err;
7647 6458efa5 2020-11-24 stsp free(id_str);
7648 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7649 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7650 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7652 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7654 6458efa5 2020-11-24 stsp if (err) {
7655 6458efa5 2020-11-24 stsp free(line);
7656 6458efa5 2020-11-24 stsp return err;
7658 6458efa5 2020-11-24 stsp if (n == s->selected) {
7659 6458efa5 2020-11-24 stsp if (view->focussed)
7660 6458efa5 2020-11-24 stsp wstandout(view->window);
7661 6458efa5 2020-11-24 stsp s->selected_entry = re;
7663 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7665 6458efa5 2020-11-24 stsp wattr_on(view->window,
7666 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7667 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7669 6458efa5 2020-11-24 stsp wattr_off(view->window,
7670 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7671 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7672 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7673 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7674 6458efa5 2020-11-24 stsp wstandend(view->window);
7675 6458efa5 2020-11-24 stsp free(line);
7676 6458efa5 2020-11-24 stsp free(wline);
7677 6458efa5 2020-11-24 stsp wline = NULL;
7679 6458efa5 2020-11-24 stsp s->ndisplayed++;
7680 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7683 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7686 a5d43cac 2022-07-01 thomas view_border(view);
7687 6458efa5 2020-11-24 stsp return err;
7690 6458efa5 2020-11-24 stsp static const struct got_error *
7691 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7692 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7694 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7695 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7696 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7698 c42c9805 2020-11-24 stsp *new_view = NULL;
7700 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7701 c42c9805 2020-11-24 stsp if (err) {
7702 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7703 c42c9805 2020-11-24 stsp return err;
7705 c42c9805 2020-11-24 stsp return NULL;
7709 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7710 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7711 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7712 c42c9805 2020-11-24 stsp goto done;
7715 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7716 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7718 c42c9805 2020-11-24 stsp goto done;
7720 c42c9805 2020-11-24 stsp *new_view = tree_view;
7722 c42c9805 2020-11-24 stsp free(commit_id);
7723 c42c9805 2020-11-24 stsp return err;
7725 c42c9805 2020-11-24 stsp static const struct got_error *
7726 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7728 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7729 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7730 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7731 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7732 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7734 6458efa5 2020-11-24 stsp switch (ch) {
7736 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7737 07b0611c 2022-06-23 thomas view->count = 0;
7739 84227eb1 2022-06-23 thomas case 'm':
7740 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7741 07b0611c 2022-06-23 thomas view->count = 0;
7743 98182bd0 2021-11-20 thomas case 'o':
7744 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7745 07b0611c 2022-06-23 thomas view->count = 0;
7746 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7747 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7748 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7749 c591440f 2021-11-20 thomas if (err)
7751 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7752 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7753 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7754 3bfadbd4 2021-11-20 thomas if (err)
7756 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7757 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7759 6458efa5 2020-11-24 stsp case KEY_ENTER:
7760 6458efa5 2020-11-24 stsp case '\r':
7761 07b0611c 2022-06-23 thomas view->count = 0;
7762 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7764 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7765 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7767 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7768 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7769 a5d43cac 2022-07-01 thomas if (err)
7772 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7773 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7774 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7775 a5d43cac 2022-07-01 thomas if (err)
7779 e78dc838 2020-12-04 stsp view->focussed = 0;
7780 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7781 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7782 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7783 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7784 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7785 6458efa5 2020-11-24 stsp err = view_close_child(view);
7787 6458efa5 2020-11-24 stsp return err;
7788 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7789 40236d76 2022-06-23 thomas if (err)
7790 40236d76 2022-06-23 thomas return err;
7791 e78dc838 2020-12-04 stsp view->focus_child = 1;
7793 6458efa5 2020-11-24 stsp *new_view = log_view;
7796 07b0611c 2022-06-23 thomas view->count = 0;
7797 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7799 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7800 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7801 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7802 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7803 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7805 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7806 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7807 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7808 444d5325 2022-07-03 thomas if (err)
7811 e78dc838 2020-12-04 stsp view->focussed = 0;
7812 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7813 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7814 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7815 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7816 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7817 c42c9805 2020-11-24 stsp err = view_close_child(view);
7819 c42c9805 2020-11-24 stsp return err;
7820 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7821 40236d76 2022-06-23 thomas if (err)
7822 40236d76 2022-06-23 thomas return err;
7823 e78dc838 2020-12-04 stsp view->focus_child = 1;
7825 c42c9805 2020-11-24 stsp *new_view = tree_view;
7827 e4526bf5 2021-09-03 naddy case 'g':
7828 e4526bf5 2021-09-03 naddy case KEY_HOME:
7829 e4526bf5 2021-09-03 naddy s->selected = 0;
7830 07b0611c 2022-06-23 thomas view->count = 0;
7831 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7833 e4526bf5 2021-09-03 naddy case 'G':
7834 a5d43cac 2022-07-01 thomas case KEY_END: {
7835 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7837 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7838 a5d43cac 2022-07-01 thomas --eos; /* border */
7839 e4526bf5 2021-09-03 naddy s->selected = 0;
7840 07b0611c 2022-06-23 thomas view->count = 0;
7841 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7842 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7843 e4526bf5 2021-09-03 naddy if (re == NULL)
7845 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7846 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7848 e4526bf5 2021-09-03 naddy if (n > 0)
7849 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7853 6458efa5 2020-11-24 stsp case KEY_UP:
7854 f7140bf5 2021-10-17 thomas case CTRL('p'):
7855 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7856 6458efa5 2020-11-24 stsp s->selected--;
7859 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7860 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7861 07b0611c 2022-06-23 thomas view->count = 0;
7863 70f17a53 2022-06-13 thomas case CTRL('u'):
7864 23427b14 2022-06-23 thomas case 'u':
7865 70f17a53 2022-06-13 thomas nscroll /= 2;
7866 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7867 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7868 6458efa5 2020-11-24 stsp case CTRL('b'):
7869 1c5e5faa 2022-06-23 thomas case 'b':
7870 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7871 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7872 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7873 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7874 07b0611c 2022-06-23 thomas view->count = 0;
7877 6458efa5 2020-11-24 stsp case KEY_DOWN:
7878 f7140bf5 2021-10-17 thomas case CTRL('n'):
7879 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7880 6458efa5 2020-11-24 stsp s->selected++;
7883 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7884 6458efa5 2020-11-24 stsp /* can't scroll any further */
7885 07b0611c 2022-06-23 thomas view->count = 0;
7888 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7890 70f17a53 2022-06-13 thomas case CTRL('d'):
7891 23427b14 2022-06-23 thomas case 'd':
7892 70f17a53 2022-06-13 thomas nscroll /= 2;
7893 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7894 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7895 6458efa5 2020-11-24 stsp case CTRL('f'):
7896 1c5e5faa 2022-06-23 thomas case 'f':
7897 4c2d69cb 2022-06-23 thomas case ' ':
7898 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7899 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7900 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7901 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7902 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7903 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7904 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7905 07b0611c 2022-06-23 thomas view->count = 0;
7908 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7910 6458efa5 2020-11-24 stsp case CTRL('l'):
7911 07b0611c 2022-06-23 thomas view->count = 0;
7912 8924d611 2020-12-26 stsp tog_free_refs();
7913 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7916 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7917 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7919 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7920 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7921 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7924 07b0611c 2022-06-23 thomas view->count = 0;
7928 6458efa5 2020-11-24 stsp return err;
7931 6458efa5 2020-11-24 stsp __dead static void
7932 6458efa5 2020-11-24 stsp usage_ref(void)
7935 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7936 6458efa5 2020-11-24 stsp getprogname());
7940 6458efa5 2020-11-24 stsp static const struct got_error *
7941 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7943 6458efa5 2020-11-24 stsp const struct got_error *error;
7944 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7945 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7946 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7948 6458efa5 2020-11-24 stsp struct tog_view *view;
7949 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7951 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7952 6458efa5 2020-11-24 stsp switch (ch) {
7954 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7955 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7956 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7960 e99e2d15 2020-11-24 naddy usage_ref();
7961 6458efa5 2020-11-24 stsp /* NOTREACHED */
7965 6458efa5 2020-11-24 stsp argc -= optind;
7966 6458efa5 2020-11-24 stsp argv += optind;
7968 6458efa5 2020-11-24 stsp if (argc > 1)
7969 e99e2d15 2020-11-24 naddy usage_ref();
7971 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7972 7cd52833 2022-06-23 thomas if (error != NULL)
7973 7cd52833 2022-06-23 thomas goto done;
7975 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7976 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7977 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7978 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7979 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7980 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7981 c156c7a4 2020-12-18 stsp goto done;
7982 6458efa5 2020-11-24 stsp if (worktree)
7983 6458efa5 2020-11-24 stsp repo_path =
7984 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7986 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7987 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7988 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7989 c156c7a4 2020-12-18 stsp goto done;
7993 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7994 6458efa5 2020-11-24 stsp if (error != NULL)
7995 6458efa5 2020-11-24 stsp goto done;
7997 6458efa5 2020-11-24 stsp init_curses();
7999 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8000 51a10b52 2020-12-26 stsp if (error)
8001 51a10b52 2020-12-26 stsp goto done;
8003 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8004 6458efa5 2020-11-24 stsp if (error)
8005 6458efa5 2020-11-24 stsp goto done;
8007 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8008 6458efa5 2020-11-24 stsp if (view == NULL) {
8009 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8010 6458efa5 2020-11-24 stsp goto done;
8013 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8014 6458efa5 2020-11-24 stsp if (error)
8015 6458efa5 2020-11-24 stsp goto done;
8017 6458efa5 2020-11-24 stsp if (worktree) {
8018 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8019 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8020 6458efa5 2020-11-24 stsp worktree = NULL;
8022 6458efa5 2020-11-24 stsp error = view_loop(view);
8024 6458efa5 2020-11-24 stsp free(repo_path);
8025 6458efa5 2020-11-24 stsp free(cwd);
8026 1d0f4054 2021-06-17 stsp if (repo) {
8027 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8028 1d0f4054 2021-06-17 stsp if (close_err)
8029 1d0f4054 2021-06-17 stsp error = close_err;
8031 7cd52833 2022-06-23 thomas if (pack_fds) {
8032 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8033 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8034 7cd52833 2022-06-23 thomas if (error == NULL)
8035 7cd52833 2022-06-23 thomas error = pack_err;
8037 51a10b52 2020-12-26 stsp tog_free_refs();
8038 6458efa5 2020-11-24 stsp return error;
8042 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8043 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8045 6458efa5 2020-11-24 stsp static void
8046 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8048 a5d43cac 2022-07-01 thomas switch (view->type) {
8049 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8050 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8051 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8052 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8056 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8057 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8059 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8060 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8063 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8064 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8065 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8067 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8068 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8069 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8071 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8072 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8073 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8075 a5d43cac 2022-07-01 thomas default:
8079 a5d43cac 2022-07-01 thomas view->offset = 0;
8083 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8084 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8086 a5d43cac 2022-07-01 thomas static const struct got_error *
8087 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8089 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8090 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8091 a5d43cac 2022-07-01 thomas int *selected = NULL;
8092 a5d43cac 2022-07-01 thomas int header, offset;
8094 a5d43cac 2022-07-01 thomas switch (view->type) {
8095 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8096 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8097 a5d43cac 2022-07-01 thomas header = 3;
8098 a5d43cac 2022-07-01 thomas scrolld = NULL;
8099 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8100 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8101 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8102 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8103 a5d43cac 2022-07-01 thomas view->offset = offset;
8107 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8108 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8109 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8110 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8111 a5d43cac 2022-07-01 thomas selected = &s->selected;
8114 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8115 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8116 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8117 a5d43cac 2022-07-01 thomas header = 3;
8118 a5d43cac 2022-07-01 thomas selected = &s->selected;
8121 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8122 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8123 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8124 a5d43cac 2022-07-01 thomas header = 5;
8125 a5d43cac 2022-07-01 thomas selected = &s->selected;
8128 a5d43cac 2022-07-01 thomas default:
8129 a5d43cac 2022-07-01 thomas selected = NULL;
8130 a5d43cac 2022-07-01 thomas scrolld = NULL;
8131 a5d43cac 2022-07-01 thomas header = 0;
8135 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8136 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8137 a5d43cac 2022-07-01 thomas view->offset = offset;
8138 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8139 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8140 a5d43cac 2022-07-01 thomas *selected -= offset;
8144 a5d43cac 2022-07-01 thomas return err;
8147 a5d43cac 2022-07-01 thomas static void
8148 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8152 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8153 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8154 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8155 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8157 6879ba42 2020-10-01 naddy fputc('\n', fp);
8160 4ed7e80c 2018-05-20 stsp __dead static void
8161 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8163 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8165 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8166 6879ba42 2020-10-01 naddy getprogname());
8167 6879ba42 2020-10-01 naddy if (hflag) {
8168 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8169 6879ba42 2020-10-01 naddy list_commands(fp);
8171 6879ba42 2020-10-01 naddy exit(status);
8174 c2301be8 2018-04-30 stsp static char **
8175 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8177 ee85c5e8 2020-02-29 stsp va_list ap;
8178 c2301be8 2018-04-30 stsp char **argv;
8181 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8183 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8184 c2301be8 2018-04-30 stsp if (argv == NULL)
8185 c2301be8 2018-04-30 stsp err(1, "calloc");
8186 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8187 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8188 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8189 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8192 ee85c5e8 2020-02-29 stsp va_end(ap);
8193 c2301be8 2018-04-30 stsp return argv;
8197 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8198 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8199 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8200 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8201 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8203 ee85c5e8 2020-02-29 stsp static const struct got_error *
8204 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8206 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8207 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8208 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8209 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8210 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8211 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8212 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8213 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8214 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8216 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8217 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8218 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8220 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8221 7cd52833 2022-06-23 thomas if (error != NULL)
8222 7cd52833 2022-06-23 thomas goto done;
8224 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8225 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8226 ee85c5e8 2020-02-29 stsp goto done;
8228 ee85c5e8 2020-02-29 stsp if (worktree)
8229 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8231 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8232 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8233 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8234 ee85c5e8 2020-02-29 stsp goto done;
8237 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8238 ee85c5e8 2020-02-29 stsp if (error != NULL)
8239 ee85c5e8 2020-02-29 stsp goto done;
8241 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8242 ee85c5e8 2020-02-29 stsp repo, worktree);
8243 ee85c5e8 2020-02-29 stsp if (error)
8244 ee85c5e8 2020-02-29 stsp goto done;
8246 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8247 84de9106 2020-12-26 stsp if (error)
8248 84de9106 2020-12-26 stsp goto done;
8249 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8250 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8251 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8252 ee85c5e8 2020-02-29 stsp if (error)
8253 ee85c5e8 2020-02-29 stsp goto done;
8255 ee85c5e8 2020-02-29 stsp if (worktree) {
8256 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8257 ee85c5e8 2020-02-29 stsp worktree = NULL;
8260 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8261 945f9229 2022-04-16 thomas if (error)
8262 945f9229 2022-04-16 thomas goto done;
8264 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8265 ee85c5e8 2020-02-29 stsp if (error) {
8266 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8267 ee85c5e8 2020-02-29 stsp goto done;
8268 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8269 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8270 6879ba42 2020-10-01 naddy usage(1, 1);
8271 ee85c5e8 2020-02-29 stsp /* not reached */
8274 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8275 1d0f4054 2021-06-17 stsp if (error == NULL)
8276 1d0f4054 2021-06-17 stsp error = close_err;
8277 ee85c5e8 2020-02-29 stsp repo = NULL;
8279 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8280 ee85c5e8 2020-02-29 stsp if (error)
8281 ee85c5e8 2020-02-29 stsp goto done;
8283 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8285 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8286 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8288 1d0f4054 2021-06-17 stsp if (repo) {
8289 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8290 1d0f4054 2021-06-17 stsp if (error == NULL)
8291 1d0f4054 2021-06-17 stsp error = close_err;
8293 945f9229 2022-04-16 thomas if (commit)
8294 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8295 ee85c5e8 2020-02-29 stsp if (worktree)
8296 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8297 7cd52833 2022-06-23 thomas if (pack_fds) {
8298 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8299 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8300 7cd52833 2022-06-23 thomas if (error == NULL)
8301 7cd52833 2022-06-23 thomas error = pack_err;
8304 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8305 ee85c5e8 2020-02-29 stsp free(commit_id);
8306 ee85c5e8 2020-02-29 stsp free(cwd);
8307 ee85c5e8 2020-02-29 stsp free(repo_path);
8308 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8309 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8311 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8312 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8313 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8315 87670572 2020-12-26 naddy tog_free_refs();
8316 ee85c5e8 2020-02-29 stsp return error;
8320 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8322 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8323 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8324 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8325 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8326 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8327 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8328 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8330 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8332 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8334 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8335 9f7d7167 2018-04-29 stsp switch (ch) {
8337 9f7d7167 2018-04-29 stsp hflag = 1;
8340 53ccebc2 2019-07-30 stsp Vflag = 1;
8343 6879ba42 2020-10-01 naddy usage(hflag, 1);
8344 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8348 9f7d7167 2018-04-29 stsp argc -= optind;
8349 9f7d7167 2018-04-29 stsp argv += optind;
8350 9814e6a3 2020-09-27 naddy optind = 1;
8351 c2301be8 2018-04-30 stsp optreset = 1;
8353 53ccebc2 2019-07-30 stsp if (Vflag) {
8354 53ccebc2 2019-07-30 stsp got_version_print_str();
8355 6879ba42 2020-10-01 naddy return 0;
8358 4010e238 2020-12-04 stsp #ifndef PROFILE
8359 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8360 4010e238 2020-12-04 stsp NULL) == -1)
8361 4010e238 2020-12-04 stsp err(1, "pledge");
8364 c2301be8 2018-04-30 stsp if (argc == 0) {
8365 f29d3e89 2018-06-23 stsp if (hflag)
8366 6879ba42 2020-10-01 naddy usage(hflag, 0);
8367 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8368 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8370 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8374 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8375 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8376 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8377 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8378 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8384 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8385 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8386 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8387 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8388 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8389 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8392 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8393 ee85c5e8 2020-02-29 stsp if (argc != 1)
8394 6879ba42 2020-10-01 naddy usage(0, 1);
8395 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8396 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8398 ee85c5e8 2020-02-29 stsp if (hflag)
8399 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8401 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8405 b46c1e04 2020-09-20 naddy putchar('\n');
8406 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8408 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8409 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8410 a2f4a359 2020-02-28 stsp free(cmd_argv);
8413 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8414 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);