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 ea0bff04 2022-07-19 thomas struct tog_log_view_state *s = &view->state.log;
932 ea0bff04 2022-07-19 thomas const struct got_error *err = NULL;
933 ea0bff04 2022-07-19 thomas int n = s->selected_entry->idx + view->lines - s->selected;
936 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
937 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
939 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
940 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
941 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
944 ea0bff04 2022-07-19 thomas return err;
947 97cb21cd 2022-07-12 thomas static void
948 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
950 97cb21cd 2022-07-12 thomas if (n == 0)
953 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
954 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
955 97cb21cd 2022-07-12 thomas view->parent->offset += n;
957 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
958 97cb21cd 2022-07-12 thomas } else if (view->offset) {
959 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
960 97cb21cd 2022-07-12 thomas view->offset -= n;
962 97cb21cd 2022-07-12 thomas view->offset = 0;
966 53d2bdd3 2022-07-10 thomas static const struct got_error *
967 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
969 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
970 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
972 53d2bdd3 2022-07-10 thomas if (view->parent)
973 53d2bdd3 2022-07-10 thomas v = view->parent;
975 53d2bdd3 2022-07-10 thomas v = view;
977 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
978 53d2bdd3 2022-07-10 thomas return NULL;
980 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
982 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 ae98518f 2022-07-12 thomas int y = v->child->begin_y;
985 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
986 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
987 53d2bdd3 2022-07-10 thomas if (view->parent)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
990 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
991 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
992 53d2bdd3 2022-07-10 thomas view->count = 0;
993 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
994 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
995 53d2bdd3 2022-07-10 thomas view->count = 0;
996 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
998 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
999 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1000 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1001 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1002 53d2bdd3 2022-07-10 thomas if (err)
1003 53d2bdd3 2022-07-10 thomas return err;
1004 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1005 fe731b51 2022-07-14 thomas if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1006 ae98518f 2022-07-12 thomas v->child->nscrolled = y - v->child->begin_y;
1007 fe731b51 2022-07-14 thomas else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1008 fe731b51 2022-07-14 thomas v->nscrolled = v->child->begin_y - y;
1009 53d2bdd3 2022-07-10 thomas } else {
1010 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1012 53d2bdd3 2022-07-10 thomas if (view->parent)
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1015 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1016 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1017 53d2bdd3 2022-07-10 thomas view->count = 0;
1018 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1019 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1020 53d2bdd3 2022-07-10 thomas view->count = 0;
1021 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1023 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1026 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1027 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1028 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1029 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1031 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1032 53d2bdd3 2022-07-10 thomas if (err)
1033 53d2bdd3 2022-07-10 thomas return err;
1034 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1035 53d2bdd3 2022-07-10 thomas if (err)
1036 53d2bdd3 2022-07-10 thomas return err;
1038 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1039 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1040 53d2bdd3 2022-07-10 thomas if (err)
1041 53d2bdd3 2022-07-10 thomas return err;
1044 fe731b51 2022-07-14 thomas if (v->nscrolled)
1045 53d2bdd3 2022-07-10 thomas err = request_log_commits(v);
1046 fe731b51 2022-07-14 thomas else if (v->child->nscrolled)
1047 53d2bdd3 2022-07-10 thomas err = request_log_commits(v->child);
1049 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1051 53d2bdd3 2022-07-10 thomas return err;
1054 53d2bdd3 2022-07-10 thomas static void
1055 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1057 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1059 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1060 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1063 669b5ffa 2018-10-07 stsp static const struct got_error *
1064 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1066 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1068 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1069 669b5ffa 2018-10-07 stsp return NULL;
1071 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1072 669b5ffa 2018-10-07 stsp view->child = NULL;
1073 669b5ffa 2018-10-07 stsp return err;
1076 40236d76 2022-06-23 thomas static const struct got_error *
1077 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1079 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1081 669b5ffa 2018-10-07 stsp view->child = child;
1082 669b5ffa 2018-10-07 stsp child->parent = view;
1084 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1085 53d2bdd3 2022-07-10 thomas if (err)
1086 53d2bdd3 2022-07-10 thomas return err;
1088 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1089 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1091 53d2bdd3 2022-07-10 thomas return err;
1094 34bc9ec9 2019-02-22 stsp static void
1095 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1097 25791caa 2018-10-24 stsp int cols, lines;
1098 25791caa 2018-10-24 stsp struct winsize size;
1100 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1101 25791caa 2018-10-24 stsp cols = 80; /* Default */
1102 25791caa 2018-10-24 stsp lines = 24;
1104 25791caa 2018-10-24 stsp cols = size.ws_col;
1105 25791caa 2018-10-24 stsp lines = size.ws_row;
1107 25791caa 2018-10-24 stsp resize_term(lines, cols);
1110 2b49a8ae 2019-06-22 stsp static const struct got_error *
1111 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1113 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1114 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1115 2b49a8ae 2019-06-22 stsp char pattern[1024];
1118 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1119 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1120 c0c4acc8 2021-01-24 stsp view->searching = 0;
1121 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1123 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1125 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1126 2b49a8ae 2019-06-22 stsp return NULL;
1128 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1129 a5d43cac 2022-07-01 thomas v = view->child;
1131 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1132 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1134 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1135 2b49a8ae 2019-06-22 stsp nocbreak();
1137 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1138 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1141 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1142 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1143 2b49a8ae 2019-06-22 stsp return NULL;
1145 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1146 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1147 7c32bd05 2019-06-22 stsp if (err) {
1148 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1149 7c32bd05 2019-06-22 stsp return err;
1151 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1152 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1153 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1154 2b49a8ae 2019-06-22 stsp view->search_next(view);
1157 2b49a8ae 2019-06-22 stsp return NULL;
1160 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1161 64486692 2022-07-07 thomas static const struct got_error *
1162 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1164 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1165 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1167 64486692 2022-07-07 thomas if (view->parent)
1168 64486692 2022-07-07 thomas v = view->parent;
1170 64486692 2022-07-07 thomas v = view;
1172 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1175 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1177 ddbc4d37 2022-07-12 thomas if (!v->child)
1178 ddbc4d37 2022-07-12 thomas return NULL;
1179 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1180 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1182 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1183 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1184 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1185 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1186 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1189 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1190 64486692 2022-07-07 thomas v->ncols = COLS;
1191 64486692 2022-07-07 thomas v->child->ncols = COLS;
1192 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1194 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1195 64486692 2022-07-07 thomas if (err)
1196 64486692 2022-07-07 thomas return err;
1198 64486692 2022-07-07 thomas v->child->mode = v->mode;
1199 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1200 64486692 2022-07-07 thomas v->focus_child = 1;
1202 64486692 2022-07-07 thomas err = view_fullscreen(v);
1203 64486692 2022-07-07 thomas if (err)
1204 64486692 2022-07-07 thomas return err;
1205 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1206 64486692 2022-07-07 thomas if (err)
1207 64486692 2022-07-07 thomas return err;
1209 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1210 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1211 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1212 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1213 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1214 ddbc4d37 2022-07-12 thomas } else {
1215 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1216 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1218 ae98518f 2022-07-12 thomas if (v->type == TOG_VIEW_LOG && v->nscrolled)
1219 ddbc4d37 2022-07-12 thomas err = request_log_commits(v);
1220 ae98518f 2022-07-12 thomas else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1221 ddbc4d37 2022-07-12 thomas err = request_log_commits(v->child);
1223 64486692 2022-07-07 thomas return err;
1227 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1228 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1230 07b0611c 2022-06-23 thomas static int
1231 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1233 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1234 a5d43cac 2022-07-01 thomas int x, n = 0;
1236 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1237 a5d43cac 2022-07-01 thomas v = view->child;
1238 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1239 a5d43cac 2022-07-01 thomas v = view->parent;
1241 07b0611c 2022-06-23 thomas view->count = 0;
1242 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1243 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1244 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1245 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1248 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1249 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1250 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1251 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1255 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1256 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1258 07b0611c 2022-06-23 thomas if (n >= 9999999)
1259 07b0611c 2022-06-23 thomas n = 9999999;
1261 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1262 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1264 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1265 07b0611c 2022-06-23 thomas view->count = n;
1267 07b0611c 2022-06-23 thomas return c;
1270 0cf4efb1 2018-09-29 stsp static const struct got_error *
1271 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1272 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1274 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1275 669b5ffa 2018-10-07 stsp struct tog_view *v;
1276 1a76625f 2018-10-22 stsp int ch, errcode;
1278 e5a0f69f 2018-08-18 stsp *new = NULL;
1280 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1281 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1282 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1283 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1284 07b0611c 2022-06-23 thomas view->count = 0;
1287 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1288 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1289 82954512 2020-02-03 stsp if (errcode)
1290 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1291 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1292 3da8ef85 2021-09-21 stsp sched_yield();
1293 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1294 82954512 2020-02-03 stsp if (errcode)
1295 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1296 82954512 2020-02-03 stsp "pthread_mutex_lock");
1297 60493ae3 2019-06-20 stsp view->search_next(view);
1298 60493ae3 2019-06-20 stsp return NULL;
1301 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1302 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1303 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1304 1a76625f 2018-10-22 stsp if (errcode)
1305 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1306 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1307 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1308 634cb454 2022-07-03 thomas cbreak();
1309 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1310 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1311 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1312 634cb454 2022-07-03 thomas view->count = 0;
1314 634cb454 2022-07-03 thomas ch = view->ch;
1315 634cb454 2022-07-03 thomas } else {
1316 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1317 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1318 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1320 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1321 1a76625f 2018-10-22 stsp if (errcode)
1322 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1323 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1325 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1326 25791caa 2018-10-24 stsp tog_resizeterm();
1327 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1328 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1329 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1330 25791caa 2018-10-24 stsp err = view_resize(v);
1332 25791caa 2018-10-24 stsp return err;
1333 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1335 25791caa 2018-10-24 stsp return err;
1336 cdfcfb03 2020-12-06 stsp if (v->child) {
1337 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1339 cdfcfb03 2020-12-06 stsp return err;
1340 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1341 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1343 cdfcfb03 2020-12-06 stsp return err;
1344 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1345 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1346 53d2bdd3 2022-07-10 thomas if (err)
1347 53d2bdd3 2022-07-10 thomas return err;
1353 e5a0f69f 2018-08-18 stsp switch (ch) {
1355 07b0611c 2022-06-23 thomas view->count = 0;
1356 1e37a5c2 2019-05-12 jcs if (view->child) {
1357 e78dc838 2020-12-04 stsp view->focussed = 0;
1358 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1359 e78dc838 2020-12-04 stsp view->focus_child = 1;
1360 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1361 e78dc838 2020-12-04 stsp view->focussed = 0;
1362 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1363 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1364 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1365 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1366 a5d43cac 2022-07-01 thomas view->parent->type == TOG_VIEW_LOG) {
1367 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1368 a5d43cac 2022-07-01 thomas if (err)
1369 a5d43cac 2022-07-01 thomas return err;
1371 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1372 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1373 a5d43cac 2022-07-01 thomas if (err)
1374 a5d43cac 2022-07-01 thomas return err;
1379 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1380 a5d43cac 2022-07-01 thomas if (view->parent->type == TOG_VIEW_LOG) {
1381 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1382 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1383 a5d43cac 2022-07-01 thomas if (err)
1386 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1388 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1389 9970f7fc 2020-12-03 stsp view->dying = 1;
1394 1c5e5faa 2022-06-23 thomas case 'F':
1395 07b0611c 2022-06-23 thomas view->count = 0;
1396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1397 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1399 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1400 e78dc838 2020-12-04 stsp view->focussed = 0;
1401 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1402 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1403 53d2bdd3 2022-07-10 thomas } else {
1404 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1405 53d2bdd3 2022-07-10 thomas if (!err)
1406 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1410 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1411 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1413 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1414 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1415 e78dc838 2020-12-04 stsp view->focussed = 1;
1416 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1418 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1419 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1420 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1421 53d2bdd3 2022-07-10 thomas if (!err)
1422 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1426 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1428 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG) {
1431 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
1432 a5d43cac 2022-07-01 thomas if (err)
1435 a5d43cac 2022-07-01 thomas if (view->parent)
1436 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1437 a5d43cac 2022-07-01 thomas if (!err)
1438 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1440 64486692 2022-07-07 thomas case 'S':
1441 53d2bdd3 2022-07-10 thomas view->count = 0;
1442 64486692 2022-07-07 thomas err = switch_split(view);
1444 53d2bdd3 2022-07-10 thomas case '-':
1445 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1447 53d2bdd3 2022-07-10 thomas case '+':
1448 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1450 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1453 07b0611c 2022-06-23 thomas view->count = 0;
1454 60493ae3 2019-06-20 stsp if (view->search_start)
1455 2b49a8ae 2019-06-22 stsp view_search_start(view);
1457 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1461 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1462 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1463 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1464 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1465 60493ae3 2019-06-20 stsp view->search_next(view);
1467 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1469 adf4c9e0 2022-07-03 thomas case 'A':
1470 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1471 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1473 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1474 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1475 adf4c9e0 2022-07-03 thomas if (v->reset) {
1476 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1477 adf4c9e0 2022-07-03 thomas if (err)
1478 adf4c9e0 2022-07-03 thomas return err;
1480 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1481 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1482 adf4c9e0 2022-07-03 thomas if (err)
1483 adf4c9e0 2022-07-03 thomas return err;
1488 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1492 e5a0f69f 2018-08-18 stsp return err;
1495 ef20f542 2022-06-26 thomas static int
1496 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1498 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1499 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1501 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1503 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1506 669b5ffa 2018-10-07 stsp return view->focussed;
1509 bcbd79e2 2018-08-19 stsp static const struct got_error *
1510 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1512 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1513 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1514 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1515 64486692 2022-07-07 thomas char *mode;
1516 fd823528 2018-10-22 stsp int fast_refresh = 10;
1517 1a76625f 2018-10-22 stsp int done = 0, errcode;
1519 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1520 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1521 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1523 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1525 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1526 1a76625f 2018-10-22 stsp if (errcode)
1527 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1529 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1530 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1532 1004088d 2018-09-29 stsp view->focussed = 1;
1533 878940b7 2018-09-29 stsp err = view->show(view);
1535 0cf4efb1 2018-09-29 stsp return err;
1536 0cf4efb1 2018-09-29 stsp update_panels();
1537 0cf4efb1 2018-09-29 stsp doupdate();
1538 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1539 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1540 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1541 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1542 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1544 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1547 9970f7fc 2020-12-03 stsp if (view->dying) {
1548 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1550 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1551 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1553 e78dc838 2020-12-04 stsp else if (view->parent)
1554 669b5ffa 2018-10-07 stsp prev = view->parent;
1556 e78dc838 2020-12-04 stsp if (view->parent) {
1557 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1558 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1559 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1560 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1561 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1562 40236d76 2022-06-23 thomas if (err)
1564 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1565 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1567 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1569 9970f7fc 2020-12-03 stsp err = view_close(view);
1571 e5a0f69f 2018-08-18 stsp goto done;
1573 e78dc838 2020-12-04 stsp view = NULL;
1574 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1575 e78dc838 2020-12-04 stsp if (v->focussed)
1578 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1579 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1581 e78dc838 2020-12-04 stsp view = prev;
1582 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1583 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1584 e78dc838 2020-12-04 stsp tog_view_list_head);
1586 e78dc838 2020-12-04 stsp if (view) {
1587 e78dc838 2020-12-04 stsp if (view->focus_child) {
1588 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1589 e78dc838 2020-12-04 stsp view = view->child;
1591 e78dc838 2020-12-04 stsp view->focussed = 1;
1595 bcbd79e2 2018-08-19 stsp if (new_view) {
1596 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1597 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1598 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1599 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1601 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1602 86c66b02 2018-10-18 stsp err = view_close(v);
1604 86c66b02 2018-10-18 stsp goto done;
1607 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1608 fed7eaa8 2018-10-24 stsp view = new_view;
1610 669b5ffa 2018-10-07 stsp if (view) {
1611 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1612 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1613 e78dc838 2020-12-04 stsp view = view->child;
1615 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1616 e78dc838 2020-12-04 stsp view = view->parent;
1618 e78dc838 2020-12-04 stsp show_panel(view->panel);
1619 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1620 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1621 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1622 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1624 1a76625f 2018-10-22 stsp goto done;
1626 669b5ffa 2018-10-07 stsp err = view->show(view);
1628 1a76625f 2018-10-22 stsp goto done;
1629 669b5ffa 2018-10-07 stsp if (view->child) {
1630 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1632 1a76625f 2018-10-22 stsp goto done;
1634 1a76625f 2018-10-22 stsp update_panels();
1635 1a76625f 2018-10-22 stsp doupdate();
1639 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1640 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1641 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1642 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1643 f2d749db 2022-07-12 thomas close_err = view_close(view);
1644 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1645 f2d749db 2022-07-12 thomas err = close_err;
1648 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1649 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1650 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1652 e5a0f69f 2018-08-18 stsp return err;
1655 4ed7e80c 2018-05-20 stsp __dead static void
1656 9f7d7167 2018-04-29 stsp usage_log(void)
1659 c70c5802 2018-08-01 stsp fprintf(stderr,
1660 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1661 9f7d7167 2018-04-29 stsp getprogname());
1665 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1666 80ddbec8 2018-04-29 stsp static const struct got_error *
1667 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1669 00dfcb92 2018-06-11 stsp char *vis = NULL;
1670 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1672 963b370f 2018-05-20 stsp *ws = NULL;
1673 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1674 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1675 00dfcb92 2018-06-11 stsp int vislen;
1676 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1677 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1679 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1680 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1682 00dfcb92 2018-06-11 stsp return err;
1683 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1684 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1685 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1686 a7f50699 2018-06-11 stsp goto done;
1690 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1691 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1693 a7f50699 2018-06-11 stsp goto done;
1696 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1697 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1699 00dfcb92 2018-06-11 stsp free(vis);
1700 963b370f 2018-05-20 stsp if (err) {
1701 963b370f 2018-05-20 stsp free(*ws);
1702 963b370f 2018-05-20 stsp *ws = NULL;
1703 963b370f 2018-05-20 stsp *wlen = 0;
1705 963b370f 2018-05-20 stsp return err;
1708 05171be4 2022-06-23 thomas static const struct got_error *
1709 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1711 05171be4 2022-06-23 thomas char *dst;
1712 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1714 05171be4 2022-06-23 thomas *ptr = NULL;
1715 05171be4 2022-06-23 thomas n = len = strlen(src);
1716 83316834 2022-06-23 thomas dst = malloc(n + 1);
1717 05171be4 2022-06-23 thomas if (dst == NULL)
1718 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1720 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1721 05171be4 2022-06-23 thomas const char c = src[idx];
1723 05171be4 2022-06-23 thomas if (c == '\t') {
1724 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1725 b235ad5d 2022-06-23 thomas char *p;
1727 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1728 83316834 2022-06-23 thomas if (p == NULL) {
1729 83316834 2022-06-23 thomas free(dst);
1730 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1733 83316834 2022-06-23 thomas dst = p;
1734 05171be4 2022-06-23 thomas n += nb;
1735 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1736 05171be4 2022-06-23 thomas sz += nb;
1738 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1742 05171be4 2022-06-23 thomas dst[sz] = '\0';
1743 05171be4 2022-06-23 thomas *ptr = dst;
1744 05171be4 2022-06-23 thomas return NULL;
1748 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1749 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1750 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1751 8d208d34 2022-06-23 thomas * *rcol.
1753 8d208d34 2022-06-23 thomas static int
1754 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1756 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1758 8d208d34 2022-06-23 thomas if (n == 0) {
1759 8d208d34 2022-06-23 thomas *rcol = cols;
1760 8d208d34 2022-06-23 thomas return off;
1763 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1764 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1765 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1767 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1769 8d208d34 2022-06-23 thomas if (width == -1) {
1770 8d208d34 2022-06-23 thomas width = 1;
1771 8d208d34 2022-06-23 thomas wline[i] = L'.';
1774 8d208d34 2022-06-23 thomas if (cols + width > n)
1776 8d208d34 2022-06-23 thomas cols += width;
1779 8d208d34 2022-06-23 thomas *rcol = cols;
1780 8d208d34 2022-06-23 thomas return i;
1784 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1785 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1786 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1788 f91a2b48 2022-06-23 thomas static const struct got_error *
1789 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1790 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1792 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1793 8d208d34 2022-06-23 thomas int cols;
1794 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1795 05171be4 2022-06-23 thomas char *exstr = NULL;
1796 963b370f 2018-05-20 stsp size_t wlen;
1797 8d208d34 2022-06-23 thomas int i, scrollx;
1799 963b370f 2018-05-20 stsp *wlinep = NULL;
1800 b700b5d6 2018-07-10 stsp *widthp = 0;
1802 05171be4 2022-06-23 thomas if (expand) {
1803 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1804 05171be4 2022-06-23 thomas if (err)
1805 05171be4 2022-06-23 thomas return err;
1808 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1809 05171be4 2022-06-23 thomas free(exstr);
1811 963b370f 2018-05-20 stsp return err;
1813 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1815 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1816 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1819 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1820 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1824 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1825 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1827 b700b5d6 2018-07-10 stsp if (widthp)
1828 b700b5d6 2018-07-10 stsp *widthp = cols;
1829 f91a2b48 2022-06-23 thomas if (scrollxp)
1830 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1832 963b370f 2018-05-20 stsp free(wline);
1834 963b370f 2018-05-20 stsp *wlinep = wline;
1835 963b370f 2018-05-20 stsp return err;
1838 8b473291 2019-02-21 stsp static const struct got_error*
1839 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1840 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1842 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1843 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1845 8b473291 2019-02-21 stsp const char *name;
1847 8b473291 2019-02-21 stsp *refs_str = NULL;
1849 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1850 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1851 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1854 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1855 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1857 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1858 8b473291 2019-02-21 stsp name += 5;
1859 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1860 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1862 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1863 8b473291 2019-02-21 stsp name += 6;
1864 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1865 8b473291 2019-02-21 stsp name += 8;
1866 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1867 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1870 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1873 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1874 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1875 5d844a1e 2019-08-13 stsp if (err) {
1876 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1877 48cae60d 2020-09-22 stsp free(ref_id);
1880 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1881 5d844a1e 2019-08-13 stsp err = NULL;
1882 5d844a1e 2019-08-13 stsp tag = NULL;
1885 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1886 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1887 48cae60d 2020-09-22 stsp free(ref_id);
1889 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1890 52b5abe1 2019-08-13 stsp if (cmp != 0)
1892 8b473291 2019-02-21 stsp s = *refs_str;
1893 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1894 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1895 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1897 8b473291 2019-02-21 stsp *refs_str = NULL;
1903 8b473291 2019-02-21 stsp return err;
1906 963b370f 2018-05-20 stsp static const struct got_error *
1907 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1908 27a741e5 2019-09-11 stsp int col_tab_align)
1910 e6b8b890 2020-12-29 naddy char *smallerthan;
1912 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1913 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1914 5813d178 2019-03-09 stsp author = smallerthan + 1;
1915 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1916 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1917 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1920 5813d178 2019-03-09 stsp static const struct got_error *
1921 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1922 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1923 8fdc79fe 2020-12-01 naddy int author_display_cols)
1925 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1926 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1927 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1928 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1929 5813d178 2019-03-09 stsp char *author = NULL;
1930 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1931 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1932 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1933 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1934 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1935 ccb26ccd 2018-11-05 stsp struct tm tm;
1936 45d799e2 2018-12-23 stsp time_t committer_time;
1937 11b20872 2019-11-08 stsp struct tog_color *tc;
1939 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1940 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1941 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1942 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1943 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1945 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1946 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1948 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1949 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1951 11b20872 2019-11-08 stsp wattr_on(view->window,
1952 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1953 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1955 11b20872 2019-11-08 stsp wattr_off(view->window,
1956 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1957 27a741e5 2019-09-11 stsp col = limit;
1958 b39d25c7 2018-07-10 stsp if (col > avail)
1959 b39d25c7 2018-07-10 stsp goto done;
1961 6570a66d 2019-11-08 stsp if (avail >= 120) {
1962 6570a66d 2019-11-08 stsp char *id_str;
1963 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1965 6570a66d 2019-11-08 stsp goto done;
1966 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1968 11b20872 2019-11-08 stsp wattr_on(view->window,
1969 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1970 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1972 11b20872 2019-11-08 stsp wattr_off(view->window,
1973 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1974 6570a66d 2019-11-08 stsp free(id_str);
1976 6570a66d 2019-11-08 stsp if (col > avail)
1977 6570a66d 2019-11-08 stsp goto done;
1980 f69c5a46 2022-07-19 thomas if (s->use_committer)
1981 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
1983 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
1984 5813d178 2019-03-09 stsp if (author == NULL) {
1985 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1986 80ddbec8 2018-04-29 stsp goto done;
1988 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1990 bb737323 2018-05-20 stsp goto done;
1991 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1993 11b20872 2019-11-08 stsp wattr_on(view->window,
1994 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1995 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1997 11b20872 2019-11-08 stsp wattr_off(view->window,
1998 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1999 bb737323 2018-05-20 stsp col += author_width;
2000 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2001 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2003 bb737323 2018-05-20 stsp author_width++;
2005 9c2eaf34 2018-05-20 stsp if (col > avail)
2006 9c2eaf34 2018-05-20 stsp goto done;
2008 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2010 6d9fbc00 2018-04-29 stsp goto done;
2011 bb737323 2018-05-20 stsp logmsg = logmsg0;
2012 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2014 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2015 bb737323 2018-05-20 stsp if (newline)
2016 bb737323 2018-05-20 stsp *newline = '\0';
2017 f91a2b48 2022-06-23 thomas limit = avail - col;
2018 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2019 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2020 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2021 f91a2b48 2022-06-23 thomas limit, col, 1);
2022 331b1a16 2022-06-23 thomas if (err)
2023 331b1a16 2022-06-23 thomas goto done;
2024 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2025 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2026 27a741e5 2019-09-11 stsp while (col < avail) {
2027 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2031 80ddbec8 2018-04-29 stsp free(logmsg0);
2032 bb737323 2018-05-20 stsp free(wlogmsg);
2033 5813d178 2019-03-09 stsp free(author);
2034 bb737323 2018-05-20 stsp free(wauthor);
2035 80ddbec8 2018-04-29 stsp free(line);
2036 80ddbec8 2018-04-29 stsp return err;
2039 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2040 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2041 899d86c2 2018-05-10 stsp struct got_object_id *id)
2043 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2045 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2046 80ddbec8 2018-04-29 stsp if (entry == NULL)
2047 899d86c2 2018-05-10 stsp return NULL;
2049 899d86c2 2018-05-10 stsp entry->id = id;
2050 99db9666 2018-05-07 stsp entry->commit = commit;
2051 899d86c2 2018-05-10 stsp return entry;
2054 99db9666 2018-05-07 stsp static void
2055 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2057 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2059 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2060 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2061 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2062 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2063 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2064 99db9666 2018-05-07 stsp free(entry);
2067 99db9666 2018-05-07 stsp static void
2068 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2070 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2071 99db9666 2018-05-07 stsp pop_commit(commits);
2074 c4972b91 2018-05-07 stsp static const struct got_error *
2075 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2076 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2078 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2079 13add988 2019-10-15 stsp regmatch_t regmatch;
2080 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2082 13add988 2019-10-15 stsp *have_match = 0;
2084 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2086 13add988 2019-10-15 stsp return err;
2088 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2090 13add988 2019-10-15 stsp goto done;
2092 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2093 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2094 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2095 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2096 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2097 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2098 13add988 2019-10-15 stsp *have_match = 1;
2100 13add988 2019-10-15 stsp free(id_str);
2101 13add988 2019-10-15 stsp free(logmsg);
2102 13add988 2019-10-15 stsp return err;
2105 13add988 2019-10-15 stsp static const struct got_error *
2106 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2108 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2111 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2112 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2113 1a76625f 2018-10-22 stsp * while updating the display.
2116 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2117 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2118 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2119 1a76625f 2018-10-22 stsp int errcode;
2121 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2122 4e0d2870 2020-12-07 naddy NULL, NULL);
2123 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2126 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2129 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2130 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2131 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2135 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2136 1a76625f 2018-10-22 stsp if (errcode) {
2137 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2138 13add988 2019-10-15 stsp "pthread_mutex_lock");
2142 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2143 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2144 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2146 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2147 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2148 7c1452c1 2020-03-26 stsp int have_match;
2149 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2152 7c1452c1 2020-03-26 stsp if (have_match)
2153 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2156 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2157 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2158 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2159 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2162 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2164 9ba79e04 2018-06-11 stsp return err;
2167 2b779855 2020-12-05 naddy static void
2168 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2170 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2171 2b779855 2020-12-05 naddy int ncommits = 0;
2173 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2174 2b779855 2020-12-05 naddy while (entry) {
2175 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2176 2b779855 2020-12-05 naddy s->selected_entry = entry;
2179 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2180 2b779855 2020-12-05 naddy ncommits++;
2184 0553a4e3 2018-04-30 stsp static const struct got_error *
2185 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2187 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2188 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2189 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2190 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2191 60493ae3 2019-06-20 stsp int width;
2192 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2193 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2194 8b473291 2019-02-21 stsp char *refs_str = NULL;
2195 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2196 11b20872 2019-11-08 stsp struct tog_color *tc;
2197 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2199 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2200 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2201 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2202 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2204 ecb28ae0 2018-07-16 stsp return err;
2205 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2206 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2207 d2075bf3 2020-12-25 stsp if (refs) {
2208 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2209 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2211 d2075bf3 2020-12-25 stsp goto done;
2215 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2216 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2218 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2219 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2220 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2221 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2222 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2223 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2224 8f4ed634 2020-03-26 stsp goto done;
2227 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2229 f9686aa5 2020-03-27 stsp if (view->searching) {
2230 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2231 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2232 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2233 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2234 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2235 f9686aa5 2020-03-27 stsp search_str = "searching...";
2238 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2239 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2240 f9686aa5 2020-03-27 stsp search_str ? search_str :
2241 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2242 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2243 8f4ed634 2020-03-26 stsp goto done;
2247 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2248 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2249 91198554 2022-06-23 thomas "........................................",
2250 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2251 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2252 1a76625f 2018-10-22 stsp header = NULL;
2253 1a76625f 2018-10-22 stsp goto done;
2255 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2256 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2257 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2258 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2259 1a76625f 2018-10-22 stsp header = NULL;
2260 1a76625f 2018-10-22 stsp goto done;
2262 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2264 1a76625f 2018-10-22 stsp goto done;
2266 2814baeb 2018-08-01 stsp werase(view->window);
2268 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2269 a3404814 2018-09-02 stsp wstandout(view->window);
2270 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2272 11b20872 2019-11-08 stsp wattr_on(view->window,
2273 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2274 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2276 11b20872 2019-11-08 stsp wattr_off(view->window,
2277 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2278 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2279 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2282 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2283 a3404814 2018-09-02 stsp wstandend(view->window);
2284 ecb28ae0 2018-07-16 stsp free(wline);
2285 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2286 1a76625f 2018-10-22 stsp goto done;
2288 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2289 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2290 5813d178 2019-03-09 stsp ncommits = 0;
2291 05171be4 2022-06-23 thomas view->maxx = 0;
2292 5813d178 2019-03-09 stsp while (entry) {
2293 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2294 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2295 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2296 5813d178 2019-03-09 stsp int width;
2297 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2299 f69c5a46 2022-07-19 thomas if (s->use_committer)
2300 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2302 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2303 5813d178 2019-03-09 stsp if (author == NULL) {
2304 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2305 5813d178 2019-03-09 stsp goto done;
2307 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2308 27a741e5 2019-09-11 stsp date_display_cols);
2309 5813d178 2019-03-09 stsp if (author_cols < width)
2310 5813d178 2019-03-09 stsp author_cols = width;
2311 5813d178 2019-03-09 stsp free(wauthor);
2312 5813d178 2019-03-09 stsp free(author);
2313 ef944b8b 2022-07-21 thomas if (err)
2314 ef944b8b 2022-07-21 thomas goto done;
2315 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2316 05171be4 2022-06-23 thomas if (err)
2317 05171be4 2022-06-23 thomas goto done;
2318 05171be4 2022-06-23 thomas msg = msg0;
2319 05171be4 2022-06-23 thomas while (*msg == '\n')
2321 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2322 331b1a16 2022-06-23 thomas *eol = '\0';
2323 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2324 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2325 331b1a16 2022-06-23 thomas if (err)
2326 331b1a16 2022-06-23 thomas goto done;
2327 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2328 05171be4 2022-06-23 thomas free(msg0);
2329 331b1a16 2022-06-23 thomas free(wmsg);
2330 7ca04879 2019-10-19 stsp ncommits++;
2331 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2334 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2335 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2336 867c6645 2018-07-10 stsp ncommits = 0;
2337 899d86c2 2018-05-10 stsp while (entry) {
2338 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2340 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2341 2814baeb 2018-08-01 stsp wstandout(view->window);
2342 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2343 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2344 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2345 2814baeb 2018-08-01 stsp wstandend(view->window);
2347 60493ae3 2019-06-20 stsp goto done;
2348 0553a4e3 2018-04-30 stsp ncommits++;
2349 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2350 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2353 a5d43cac 2022-07-01 thomas view_border(view);
2355 1a76625f 2018-10-22 stsp free(id_str);
2356 8b473291 2019-02-21 stsp free(refs_str);
2357 1a76625f 2018-10-22 stsp free(ncommits_str);
2358 1a76625f 2018-10-22 stsp free(header);
2359 80ddbec8 2018-04-29 stsp return err;
2362 07b55e75 2018-05-10 stsp static void
2363 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2365 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2366 07b55e75 2018-05-10 stsp int nscrolled = 0;
2368 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2369 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2372 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2373 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2374 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2375 07b55e75 2018-05-10 stsp if (entry) {
2376 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2377 07b55e75 2018-05-10 stsp nscrolled++;
2382 aa075928 2018-05-10 stsp static const struct got_error *
2383 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2385 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2386 5e224a3e 2019-02-22 stsp int errcode;
2388 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2390 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2391 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2392 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2393 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2394 7aafa0d1 2019-02-22 stsp if (errcode)
2395 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2396 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2399 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2400 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2402 7c1452c1 2020-03-26 stsp if (!wait)
2405 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2406 ffe38506 2020-12-01 naddy show_log_view(view);
2407 7c1452c1 2020-03-26 stsp update_panels();
2408 7c1452c1 2020-03-26 stsp doupdate();
2410 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2411 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2412 82954512 2020-02-03 stsp if (errcode)
2413 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2414 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2416 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2417 ffe38506 2020-12-01 naddy show_log_view(view);
2418 7c1452c1 2020-03-26 stsp update_panels();
2419 7c1452c1 2020-03-26 stsp doupdate();
2422 5e224a3e 2019-02-22 stsp return NULL;
2425 a5d43cac 2022-07-01 thomas static const struct got_error *
2426 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2428 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2429 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2431 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2432 fe731b51 2022-07-14 thomas return NULL;
2434 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2435 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2436 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2438 a5d43cac 2022-07-01 thomas return err;
2441 5e224a3e 2019-02-22 stsp static const struct got_error *
2442 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2444 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2445 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2446 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2447 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2449 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2450 5e224a3e 2019-02-22 stsp return NULL;
2452 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2453 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2454 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2456 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2458 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2459 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2461 5e224a3e 2019-02-22 stsp return err;
2465 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2466 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2469 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2470 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2472 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2473 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2475 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2476 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2478 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2479 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2481 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2483 dd0a52c1 2018-05-20 stsp return err;
2486 cd0acaa7 2018-05-20 stsp static const struct got_error *
2487 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2488 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2489 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2491 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2492 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2493 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2495 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2496 15a94983 2018-12-23 stsp if (diff_view == NULL)
2497 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2499 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2500 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2501 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2502 e5a0f69f 2018-08-18 stsp if (err == NULL)
2503 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2504 cd0acaa7 2018-05-20 stsp return err;
2507 80ddbec8 2018-04-29 stsp static const struct got_error *
2508 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2509 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2511 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2513 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2514 941e9f74 2019-05-21 stsp if (parent == NULL)
2515 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2517 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2518 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2519 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2520 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2521 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2522 941e9f74 2019-05-21 stsp s->tree = subtree;
2523 941e9f74 2019-05-21 stsp s->selected = 0;
2524 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2525 941e9f74 2019-05-21 stsp return NULL;
2528 941e9f74 2019-05-21 stsp static const struct got_error *
2529 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2530 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2532 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2533 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2534 941e9f74 2019-05-21 stsp const char *p;
2535 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2537 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2539 941e9f74 2019-05-21 stsp while (*p) {
2540 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2541 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2542 56e0773d 2019-11-28 stsp char *te_name;
2544 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2547 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2548 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2549 941e9f74 2019-05-21 stsp if (slash == NULL)
2550 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2552 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2553 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2554 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2557 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2558 56e0773d 2019-11-28 stsp if (te == NULL) {
2559 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2560 56e0773d 2019-11-28 stsp free(te_name);
2563 56e0773d 2019-11-28 stsp free(te_name);
2564 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2566 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2567 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2569 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2570 941e9f74 2019-05-21 stsp if (slash)
2571 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2573 941e9f74 2019-05-21 stsp subpath = strdup(path);
2574 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2575 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2579 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2584 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2585 941e9f74 2019-05-21 stsp free(tree_id);
2589 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2590 941e9f74 2019-05-21 stsp if (err) {
2591 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2594 941e9f74 2019-05-21 stsp if (slash == NULL)
2596 941e9f74 2019-05-21 stsp free(subpath);
2597 941e9f74 2019-05-21 stsp subpath = NULL;
2598 941e9f74 2019-05-21 stsp p = slash;
2601 941e9f74 2019-05-21 stsp free(subpath);
2602 1a76625f 2018-10-22 stsp return err;
2605 61266923 2020-01-14 stsp static const struct got_error *
2606 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2607 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2608 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2610 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2611 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2612 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2614 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2615 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2616 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2618 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2620 55cccc34 2020-02-20 stsp return err;
2621 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2623 55cccc34 2020-02-20 stsp *new_view = tree_view;
2625 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2626 55cccc34 2020-02-20 stsp return NULL;
2628 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2631 55cccc34 2020-02-20 stsp static const struct got_error *
2632 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2634 61266923 2020-01-14 stsp sigset_t sigset;
2635 61266923 2020-01-14 stsp int errcode;
2637 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2638 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2640 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2641 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2642 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2643 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2644 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2645 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2646 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2647 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2648 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2650 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2651 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2652 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2654 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2655 61266923 2020-01-14 stsp if (errcode)
2656 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2658 61266923 2020-01-14 stsp return NULL;
2661 1a76625f 2018-10-22 stsp static void *
2662 1a76625f 2018-10-22 stsp log_thread(void *arg)
2664 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2665 1a76625f 2018-10-22 stsp int errcode = 0;
2666 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2667 1a76625f 2018-10-22 stsp int done = 0;
2670 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2671 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2673 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2674 f2d749db 2022-07-12 thomas if (errcode) {
2675 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2676 61266923 2020-01-14 stsp return (void *)err;
2679 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2680 f2d749db 2022-07-12 thomas if (err) {
2681 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2682 f2d749db 2022-07-12 thomas goto done;
2685 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2686 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2687 f2d749db 2022-07-12 thomas if (errcode) {
2688 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2689 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2690 f2d749db 2022-07-12 thomas goto done;
2692 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2693 1a76625f 2018-10-22 stsp if (err) {
2694 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2695 f2d749db 2022-07-12 thomas goto done;
2696 1a76625f 2018-10-22 stsp err = NULL;
2698 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2699 1a76625f 2018-10-22 stsp a->commits_needed--;
2701 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2702 3abe8080 2019-04-10 stsp if (errcode) {
2703 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2704 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2705 f2d749db 2022-07-12 thomas goto done;
2706 3abe8080 2019-04-10 stsp } else if (*a->quit)
2708 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2709 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2710 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2711 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2714 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2715 7c1452c1 2020-03-26 stsp if (errcode) {
2716 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2717 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2718 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2719 f2d749db 2022-07-12 thomas goto done;
2723 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2725 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2726 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2727 7c1452c1 2020-03-26 stsp &tog_mutex);
2728 f2d749db 2022-07-12 thomas if (errcode) {
2729 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2730 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2731 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2732 f2d749db 2022-07-12 thomas goto done;
2734 21355643 2020-12-06 stsp if (*a->quit)
2739 3abe8080 2019-04-10 stsp a->log_complete = 1;
2740 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2741 f2d749db 2022-07-12 thomas if (errcode)
2742 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2744 f2d749db 2022-07-12 thomas if (err) {
2745 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2746 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2748 1a76625f 2018-10-22 stsp return (void *)err;
2751 1a76625f 2018-10-22 stsp static const struct got_error *
2752 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2754 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2755 1a76625f 2018-10-22 stsp int errcode;
2757 1a76625f 2018-10-22 stsp if (s->thread) {
2758 1a76625f 2018-10-22 stsp s->quit = 1;
2759 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2760 1a76625f 2018-10-22 stsp if (errcode)
2761 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2762 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2763 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2764 1a76625f 2018-10-22 stsp if (errcode)
2765 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2766 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2767 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2768 1a76625f 2018-10-22 stsp if (errcode)
2769 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2770 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2771 1a76625f 2018-10-22 stsp if (errcode)
2772 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2773 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2774 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2777 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2778 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2779 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2782 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2783 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2784 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2785 1af5eddf 2022-06-23 thomas if (err == NULL)
2786 1af5eddf 2022-06-23 thomas err = pack_err;
2787 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2790 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2791 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2792 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2795 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2798 9343a5fb 2018-06-23 stsp static const struct got_error *
2799 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2801 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2802 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2803 276b94a1 2020-11-13 naddy int errcode;
2805 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2807 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2808 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2809 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2811 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2812 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2813 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2815 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2816 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2817 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2818 1a76625f 2018-10-22 stsp free(s->start_id);
2819 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2820 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2821 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2822 1a76625f 2018-10-22 stsp return err;
2825 1a76625f 2018-10-22 stsp static const struct got_error *
2826 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2828 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2830 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2831 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2832 60493ae3 2019-06-20 stsp return NULL;
2835 60493ae3 2019-06-20 stsp static const struct got_error *
2836 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2838 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2839 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2840 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2842 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2843 f9686aa5 2020-03-27 stsp show_log_view(view);
2844 f9686aa5 2020-03-27 stsp update_panels();
2845 f9686aa5 2020-03-27 stsp doupdate();
2847 96e2b566 2019-07-08 stsp if (s->search_entry) {
2848 13add988 2019-10-15 stsp int errcode, ch;
2849 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2850 13add988 2019-10-15 stsp if (errcode)
2851 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2852 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2853 13add988 2019-10-15 stsp ch = wgetch(view->window);
2854 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2855 13add988 2019-10-15 stsp if (errcode)
2856 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2857 13add988 2019-10-15 stsp "pthread_mutex_lock");
2858 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2859 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2860 678cbce5 2019-07-28 stsp return NULL;
2862 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2863 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2865 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2866 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2867 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2868 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2869 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2872 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2873 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2874 1f4d5162 2022-06-23 thomas * might have changed.
2876 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2877 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2878 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2880 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2881 b74feda6 2022-06-23 thomas } else {
2882 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2883 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2884 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2886 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2887 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2890 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2893 60493ae3 2019-06-20 stsp while (1) {
2894 13add988 2019-10-15 stsp int have_match = 0;
2896 60493ae3 2019-06-20 stsp if (entry == NULL) {
2897 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2898 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2899 f9967bca 2020-03-27 stsp view->search_next_done =
2900 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2901 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2902 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2903 f801134a 2019-06-25 stsp return NULL;
2906 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2907 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2908 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2910 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2911 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2914 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2915 13add988 2019-10-15 stsp &view->regex);
2918 13add988 2019-10-15 stsp if (have_match) {
2919 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2920 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2924 96e2b566 2019-07-08 stsp s->search_entry = entry;
2925 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2926 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2928 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2931 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2932 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2933 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2934 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2936 60493ae3 2019-06-20 stsp return err;
2939 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2940 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2942 60493ae3 2019-06-20 stsp return err;
2947 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2949 60493ae3 2019-06-20 stsp return NULL;
2952 60493ae3 2019-06-20 stsp static const struct got_error *
2953 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2954 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2955 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2957 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2958 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2959 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2960 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2961 1a76625f 2018-10-22 stsp int errcode;
2963 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2964 f135c941 2020-02-20 stsp free(s->in_repo_path);
2965 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2966 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2967 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2970 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2971 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2972 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2974 fb2756b9 2018-08-04 stsp s->repo = repo;
2975 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2976 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2977 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2978 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2979 9cd7cbd1 2020-12-07 stsp goto done;
2982 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2983 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2984 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2985 5036bf37 2018-09-24 stsp goto done;
2987 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2989 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2990 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2991 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2992 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2994 11b20872 2019-11-08 stsp goto done;
2995 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2996 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2997 11b20872 2019-11-08 stsp if (err) {
2998 11b20872 2019-11-08 stsp free_colors(&s->colors);
2999 11b20872 2019-11-08 stsp goto done;
3001 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3002 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3003 11b20872 2019-11-08 stsp if (err) {
3004 11b20872 2019-11-08 stsp free_colors(&s->colors);
3005 11b20872 2019-11-08 stsp goto done;
3009 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3010 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3011 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3012 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3013 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3014 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3016 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3017 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3018 1af5eddf 2022-06-23 thomas if (err)
3019 1af5eddf 2022-06-23 thomas goto done;
3021 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3022 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3023 7cd52833 2022-06-23 thomas if (err)
3024 7cd52833 2022-06-23 thomas goto done;
3025 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3026 b672a97a 2020-01-27 stsp !s->log_branches);
3028 1a76625f 2018-10-22 stsp goto done;
3029 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3030 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3032 c5b78334 2020-01-12 stsp goto done;
3034 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3035 1a76625f 2018-10-22 stsp if (errcode) {
3036 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3037 1a76625f 2018-10-22 stsp goto done;
3039 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3040 7c1452c1 2020-03-26 stsp if (errcode) {
3041 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3042 7c1452c1 2020-03-26 stsp goto done;
3045 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3046 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3047 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3048 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3049 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3050 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3051 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3052 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3053 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3054 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3055 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3056 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3057 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3060 1a76625f 2018-10-22 stsp close_log_view(view);
3061 ba4f502b 2018-08-04 stsp return err;
3064 e5a0f69f 2018-08-18 stsp static const struct got_error *
3065 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3067 f2f6d207 2020-11-24 stsp const struct got_error *err;
3068 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3070 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3071 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3072 2b380cc8 2018-10-24 stsp &s->thread_args);
3073 2b380cc8 2018-10-24 stsp if (errcode)
3074 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3075 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3076 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3078 f2f6d207 2020-11-24 stsp return err;
3082 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3085 b31f89ff 2022-07-01 thomas static void
3086 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3088 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3090 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3091 b31f89ff 2022-07-01 thomas view->count = 0;
3092 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3095 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3096 b31f89ff 2022-07-01 thomas || home)
3097 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3099 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3100 b31f89ff 2022-07-01 thomas --s->selected;
3102 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3104 b31f89ff 2022-07-01 thomas select_commit(s);
3108 b31f89ff 2022-07-01 thomas static const struct got_error *
3109 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3111 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3112 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3113 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3115 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3116 b31f89ff 2022-07-01 thomas if (first == NULL) {
3117 b31f89ff 2022-07-01 thomas view->count = 0;
3118 b31f89ff 2022-07-01 thomas return NULL;
3121 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3122 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3123 b31f89ff 2022-07-01 thomas return NULL;
3125 b31f89ff 2022-07-01 thomas if (!page) {
3126 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3128 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3129 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3130 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3131 b31f89ff 2022-07-01 thomas ++s->selected;
3133 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3134 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3135 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3136 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3137 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3139 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3140 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3141 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3142 b31f89ff 2022-07-01 thomas } else {
3143 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3144 b31f89ff 2022-07-01 thomas if (err)
3145 b31f89ff 2022-07-01 thomas return err;
3146 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3147 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3148 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3151 b31f89ff 2022-07-01 thomas if (err)
3152 b31f89ff 2022-07-01 thomas return err;
3155 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3156 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3158 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3159 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3161 b31f89ff 2022-07-01 thomas select_commit(s);
3163 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3164 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3165 b31f89ff 2022-07-01 thomas view->count = 0;
3167 b31f89ff 2022-07-01 thomas return NULL;
3170 a5d43cac 2022-07-01 thomas static void
3171 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3176 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3177 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3178 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3179 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3180 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3182 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3183 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3184 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3185 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3186 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3187 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3189 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3193 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3194 e5a0f69f 2018-08-18 stsp static const struct got_error *
3195 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3197 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3199 a5d43cac 2022-07-01 thomas view->nlines = y;
3200 64486692 2022-07-07 thomas view->ncols = COLS;
3201 a5d43cac 2022-07-01 thomas err = view_resize(view);
3202 a5d43cac 2022-07-01 thomas if (err)
3203 a5d43cac 2022-07-01 thomas return err;
3205 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3207 a5d43cac 2022-07-01 thomas return err;
3210 a5d43cac 2022-07-01 thomas static const struct got_error *
3211 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3213 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3214 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3215 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3216 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3217 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3218 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3220 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3221 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3222 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3223 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3224 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3225 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3227 b31f89ff 2022-07-01 thomas return err;
3230 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3231 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3232 068ab281 2022-07-01 thomas --eos; /* border */
3234 528dedf3 2021-08-30 stsp switch (ch) {
3236 1e37a5c2 2019-05-12 jcs s->quit = 1;
3238 05171be4 2022-06-23 thomas case '0':
3239 05171be4 2022-06-23 thomas view->x = 0;
3241 05171be4 2022-06-23 thomas case '$':
3242 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3243 07b0611c 2022-06-23 thomas view->count = 0;
3245 05171be4 2022-06-23 thomas case KEY_RIGHT:
3246 05171be4 2022-06-23 thomas case 'l':
3247 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3248 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3250 07b0611c 2022-06-23 thomas view->count = 0;
3252 05171be4 2022-06-23 thomas case KEY_LEFT:
3253 05171be4 2022-06-23 thomas case 'h':
3254 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3255 07b0611c 2022-06-23 thomas if (view->x <= 0)
3256 07b0611c 2022-06-23 thomas view->count = 0;
3259 1e37a5c2 2019-05-12 jcs case KEY_UP:
3262 f7140bf5 2021-10-17 thomas case CTRL('p'):
3263 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3266 912a3f79 2021-08-30 j case KEY_HOME:
3267 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3268 07b0611c 2022-06-23 thomas view->count = 0;
3270 70f17a53 2022-06-13 thomas case CTRL('u'):
3271 23427b14 2022-06-23 thomas case 'u':
3272 70f17a53 2022-06-13 thomas nscroll /= 2;
3273 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3274 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3275 a4292ac5 2019-05-12 jcs case CTRL('b'):
3276 1c5e5faa 2022-06-23 thomas case 'b':
3277 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3280 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3283 f7140bf5 2021-10-17 thomas case CTRL('n'):
3284 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3286 f69c5a46 2022-07-19 thomas case '@':
3287 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3290 912a3f79 2021-08-30 j case KEY_END: {
3291 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3292 912a3f79 2021-08-30 j * traverse them all. */
3293 07b0611c 2022-06-23 thomas view->count = 0;
3294 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3295 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3296 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3299 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3300 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3301 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3302 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3304 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3305 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3307 f3bc9f1d 2021-09-05 naddy if (n > 0)
3308 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3309 2b779855 2020-12-05 naddy select_commit(s);
3312 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3313 23427b14 2022-06-23 thomas case 'd':
3314 70f17a53 2022-06-13 thomas nscroll /= 2;
3315 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3316 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3317 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3318 4c2d69cb 2022-06-23 thomas case 'f':
3319 b31f89ff 2022-07-01 thomas case ' ':
3320 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3322 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3323 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3324 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3325 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3326 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3327 2b779855 2020-12-05 naddy select_commit(s);
3328 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3329 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3330 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3331 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3332 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3335 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3336 444d5325 2022-07-03 thomas case '\r':
3337 07b0611c 2022-06-23 thomas view->count = 0;
3338 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3341 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3342 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3343 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3345 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3346 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3347 78756c87 2020-11-24 stsp view, s->repo);
3351 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3352 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3353 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3354 a5d43cac 2022-07-01 thomas if (err)
3358 e78dc838 2020-12-04 stsp view->focussed = 0;
3359 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3360 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3361 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3363 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3364 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3365 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3367 1e37a5c2 2019-05-12 jcs return err;
3368 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3369 40236d76 2022-06-23 thomas if (err)
3370 40236d76 2022-06-23 thomas return err;
3371 e78dc838 2020-12-04 stsp view->focus_child = 1;
3373 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3376 07b0611c 2022-06-23 thomas view->count = 0;
3377 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3379 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3380 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3381 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3382 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3386 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3387 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3388 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3389 444d5325 2022-07-03 thomas if (err)
3392 e78dc838 2020-12-04 stsp view->focussed = 0;
3393 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3394 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3395 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3397 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3398 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3400 1e37a5c2 2019-05-12 jcs return err;
3401 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3402 40236d76 2022-06-23 thomas if (err)
3403 40236d76 2022-06-23 thomas return err;
3404 e78dc838 2020-12-04 stsp view->focus_child = 1;
3406 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3408 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3409 21355643 2020-12-06 stsp case CTRL('l'):
3411 07b0611c 2022-06-23 thomas view->count = 0;
3412 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3413 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3415 21355643 2020-12-06 stsp err = stop_log_thread(s);
3417 74cfe85e 2020-10-20 stsp return err;
3418 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3419 21355643 2020-12-06 stsp char *parent_path;
3420 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3422 21355643 2020-12-06 stsp return err;
3423 21355643 2020-12-06 stsp free(s->in_repo_path);
3424 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3425 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3426 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3427 21355643 2020-12-06 stsp struct got_object_id *start_id;
3428 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3429 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3430 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3432 21355643 2020-12-06 stsp return err;
3433 21355643 2020-12-06 stsp free(s->start_id);
3434 21355643 2020-12-06 stsp s->start_id = start_id;
3435 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3436 21355643 2020-12-06 stsp } else /* 'B' */
3437 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3439 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3440 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3441 bf7e79b3 2022-06-23 thomas if (err)
3442 bf7e79b3 2022-06-23 thomas return err;
3444 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3445 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3446 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3448 21355643 2020-12-06 stsp return err;
3449 51a10b52 2020-12-26 stsp tog_free_refs();
3450 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3452 ca51c541 2020-12-07 stsp return err;
3453 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3454 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3456 d01904d4 2019-06-25 stsp return err;
3457 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3458 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3460 b672a97a 2020-01-27 stsp return err;
3461 21355643 2020-12-06 stsp free_commits(&s->commits);
3462 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3463 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3464 21355643 2020-12-06 stsp s->selected_entry = NULL;
3465 21355643 2020-12-06 stsp s->selected = 0;
3466 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3467 21355643 2020-12-06 stsp s->quit = 0;
3468 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3469 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3470 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3473 07b0611c 2022-06-23 thomas view->count = 0;
3474 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3475 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3476 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3477 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3478 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3479 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3480 6458efa5 2020-11-24 stsp if (err) {
3481 6458efa5 2020-11-24 stsp view_close(ref_view);
3482 6458efa5 2020-11-24 stsp return err;
3484 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3485 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3486 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3487 444d5325 2022-07-03 thomas if (err)
3490 e78dc838 2020-12-04 stsp view->focussed = 0;
3491 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3492 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3493 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3494 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3495 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3496 6458efa5 2020-11-24 stsp err = view_close_child(view);
3498 6458efa5 2020-11-24 stsp return err;
3499 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3500 40236d76 2022-06-23 thomas if (err)
3501 40236d76 2022-06-23 thomas return err;
3502 e78dc838 2020-12-04 stsp view->focus_child = 1;
3504 6458efa5 2020-11-24 stsp *new_view = ref_view;
3507 07b0611c 2022-06-23 thomas view->count = 0;
3511 80ddbec8 2018-04-29 stsp return err;
3514 4ed7e80c 2018-05-20 stsp static const struct got_error *
3515 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3517 c2db6724 2019-01-04 stsp const struct got_error *error;
3519 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3520 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3521 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3523 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3524 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3526 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3527 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3529 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3530 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3532 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3533 c2db6724 2019-01-04 stsp if (error != NULL)
3534 c2db6724 2019-01-04 stsp return error;
3536 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3537 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3539 c2db6724 2019-01-04 stsp return NULL;
3542 a915003a 2019-02-05 stsp static void
3543 a915003a 2019-02-05 stsp init_curses(void)
3546 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3547 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3548 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3550 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3551 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3552 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3553 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3554 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3556 a915003a 2019-02-05 stsp initscr();
3558 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3561 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3562 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3563 a915003a 2019-02-05 stsp curs_set(0);
3564 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3565 6d17833f 2019-11-08 stsp start_color();
3566 6d17833f 2019-11-08 stsp use_default_colors();
3570 c2db6724 2019-01-04 stsp static const struct got_error *
3571 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3572 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3574 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3576 f135c941 2020-02-20 stsp if (argc == 0) {
3577 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3578 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3579 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3580 f135c941 2020-02-20 stsp return NULL;
3583 f135c941 2020-02-20 stsp if (worktree) {
3584 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3587 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3589 f135c941 2020-02-20 stsp return err;
3590 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3591 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3592 bfd61697 2020-11-14 stsp p) == -1) {
3593 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3594 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3598 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3600 f135c941 2020-02-20 stsp return err;
3603 f135c941 2020-02-20 stsp static const struct got_error *
3604 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3606 80ddbec8 2018-04-29 stsp const struct got_error *error;
3607 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3608 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3609 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3610 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3611 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3612 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3613 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3614 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3615 04cc582a 2018-08-01 stsp struct tog_view *view;
3616 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3618 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3619 80ddbec8 2018-04-29 stsp switch (ch) {
3621 b672a97a 2020-01-27 stsp log_branches = 1;
3624 80ddbec8 2018-04-29 stsp start_commit = optarg;
3627 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3628 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3629 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3633 17020d27 2019-03-07 stsp usage_log();
3634 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3638 80ddbec8 2018-04-29 stsp argc -= optind;
3639 80ddbec8 2018-04-29 stsp argv += optind;
3641 f135c941 2020-02-20 stsp if (argc > 1)
3642 f135c941 2020-02-20 stsp usage_log();
3644 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3645 7cd52833 2022-06-23 thomas if (error != NULL)
3646 7cd52833 2022-06-23 thomas goto done;
3648 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3649 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3650 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3651 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3652 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3653 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3654 c156c7a4 2020-12-18 stsp goto done;
3655 a1fbf39a 2019-08-11 stsp if (worktree)
3656 6962eb72 2020-02-20 stsp repo_path =
3657 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3659 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3660 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3661 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3662 c156c7a4 2020-12-18 stsp goto done;
3666 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3667 80ddbec8 2018-04-29 stsp if (error != NULL)
3668 ecb28ae0 2018-07-16 stsp goto done;
3670 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3671 f135c941 2020-02-20 stsp repo, worktree);
3672 f135c941 2020-02-20 stsp if (error)
3673 f135c941 2020-02-20 stsp goto done;
3675 f135c941 2020-02-20 stsp init_curses();
3677 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3678 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3679 c02c541e 2019-03-29 stsp if (error)
3680 c02c541e 2019-03-29 stsp goto done;
3682 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3683 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3684 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3685 87670572 2020-12-26 naddy if (error)
3686 87670572 2020-12-26 naddy goto done;
3689 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3690 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3691 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3692 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3693 d8f38dc4 2020-12-05 stsp if (error)
3694 d8f38dc4 2020-12-05 stsp goto done;
3695 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3697 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3698 d8f38dc4 2020-12-05 stsp if (error == NULL)
3699 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3700 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3701 d8f38dc4 2020-12-05 stsp goto done;
3702 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3703 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3704 d8f38dc4 2020-12-05 stsp if (error)
3705 d8f38dc4 2020-12-05 stsp goto done;
3708 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3709 04cc582a 2018-08-01 stsp if (view == NULL) {
3710 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3711 04cc582a 2018-08-01 stsp goto done;
3713 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3714 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3715 ba4f502b 2018-08-04 stsp if (error)
3716 ba4f502b 2018-08-04 stsp goto done;
3717 2fc00ff4 2019-08-31 stsp if (worktree) {
3718 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3719 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3720 2fc00ff4 2019-08-31 stsp worktree = NULL;
3722 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3724 f135c941 2020-02-20 stsp free(in_repo_path);
3725 ecb28ae0 2018-07-16 stsp free(repo_path);
3726 ecb28ae0 2018-07-16 stsp free(cwd);
3727 899d86c2 2018-05-10 stsp free(start_id);
3728 d8f38dc4 2020-12-05 stsp free(label);
3730 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3731 1d0f4054 2021-06-17 stsp if (repo) {
3732 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3733 1d0f4054 2021-06-17 stsp if (error == NULL)
3734 1d0f4054 2021-06-17 stsp error = close_err;
3736 ec142235 2019-03-07 stsp if (worktree)
3737 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3738 7cd52833 2022-06-23 thomas if (pack_fds) {
3739 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3740 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3741 7cd52833 2022-06-23 thomas if (error == NULL)
3742 7cd52833 2022-06-23 thomas error = pack_err;
3744 51a10b52 2020-12-26 stsp tog_free_refs();
3745 80ddbec8 2018-04-29 stsp return error;
3748 4ed7e80c 2018-05-20 stsp __dead static void
3749 9f7d7167 2018-04-29 stsp usage_diff(void)
3752 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3753 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3757 6d17833f 2019-11-08 stsp static int
3758 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3759 41605754 2020-11-12 stsp regmatch_t *regmatch)
3761 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3764 ef20f542 2022-06-26 thomas static struct tog_color *
3765 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3767 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3769 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3770 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3771 6d17833f 2019-11-08 stsp return tc;
3774 6d17833f 2019-11-08 stsp return NULL;
3777 4ed7e80c 2018-05-20 stsp static const struct got_error *
3778 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3779 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3781 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3782 666f7b10 2022-06-23 thomas char *exstr = NULL;
3783 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3784 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3785 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3786 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3788 41605754 2020-11-12 stsp *wtotal = 0;
3790 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3791 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3793 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3794 666f7b10 2022-06-23 thomas if (err)
3795 666f7b10 2022-06-23 thomas return err;
3797 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3798 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3799 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3800 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3801 666f7b10 2022-06-23 thomas goto done;
3803 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3804 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3805 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3806 cbea3800 2022-06-23 thomas goto done;
3808 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3809 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3810 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3811 cbea3800 2022-06-23 thomas goto done;
3814 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3815 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3816 cbea3800 2022-06-23 thomas col_tab_align, 1);
3817 cbea3800 2022-06-23 thomas if (err)
3818 cbea3800 2022-06-23 thomas goto done;
3819 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3820 05171be4 2022-06-23 thomas if (n) {
3821 cbea3800 2022-06-23 thomas free(wline);
3822 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3823 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3824 cbea3800 2022-06-23 thomas if (err)
3825 cbea3800 2022-06-23 thomas goto done;
3826 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3827 cbea3800 2022-06-23 thomas wlimit -= width;
3828 cbea3800 2022-06-23 thomas *wtotal += width;
3831 41605754 2020-11-12 stsp if (wlimit > 0) {
3832 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3833 cbea3800 2022-06-23 thomas size_t wlen;
3835 cbea3800 2022-06-23 thomas free(wline);
3836 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3837 cbea3800 2022-06-23 thomas col_tab_align, 1);
3838 cbea3800 2022-06-23 thomas if (err)
3839 cbea3800 2022-06-23 thomas goto done;
3840 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3841 cbea3800 2022-06-23 thomas while (i < wlen) {
3842 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3843 cbea3800 2022-06-23 thomas if (width == -1) {
3844 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3845 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3846 cbea3800 2022-06-23 thomas goto done;
3848 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3850 1c5e5faa 2022-06-23 thomas w += width;
3853 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3854 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3855 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3856 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3857 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3858 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3859 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3863 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3864 cbea3800 2022-06-23 thomas free(wline);
3865 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3866 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3867 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3868 cbea3800 2022-06-23 thomas col_tab_align, 1);
3869 cbea3800 2022-06-23 thomas if (err)
3870 cbea3800 2022-06-23 thomas goto done;
3871 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3872 cbea3800 2022-06-23 thomas } else {
3873 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3874 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3875 cbea3800 2022-06-23 thomas if (err)
3876 cbea3800 2022-06-23 thomas goto done;
3877 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3879 cbea3800 2022-06-23 thomas *wtotal += width2;
3882 05171be4 2022-06-23 thomas free(wline);
3883 666f7b10 2022-06-23 thomas free(exstr);
3884 cbea3800 2022-06-23 thomas free(seg0);
3885 cbea3800 2022-06-23 thomas free(seg1);
3886 cbea3800 2022-06-23 thomas free(seg2);
3887 cbea3800 2022-06-23 thomas return err;
3890 41605754 2020-11-12 stsp static const struct got_error *
3891 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3893 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3894 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3895 61e69b96 2018-05-20 stsp const struct got_error *err;
3896 fe621944 2020-11-10 stsp int nprinted = 0;
3897 b304db33 2018-05-20 stsp char *line;
3898 826082fe 2020-12-10 stsp size_t linesize = 0;
3899 826082fe 2020-12-10 stsp ssize_t linelen;
3900 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3901 61e69b96 2018-05-20 stsp wchar_t *wline;
3902 e0b650dd 2018-05-20 stsp int width;
3903 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3904 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3905 fe621944 2020-11-10 stsp off_t line_offset;
3907 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3908 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3909 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3911 f7d12f7e 2018-08-01 stsp werase(view->window);
3913 a3404814 2018-09-02 stsp if (header) {
3914 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3915 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3916 135a2da0 2020-11-11 stsp header) == -1)
3917 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3918 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3920 135a2da0 2020-11-11 stsp free(line);
3922 a3404814 2018-09-02 stsp return err;
3924 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3925 a3404814 2018-09-02 stsp wstandout(view->window);
3926 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3927 e54cc94a 2020-11-11 stsp free(wline);
3928 e54cc94a 2020-11-11 stsp wline = NULL;
3929 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3930 a3404814 2018-09-02 stsp wstandend(view->window);
3931 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3932 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3934 a3404814 2018-09-02 stsp if (max_lines <= 1)
3935 a3404814 2018-09-02 stsp return NULL;
3936 a3404814 2018-09-02 stsp max_lines--;
3939 89f1a395 2020-12-01 naddy s->eof = 0;
3940 05171be4 2022-06-23 thomas view->maxx = 0;
3941 826082fe 2020-12-10 stsp line = NULL;
3942 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3943 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3944 826082fe 2020-12-10 stsp if (linelen == -1) {
3945 826082fe 2020-12-10 stsp if (feof(s->f)) {
3946 826082fe 2020-12-10 stsp s->eof = 1;
3949 826082fe 2020-12-10 stsp free(line);
3950 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3953 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3954 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3955 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3956 cbea3800 2022-06-23 thomas if (err) {
3957 cbea3800 2022-06-23 thomas free(line);
3958 cbea3800 2022-06-23 thomas return err;
3960 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3961 cbea3800 2022-06-23 thomas free(wline);
3962 cbea3800 2022-06-23 thomas wline = NULL;
3964 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3966 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3967 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3968 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3969 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3970 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3971 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3972 41605754 2020-11-12 stsp if (err) {
3973 41605754 2020-11-12 stsp free(line);
3974 41605754 2020-11-12 stsp return err;
3977 f1c0ec19 2022-06-23 thomas int skip;
3978 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3979 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3980 f1c0ec19 2022-06-23 thomas if (err) {
3981 f1c0ec19 2022-06-23 thomas free(line);
3982 f1c0ec19 2022-06-23 thomas return err;
3984 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3985 f1c0ec19 2022-06-23 thomas free(wline);
3986 41605754 2020-11-12 stsp wline = NULL;
3989 6d17833f 2019-11-08 stsp wattr_off(view->window,
3990 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3991 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3992 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3993 fe621944 2020-11-10 stsp nprinted++;
3995 826082fe 2020-12-10 stsp free(line);
3996 fe621944 2020-11-10 stsp if (nprinted >= 1)
3997 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3998 89f1a395 2020-12-01 naddy (nprinted - 1);
4000 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4002 a5d43cac 2022-07-01 thomas view_border(view);
4004 89f1a395 2020-12-01 naddy if (s->eof) {
4005 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4006 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4007 c3e9aa98 2019-05-13 jcs nprinted++;
4010 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4011 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4013 c3e9aa98 2019-05-13 jcs return err;
4016 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4017 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4018 e54cc94a 2020-11-11 stsp free(wline);
4019 e54cc94a 2020-11-11 stsp wline = NULL;
4020 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4023 26ed57b2 2018-05-19 stsp return NULL;
4026 abd2672a 2018-12-23 stsp static char *
4027 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4029 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4030 09867e48 2019-08-13 stsp char *p, *s;
4032 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4033 09867e48 2019-08-13 stsp if (tm == NULL)
4034 09867e48 2019-08-13 stsp return NULL;
4035 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4036 09867e48 2019-08-13 stsp if (s == NULL)
4037 09867e48 2019-08-13 stsp return NULL;
4038 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4040 abd2672a 2018-12-23 stsp *p = '\0';
4044 4ed7e80c 2018-05-20 stsp static const struct got_error *
4045 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4046 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4048 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4049 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4050 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4051 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4053 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4054 0208f208 2020-05-05 stsp if (qid != NULL) {
4055 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4056 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4057 ec242592 2022-04-22 thomas &qid->id);
4059 0208f208 2020-05-05 stsp return err;
4061 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4062 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4063 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4064 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4065 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4067 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4071 0208f208 2020-05-05 stsp if (tree_id1) {
4072 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4074 0208f208 2020-05-05 stsp goto done;
4077 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4078 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4080 0208f208 2020-05-05 stsp goto done;
4082 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4083 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4085 0208f208 2020-05-05 stsp if (tree1)
4086 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4087 0208f208 2020-05-05 stsp if (tree2)
4088 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4089 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4090 0208f208 2020-05-05 stsp return err;
4093 0208f208 2020-05-05 stsp static const struct got_error *
4094 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4098 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4099 fe621944 2020-11-10 stsp if (p == NULL)
4100 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4101 fe621944 2020-11-10 stsp *line_offsets = p;
4102 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4103 fe621944 2020-11-10 stsp (*nlines)++;
4104 fe621944 2020-11-10 stsp return NULL;
4107 fe621944 2020-11-10 stsp static const struct got_error *
4108 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4109 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4110 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4112 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4113 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4114 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4115 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4116 45d799e2 2018-12-23 stsp time_t committer_time;
4117 45d799e2 2018-12-23 stsp const char *author, *committer;
4118 8b473291 2019-02-21 stsp char *refs_str = NULL;
4119 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4120 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4121 fe621944 2020-11-10 stsp off_t outoff = 0;
4124 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4126 8b473291 2019-02-21 stsp if (refs) {
4127 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4129 8b473291 2019-02-21 stsp return err;
4132 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4134 abd2672a 2018-12-23 stsp return err;
4136 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4137 15a94983 2018-12-23 stsp if (err) {
4138 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4139 15a94983 2018-12-23 stsp goto done;
4142 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4144 fe621944 2020-11-10 stsp goto done;
4146 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4147 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4148 fe621944 2020-11-10 stsp if (n < 0) {
4149 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4150 abd2672a 2018-12-23 stsp goto done;
4152 fe621944 2020-11-10 stsp outoff += n;
4153 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4155 fe621944 2020-11-10 stsp goto done;
4157 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4158 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4159 fe621944 2020-11-10 stsp if (n < 0) {
4160 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4161 abd2672a 2018-12-23 stsp goto done;
4163 fe621944 2020-11-10 stsp outoff += n;
4164 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4166 fe621944 2020-11-10 stsp goto done;
4168 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4169 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4170 fe621944 2020-11-10 stsp if (datestr) {
4171 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4172 fe621944 2020-11-10 stsp if (n < 0) {
4173 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4174 fe621944 2020-11-10 stsp goto done;
4176 fe621944 2020-11-10 stsp outoff += n;
4177 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4179 fe621944 2020-11-10 stsp goto done;
4181 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4182 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4183 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4184 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4185 fe621944 2020-11-10 stsp if (n < 0) {
4186 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4187 fe621944 2020-11-10 stsp goto done;
4189 fe621944 2020-11-10 stsp outoff += n;
4190 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4192 fe621944 2020-11-10 stsp goto done;
4194 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4195 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4196 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4197 ac4dc263 2021-09-24 thomas int pn = 1;
4198 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4199 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4200 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4201 ac4dc263 2021-09-24 thomas if (err)
4202 ac4dc263 2021-09-24 thomas goto done;
4203 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4204 ac4dc263 2021-09-24 thomas if (n < 0) {
4205 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4206 ac4dc263 2021-09-24 thomas goto done;
4208 ac4dc263 2021-09-24 thomas outoff += n;
4209 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4210 ac4dc263 2021-09-24 thomas if (err)
4211 ac4dc263 2021-09-24 thomas goto done;
4212 ac4dc263 2021-09-24 thomas free(id_str);
4213 ac4dc263 2021-09-24 thomas id_str = NULL;
4217 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4219 5943eee2 2019-08-13 stsp goto done;
4220 fe621944 2020-11-10 stsp s = logmsg;
4221 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4222 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4223 fe621944 2020-11-10 stsp if (n < 0) {
4224 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4225 fe621944 2020-11-10 stsp goto done;
4227 fe621944 2020-11-10 stsp outoff += n;
4228 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4230 fe621944 2020-11-10 stsp goto done;
4233 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4235 0208f208 2020-05-05 stsp goto done;
4236 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4237 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4238 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4239 fe621944 2020-11-10 stsp if (n < 0) {
4240 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4241 fe621944 2020-11-10 stsp goto done;
4243 fe621944 2020-11-10 stsp outoff += n;
4244 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4246 fe621944 2020-11-10 stsp goto done;
4247 0208f208 2020-05-05 stsp free((char *)pe->path);
4248 0208f208 2020-05-05 stsp free(pe->data);
4251 0208f208 2020-05-05 stsp fputc('\n', outfile);
4253 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4255 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4256 abd2672a 2018-12-23 stsp free(id_str);
4257 5943eee2 2019-08-13 stsp free(logmsg);
4258 8b473291 2019-02-21 stsp free(refs_str);
4259 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4260 7510f233 2020-08-09 stsp if (err) {
4261 ae6a6978 2020-08-09 stsp free(*line_offsets);
4262 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4263 ae6a6978 2020-08-09 stsp *nlines = 0;
4265 fe621944 2020-11-10 stsp return err;
4268 abd2672a 2018-12-23 stsp static const struct got_error *
4269 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4271 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4272 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4273 15a94983 2018-12-23 stsp int obj_type;
4275 fe621944 2020-11-10 stsp free(s->line_offsets);
4276 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4277 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4278 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4279 fe621944 2020-11-10 stsp s->nlines = 0;
4281 511a516b 2018-05-19 stsp f = got_opentemp();
4282 48ae06ee 2018-10-18 stsp if (f == NULL) {
4283 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4284 48ae06ee 2018-10-18 stsp goto done;
4286 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4287 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4288 fb43ecf1 2019-02-11 stsp goto done;
4292 15a94983 2018-12-23 stsp if (s->id1)
4293 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4295 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4297 15a94983 2018-12-23 stsp goto done;
4299 15a94983 2018-12-23 stsp switch (obj_type) {
4300 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4301 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4302 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4303 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4304 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4306 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4307 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4308 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4309 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4310 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4312 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4313 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4314 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4315 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4316 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4318 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4320 3ffacbe1 2020-02-02 tracey goto done;
4321 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4322 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4323 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4324 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4325 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4326 f44b1f58 2020-02-02 tracey if (err)
4327 f44b1f58 2020-02-02 tracey goto done;
4328 f44b1f58 2020-02-02 tracey } else {
4329 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4330 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4331 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4332 fe621944 2020-11-10 stsp err = write_commit_info(
4333 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4334 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4335 f44b1f58 2020-02-02 tracey if (err)
4336 f44b1f58 2020-02-02 tracey goto done;
4341 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4343 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4344 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4345 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4346 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4350 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4353 f44b1f58 2020-02-02 tracey if (err)
4354 f44b1f58 2020-02-02 tracey goto done;
4356 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4357 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4358 48ae06ee 2018-10-18 stsp return err;
4361 f5215bb9 2019-02-22 stsp static void
4362 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4364 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4365 f5215bb9 2019-02-22 stsp update_panels();
4366 f5215bb9 2019-02-22 stsp doupdate();
4369 f44b1f58 2020-02-02 tracey static const struct got_error *
4370 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4372 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4374 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4375 f44b1f58 2020-02-02 tracey return NULL;
4378 f44b1f58 2020-02-02 tracey static const struct got_error *
4379 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4381 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4382 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4383 f44b1f58 2020-02-02 tracey int lineno;
4384 78eecffc 2022-06-23 thomas char *line = NULL;
4385 826082fe 2020-12-10 stsp size_t linesize = 0;
4386 826082fe 2020-12-10 stsp ssize_t linelen;
4388 f44b1f58 2020-02-02 tracey if (!view->searching) {
4389 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4390 f44b1f58 2020-02-02 tracey return NULL;
4393 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4394 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4395 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4397 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4399 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4401 f44b1f58 2020-02-02 tracey while (1) {
4402 f44b1f58 2020-02-02 tracey off_t offset;
4404 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4405 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4406 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4410 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4411 f44b1f58 2020-02-02 tracey lineno = 1;
4413 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4416 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4417 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4418 f44b1f58 2020-02-02 tracey free(line);
4419 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4421 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4422 78eecffc 2022-06-23 thomas if (linelen != -1) {
4423 78eecffc 2022-06-23 thomas char *exstr;
4424 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4425 78eecffc 2022-06-23 thomas if (err)
4427 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4428 78eecffc 2022-06-23 thomas &view->regmatch)) {
4429 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4430 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4431 78eecffc 2022-06-23 thomas free(exstr);
4434 78eecffc 2022-06-23 thomas free(exstr);
4436 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4437 f44b1f58 2020-02-02 tracey lineno++;
4439 f44b1f58 2020-02-02 tracey lineno--;
4441 826082fe 2020-12-10 stsp free(line);
4443 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4444 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4445 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4448 05171be4 2022-06-23 thomas return err;
4451 48ae06ee 2018-10-18 stsp static const struct got_error *
4452 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4454 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4455 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4457 a0f32f33 2022-06-13 thomas free(s->id1);
4458 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4459 a0f32f33 2022-06-13 thomas free(s->id2);
4460 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4461 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4462 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4463 a0f32f33 2022-06-13 thomas s->f = NULL;
4464 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4465 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4466 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4467 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4468 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4469 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4470 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4471 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4472 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4473 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4474 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4475 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4476 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4477 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4478 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4479 a0f32f33 2022-06-13 thomas s->nlines = 0;
4480 a0f32f33 2022-06-13 thomas return err;
4483 a0f32f33 2022-06-13 thomas static const struct got_error *
4484 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4485 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4486 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4487 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4489 48ae06ee 2018-10-18 stsp const struct got_error *err;
4490 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4492 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4493 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4494 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4496 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4497 15a94983 2018-12-23 stsp int type1, type2;
4498 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4500 15a94983 2018-12-23 stsp return err;
4501 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4503 15a94983 2018-12-23 stsp return err;
4505 15a94983 2018-12-23 stsp if (type1 != type2)
4506 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4508 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4509 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4510 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4511 f44b1f58 2020-02-02 tracey s->repo = repo;
4512 f44b1f58 2020-02-02 tracey s->id1 = id1;
4513 f44b1f58 2020-02-02 tracey s->id2 = id2;
4514 3dbaef42 2020-11-24 stsp s->label1 = label1;
4515 3dbaef42 2020-11-24 stsp s->label2 = label2;
4517 15a94983 2018-12-23 stsp if (id1) {
4518 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4519 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4520 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4522 5465d566 2020-02-01 tracey s->id1 = NULL;
4524 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4525 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4526 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4527 a0f32f33 2022-06-13 thomas goto done;
4530 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4531 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4532 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4533 9a1d7435 2022-06-23 thomas goto done;
4536 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4537 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4538 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4539 a0f32f33 2022-06-13 thomas goto done;
4542 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4543 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4544 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4545 19a6a6b5 2022-07-01 thomas goto done;
4548 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4549 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4550 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4551 19a6a6b5 2022-07-01 thomas goto done;
4554 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4555 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4556 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4557 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4558 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4559 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4560 5465d566 2020-02-01 tracey s->repo = repo;
4562 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4563 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4564 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4565 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4566 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4568 a0f32f33 2022-06-13 thomas goto done;
4569 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4570 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4571 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4572 a0f32f33 2022-06-13 thomas if (err)
4573 a0f32f33 2022-06-13 thomas goto done;
4574 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4575 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4576 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4577 a0f32f33 2022-06-13 thomas if (err)
4578 a0f32f33 2022-06-13 thomas goto done;
4580 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4581 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4582 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4583 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4584 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4585 a0f32f33 2022-06-13 thomas if (err)
4586 a0f32f33 2022-06-13 thomas goto done;
4588 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4589 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4590 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4591 a0f32f33 2022-06-13 thomas if (err)
4592 a0f32f33 2022-06-13 thomas goto done;
4594 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4595 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4596 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4597 a0f32f33 2022-06-13 thomas if (err)
4598 a0f32f33 2022-06-13 thomas goto done;
4601 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4602 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4603 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4604 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4606 5465d566 2020-02-01 tracey err = create_diff(s);
4608 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4609 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4610 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4611 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4612 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4613 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4615 a0f32f33 2022-06-13 thomas if (err)
4616 a0f32f33 2022-06-13 thomas close_diff_view(view);
4617 e5a0f69f 2018-08-18 stsp return err;
4620 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4621 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4623 a3404814 2018-09-02 stsp const struct got_error *err;
4624 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4625 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4626 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4628 a3404814 2018-09-02 stsp if (s->id1) {
4629 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4631 a3404814 2018-09-02 stsp return err;
4632 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4634 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4636 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4638 a3404814 2018-09-02 stsp return err;
4639 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4641 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4642 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4643 a3404814 2018-09-02 stsp free(id_str1);
4644 a3404814 2018-09-02 stsp free(id_str2);
4645 a3404814 2018-09-02 stsp return err;
4647 a3404814 2018-09-02 stsp free(id_str1);
4648 a3404814 2018-09-02 stsp free(id_str2);
4650 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4651 267bb3b8 2021-08-01 stsp free(header);
4652 267bb3b8 2021-08-01 stsp return err;
4655 15a087fe 2019-02-21 stsp static const struct got_error *
4656 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4657 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4659 d7a04538 2019-02-21 stsp const struct got_error *err;
4660 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4661 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4662 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4664 15a087fe 2019-02-21 stsp free(s->id2);
4665 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4666 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4667 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4669 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4671 d7a04538 2019-02-21 stsp return err;
4672 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4673 15a087fe 2019-02-21 stsp free(s->id1);
4674 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4675 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4676 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4677 15a087fe 2019-02-21 stsp return NULL;
4680 0cf4efb1 2018-09-29 stsp static const struct got_error *
4681 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4683 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4685 adf4c9e0 2022-07-03 thomas view->count = 0;
4686 adf4c9e0 2022-07-03 thomas wclear(view->window);
4687 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4688 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4689 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4690 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4691 adf4c9e0 2022-07-03 thomas return create_diff(s);
4694 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4695 4fc71f3b 2022-07-12 thomas int, int, int);
4696 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4697 4fc71f3b 2022-07-12 thomas int, int);
4699 adf4c9e0 2022-07-03 thomas static const struct got_error *
4700 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4702 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4703 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4704 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4705 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4706 826082fe 2020-12-10 stsp char *line = NULL;
4707 826082fe 2020-12-10 stsp size_t linesize = 0;
4708 826082fe 2020-12-10 stsp ssize_t linelen;
4709 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4711 e5a0f69f 2018-08-18 stsp switch (ch) {
4712 05171be4 2022-06-23 thomas case '0':
4713 05171be4 2022-06-23 thomas view->x = 0;
4715 05171be4 2022-06-23 thomas case '$':
4716 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4717 07b0611c 2022-06-23 thomas view->count = 0;
4719 05171be4 2022-06-23 thomas case KEY_RIGHT:
4720 05171be4 2022-06-23 thomas case 'l':
4721 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4722 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4724 07b0611c 2022-06-23 thomas view->count = 0;
4726 05171be4 2022-06-23 thomas case KEY_LEFT:
4727 05171be4 2022-06-23 thomas case 'h':
4728 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4729 07b0611c 2022-06-23 thomas if (view->x <= 0)
4730 07b0611c 2022-06-23 thomas view->count = 0;
4734 3dbaef42 2020-11-24 stsp if (ch == 'a')
4735 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4736 3dbaef42 2020-11-24 stsp if (ch == 'w')
4737 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4738 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4741 912a3f79 2021-08-30 j case KEY_HOME:
4742 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4743 07b0611c 2022-06-23 thomas view->count = 0;
4746 912a3f79 2021-08-30 j case KEY_END:
4747 07b0611c 2022-06-23 thomas view->count = 0;
4751 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4755 1e37a5c2 2019-05-12 jcs case KEY_UP:
4756 f7140bf5 2021-10-17 thomas case CTRL('p'):
4757 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4758 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4760 07b0611c 2022-06-23 thomas view->count = 0;
4762 70f17a53 2022-06-13 thomas case CTRL('u'):
4763 23427b14 2022-06-23 thomas case 'u':
4764 70f17a53 2022-06-13 thomas nscroll /= 2;
4765 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4766 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4767 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4768 1c5e5faa 2022-06-23 thomas case 'b':
4769 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4770 07b0611c 2022-06-23 thomas view->count = 0;
4774 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4775 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4778 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4779 f7140bf5 2021-10-17 thomas case CTRL('n'):
4780 1e37a5c2 2019-05-12 jcs if (!s->eof)
4781 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4783 07b0611c 2022-06-23 thomas view->count = 0;
4785 70f17a53 2022-06-13 thomas case CTRL('d'):
4786 23427b14 2022-06-23 thomas case 'd':
4787 70f17a53 2022-06-13 thomas nscroll /= 2;
4788 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4789 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4790 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4791 1c5e5faa 2022-06-23 thomas case 'f':
4793 07b0611c 2022-06-23 thomas if (s->eof) {
4794 07b0611c 2022-06-23 thomas view->count = 0;
4798 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4799 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4800 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4801 826082fe 2020-12-10 stsp if (linelen == -1) {
4802 826082fe 2020-12-10 stsp if (feof(s->f)) {
4803 826082fe 2020-12-10 stsp s->eof = 1;
4805 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4809 826082fe 2020-12-10 stsp free(line);
4812 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4813 1e37a5c2 2019-05-12 jcs s->diff_context--;
4814 aa61903a 2021-12-31 thomas s->matched_line = 0;
4815 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4816 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4817 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4818 27829c9e 2020-11-21 stsp s->nlines) {
4819 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4820 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4823 07b0611c 2022-06-23 thomas view->count = 0;
4826 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4827 1e37a5c2 2019-05-12 jcs s->diff_context++;
4828 aa61903a 2021-12-31 thomas s->matched_line = 0;
4829 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4830 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4832 07b0611c 2022-06-23 thomas view->count = 0;
4836 777aae21 2022-07-20 thomas case 'K':
4838 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4839 4fc71f3b 2022-07-12 thomas case '>':
4840 4fc71f3b 2022-07-12 thomas case '.':
4841 777aae21 2022-07-20 thomas case 'J':
4842 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4843 07b0611c 2022-06-23 thomas view->count = 0;
4846 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4848 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4849 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4850 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4852 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4853 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4854 4fc71f3b 2022-07-12 thomas if (err)
4856 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4858 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4861 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4862 4fc71f3b 2022-07-12 thomas if (err)
4864 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4865 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4866 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4868 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4869 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4870 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4872 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4873 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4874 4fc71f3b 2022-07-12 thomas if (err)
4876 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4878 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4880 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4881 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4882 4fc71f3b 2022-07-12 thomas bs->selected_line);
4883 4fc71f3b 2022-07-12 thomas if (id == NULL)
4886 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4889 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4890 4fc71f3b 2022-07-12 thomas if (err)
4893 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4894 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4895 aa61903a 2021-12-31 thomas s->matched_line = 0;
4896 05171be4 2022-06-23 thomas view->x = 0;
4898 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4899 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4902 07b0611c 2022-06-23 thomas view->count = 0;
4906 bcbd79e2 2018-08-19 stsp return err;
4909 4ed7e80c 2018-05-20 stsp static const struct got_error *
4910 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4912 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4913 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4914 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4915 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4916 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4917 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4918 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4919 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4920 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4921 3dbaef42 2020-11-24 stsp const char *errstr;
4922 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4923 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4925 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4926 26ed57b2 2018-05-19 stsp switch (ch) {
4928 64453f7e 2020-11-21 stsp force_text_diff = 1;
4931 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4933 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4934 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4935 8f666e67 2022-02-12 thomas errstr, errstr);
4937 09b5bff8 2020-02-23 naddy case 'r':
4938 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4939 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4940 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4942 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4945 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4948 17020d27 2019-03-07 stsp usage_diff();
4949 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4953 26ed57b2 2018-05-19 stsp argc -= optind;
4954 26ed57b2 2018-05-19 stsp argv += optind;
4956 26ed57b2 2018-05-19 stsp if (argc == 0) {
4957 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4958 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4959 15a94983 2018-12-23 stsp id_str1 = argv[0];
4960 15a94983 2018-12-23 stsp id_str2 = argv[1];
4962 26ed57b2 2018-05-19 stsp usage_diff();
4964 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4965 7cd52833 2022-06-23 thomas if (error)
4966 7cd52833 2022-06-23 thomas goto done;
4968 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4969 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4970 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4971 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4972 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4973 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4974 c156c7a4 2020-12-18 stsp goto done;
4975 a273ac94 2020-02-23 naddy if (worktree)
4976 a273ac94 2020-02-23 naddy repo_path =
4977 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4979 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4980 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4981 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4982 c156c7a4 2020-12-18 stsp goto done;
4986 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4987 eb6600df 2019-01-04 stsp if (error)
4988 eb6600df 2019-01-04 stsp goto done;
4990 a273ac94 2020-02-23 naddy init_curses();
4992 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4993 51a10b52 2020-12-26 stsp if (error)
4994 51a10b52 2020-12-26 stsp goto done;
4996 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4997 26ed57b2 2018-05-19 stsp if (error)
4998 26ed57b2 2018-05-19 stsp goto done;
5000 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5001 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5002 26ed57b2 2018-05-19 stsp if (error)
5003 26ed57b2 2018-05-19 stsp goto done;
5005 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5006 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5007 26ed57b2 2018-05-19 stsp if (error)
5008 26ed57b2 2018-05-19 stsp goto done;
5010 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5011 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5012 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5013 ea5e7bb5 2018-08-01 stsp goto done;
5015 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5016 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5017 5dc9f4bc 2018-08-04 stsp if (error)
5018 5dc9f4bc 2018-08-04 stsp goto done;
5019 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5021 3dbaef42 2020-11-24 stsp free(label1);
5022 3dbaef42 2020-11-24 stsp free(label2);
5023 c02c541e 2019-03-29 stsp free(repo_path);
5024 a273ac94 2020-02-23 naddy free(cwd);
5025 1d0f4054 2021-06-17 stsp if (repo) {
5026 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5027 1d0f4054 2021-06-17 stsp if (error == NULL)
5028 1d0f4054 2021-06-17 stsp error = close_err;
5030 a273ac94 2020-02-23 naddy if (worktree)
5031 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5032 7cd52833 2022-06-23 thomas if (pack_fds) {
5033 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5034 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5035 7cd52833 2022-06-23 thomas if (error == NULL)
5036 7cd52833 2022-06-23 thomas error = pack_err;
5038 51a10b52 2020-12-26 stsp tog_free_refs();
5039 26ed57b2 2018-05-19 stsp return error;
5042 4ed7e80c 2018-05-20 stsp __dead static void
5043 9f7d7167 2018-04-29 stsp usage_blame(void)
5046 91198554 2022-06-23 thomas fprintf(stderr,
5047 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5048 9f7d7167 2018-04-29 stsp getprogname());
5052 84451b3e 2018-07-10 stsp struct tog_blame_line {
5053 84451b3e 2018-07-10 stsp int annotated;
5054 84451b3e 2018-07-10 stsp struct got_object_id *id;
5057 4ed7e80c 2018-05-20 stsp static const struct got_error *
5058 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5060 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5061 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5062 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5063 84451b3e 2018-07-10 stsp const struct got_error *err;
5064 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5065 826082fe 2020-12-10 stsp char *line = NULL;
5066 826082fe 2020-12-10 stsp size_t linesize = 0;
5067 826082fe 2020-12-10 stsp ssize_t linelen;
5068 84451b3e 2018-07-10 stsp wchar_t *wline;
5069 27a741e5 2019-09-11 stsp int width;
5070 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5071 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5072 ab089a2a 2018-07-12 stsp char *id_str;
5073 11b20872 2019-11-08 stsp struct tog_color *tc;
5075 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5077 ab089a2a 2018-07-12 stsp return err;
5079 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5080 f7d12f7e 2018-08-01 stsp werase(view->window);
5082 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5083 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5084 ab089a2a 2018-07-12 stsp free(id_str);
5085 ab089a2a 2018-07-12 stsp return err;
5088 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5089 ab089a2a 2018-07-12 stsp free(line);
5090 2550e4c3 2018-07-13 stsp line = NULL;
5092 1cae65b4 2019-09-22 stsp return err;
5093 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5094 a3404814 2018-09-02 stsp wstandout(view->window);
5095 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5097 11b20872 2019-11-08 stsp wattr_on(view->window,
5098 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5099 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5101 11b20872 2019-11-08 stsp wattr_off(view->window,
5102 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5103 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5104 a3404814 2018-09-02 stsp wstandend(view->window);
5105 2550e4c3 2018-07-13 stsp free(wline);
5106 2550e4c3 2018-07-13 stsp wline = NULL;
5107 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5108 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5110 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5111 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5112 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5113 ab089a2a 2018-07-12 stsp free(id_str);
5114 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5116 ab089a2a 2018-07-12 stsp free(id_str);
5117 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5118 3f60a8ef 2018-07-10 stsp free(line);
5119 2550e4c3 2018-07-13 stsp line = NULL;
5121 3f60a8ef 2018-07-10 stsp return err;
5122 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5123 2550e4c3 2018-07-13 stsp free(wline);
5124 2550e4c3 2018-07-13 stsp wline = NULL;
5125 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5126 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5128 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5129 05171be4 2022-06-23 thomas view->maxx = 0;
5130 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5131 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5132 826082fe 2020-12-10 stsp if (linelen == -1) {
5133 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5134 826082fe 2020-12-10 stsp s->eof = 1;
5137 84451b3e 2018-07-10 stsp free(line);
5138 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5140 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5143 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5144 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5145 cbea3800 2022-06-23 thomas if (err) {
5146 cbea3800 2022-06-23 thomas free(line);
5147 cbea3800 2022-06-23 thomas return err;
5149 cbea3800 2022-06-23 thomas free(wline);
5150 cbea3800 2022-06-23 thomas wline = NULL;
5151 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5153 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5154 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5156 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5157 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5158 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5159 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5160 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5161 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5162 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5163 8d0fe45a 2019-08-12 stsp char *id_str;
5164 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5165 91198554 2022-06-23 thomas blame_line->id);
5166 8d0fe45a 2019-08-12 stsp if (err) {
5167 8d0fe45a 2019-08-12 stsp free(line);
5168 8d0fe45a 2019-08-12 stsp return err;
5170 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5172 11b20872 2019-11-08 stsp wattr_on(view->window,
5173 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5174 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5176 11b20872 2019-11-08 stsp wattr_off(view->window,
5177 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5178 8d0fe45a 2019-08-12 stsp free(id_str);
5179 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5181 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5182 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5185 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5186 ee41ec32 2018-07-10 stsp prev_id = NULL;
5189 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5190 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5191 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5193 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5194 41605754 2020-11-12 stsp width = 9;
5195 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5196 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5197 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5198 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5199 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5200 41605754 2020-11-12 stsp if (err) {
5201 41605754 2020-11-12 stsp free(line);
5202 41605754 2020-11-12 stsp return err;
5204 41605754 2020-11-12 stsp width += 9;
5206 923086fd 2022-06-23 thomas int skip;
5207 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5208 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5209 923086fd 2022-06-23 thomas if (err) {
5210 923086fd 2022-06-23 thomas free(line);
5211 923086fd 2022-06-23 thomas return err;
5213 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5214 02bce7e2 2022-06-23 thomas width += 9;
5215 41605754 2020-11-12 stsp free(wline);
5216 41605754 2020-11-12 stsp wline = NULL;
5219 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5220 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5221 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5222 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5224 826082fe 2020-12-10 stsp free(line);
5225 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5227 a5d43cac 2022-07-01 thomas view_border(view);
5229 84451b3e 2018-07-10 stsp return NULL;
5232 84451b3e 2018-07-10 stsp static const struct got_error *
5233 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5234 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5236 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5237 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5238 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5239 1a76625f 2018-10-22 stsp int errcode;
5241 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5242 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5243 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5245 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5246 1a76625f 2018-10-22 stsp if (errcode)
5247 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5249 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5250 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5251 d68a0a7d 2018-07-10 stsp goto done;
5254 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5255 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5257 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5258 d68a0a7d 2018-07-10 stsp if (line->annotated)
5259 d68a0a7d 2018-07-10 stsp goto done;
5261 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5262 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5263 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5264 84451b3e 2018-07-10 stsp goto done;
5266 84451b3e 2018-07-10 stsp line->annotated = 1;
5268 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5269 1a76625f 2018-10-22 stsp if (errcode)
5270 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5271 84451b3e 2018-07-10 stsp return err;
5274 84451b3e 2018-07-10 stsp static void *
5275 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5277 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5278 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5279 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5280 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5281 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5283 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5284 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5285 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5287 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5288 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5289 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5290 9117a7b7 2022-07-01 thomas goto done;
5293 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5294 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5295 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5296 9117a7b7 2022-07-01 thomas goto done;
5298 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5299 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5300 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5301 9117a7b7 2022-07-01 thomas goto done;
5304 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5306 9117a7b7 2022-07-01 thomas goto done;
5308 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5309 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5310 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5311 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5312 fc06ba56 2019-08-22 stsp err = NULL;
5314 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5315 9117a7b7 2022-07-01 thomas if (errcode) {
5316 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5317 9117a7b7 2022-07-01 thomas goto done;
5320 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5321 1d0f4054 2021-06-17 stsp if (err == NULL)
5322 1d0f4054 2021-06-17 stsp err = close_err;
5323 c9beca56 2018-07-22 stsp ta->repo = NULL;
5324 c9beca56 2018-07-22 stsp *ta->complete = 1;
5326 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5327 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5328 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5331 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5332 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5333 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5334 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5335 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5336 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5337 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5338 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5340 18430de3 2018-07-10 stsp return (void *)err;
5343 245d91c1 2018-07-12 stsp static struct got_object_id *
5344 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5345 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5347 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5349 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5350 8d0fe45a 2019-08-12 stsp return NULL;
5352 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5353 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5354 4fc71f3b 2022-07-12 thomas return NULL;
5356 4fc71f3b 2022-07-12 thomas return line->id;
5359 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5360 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5361 4fc71f3b 2022-07-12 thomas int lineno)
5363 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5365 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5366 4fc71f3b 2022-07-12 thomas return NULL;
5368 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5369 245d91c1 2018-07-12 stsp if (!line->annotated)
5370 245d91c1 2018-07-12 stsp return NULL;
5372 245d91c1 2018-07-12 stsp return line->id;
5375 b880a918 2018-07-10 stsp static const struct got_error *
5376 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5378 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5381 245d91c1 2018-07-12 stsp if (blame->thread) {
5382 1a76625f 2018-10-22 stsp int errcode;
5383 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5384 1a76625f 2018-10-22 stsp if (errcode)
5385 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5386 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5387 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5388 1a76625f 2018-10-22 stsp if (errcode)
5389 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5390 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5391 1a76625f 2018-10-22 stsp if (errcode)
5392 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5393 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5394 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5395 245d91c1 2018-07-12 stsp err = NULL;
5396 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5398 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5399 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5400 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5401 1d0f4054 2021-06-17 stsp if (err == NULL)
5402 1d0f4054 2021-06-17 stsp err = close_err;
5403 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5405 245d91c1 2018-07-12 stsp if (blame->f) {
5406 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5407 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5408 245d91c1 2018-07-12 stsp blame->f = NULL;
5410 57670559 2018-12-23 stsp if (blame->lines) {
5411 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5412 57670559 2018-12-23 stsp free(blame->lines[i].id);
5413 57670559 2018-12-23 stsp free(blame->lines);
5414 57670559 2018-12-23 stsp blame->lines = NULL;
5416 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5417 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5418 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5419 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5420 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5421 7cd52833 2022-06-23 thomas if (err == NULL)
5422 7cd52833 2022-06-23 thomas err = pack_err;
5423 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5425 245d91c1 2018-07-12 stsp return err;
5428 245d91c1 2018-07-12 stsp static const struct got_error *
5429 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5431 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5432 fc06ba56 2019-08-22 stsp int *done = arg;
5433 fc06ba56 2019-08-22 stsp int errcode;
5435 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5436 fc06ba56 2019-08-22 stsp if (errcode)
5437 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5438 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5440 fc06ba56 2019-08-22 stsp if (*done)
5441 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5443 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5444 fc06ba56 2019-08-22 stsp if (errcode)
5445 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5446 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5448 fc06ba56 2019-08-22 stsp return err;
5451 fc06ba56 2019-08-22 stsp static const struct got_error *
5452 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5454 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5455 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5456 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5457 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5458 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5459 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5460 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5461 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5462 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5464 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5465 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5467 15a94983 2018-12-23 stsp return err;
5469 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5470 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5471 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5472 f4ae6ddb 2022-07-01 thomas goto done;
5475 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5476 945f9229 2022-04-16 thomas if (err)
5477 945f9229 2022-04-16 thomas goto done;
5479 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5481 84451b3e 2018-07-10 stsp goto done;
5483 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5484 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5485 84451b3e 2018-07-10 stsp goto done;
5488 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5490 a70480e0 2018-06-23 stsp goto done;
5491 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5492 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5493 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5494 84451b3e 2018-07-10 stsp goto done;
5496 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5497 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5499 1fddf795 2021-01-20 stsp goto done;
5500 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5501 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5502 84451b3e 2018-07-10 stsp goto done;
5505 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5506 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5507 b02560ec 2019-08-19 stsp blame->nlines--;
5509 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5510 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5511 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5512 84451b3e 2018-07-10 stsp goto done;
5515 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5517 bd24772e 2018-07-11 stsp goto done;
5518 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5519 7cd52833 2022-06-23 thomas pack_fds);
5520 7cd52833 2022-06-23 thomas if (err)
5521 7cd52833 2022-06-23 thomas goto done;
5523 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5524 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5525 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5526 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5527 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5528 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5529 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5530 245d91c1 2018-07-12 stsp goto done;
5532 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5534 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5535 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5536 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5537 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5538 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5539 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5540 a5388363 2020-12-01 naddy s->blame_complete = 0;
5542 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5543 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5544 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5545 f5a09613 2020-12-13 naddy s->selected_line = 1;
5547 aa61903a 2021-12-31 thomas s->matched_line = 0;
5550 945f9229 2022-04-16 thomas if (commit)
5551 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5552 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5553 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5555 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5556 27d434c2 2018-09-15 stsp free(obj_id);
5558 245d91c1 2018-07-12 stsp stop_blame(blame);
5559 245d91c1 2018-07-12 stsp return err;
5562 245d91c1 2018-07-12 stsp static const struct got_error *
5563 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5564 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5566 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5567 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5569 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5571 c4843652 2019-08-12 stsp s->path = strdup(path);
5572 c4843652 2019-08-12 stsp if (s->path == NULL)
5573 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5575 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5576 c4843652 2019-08-12 stsp if (err) {
5577 c4843652 2019-08-12 stsp free(s->path);
5578 7cbe629d 2018-08-04 stsp return err;
5581 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5582 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5583 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5584 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5585 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5586 fb2756b9 2018-08-04 stsp s->repo = repo;
5587 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5588 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5590 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5591 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5592 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5593 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5595 11b20872 2019-11-08 stsp return err;
5598 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5599 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5600 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5601 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5602 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5603 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5605 a5388363 2020-12-01 naddy return run_blame(view);
5608 e5a0f69f 2018-08-18 stsp static const struct got_error *
5609 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5611 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5612 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5614 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5615 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5617 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5618 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5619 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5620 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5621 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5624 e5a0f69f 2018-08-18 stsp free(s->path);
5625 11b20872 2019-11-08 stsp free_colors(&s->colors);
5626 e5a0f69f 2018-08-18 stsp return err;
5629 7cbe629d 2018-08-04 stsp static const struct got_error *
5630 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5632 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5634 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5635 6c4c42e0 2019-06-24 stsp return NULL;
5638 6c4c42e0 2019-06-24 stsp static const struct got_error *
5639 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5641 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5642 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5643 6c4c42e0 2019-06-24 stsp int lineno;
5644 78eecffc 2022-06-23 thomas char *line = NULL;
5645 826082fe 2020-12-10 stsp size_t linesize = 0;
5646 826082fe 2020-12-10 stsp ssize_t linelen;
5648 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5649 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5650 6c4c42e0 2019-06-24 stsp return NULL;
5653 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5654 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5655 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5657 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5659 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5661 6c4c42e0 2019-06-24 stsp while (1) {
5662 6c4c42e0 2019-06-24 stsp off_t offset;
5664 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5665 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5666 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5670 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5671 6c4c42e0 2019-06-24 stsp lineno = 1;
5673 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5676 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5677 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5678 6c4c42e0 2019-06-24 stsp free(line);
5679 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5681 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5682 78eecffc 2022-06-23 thomas if (linelen != -1) {
5683 78eecffc 2022-06-23 thomas char *exstr;
5684 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5685 78eecffc 2022-06-23 thomas if (err)
5687 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5688 78eecffc 2022-06-23 thomas &view->regmatch)) {
5689 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5690 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5691 78eecffc 2022-06-23 thomas free(exstr);
5694 78eecffc 2022-06-23 thomas free(exstr);
5696 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5701 826082fe 2020-12-10 stsp free(line);
5703 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5704 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5705 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5708 05171be4 2022-06-23 thomas return err;
5711 6c4c42e0 2019-06-24 stsp static const struct got_error *
5712 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5714 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5715 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5716 2b380cc8 2018-10-24 stsp int errcode;
5718 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5719 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5720 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5721 2b380cc8 2018-10-24 stsp if (errcode)
5722 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5724 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5727 51fe7530 2019-08-19 stsp if (s->blame_complete)
5728 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5730 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5732 a5d43cac 2022-07-01 thomas view_border(view);
5733 e5a0f69f 2018-08-18 stsp return err;
5736 e5a0f69f 2018-08-18 stsp static const struct got_error *
5737 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5738 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5740 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5741 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5743 eaeaa612 2022-07-20 thomas *new_view = NULL;
5745 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5746 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5747 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5749 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5750 eaeaa612 2022-07-20 thomas if (err)
5751 eaeaa612 2022-07-20 thomas view_close(log_view);
5753 eaeaa612 2022-07-20 thomas *new_view = log_view;
5755 eaeaa612 2022-07-20 thomas return err;
5758 eaeaa612 2022-07-20 thomas static const struct got_error *
5759 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5761 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5762 eaeaa612 2022-07-20 thomas struct tog_view *diff_view, *log_view;
5763 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5764 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5766 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5767 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5768 a5d43cac 2022-07-01 thomas --eos; /* border */
5770 e5a0f69f 2018-08-18 stsp switch (ch) {
5771 05171be4 2022-06-23 thomas case '0':
5772 05171be4 2022-06-23 thomas view->x = 0;
5774 05171be4 2022-06-23 thomas case '$':
5775 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5776 07b0611c 2022-06-23 thomas view->count = 0;
5778 05171be4 2022-06-23 thomas case KEY_RIGHT:
5779 05171be4 2022-06-23 thomas case 'l':
5780 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5781 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5783 07b0611c 2022-06-23 thomas view->count = 0;
5785 05171be4 2022-06-23 thomas case KEY_LEFT:
5786 05171be4 2022-06-23 thomas case 'h':
5787 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5788 07b0611c 2022-06-23 thomas if (view->x <= 0)
5789 07b0611c 2022-06-23 thomas view->count = 0;
5792 1e37a5c2 2019-05-12 jcs s->done = 1;
5794 4deef56f 2021-09-02 naddy case 'g':
5795 4deef56f 2021-09-02 naddy case KEY_HOME:
5796 4deef56f 2021-09-02 naddy s->selected_line = 1;
5797 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5798 07b0611c 2022-06-23 thomas view->count = 0;
5800 4deef56f 2021-09-02 naddy case 'G':
5801 4deef56f 2021-09-02 naddy case KEY_END:
5802 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5803 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5804 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5806 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5807 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5809 07b0611c 2022-06-23 thomas view->count = 0;
5812 1e37a5c2 2019-05-12 jcs case KEY_UP:
5813 f7140bf5 2021-10-17 thomas case CTRL('p'):
5814 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5815 1e37a5c2 2019-05-12 jcs s->selected_line--;
5816 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5817 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5818 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5820 07b0611c 2022-06-23 thomas view->count = 0;
5822 70f17a53 2022-06-13 thomas case CTRL('u'):
5823 23427b14 2022-06-23 thomas case 'u':
5824 70f17a53 2022-06-13 thomas nscroll /= 2;
5825 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5826 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5827 ea025d1d 2020-02-22 naddy case CTRL('b'):
5828 1c5e5faa 2022-06-23 thomas case 'b':
5829 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5830 07b0611c 2022-06-23 thomas if (view->count > 1)
5831 07b0611c 2022-06-23 thomas nscroll += nscroll;
5832 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5833 07b0611c 2022-06-23 thomas view->count = 0;
5836 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5837 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5839 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5842 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5843 f7140bf5 2021-10-17 thomas case CTRL('n'):
5844 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5845 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5846 1e37a5c2 2019-05-12 jcs s->selected_line++;
5847 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5848 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5850 07b0611c 2022-06-23 thomas view->count = 0;
5852 1c5e5faa 2022-06-23 thomas case 'c':
5853 1e37a5c2 2019-05-12 jcs case 'p': {
5854 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5856 07b0611c 2022-06-23 thomas view->count = 0;
5857 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5858 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5859 1e37a5c2 2019-05-12 jcs if (id == NULL)
5861 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5862 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5863 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5864 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5865 1e37a5c2 2019-05-12 jcs int obj_type;
5866 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5867 1e37a5c2 2019-05-12 jcs s->repo, id);
5870 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5871 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5872 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5873 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5876 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5877 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5878 ec242592 2022-04-22 thomas s->repo, &pid->id);
5879 945f9229 2022-04-16 thomas if (err)
5881 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5882 945f9229 2022-04-16 thomas pcommit, s->path);
5883 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5884 e5a0f69f 2018-08-18 stsp if (err) {
5885 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5886 1e37a5c2 2019-05-12 jcs err = NULL;
5887 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5890 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5892 1e37a5c2 2019-05-12 jcs free(blob_id);
5893 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5894 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5895 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5898 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5899 ec242592 2022-04-22 thomas &pid->id);
5900 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5902 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5903 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5905 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5910 1e37a5c2 2019-05-12 jcs s->done = 1;
5911 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5912 1e37a5c2 2019-05-12 jcs s->done = 0;
5913 1e37a5c2 2019-05-12 jcs if (thread_err)
5915 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5916 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5917 a5388363 2020-12-01 naddy err = run_blame(view);
5922 1c5e5faa 2022-06-23 thomas case 'C': {
5923 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5925 07b0611c 2022-06-23 thomas view->count = 0;
5926 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5927 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5929 1e37a5c2 2019-05-12 jcs s->done = 1;
5930 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5931 1e37a5c2 2019-05-12 jcs s->done = 0;
5932 1e37a5c2 2019-05-12 jcs if (thread_err)
5934 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5935 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5936 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5937 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5938 a5388363 2020-12-01 naddy err = run_blame(view);
5943 eaeaa612 2022-07-20 thomas case 'L': {
5944 eaeaa612 2022-07-20 thomas struct got_object_id *id = NULL;
5946 eaeaa612 2022-07-20 thomas view->count = 0;
5947 eaeaa612 2022-07-20 thomas id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5948 eaeaa612 2022-07-20 thomas s->first_displayed_line, s->selected_line);
5949 eaeaa612 2022-07-20 thomas if (id == NULL)
5952 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view))
5953 eaeaa612 2022-07-20 thomas view_get_split(view, &begin_y, &begin_x);
5954 eaeaa612 2022-07-20 thomas err = log_annotated_line(&log_view, begin_y, begin_x,
5955 eaeaa612 2022-07-20 thomas s->repo, id);
5956 eaeaa612 2022-07-20 thomas if (err)
5958 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view) &&
5959 eaeaa612 2022-07-20 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5960 eaeaa612 2022-07-20 thomas err = view_init_hsplit(view, begin_y);
5961 eaeaa612 2022-07-20 thomas if (err)
5965 eaeaa612 2022-07-20 thomas view->focussed = 0;
5966 eaeaa612 2022-07-20 thomas log_view->focussed = 1;
5967 eaeaa612 2022-07-20 thomas log_view->mode = view->mode;
5968 eaeaa612 2022-07-20 thomas log_view->nlines = view->lines - begin_y;
5969 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view)) {
5970 eaeaa612 2022-07-20 thomas view_transfer_size(log_view, view);
5971 eaeaa612 2022-07-20 thomas err = view_close_child(view);
5972 eaeaa612 2022-07-20 thomas if (err)
5973 eaeaa612 2022-07-20 thomas return err;
5974 eaeaa612 2022-07-20 thomas err = view_set_child(view, log_view);
5975 eaeaa612 2022-07-20 thomas if (err)
5976 eaeaa612 2022-07-20 thomas return err;
5977 eaeaa612 2022-07-20 thomas view->focus_child = 1;
5979 eaeaa612 2022-07-20 thomas *new_view = log_view;
5982 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5983 1e37a5c2 2019-05-12 jcs case '\r': {
5984 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5985 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5986 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5988 07b0611c 2022-06-23 thomas view->count = 0;
5989 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5990 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5991 1e37a5c2 2019-05-12 jcs if (id == NULL)
5993 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5996 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5997 4fc71f3b 2022-07-12 thomas if (*new_view) {
5998 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5999 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6000 4fc71f3b 2022-07-12 thomas if (err)
6002 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6003 4fc71f3b 2022-07-12 thomas } else {
6004 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6005 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6007 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6008 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6009 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6010 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6011 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6015 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6016 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6017 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6019 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6022 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6023 4fc71f3b 2022-07-12 thomas s->selected_line;
6024 4fc71f3b 2022-07-12 thomas if (*new_view)
6025 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6026 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6027 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6028 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6029 a5d43cac 2022-07-01 thomas if (err)
6033 e78dc838 2020-12-04 stsp view->focussed = 0;
6034 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6035 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6036 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6037 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6038 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6039 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6042 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6043 40236d76 2022-06-23 thomas if (err)
6045 e78dc838 2020-12-04 stsp view->focus_child = 1;
6047 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6052 70f17a53 2022-06-13 thomas case CTRL('d'):
6053 23427b14 2022-06-23 thomas case 'd':
6054 70f17a53 2022-06-13 thomas nscroll /= 2;
6055 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6056 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6057 ea025d1d 2020-02-22 naddy case CTRL('f'):
6058 1c5e5faa 2022-06-23 thomas case 'f':
6060 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6061 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6062 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6063 07b0611c 2022-06-23 thomas view->count = 0;
6066 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6067 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6068 70f17a53 2022-06-13 thomas s->selected_line +=
6069 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6070 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6072 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6073 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6075 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6076 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6078 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6079 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6080 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6081 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6085 07b0611c 2022-06-23 thomas view->count = 0;
6088 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6091 adf4c9e0 2022-07-03 thomas static const struct got_error *
6092 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6094 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6095 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6097 adf4c9e0 2022-07-03 thomas view->count = 0;
6098 adf4c9e0 2022-07-03 thomas s->done = 1;
6099 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6100 adf4c9e0 2022-07-03 thomas s->done = 0;
6101 adf4c9e0 2022-07-03 thomas if (err)
6102 adf4c9e0 2022-07-03 thomas return err;
6103 adf4c9e0 2022-07-03 thomas return run_blame(view);
6106 a70480e0 2018-06-23 stsp static const struct got_error *
6107 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6109 a70480e0 2018-06-23 stsp const struct got_error *error;
6110 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6111 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6112 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6113 0587e10c 2020-07-23 stsp char *link_target = NULL;
6114 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6115 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6116 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6118 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6119 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6121 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6122 a70480e0 2018-06-23 stsp switch (ch) {
6124 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6127 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6128 69069811 2018-08-02 stsp if (repo_path == NULL)
6129 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6133 17020d27 2019-03-07 stsp usage_blame();
6134 a70480e0 2018-06-23 stsp /* NOTREACHED */
6138 a70480e0 2018-06-23 stsp argc -= optind;
6139 a70480e0 2018-06-23 stsp argv += optind;
6141 f135c941 2020-02-20 stsp if (argc != 1)
6142 a70480e0 2018-06-23 stsp usage_blame();
6144 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6145 7cd52833 2022-06-23 thomas if (error != NULL)
6146 7cd52833 2022-06-23 thomas goto done;
6148 69069811 2018-08-02 stsp if (repo_path == NULL) {
6149 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6150 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6151 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6152 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6153 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6154 c156c7a4 2020-12-18 stsp goto done;
6155 f135c941 2020-02-20 stsp if (worktree)
6156 eb41ed75 2019-02-05 stsp repo_path =
6157 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6159 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6160 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6161 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6162 c156c7a4 2020-12-18 stsp goto done;
6166 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6167 c02c541e 2019-03-29 stsp if (error != NULL)
6168 8e94dd5b 2019-01-04 stsp goto done;
6170 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6171 f135c941 2020-02-20 stsp worktree);
6172 c02c541e 2019-03-29 stsp if (error)
6173 92205607 2019-01-04 stsp goto done;
6175 f135c941 2020-02-20 stsp init_curses();
6177 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6178 51a10b52 2020-12-26 stsp if (error)
6179 51a10b52 2020-12-26 stsp goto done;
6181 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6182 eb41ed75 2019-02-05 stsp if (error)
6183 69069811 2018-08-02 stsp goto done;
6185 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6186 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6187 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6188 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6189 a70480e0 2018-06-23 stsp if (error != NULL)
6190 66b4983c 2018-06-23 stsp goto done;
6191 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6192 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6194 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6195 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6197 a19e88aa 2018-06-23 stsp if (error != NULL)
6198 8b473291 2019-02-21 stsp goto done;
6200 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6201 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6202 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6203 e1cd8fed 2018-08-01 stsp goto done;
6206 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6207 945f9229 2022-04-16 thomas if (error)
6208 945f9229 2022-04-16 thomas goto done;
6210 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6211 945f9229 2022-04-16 thomas commit, repo);
6212 7cbe629d 2018-08-04 stsp if (error)
6213 7cbe629d 2018-08-04 stsp goto done;
6215 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6216 78756c87 2020-11-24 stsp commit_id, repo);
6217 0587e10c 2020-07-23 stsp if (error)
6218 0587e10c 2020-07-23 stsp goto done;
6219 12314ad4 2019-08-31 stsp if (worktree) {
6220 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6221 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6222 12314ad4 2019-08-31 stsp worktree = NULL;
6224 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6226 69069811 2018-08-02 stsp free(repo_path);
6227 f135c941 2020-02-20 stsp free(in_repo_path);
6228 0587e10c 2020-07-23 stsp free(link_target);
6229 69069811 2018-08-02 stsp free(cwd);
6230 a70480e0 2018-06-23 stsp free(commit_id);
6231 945f9229 2022-04-16 thomas if (commit)
6232 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6233 eb41ed75 2019-02-05 stsp if (worktree)
6234 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6235 1d0f4054 2021-06-17 stsp if (repo) {
6236 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6237 1d0f4054 2021-06-17 stsp if (error == NULL)
6238 1d0f4054 2021-06-17 stsp error = close_err;
6240 7cd52833 2022-06-23 thomas if (pack_fds) {
6241 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6242 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6243 7cd52833 2022-06-23 thomas if (error == NULL)
6244 7cd52833 2022-06-23 thomas error = pack_err;
6246 51a10b52 2020-12-26 stsp tog_free_refs();
6247 a70480e0 2018-06-23 stsp return error;
6250 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6251 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6253 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6254 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6255 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6256 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6257 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6258 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6259 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6261 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6262 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6263 a5d43cac 2022-07-01 thomas --limit; /* border */
6265 f7d12f7e 2018-08-01 stsp werase(view->window);
6267 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6268 ffd1d5e5 2018-06-23 stsp return NULL;
6270 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6273 ffd1d5e5 2018-06-23 stsp return err;
6274 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6275 a3404814 2018-09-02 stsp wstandout(view->window);
6276 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6278 11b20872 2019-11-08 stsp wattr_on(view->window,
6279 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6280 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6282 11b20872 2019-11-08 stsp wattr_off(view->window,
6283 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6284 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6285 a3404814 2018-09-02 stsp wstandend(view->window);
6286 2550e4c3 2018-07-13 stsp free(wline);
6287 2550e4c3 2018-07-13 stsp wline = NULL;
6288 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6289 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6290 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6291 ffd1d5e5 2018-06-23 stsp return NULL;
6292 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6295 ce52c690 2018-06-23 stsp return err;
6296 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6297 2550e4c3 2018-07-13 stsp free(wline);
6298 2550e4c3 2018-07-13 stsp wline = NULL;
6299 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6300 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6301 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6302 ffd1d5e5 2018-06-23 stsp return NULL;
6303 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6304 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6305 a1eca9bb 2018-06-23 stsp return NULL;
6307 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6308 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6309 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6310 0cf4efb1 2018-09-29 stsp if (view->focussed)
6311 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6312 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6314 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6315 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6316 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6317 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6318 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6319 ffd1d5e5 2018-06-23 stsp return NULL;
6323 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6326 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6327 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6328 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6329 848d6979 2019-08-12 stsp const char *modestr = "";
6330 56e0773d 2019-11-28 stsp mode_t mode;
6332 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6333 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6335 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6336 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6337 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6339 638f9024 2019-05-13 stsp return got_error_from_errno(
6340 230a42bd 2019-05-11 jcs "got_object_id_str");
6342 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6343 63c5ca5d 2019-08-24 stsp modestr = "$";
6344 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6347 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6348 d86d3b18 2020-12-01 naddy te, s->repo);
6349 0d6c6ee3 2020-05-20 stsp if (err) {
6350 0d6c6ee3 2020-05-20 stsp free(id_str);
6351 0d6c6ee3 2020-05-20 stsp return err;
6353 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6354 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6355 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6357 848d6979 2019-08-12 stsp modestr = "@";
6359 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6360 848d6979 2019-08-12 stsp modestr = "/";
6361 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6362 848d6979 2019-08-12 stsp modestr = "*";
6363 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6364 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6365 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6366 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6367 1d13200f 2018-07-12 stsp free(id_str);
6368 0d6c6ee3 2020-05-20 stsp free(link_target);
6369 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6371 1d13200f 2018-07-12 stsp free(id_str);
6372 0d6c6ee3 2020-05-20 stsp free(link_target);
6373 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6375 ffd1d5e5 2018-06-23 stsp if (err) {
6376 ffd1d5e5 2018-06-23 stsp free(line);
6379 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6380 0cf4efb1 2018-09-29 stsp if (view->focussed)
6381 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6382 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6384 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6386 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6387 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6388 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6390 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6391 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6392 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6393 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6394 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6395 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6396 ffd1d5e5 2018-06-23 stsp free(line);
6397 2550e4c3 2018-07-13 stsp free(wline);
6398 2550e4c3 2018-07-13 stsp wline = NULL;
6400 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6401 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6402 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6406 ffd1d5e5 2018-06-23 stsp return err;
6409 ffd1d5e5 2018-06-23 stsp static void
6410 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6412 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6413 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6414 fa86c4bf 2020-11-29 stsp int i = 0;
6416 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6419 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6420 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6421 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6422 fa86c4bf 2020-11-29 stsp if (!isroot)
6423 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6426 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6427 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6431 a5d43cac 2022-07-01 thomas static const struct got_error *
6432 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6434 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6435 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6436 ffd1d5e5 2018-06-23 stsp int n = 0;
6438 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6439 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6440 694d3271 2020-12-01 naddy s->first_displayed_entry);
6442 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6444 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6445 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6446 a5d43cac 2022-07-01 thomas if (last)
6447 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6448 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6449 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6450 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6454 a5d43cac 2022-07-01 thomas return NULL;
6457 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6458 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6459 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6461 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6462 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6463 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6465 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6466 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6467 56e0773d 2019-11-28 stsp + 1 /* slash */;
6469 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6471 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6472 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6473 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6475 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6476 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6477 d9765a41 2018-06-23 stsp while (pt) {
6478 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6479 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6480 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6481 cb2ebc8a 2018-06-23 stsp goto done;
6483 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6484 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6485 cb2ebc8a 2018-06-23 stsp goto done;
6487 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6490 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6491 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6492 ce52c690 2018-06-23 stsp goto done;
6496 ce52c690 2018-06-23 stsp if (err) {
6497 ce52c690 2018-06-23 stsp free(*path);
6498 ce52c690 2018-06-23 stsp *path = NULL;
6500 ce52c690 2018-06-23 stsp return err;
6503 ce52c690 2018-06-23 stsp static const struct got_error *
6504 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6505 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6506 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6508 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6509 ce52c690 2018-06-23 stsp char *path;
6510 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6512 a0de39f3 2019-08-09 stsp *new_view = NULL;
6514 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6516 ce52c690 2018-06-23 stsp return err;
6518 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6519 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6520 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6521 83ce39e3 2019-08-12 stsp goto done;
6524 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6525 e5a0f69f 2018-08-18 stsp if (err) {
6526 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6527 fc06ba56 2019-08-22 stsp err = NULL;
6528 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6530 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6532 83ce39e3 2019-08-12 stsp free(path);
6533 69efd4c4 2018-07-18 stsp return err;
6536 69efd4c4 2018-07-18 stsp static const struct got_error *
6537 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6538 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6540 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6541 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6542 69efd4c4 2018-07-18 stsp char *path;
6544 a0de39f3 2019-08-09 stsp *new_view = NULL;
6546 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6547 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6548 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6550 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6552 69efd4c4 2018-07-18 stsp return err;
6554 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6557 e5a0f69f 2018-08-18 stsp view_close(log_view);
6559 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6560 cb2ebc8a 2018-06-23 stsp free(path);
6561 cb2ebc8a 2018-06-23 stsp return err;
6564 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6565 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6566 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6568 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6569 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6570 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6571 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6573 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6574 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6576 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6577 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6578 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6580 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6582 bc573f3b 2021-07-10 stsp goto done;
6585 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6586 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6587 bc573f3b 2021-07-10 stsp * closed on demand.
6589 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6590 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6592 bc573f3b 2021-07-10 stsp goto done;
6593 bc573f3b 2021-07-10 stsp s->tree = s->root;
6595 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6596 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6597 ffd1d5e5 2018-06-23 stsp goto done;
6599 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6600 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6601 ffd1d5e5 2018-06-23 stsp goto done;
6604 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6605 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6606 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6607 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6608 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6609 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6610 9cd7cbd1 2020-12-07 stsp goto done;
6613 fb2756b9 2018-08-04 stsp s->repo = repo;
6615 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6616 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6617 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6618 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6620 c0b01bdb 2019-11-08 stsp goto done;
6621 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6622 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6624 c0b01bdb 2019-11-08 stsp goto done;
6625 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6626 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6627 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6629 c0b01bdb 2019-11-08 stsp goto done;
6631 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6632 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6633 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6635 11b20872 2019-11-08 stsp goto done;
6637 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6638 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6640 c0b01bdb 2019-11-08 stsp goto done;
6643 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6644 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6645 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6646 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6647 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6649 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6650 bc573f3b 2021-07-10 stsp if (commit)
6651 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6653 bc573f3b 2021-07-10 stsp close_tree_view(view);
6654 ad80ab7b 2018-08-04 stsp return err;
6657 e5a0f69f 2018-08-18 stsp static const struct got_error *
6658 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6660 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6662 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6663 fb2756b9 2018-08-04 stsp free(s->tree_label);
6664 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6665 6484ec90 2018-09-29 stsp free(s->commit_id);
6666 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6667 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6668 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6669 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6670 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6671 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6672 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6673 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6674 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6675 ad80ab7b 2018-08-04 stsp free(parent);
6678 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6679 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6680 bc573f3b 2021-07-10 stsp if (s->root)
6681 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6682 7c32bd05 2019-06-22 stsp return NULL;
6685 7c32bd05 2019-06-22 stsp static const struct got_error *
6686 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6688 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6690 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6691 7c32bd05 2019-06-22 stsp return NULL;
6694 7c32bd05 2019-06-22 stsp static int
6695 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6697 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6699 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6703 7c32bd05 2019-06-22 stsp static const struct got_error *
6704 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6706 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6707 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6709 7c32bd05 2019-06-22 stsp if (!view->searching) {
6710 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6711 7c32bd05 2019-06-22 stsp return NULL;
6714 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6715 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6716 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6717 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6718 56e0773d 2019-11-28 stsp s->selected_entry);
6720 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6722 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6723 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6725 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6726 56e0773d 2019-11-28 stsp s->selected_entry);
6729 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6730 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6731 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6732 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6734 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6737 7c32bd05 2019-06-22 stsp while (1) {
6738 56e0773d 2019-11-28 stsp if (te == NULL) {
6739 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6740 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6741 ac66afb8 2019-06-24 stsp return NULL;
6743 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6744 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6746 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6749 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6750 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6751 56e0773d 2019-11-28 stsp s->matched_entry = te;
6755 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6756 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6758 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6761 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6762 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6763 7c32bd05 2019-06-22 stsp s->selected = 0;
6766 e5a0f69f 2018-08-18 stsp return NULL;
6769 ad80ab7b 2018-08-04 stsp static const struct got_error *
6770 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6772 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6773 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6774 e5a0f69f 2018-08-18 stsp char *parent_path;
6776 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6778 e5a0f69f 2018-08-18 stsp return err;
6780 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6781 e5a0f69f 2018-08-18 stsp free(parent_path);
6783 a5d43cac 2022-07-01 thomas view_border(view);
6784 e5a0f69f 2018-08-18 stsp return err;
6787 e5a0f69f 2018-08-18 stsp static const struct got_error *
6788 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6790 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6791 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6792 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6793 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6794 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6796 e5a0f69f 2018-08-18 stsp switch (ch) {
6798 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6799 07b0611c 2022-06-23 thomas view->count = 0;
6802 07b0611c 2022-06-23 thomas view->count = 0;
6803 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6805 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6806 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6807 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6808 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6809 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6810 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6811 444d5325 2022-07-03 thomas if (err)
6814 e78dc838 2020-12-04 stsp view->focussed = 0;
6815 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6816 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6817 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6818 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6819 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6820 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6822 1e37a5c2 2019-05-12 jcs return err;
6823 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6824 40236d76 2022-06-23 thomas if (err)
6825 40236d76 2022-06-23 thomas return err;
6826 e78dc838 2020-12-04 stsp view->focus_child = 1;
6828 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6831 07b0611c 2022-06-23 thomas view->count = 0;
6832 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6833 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6834 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6835 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6836 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6837 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6838 152c1c93 2020-11-29 stsp if (err) {
6839 152c1c93 2020-11-29 stsp view_close(ref_view);
6840 152c1c93 2020-11-29 stsp return err;
6842 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6843 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6844 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6845 444d5325 2022-07-03 thomas if (err)
6848 e78dc838 2020-12-04 stsp view->focussed = 0;
6849 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6850 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6851 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6852 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6853 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6854 152c1c93 2020-11-29 stsp err = view_close_child(view);
6856 152c1c93 2020-11-29 stsp return err;
6857 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6858 40236d76 2022-06-23 thomas if (err)
6859 40236d76 2022-06-23 thomas return err;
6860 e78dc838 2020-12-04 stsp view->focus_child = 1;
6862 152c1c93 2020-11-29 stsp *new_view = ref_view;
6864 e4526bf5 2021-09-03 naddy case 'g':
6865 e4526bf5 2021-09-03 naddy case KEY_HOME:
6866 e4526bf5 2021-09-03 naddy s->selected = 0;
6867 07b0611c 2022-06-23 thomas view->count = 0;
6868 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6869 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6870 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6872 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6874 e4526bf5 2021-09-03 naddy case 'G':
6875 a5d43cac 2022-07-01 thomas case KEY_END: {
6876 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6878 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6879 a5d43cac 2022-07-01 thomas --eos; /* border */
6880 e4526bf5 2021-09-03 naddy s->selected = 0;
6881 07b0611c 2022-06-23 thomas view->count = 0;
6882 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6883 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6884 e4526bf5 2021-09-03 naddy if (te == NULL) {
6885 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6886 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6891 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6892 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6894 e4526bf5 2021-09-03 naddy if (n > 0)
6895 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6899 1e37a5c2 2019-05-12 jcs case KEY_UP:
6900 f7140bf5 2021-10-17 thomas case CTRL('p'):
6901 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6902 1e37a5c2 2019-05-12 jcs s->selected--;
6905 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6906 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6907 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6908 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6909 07b0611c 2022-06-23 thomas view->count = 0;
6911 70f17a53 2022-06-13 thomas case CTRL('u'):
6912 23427b14 2022-06-23 thomas case 'u':
6913 70f17a53 2022-06-13 thomas nscroll /= 2;
6914 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6915 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6916 ea025d1d 2020-02-22 naddy case CTRL('b'):
6917 1c5e5faa 2022-06-23 thomas case 'b':
6918 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6919 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6920 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6921 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6923 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6924 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6926 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6927 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6928 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6929 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6930 07b0611c 2022-06-23 thomas view->count = 0;
6933 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6934 f7140bf5 2021-10-17 thomas case CTRL('n'):
6935 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6936 1e37a5c2 2019-05-12 jcs s->selected++;
6939 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6940 07b0611c 2022-06-23 thomas == NULL) {
6941 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6942 07b0611c 2022-06-23 thomas view->count = 0;
6945 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6947 70f17a53 2022-06-13 thomas case CTRL('d'):
6948 23427b14 2022-06-23 thomas case 'd':
6949 70f17a53 2022-06-13 thomas nscroll /= 2;
6950 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6951 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6952 ea025d1d 2020-02-22 naddy case CTRL('f'):
6953 1c5e5faa 2022-06-23 thomas case 'f':
6954 4c2d69cb 2022-06-23 thomas case ' ':
6955 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6957 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6958 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6959 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6960 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6962 07b0611c 2022-06-23 thomas view->count = 0;
6965 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6967 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6969 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6970 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6971 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6972 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6973 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6974 07b0611c 2022-06-23 thomas view->count = 0;
6977 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6978 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6980 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6981 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6982 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6983 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6984 1e37a5c2 2019-05-12 jcs s->selected_entry =
6985 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6986 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6987 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6988 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6989 a5d43cac 2022-07-01 thomas if (err)
6992 1e37a5c2 2019-05-12 jcs free(parent);
6993 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6994 56e0773d 2019-11-28 stsp s->selected_entry))) {
6995 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6996 07b0611c 2022-06-23 thomas view->count = 0;
6997 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6998 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7001 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7002 941e9f74 2019-05-21 stsp if (err) {
7003 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7006 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7007 56e0773d 2019-11-28 stsp s->selected_entry))) {
7008 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7009 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
7011 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
7012 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7014 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
7015 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7016 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7020 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7021 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7022 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7023 a5d43cac 2022-07-01 thomas if (err)
7027 07b0611c 2022-06-23 thomas view->count = 0;
7028 e78dc838 2020-12-04 stsp view->focussed = 0;
7029 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7030 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
7031 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
7032 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7033 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
7034 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7036 669b5ffa 2018-10-07 stsp return err;
7037 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
7038 40236d76 2022-06-23 thomas if (err)
7039 40236d76 2022-06-23 thomas return err;
7040 e78dc838 2020-12-04 stsp view->focus_child = 1;
7042 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7045 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7046 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7047 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7048 07b0611c 2022-06-23 thomas view->count = 0;
7051 07b0611c 2022-06-23 thomas view->count = 0;
7055 ffd1d5e5 2018-06-23 stsp return err;
7058 ffd1d5e5 2018-06-23 stsp __dead static void
7059 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7062 91198554 2022-06-23 thomas fprintf(stderr,
7063 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7064 ffd1d5e5 2018-06-23 stsp getprogname());
7068 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7069 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7071 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7072 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7073 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7074 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7075 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7076 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7077 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7078 4e97c21c 2020-12-06 stsp char *label = NULL;
7079 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7080 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7082 5221c383 2018-08-01 stsp struct tog_view *view;
7083 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7085 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7086 ffd1d5e5 2018-06-23 stsp switch (ch) {
7088 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7091 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7092 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7093 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7097 e99e2d15 2020-11-24 naddy usage_tree();
7098 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7102 ffd1d5e5 2018-06-23 stsp argc -= optind;
7103 ffd1d5e5 2018-06-23 stsp argv += optind;
7105 55cccc34 2020-02-20 stsp if (argc > 1)
7106 e99e2d15 2020-11-24 naddy usage_tree();
7108 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7109 7cd52833 2022-06-23 thomas if (error != NULL)
7110 7cd52833 2022-06-23 thomas goto done;
7112 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7113 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7114 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7115 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7116 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7117 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7118 c156c7a4 2020-12-18 stsp goto done;
7119 55cccc34 2020-02-20 stsp if (worktree)
7120 52185f70 2019-02-05 stsp repo_path =
7121 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7123 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7124 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7125 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7126 c156c7a4 2020-12-18 stsp goto done;
7130 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7131 c02c541e 2019-03-29 stsp if (error != NULL)
7132 52185f70 2019-02-05 stsp goto done;
7134 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7135 55cccc34 2020-02-20 stsp repo, worktree);
7136 55cccc34 2020-02-20 stsp if (error)
7137 55cccc34 2020-02-20 stsp goto done;
7139 55cccc34 2020-02-20 stsp init_curses();
7141 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7142 c02c541e 2019-03-29 stsp if (error)
7143 52185f70 2019-02-05 stsp goto done;
7145 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7146 51a10b52 2020-12-26 stsp if (error)
7147 51a10b52 2020-12-26 stsp goto done;
7149 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7150 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7151 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7152 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7153 4e97c21c 2020-12-06 stsp if (error)
7154 4e97c21c 2020-12-06 stsp goto done;
7155 4e97c21c 2020-12-06 stsp head_ref_name = label;
7157 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7158 4e97c21c 2020-12-06 stsp if (error == NULL)
7159 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7160 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7161 4e97c21c 2020-12-06 stsp goto done;
7162 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7163 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7164 4e97c21c 2020-12-06 stsp if (error)
7165 4e97c21c 2020-12-06 stsp goto done;
7168 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7169 945f9229 2022-04-16 thomas if (error)
7170 945f9229 2022-04-16 thomas goto done;
7172 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7173 5221c383 2018-08-01 stsp if (view == NULL) {
7174 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7175 5221c383 2018-08-01 stsp goto done;
7177 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7178 ad80ab7b 2018-08-04 stsp if (error)
7179 ad80ab7b 2018-08-04 stsp goto done;
7180 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7181 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7182 d91faf3b 2020-12-01 naddy in_repo_path);
7183 55cccc34 2020-02-20 stsp if (error)
7184 55cccc34 2020-02-20 stsp goto done;
7187 55cccc34 2020-02-20 stsp if (worktree) {
7188 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7189 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7190 55cccc34 2020-02-20 stsp worktree = NULL;
7192 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7194 52185f70 2019-02-05 stsp free(repo_path);
7195 e4a0e26d 2020-02-20 stsp free(cwd);
7196 ffd1d5e5 2018-06-23 stsp free(commit_id);
7197 4e97c21c 2020-12-06 stsp free(label);
7199 486cd271 2020-12-06 stsp got_ref_close(ref);
7200 1d0f4054 2021-06-17 stsp if (repo) {
7201 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7202 1d0f4054 2021-06-17 stsp if (error == NULL)
7203 1d0f4054 2021-06-17 stsp error = close_err;
7205 7cd52833 2022-06-23 thomas if (pack_fds) {
7206 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7207 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7208 7cd52833 2022-06-23 thomas if (error == NULL)
7209 7cd52833 2022-06-23 thomas error = pack_err;
7211 51a10b52 2020-12-26 stsp tog_free_refs();
7212 ffd1d5e5 2018-06-23 stsp return error;
7215 6458efa5 2020-11-24 stsp static const struct got_error *
7216 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7218 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7219 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7221 6458efa5 2020-11-24 stsp s->nrefs = 0;
7222 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7223 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7224 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7225 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7226 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7229 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7230 6458efa5 2020-11-24 stsp if (re == NULL)
7231 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7233 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7234 8924d611 2020-12-26 stsp if (re->ref == NULL)
7235 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7236 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7237 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7240 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7241 6458efa5 2020-11-24 stsp return NULL;
7244 ef20f542 2022-06-26 thomas static void
7245 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7247 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7249 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7250 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7251 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7252 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7257 6458efa5 2020-11-24 stsp static const struct got_error *
7258 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7260 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7261 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7263 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7264 6458efa5 2020-11-24 stsp s->repo = repo;
7266 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7267 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7269 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7271 6458efa5 2020-11-24 stsp return err;
7273 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7274 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7275 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7276 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7278 6458efa5 2020-11-24 stsp goto done;
7280 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7281 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7282 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7284 6458efa5 2020-11-24 stsp goto done;
7286 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7287 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7288 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7289 2183bbf6 2022-01-23 thomas if (err)
7290 2183bbf6 2022-01-23 thomas goto done;
7292 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7293 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7294 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7296 6458efa5 2020-11-24 stsp goto done;
7299 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7300 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7301 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7302 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7303 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7306 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7307 6458efa5 2020-11-24 stsp return err;
7310 6458efa5 2020-11-24 stsp static const struct got_error *
7311 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7313 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7315 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7316 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7318 6458efa5 2020-11-24 stsp return NULL;
7321 6458efa5 2020-11-24 stsp static const struct got_error *
7322 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7323 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7325 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7326 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7327 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7328 6458efa5 2020-11-24 stsp int obj_type;
7330 c42c9805 2020-11-24 stsp *commit_id = NULL;
7332 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7334 6458efa5 2020-11-24 stsp return err;
7336 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7338 6458efa5 2020-11-24 stsp goto done;
7340 6458efa5 2020-11-24 stsp switch (obj_type) {
7341 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7342 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7344 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7345 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7347 6458efa5 2020-11-24 stsp goto done;
7348 c42c9805 2020-11-24 stsp free(obj_id);
7349 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7350 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7352 6458efa5 2020-11-24 stsp goto done;
7353 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7354 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7355 c42c9805 2020-11-24 stsp goto done;
7357 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7358 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7359 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7360 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7361 c42c9805 2020-11-24 stsp goto done;
7365 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7371 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7372 c42c9805 2020-11-24 stsp if (err) {
7373 c42c9805 2020-11-24 stsp free(*commit_id);
7374 c42c9805 2020-11-24 stsp *commit_id = NULL;
7376 c42c9805 2020-11-24 stsp return err;
7379 c42c9805 2020-11-24 stsp static const struct got_error *
7380 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7381 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7383 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7384 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7385 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7387 c42c9805 2020-11-24 stsp *new_view = NULL;
7389 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7390 c42c9805 2020-11-24 stsp if (err) {
7391 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7392 c42c9805 2020-11-24 stsp return err;
7394 c42c9805 2020-11-24 stsp return NULL;
7397 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7398 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7399 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7400 6458efa5 2020-11-24 stsp goto done;
7403 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7404 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7407 6458efa5 2020-11-24 stsp view_close(log_view);
7409 6458efa5 2020-11-24 stsp *new_view = log_view;
7410 c42c9805 2020-11-24 stsp free(commit_id);
7411 6458efa5 2020-11-24 stsp return err;
7414 ce5b7c56 2019-07-09 stsp static void
7415 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7417 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7418 34ba6917 2020-11-30 stsp int i = 0;
7420 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7423 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7424 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7425 34ba6917 2020-11-30 stsp if (re == NULL)
7427 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7428 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7432 a5d43cac 2022-07-01 thomas static const struct got_error *
7433 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7435 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7436 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7437 6458efa5 2020-11-24 stsp int n = 0;
7439 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7440 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7442 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7444 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7445 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7446 a5d43cac 2022-07-01 thomas if (last)
7447 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7448 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7449 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7450 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7454 a5d43cac 2022-07-01 thomas return NULL;
7457 6458efa5 2020-11-24 stsp static const struct got_error *
7458 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7460 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7462 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7463 6458efa5 2020-11-24 stsp return NULL;
7466 6458efa5 2020-11-24 stsp static int
7467 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7469 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7471 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7475 6458efa5 2020-11-24 stsp static const struct got_error *
7476 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7478 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7479 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7481 6458efa5 2020-11-24 stsp if (!view->searching) {
7482 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7483 6458efa5 2020-11-24 stsp return NULL;
7486 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7487 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7488 6458efa5 2020-11-24 stsp if (s->selected_entry)
7489 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7491 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7492 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7494 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7495 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7497 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7498 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7501 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7502 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7503 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7504 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7506 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7509 6458efa5 2020-11-24 stsp while (1) {
7510 6458efa5 2020-11-24 stsp if (re == NULL) {
7511 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7512 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7513 6458efa5 2020-11-24 stsp return NULL;
7515 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7516 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7518 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7521 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7522 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7523 6458efa5 2020-11-24 stsp s->matched_entry = re;
7527 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7528 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7530 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7533 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7534 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7535 6458efa5 2020-11-24 stsp s->selected = 0;
7538 6458efa5 2020-11-24 stsp return NULL;
7541 6458efa5 2020-11-24 stsp static const struct got_error *
7542 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7544 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7545 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7546 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7547 6458efa5 2020-11-24 stsp char *line = NULL;
7548 6458efa5 2020-11-24 stsp wchar_t *wline;
7549 6458efa5 2020-11-24 stsp struct tog_color *tc;
7550 6458efa5 2020-11-24 stsp int width, n;
7551 6458efa5 2020-11-24 stsp int limit = view->nlines;
7553 6458efa5 2020-11-24 stsp werase(view->window);
7555 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7556 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7557 a5d43cac 2022-07-01 thomas --limit; /* border */
7559 6458efa5 2020-11-24 stsp if (limit == 0)
7560 6458efa5 2020-11-24 stsp return NULL;
7562 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7564 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7565 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7566 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7568 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7569 6458efa5 2020-11-24 stsp if (err) {
7570 6458efa5 2020-11-24 stsp free(line);
7571 6458efa5 2020-11-24 stsp return err;
7573 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7574 6458efa5 2020-11-24 stsp wstandout(view->window);
7575 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7576 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7577 6458efa5 2020-11-24 stsp wstandend(view->window);
7578 6458efa5 2020-11-24 stsp free(wline);
7579 6458efa5 2020-11-24 stsp wline = NULL;
7580 6458efa5 2020-11-24 stsp free(line);
7581 6458efa5 2020-11-24 stsp line = NULL;
7582 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7583 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7584 6458efa5 2020-11-24 stsp if (--limit <= 0)
7585 6458efa5 2020-11-24 stsp return NULL;
7588 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7589 6458efa5 2020-11-24 stsp char *line = NULL;
7590 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7592 84227eb1 2022-06-23 thomas if (s->show_date) {
7593 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7594 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7595 84227eb1 2022-06-23 thomas struct got_object_id *id;
7596 84227eb1 2022-06-23 thomas struct tm tm;
7597 84227eb1 2022-06-23 thomas time_t t;
7599 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7600 84227eb1 2022-06-23 thomas if (err)
7601 84227eb1 2022-06-23 thomas return err;
7602 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7603 84227eb1 2022-06-23 thomas if (err) {
7604 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7605 84227eb1 2022-06-23 thomas free(id);
7606 84227eb1 2022-06-23 thomas return err;
7608 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7610 84227eb1 2022-06-23 thomas if (err) {
7611 84227eb1 2022-06-23 thomas free(id);
7612 84227eb1 2022-06-23 thomas return err;
7614 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7615 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7616 84227eb1 2022-06-23 thomas } else {
7617 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7618 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7620 84227eb1 2022-06-23 thomas free(id);
7621 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7622 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7623 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7624 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7626 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7627 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7628 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7629 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7630 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7631 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7632 6458efa5 2020-11-24 stsp struct got_object_id *id;
7633 6458efa5 2020-11-24 stsp char *id_str;
7634 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7636 6458efa5 2020-11-24 stsp return err;
7637 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7638 6458efa5 2020-11-24 stsp if (err) {
7640 6458efa5 2020-11-24 stsp return err;
7642 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7643 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7644 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7646 6458efa5 2020-11-24 stsp free(id_str);
7647 6458efa5 2020-11-24 stsp return err;
7650 6458efa5 2020-11-24 stsp free(id_str);
7651 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7652 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7653 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7655 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7657 6458efa5 2020-11-24 stsp if (err) {
7658 6458efa5 2020-11-24 stsp free(line);
7659 6458efa5 2020-11-24 stsp return err;
7661 6458efa5 2020-11-24 stsp if (n == s->selected) {
7662 6458efa5 2020-11-24 stsp if (view->focussed)
7663 6458efa5 2020-11-24 stsp wstandout(view->window);
7664 6458efa5 2020-11-24 stsp s->selected_entry = re;
7666 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7668 6458efa5 2020-11-24 stsp wattr_on(view->window,
7669 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7670 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7672 6458efa5 2020-11-24 stsp wattr_off(view->window,
7673 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7674 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7675 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7676 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7677 6458efa5 2020-11-24 stsp wstandend(view->window);
7678 6458efa5 2020-11-24 stsp free(line);
7679 6458efa5 2020-11-24 stsp free(wline);
7680 6458efa5 2020-11-24 stsp wline = NULL;
7682 6458efa5 2020-11-24 stsp s->ndisplayed++;
7683 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7686 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7689 a5d43cac 2022-07-01 thomas view_border(view);
7690 6458efa5 2020-11-24 stsp return err;
7693 6458efa5 2020-11-24 stsp static const struct got_error *
7694 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7695 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7697 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7698 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7699 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7701 c42c9805 2020-11-24 stsp *new_view = NULL;
7703 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7704 c42c9805 2020-11-24 stsp if (err) {
7705 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7706 c42c9805 2020-11-24 stsp return err;
7708 c42c9805 2020-11-24 stsp return NULL;
7712 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7713 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7714 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7715 c42c9805 2020-11-24 stsp goto done;
7718 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7719 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7721 c42c9805 2020-11-24 stsp goto done;
7723 c42c9805 2020-11-24 stsp *new_view = tree_view;
7725 c42c9805 2020-11-24 stsp free(commit_id);
7726 c42c9805 2020-11-24 stsp return err;
7728 c42c9805 2020-11-24 stsp static const struct got_error *
7729 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7731 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7732 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7733 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7734 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7735 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7737 6458efa5 2020-11-24 stsp switch (ch) {
7739 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7740 07b0611c 2022-06-23 thomas view->count = 0;
7742 84227eb1 2022-06-23 thomas case 'm':
7743 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7744 07b0611c 2022-06-23 thomas view->count = 0;
7746 98182bd0 2021-11-20 thomas case 'o':
7747 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7748 07b0611c 2022-06-23 thomas view->count = 0;
7749 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7750 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7751 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7752 c591440f 2021-11-20 thomas if (err)
7754 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7755 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7756 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7757 3bfadbd4 2021-11-20 thomas if (err)
7759 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7760 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7762 6458efa5 2020-11-24 stsp case KEY_ENTER:
7763 6458efa5 2020-11-24 stsp case '\r':
7764 07b0611c 2022-06-23 thomas view->count = 0;
7765 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7767 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7768 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7770 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7771 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7772 a5d43cac 2022-07-01 thomas if (err)
7775 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7776 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7777 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7778 a5d43cac 2022-07-01 thomas if (err)
7782 e78dc838 2020-12-04 stsp view->focussed = 0;
7783 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7784 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7785 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7786 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7787 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7788 6458efa5 2020-11-24 stsp err = view_close_child(view);
7790 6458efa5 2020-11-24 stsp return err;
7791 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7792 40236d76 2022-06-23 thomas if (err)
7793 40236d76 2022-06-23 thomas return err;
7794 e78dc838 2020-12-04 stsp view->focus_child = 1;
7796 6458efa5 2020-11-24 stsp *new_view = log_view;
7799 07b0611c 2022-06-23 thomas view->count = 0;
7800 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7802 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7803 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7804 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7805 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7806 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7808 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7809 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7810 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7811 444d5325 2022-07-03 thomas if (err)
7814 e78dc838 2020-12-04 stsp view->focussed = 0;
7815 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7816 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7817 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7818 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7819 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7820 c42c9805 2020-11-24 stsp err = view_close_child(view);
7822 c42c9805 2020-11-24 stsp return err;
7823 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7824 40236d76 2022-06-23 thomas if (err)
7825 40236d76 2022-06-23 thomas return err;
7826 e78dc838 2020-12-04 stsp view->focus_child = 1;
7828 c42c9805 2020-11-24 stsp *new_view = tree_view;
7830 e4526bf5 2021-09-03 naddy case 'g':
7831 e4526bf5 2021-09-03 naddy case KEY_HOME:
7832 e4526bf5 2021-09-03 naddy s->selected = 0;
7833 07b0611c 2022-06-23 thomas view->count = 0;
7834 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7836 e4526bf5 2021-09-03 naddy case 'G':
7837 a5d43cac 2022-07-01 thomas case KEY_END: {
7838 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7840 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7841 a5d43cac 2022-07-01 thomas --eos; /* border */
7842 e4526bf5 2021-09-03 naddy s->selected = 0;
7843 07b0611c 2022-06-23 thomas view->count = 0;
7844 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7845 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7846 e4526bf5 2021-09-03 naddy if (re == NULL)
7848 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7849 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7851 e4526bf5 2021-09-03 naddy if (n > 0)
7852 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7856 6458efa5 2020-11-24 stsp case KEY_UP:
7857 f7140bf5 2021-10-17 thomas case CTRL('p'):
7858 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7859 6458efa5 2020-11-24 stsp s->selected--;
7862 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7863 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7864 07b0611c 2022-06-23 thomas view->count = 0;
7866 70f17a53 2022-06-13 thomas case CTRL('u'):
7867 23427b14 2022-06-23 thomas case 'u':
7868 70f17a53 2022-06-13 thomas nscroll /= 2;
7869 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7870 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7871 6458efa5 2020-11-24 stsp case CTRL('b'):
7872 1c5e5faa 2022-06-23 thomas case 'b':
7873 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7874 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7875 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7876 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7877 07b0611c 2022-06-23 thomas view->count = 0;
7880 6458efa5 2020-11-24 stsp case KEY_DOWN:
7881 f7140bf5 2021-10-17 thomas case CTRL('n'):
7882 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7883 6458efa5 2020-11-24 stsp s->selected++;
7886 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7887 6458efa5 2020-11-24 stsp /* can't scroll any further */
7888 07b0611c 2022-06-23 thomas view->count = 0;
7891 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7893 70f17a53 2022-06-13 thomas case CTRL('d'):
7894 23427b14 2022-06-23 thomas case 'd':
7895 70f17a53 2022-06-13 thomas nscroll /= 2;
7896 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7897 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7898 6458efa5 2020-11-24 stsp case CTRL('f'):
7899 1c5e5faa 2022-06-23 thomas case 'f':
7900 4c2d69cb 2022-06-23 thomas case ' ':
7901 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7902 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7903 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7904 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7905 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7906 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7907 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7908 07b0611c 2022-06-23 thomas view->count = 0;
7911 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7913 6458efa5 2020-11-24 stsp case CTRL('l'):
7914 07b0611c 2022-06-23 thomas view->count = 0;
7915 8924d611 2020-12-26 stsp tog_free_refs();
7916 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7919 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7920 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7922 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7923 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7924 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7927 07b0611c 2022-06-23 thomas view->count = 0;
7931 6458efa5 2020-11-24 stsp return err;
7934 6458efa5 2020-11-24 stsp __dead static void
7935 6458efa5 2020-11-24 stsp usage_ref(void)
7938 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7939 6458efa5 2020-11-24 stsp getprogname());
7943 6458efa5 2020-11-24 stsp static const struct got_error *
7944 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7946 6458efa5 2020-11-24 stsp const struct got_error *error;
7947 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7948 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7949 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7951 6458efa5 2020-11-24 stsp struct tog_view *view;
7952 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7954 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7955 6458efa5 2020-11-24 stsp switch (ch) {
7957 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7958 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7959 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7963 e99e2d15 2020-11-24 naddy usage_ref();
7964 6458efa5 2020-11-24 stsp /* NOTREACHED */
7968 6458efa5 2020-11-24 stsp argc -= optind;
7969 6458efa5 2020-11-24 stsp argv += optind;
7971 6458efa5 2020-11-24 stsp if (argc > 1)
7972 e99e2d15 2020-11-24 naddy usage_ref();
7974 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7975 7cd52833 2022-06-23 thomas if (error != NULL)
7976 7cd52833 2022-06-23 thomas goto done;
7978 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7979 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7980 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7981 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7982 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7983 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7984 c156c7a4 2020-12-18 stsp goto done;
7985 6458efa5 2020-11-24 stsp if (worktree)
7986 6458efa5 2020-11-24 stsp repo_path =
7987 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7989 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7990 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7991 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7992 c156c7a4 2020-12-18 stsp goto done;
7996 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7997 6458efa5 2020-11-24 stsp if (error != NULL)
7998 6458efa5 2020-11-24 stsp goto done;
8000 6458efa5 2020-11-24 stsp init_curses();
8002 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8003 51a10b52 2020-12-26 stsp if (error)
8004 51a10b52 2020-12-26 stsp goto done;
8006 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8007 6458efa5 2020-11-24 stsp if (error)
8008 6458efa5 2020-11-24 stsp goto done;
8010 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8011 6458efa5 2020-11-24 stsp if (view == NULL) {
8012 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8013 6458efa5 2020-11-24 stsp goto done;
8016 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8017 6458efa5 2020-11-24 stsp if (error)
8018 6458efa5 2020-11-24 stsp goto done;
8020 6458efa5 2020-11-24 stsp if (worktree) {
8021 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8022 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8023 6458efa5 2020-11-24 stsp worktree = NULL;
8025 6458efa5 2020-11-24 stsp error = view_loop(view);
8027 6458efa5 2020-11-24 stsp free(repo_path);
8028 6458efa5 2020-11-24 stsp free(cwd);
8029 1d0f4054 2021-06-17 stsp if (repo) {
8030 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8031 1d0f4054 2021-06-17 stsp if (close_err)
8032 1d0f4054 2021-06-17 stsp error = close_err;
8034 7cd52833 2022-06-23 thomas if (pack_fds) {
8035 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8036 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8037 7cd52833 2022-06-23 thomas if (error == NULL)
8038 7cd52833 2022-06-23 thomas error = pack_err;
8040 51a10b52 2020-12-26 stsp tog_free_refs();
8041 6458efa5 2020-11-24 stsp return error;
8045 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8046 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8048 6458efa5 2020-11-24 stsp static void
8049 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8051 a5d43cac 2022-07-01 thomas switch (view->type) {
8052 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8053 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8054 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8055 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8059 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8060 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8062 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8063 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8066 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8067 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8068 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8070 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8071 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8072 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8074 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8075 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8076 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8078 a5d43cac 2022-07-01 thomas default:
8082 a5d43cac 2022-07-01 thomas view->offset = 0;
8086 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8087 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8089 a5d43cac 2022-07-01 thomas static const struct got_error *
8090 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8092 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8093 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8094 a5d43cac 2022-07-01 thomas int *selected = NULL;
8095 a5d43cac 2022-07-01 thomas int header, offset;
8097 a5d43cac 2022-07-01 thomas switch (view->type) {
8098 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8099 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8100 a5d43cac 2022-07-01 thomas header = 3;
8101 a5d43cac 2022-07-01 thomas scrolld = NULL;
8102 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8103 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8104 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8105 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8106 a5d43cac 2022-07-01 thomas view->offset = offset;
8110 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8111 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8112 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8113 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8114 a5d43cac 2022-07-01 thomas selected = &s->selected;
8117 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8118 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8119 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8120 a5d43cac 2022-07-01 thomas header = 3;
8121 a5d43cac 2022-07-01 thomas selected = &s->selected;
8124 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8125 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8126 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8127 a5d43cac 2022-07-01 thomas header = 5;
8128 a5d43cac 2022-07-01 thomas selected = &s->selected;
8131 a5d43cac 2022-07-01 thomas default:
8132 a5d43cac 2022-07-01 thomas selected = NULL;
8133 a5d43cac 2022-07-01 thomas scrolld = NULL;
8134 a5d43cac 2022-07-01 thomas header = 0;
8138 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8139 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8140 a5d43cac 2022-07-01 thomas view->offset = offset;
8141 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8142 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8143 a5d43cac 2022-07-01 thomas *selected -= offset;
8147 a5d43cac 2022-07-01 thomas return err;
8150 a5d43cac 2022-07-01 thomas static void
8151 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8155 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8156 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8157 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8158 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8160 6879ba42 2020-10-01 naddy fputc('\n', fp);
8163 4ed7e80c 2018-05-20 stsp __dead static void
8164 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8166 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8168 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8169 6879ba42 2020-10-01 naddy getprogname());
8170 6879ba42 2020-10-01 naddy if (hflag) {
8171 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8172 6879ba42 2020-10-01 naddy list_commands(fp);
8174 6879ba42 2020-10-01 naddy exit(status);
8177 c2301be8 2018-04-30 stsp static char **
8178 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8180 ee85c5e8 2020-02-29 stsp va_list ap;
8181 c2301be8 2018-04-30 stsp char **argv;
8184 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8186 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8187 c2301be8 2018-04-30 stsp if (argv == NULL)
8188 c2301be8 2018-04-30 stsp err(1, "calloc");
8189 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8190 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8191 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8192 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8195 ee85c5e8 2020-02-29 stsp va_end(ap);
8196 c2301be8 2018-04-30 stsp return argv;
8200 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8201 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8202 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8203 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8204 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8206 ee85c5e8 2020-02-29 stsp static const struct got_error *
8207 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8209 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8210 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8211 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8212 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8213 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8214 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8215 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8216 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8217 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8219 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8220 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8221 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8223 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8224 7cd52833 2022-06-23 thomas if (error != NULL)
8225 7cd52833 2022-06-23 thomas goto done;
8227 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8228 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8229 ee85c5e8 2020-02-29 stsp goto done;
8231 ee85c5e8 2020-02-29 stsp if (worktree)
8232 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8234 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8235 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8236 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8237 ee85c5e8 2020-02-29 stsp goto done;
8240 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8241 ee85c5e8 2020-02-29 stsp if (error != NULL)
8242 ee85c5e8 2020-02-29 stsp goto done;
8244 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8245 ee85c5e8 2020-02-29 stsp repo, worktree);
8246 ee85c5e8 2020-02-29 stsp if (error)
8247 ee85c5e8 2020-02-29 stsp goto done;
8249 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8250 84de9106 2020-12-26 stsp if (error)
8251 84de9106 2020-12-26 stsp goto done;
8252 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8253 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8254 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8255 ee85c5e8 2020-02-29 stsp if (error)
8256 ee85c5e8 2020-02-29 stsp goto done;
8258 ee85c5e8 2020-02-29 stsp if (worktree) {
8259 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8260 ee85c5e8 2020-02-29 stsp worktree = NULL;
8263 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8264 945f9229 2022-04-16 thomas if (error)
8265 945f9229 2022-04-16 thomas goto done;
8267 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8268 ee85c5e8 2020-02-29 stsp if (error) {
8269 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8270 ee85c5e8 2020-02-29 stsp goto done;
8271 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8272 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8273 6879ba42 2020-10-01 naddy usage(1, 1);
8274 ee85c5e8 2020-02-29 stsp /* not reached */
8277 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8278 1d0f4054 2021-06-17 stsp if (error == NULL)
8279 1d0f4054 2021-06-17 stsp error = close_err;
8280 ee85c5e8 2020-02-29 stsp repo = NULL;
8282 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8283 ee85c5e8 2020-02-29 stsp if (error)
8284 ee85c5e8 2020-02-29 stsp goto done;
8286 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8288 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8289 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8291 1d0f4054 2021-06-17 stsp if (repo) {
8292 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8293 1d0f4054 2021-06-17 stsp if (error == NULL)
8294 1d0f4054 2021-06-17 stsp error = close_err;
8296 945f9229 2022-04-16 thomas if (commit)
8297 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8298 ee85c5e8 2020-02-29 stsp if (worktree)
8299 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8300 7cd52833 2022-06-23 thomas if (pack_fds) {
8301 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8302 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8303 7cd52833 2022-06-23 thomas if (error == NULL)
8304 7cd52833 2022-06-23 thomas error = pack_err;
8307 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8308 ee85c5e8 2020-02-29 stsp free(commit_id);
8309 ee85c5e8 2020-02-29 stsp free(cwd);
8310 ee85c5e8 2020-02-29 stsp free(repo_path);
8311 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8312 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8314 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8315 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8316 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8318 87670572 2020-12-26 naddy tog_free_refs();
8319 ee85c5e8 2020-02-29 stsp return error;
8323 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8325 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8326 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8327 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8328 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8329 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8330 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8331 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8333 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8335 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8337 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8338 9f7d7167 2018-04-29 stsp switch (ch) {
8340 9f7d7167 2018-04-29 stsp hflag = 1;
8343 53ccebc2 2019-07-30 stsp Vflag = 1;
8346 6879ba42 2020-10-01 naddy usage(hflag, 1);
8347 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8351 9f7d7167 2018-04-29 stsp argc -= optind;
8352 9f7d7167 2018-04-29 stsp argv += optind;
8353 9814e6a3 2020-09-27 naddy optind = 1;
8354 c2301be8 2018-04-30 stsp optreset = 1;
8356 53ccebc2 2019-07-30 stsp if (Vflag) {
8357 53ccebc2 2019-07-30 stsp got_version_print_str();
8358 6879ba42 2020-10-01 naddy return 0;
8361 4010e238 2020-12-04 stsp #ifndef PROFILE
8362 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8363 4010e238 2020-12-04 stsp NULL) == -1)
8364 4010e238 2020-12-04 stsp err(1, "pledge");
8367 c2301be8 2018-04-30 stsp if (argc == 0) {
8368 f29d3e89 2018-06-23 stsp if (hflag)
8369 6879ba42 2020-10-01 naddy usage(hflag, 0);
8370 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8371 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8373 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8377 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8378 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8379 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8380 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8381 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8387 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8388 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8389 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8390 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8391 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8392 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8395 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8396 ee85c5e8 2020-02-29 stsp if (argc != 1)
8397 6879ba42 2020-10-01 naddy usage(0, 1);
8398 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8399 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8401 ee85c5e8 2020-02-29 stsp if (hflag)
8402 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8404 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8408 b46c1e04 2020-09-20 naddy putchar('\n');
8409 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8411 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8412 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8413 a2f4a359 2020-02-28 stsp free(cmd_argv);
8416 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8417 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);