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 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
448 e9424729 2018-08-04 stsp struct tog_blame blame;
449 6c4c42e0 2019-06-24 stsp int matched_line;
450 11b20872 2019-11-08 stsp struct tog_colors colors;
453 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
454 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
455 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
457 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
458 ad80ab7b 2018-08-04 stsp int selected;
461 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
463 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
464 ad80ab7b 2018-08-04 stsp char *tree_label;
465 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
467 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
470 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
471 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
472 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
473 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
474 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
475 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
476 6458efa5 2020-11-24 stsp struct tog_colors colors;
479 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
480 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
481 6458efa5 2020-11-24 stsp struct got_reference *ref;
485 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
487 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
488 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
491 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
492 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
493 6458efa5 2020-11-24 stsp struct got_repository *repo;
494 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
495 bddb1296 2019-11-08 stsp struct tog_colors colors;
499 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
501 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
502 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
503 669b5ffa 2018-10-07 stsp * there is enough screen estate.
505 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
506 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
508 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
509 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
510 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
512 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
513 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
515 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
517 cc3c9aac 2018-08-01 stsp struct tog_view {
518 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
519 26ed57b2 2018-05-19 stsp WINDOW *window;
520 26ed57b2 2018-05-19 stsp PANEL *panel;
521 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
522 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
523 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
524 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
525 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
526 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
527 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
528 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
530 669b5ffa 2018-10-07 stsp struct tog_view *parent;
531 669b5ffa 2018-10-07 stsp struct tog_view *child;
534 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
535 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
536 e78dc838 2020-12-04 stsp * between parent and child.
537 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
538 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
539 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
540 e78dc838 2020-12-04 stsp * situations.
542 e78dc838 2020-12-04 stsp int focus_child;
544 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
545 5dc9f4bc 2018-08-04 stsp /* type-specific state */
546 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
548 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
549 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
550 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
551 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
552 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
557 e78dc838 2020-12-04 stsp struct tog_view *, int);
558 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
559 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
560 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
563 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
564 c0c4acc8 2021-01-24 stsp int search_started;
565 60493ae3 2019-06-20 stsp int searching;
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
567 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
568 60493ae3 2019-06-20 stsp int search_next_done;
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
570 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
571 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
572 1803e47f 2019-06-22 stsp regex_t regex;
573 41605754 2020-11-12 stsp regmatch_t regmatch;
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
577 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
578 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
579 78756c87 2020-11-24 stsp struct got_repository *);
580 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
582 e78dc838 2020-12-04 stsp struct tog_view *, int);
583 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
584 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
586 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
588 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
589 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
590 78756c87 2020-11-24 stsp const char *, const char *, int);
591 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
593 e78dc838 2020-12-04 stsp struct tog_view *, int);
594 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
597 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
600 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
601 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
602 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
603 e78dc838 2020-12-04 stsp struct tog_view *, int);
604 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
605 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
607 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
609 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
610 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
611 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
612 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
613 e78dc838 2020-12-04 stsp struct tog_view *, int);
614 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
616 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
618 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
619 6458efa5 2020-11-24 stsp struct got_repository *);
620 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
621 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
622 e78dc838 2020-12-04 stsp struct tog_view *, int);
623 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
625 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
627 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
628 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
629 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
631 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
633 25791caa 2018-10-24 stsp static void
634 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
636 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
639 83baff54 2019-08-12 stsp static void
640 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
642 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
645 61266923 2020-01-14 stsp static void
646 61266923 2020-01-14 stsp tog_sigcont(int signo)
648 61266923 2020-01-14 stsp tog_sigcont_received = 1;
651 296152d1 2022-05-31 thomas static void
652 296152d1 2022-05-31 thomas tog_sigint(int signo)
654 296152d1 2022-05-31 thomas tog_sigint_received = 1;
657 296152d1 2022-05-31 thomas static void
658 296152d1 2022-05-31 thomas tog_sigterm(int signo)
660 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
663 296152d1 2022-05-31 thomas static int
664 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
666 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
667 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
670 e5a0f69f 2018-08-18 stsp static const struct got_error *
671 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
673 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
675 669b5ffa 2018-10-07 stsp if (view->child) {
676 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
677 669b5ffa 2018-10-07 stsp view->child = NULL;
679 e5a0f69f 2018-08-18 stsp if (view->close)
680 e5a0f69f 2018-08-18 stsp err = view->close(view);
681 ea5e7bb5 2018-08-01 stsp if (view->panel)
682 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
683 ea5e7bb5 2018-08-01 stsp if (view->window)
684 ea5e7bb5 2018-08-01 stsp delwin(view->window);
685 ea5e7bb5 2018-08-01 stsp free(view);
686 f2d749db 2022-07-12 thomas return err ? err : child_err;
689 ea5e7bb5 2018-08-01 stsp static struct tog_view *
690 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
691 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
693 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
695 ea5e7bb5 2018-08-01 stsp if (view == NULL)
696 ea5e7bb5 2018-08-01 stsp return NULL;
698 d6b05b5b 2018-08-04 stsp view->type = type;
699 f7d12f7e 2018-08-01 stsp view->lines = LINES;
700 f7d12f7e 2018-08-01 stsp view->cols = COLS;
701 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
702 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
703 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
704 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
705 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
706 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
710 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
711 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
712 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
713 96a765a8 2018-08-04 stsp view_close(view);
714 ea5e7bb5 2018-08-01 stsp return NULL;
717 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
718 ea5e7bb5 2018-08-01 stsp return view;
722 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
724 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
726 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
729 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
730 a5d43cac 2022-07-01 thomas static int
731 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
733 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
736 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
738 5c60c32a 2018-10-18 stsp static const struct got_error *
739 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
741 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
743 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
745 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
747 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
748 a5d43cac 2022-07-01 thomas view->begin_x = 0;
749 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
750 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
751 53d2bdd3 2022-07-10 thomas view->cols > 119)
752 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
754 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
755 a5d43cac 2022-07-01 thomas view->begin_y = 0;
757 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
758 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
759 5c60c32a 2018-10-18 stsp view->lines = LINES;
760 5c60c32a 2018-10-18 stsp view->cols = COLS;
761 5c60c32a 2018-10-18 stsp err = view_resize(view);
763 5c60c32a 2018-10-18 stsp return err;
765 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
768 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
771 5c60c32a 2018-10-18 stsp return NULL;
774 5c60c32a 2018-10-18 stsp static const struct got_error *
775 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
777 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
779 5c60c32a 2018-10-18 stsp view->begin_x = 0;
780 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
781 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
782 5c60c32a 2018-10-18 stsp view->ncols = COLS;
783 5c60c32a 2018-10-18 stsp view->lines = LINES;
784 5c60c32a 2018-10-18 stsp view->cols = COLS;
785 5c60c32a 2018-10-18 stsp err = view_resize(view);
787 5c60c32a 2018-10-18 stsp return err;
789 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
792 5c60c32a 2018-10-18 stsp return NULL;
796 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
798 5c60c32a 2018-10-18 stsp return view->parent == NULL;
801 b65b3ea0 2022-06-23 thomas static int
802 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
804 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
807 e44940c3 2022-07-01 thomas static int
808 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
810 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
813 444d5325 2022-07-03 thomas static int
814 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
816 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
820 a5d43cac 2022-07-01 thomas static void
821 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
823 a5d43cac 2022-07-01 thomas PANEL *panel;
824 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
826 a5d43cac 2022-07-01 thomas if (view->parent)
827 a5d43cac 2022-07-01 thomas return view_border(view->parent);
829 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
830 a5d43cac 2022-07-01 thomas if (panel == NULL)
833 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
834 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
836 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
837 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
839 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
843 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
846 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
847 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
849 4d8c2215 2018-08-19 stsp static const struct got_error *
850 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
852 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
853 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
855 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
857 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
858 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
860 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
861 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
862 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
864 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
866 4918811f 2022-07-01 thomas if (view->child) {
867 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
869 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
870 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
871 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
873 40236d76 2022-06-23 thomas ncols = COLS;
875 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
876 5c60c32a 2018-10-18 stsp if (view->child->focussed)
877 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
879 5c60c32a 2018-10-18 stsp show_panel(view->panel);
881 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
883 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
884 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
887 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
888 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
889 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
891 a5d43cac 2022-07-01 thomas if (hs) {
892 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
894 a5d43cac 2022-07-01 thomas return err;
895 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
896 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
898 a5d43cac 2022-07-01 thomas return err;
900 a5d43cac 2022-07-01 thomas view_border(view);
901 a5d43cac 2022-07-01 thomas update_panels();
902 a5d43cac 2022-07-01 thomas doupdate();
903 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
904 a5d43cac 2022-07-01 thomas nlines = view->nlines;
906 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
907 40236d76 2022-06-23 thomas ncols = COLS;
909 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
910 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
912 ea0bff04 2022-07-19 thomas return err;
915 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
916 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
917 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
918 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
919 40236d76 2022-06-23 thomas wclear(view->window);
921 40236d76 2022-06-23 thomas view->nlines = nlines;
922 40236d76 2022-06-23 thomas view->ncols = ncols;
923 40236d76 2022-06-23 thomas view->lines = LINES;
924 40236d76 2022-06-23 thomas view->cols = COLS;
926 5c60c32a 2018-10-18 stsp return NULL;
929 ea0bff04 2022-07-19 thomas static const struct got_error *
930 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
932 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
933 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
934 3a0139e8 2022-07-22 thomas int n = 0;
936 3a0139e8 2022-07-22 thomas if (s->selected_entry)
937 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
940 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
941 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
943 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
944 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
945 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
948 ea0bff04 2022-07-19 thomas return err;
951 97cb21cd 2022-07-12 thomas static void
952 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
954 97cb21cd 2022-07-12 thomas if (n == 0)
957 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
958 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
959 97cb21cd 2022-07-12 thomas view->parent->offset += n;
961 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
962 97cb21cd 2022-07-12 thomas } else if (view->offset) {
963 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
964 97cb21cd 2022-07-12 thomas view->offset -= n;
966 97cb21cd 2022-07-12 thomas view->offset = 0;
970 53d2bdd3 2022-07-10 thomas static const struct got_error *
971 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
973 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
974 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
976 53d2bdd3 2022-07-10 thomas if (view->parent)
977 53d2bdd3 2022-07-10 thomas v = view->parent;
979 53d2bdd3 2022-07-10 thomas v = view;
981 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
982 53d2bdd3 2022-07-10 thomas return NULL;
984 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
986 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
989 53d2bdd3 2022-07-10 thomas if (view->parent)
990 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
992 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
993 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
994 53d2bdd3 2022-07-10 thomas view->count = 0;
995 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
996 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
997 53d2bdd3 2022-07-10 thomas view->count = 0;
998 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1000 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1001 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1002 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1003 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1004 53d2bdd3 2022-07-10 thomas if (err)
1005 53d2bdd3 2022-07-10 thomas return err;
1006 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1007 53d2bdd3 2022-07-10 thomas } else {
1008 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1009 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1010 53d2bdd3 2022-07-10 thomas if (view->parent)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1014 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1015 53d2bdd3 2022-07-10 thomas view->count = 0;
1016 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1017 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1018 53d2bdd3 2022-07-10 thomas view->count = 0;
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1021 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1024 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1025 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1026 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1027 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1029 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1030 53d2bdd3 2022-07-10 thomas if (err)
1031 53d2bdd3 2022-07-10 thomas return err;
1032 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1033 53d2bdd3 2022-07-10 thomas if (err)
1034 53d2bdd3 2022-07-10 thomas return err;
1036 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1042 3a0139e8 2022-07-22 thomas if (v->resize)
1043 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1044 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1045 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1047 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1049 53d2bdd3 2022-07-10 thomas return err;
1052 53d2bdd3 2022-07-10 thomas static void
1053 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1055 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1057 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1058 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1061 669b5ffa 2018-10-07 stsp static const struct got_error *
1062 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1064 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1066 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1067 669b5ffa 2018-10-07 stsp return NULL;
1069 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1070 669b5ffa 2018-10-07 stsp view->child = NULL;
1071 669b5ffa 2018-10-07 stsp return err;
1074 40236d76 2022-06-23 thomas static const struct got_error *
1075 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1077 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1079 669b5ffa 2018-10-07 stsp view->child = child;
1080 669b5ffa 2018-10-07 stsp child->parent = view;
1082 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1083 53d2bdd3 2022-07-10 thomas if (err)
1084 53d2bdd3 2022-07-10 thomas return err;
1086 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1087 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1089 53d2bdd3 2022-07-10 thomas return err;
1092 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1093 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1095 2a31b33b 2022-07-23 thomas static const struct got_error *
1096 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1097 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1099 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1100 2a31b33b 2022-07-23 thomas const struct got_error *err;
1101 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1103 2a31b33b 2022-07-23 thomas *requested = NULL;
1105 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1106 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1108 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1109 2a31b33b 2022-07-23 thomas if (err)
1110 2a31b33b 2022-07-23 thomas return err;
1112 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1114 2a31b33b 2022-07-23 thomas if (err)
1115 2a31b33b 2022-07-23 thomas return err;
1118 2a31b33b 2022-07-23 thomas view->focussed = 0;
1119 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1120 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1121 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1123 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1124 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1125 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1126 2a31b33b 2022-07-23 thomas if (err)
1127 2a31b33b 2022-07-23 thomas return err;
1128 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1129 2a31b33b 2022-07-23 thomas if (err)
1130 2a31b33b 2022-07-23 thomas return err;
1131 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1133 2a31b33b 2022-07-23 thomas *requested = new_view;
1135 2a31b33b 2022-07-23 thomas return NULL;
1138 34bc9ec9 2019-02-22 stsp static void
1139 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1141 25791caa 2018-10-24 stsp int cols, lines;
1142 25791caa 2018-10-24 stsp struct winsize size;
1144 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 25791caa 2018-10-24 stsp cols = 80; /* Default */
1146 25791caa 2018-10-24 stsp lines = 24;
1148 25791caa 2018-10-24 stsp cols = size.ws_col;
1149 25791caa 2018-10-24 stsp lines = size.ws_row;
1151 25791caa 2018-10-24 stsp resize_term(lines, cols);
1154 2b49a8ae 2019-06-22 stsp static const struct got_error *
1155 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1157 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1158 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1159 2b49a8ae 2019-06-22 stsp char pattern[1024];
1162 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1163 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1164 c0c4acc8 2021-01-24 stsp view->searching = 0;
1165 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1167 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1169 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1170 2b49a8ae 2019-06-22 stsp return NULL;
1172 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1173 a5d43cac 2022-07-01 thomas v = view->child;
1175 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1178 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1179 2b49a8ae 2019-06-22 stsp nocbreak();
1181 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1185 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1186 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1187 2b49a8ae 2019-06-22 stsp return NULL;
1189 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1191 7c32bd05 2019-06-22 stsp if (err) {
1192 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1193 7c32bd05 2019-06-22 stsp return err;
1195 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1196 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1197 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1198 2b49a8ae 2019-06-22 stsp view->search_next(view);
1201 2b49a8ae 2019-06-22 stsp return NULL;
1204 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 64486692 2022-07-07 thomas static const struct got_error *
1206 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1208 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1209 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1211 64486692 2022-07-07 thomas if (view->parent)
1212 64486692 2022-07-07 thomas v = view->parent;
1214 64486692 2022-07-07 thomas v = view;
1216 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1219 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1221 ddbc4d37 2022-07-12 thomas if (!v->child)
1222 ddbc4d37 2022-07-12 thomas return NULL;
1223 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1226 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1229 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1233 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1234 64486692 2022-07-07 thomas v->ncols = COLS;
1235 64486692 2022-07-07 thomas v->child->ncols = COLS;
1236 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1238 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1239 64486692 2022-07-07 thomas if (err)
1240 64486692 2022-07-07 thomas return err;
1242 64486692 2022-07-07 thomas v->child->mode = v->mode;
1243 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1244 64486692 2022-07-07 thomas v->focus_child = 1;
1246 64486692 2022-07-07 thomas err = view_fullscreen(v);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1249 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1250 64486692 2022-07-07 thomas if (err)
1251 64486692 2022-07-07 thomas return err;
1253 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1255 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1257 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1258 ddbc4d37 2022-07-12 thomas } else {
1259 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1260 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1262 f4e6231a 2022-07-22 thomas if (v->resize)
1263 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1264 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1265 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1267 64486692 2022-07-07 thomas return err;
1271 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1272 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1274 07b0611c 2022-06-23 thomas static int
1275 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1277 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1278 a5d43cac 2022-07-01 thomas int x, n = 0;
1280 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1281 a5d43cac 2022-07-01 thomas v = view->child;
1282 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1283 a5d43cac 2022-07-01 thomas v = view->parent;
1285 07b0611c 2022-06-23 thomas view->count = 0;
1286 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1287 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1288 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1289 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1292 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1293 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1294 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1295 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1299 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1300 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1302 07b0611c 2022-06-23 thomas if (n >= 9999999)
1303 07b0611c 2022-06-23 thomas n = 9999999;
1305 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1306 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1308 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1309 07b0611c 2022-06-23 thomas view->count = n;
1311 07b0611c 2022-06-23 thomas return c;
1314 0cf4efb1 2018-09-29 stsp static const struct got_error *
1315 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1316 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1318 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1319 669b5ffa 2018-10-07 stsp struct tog_view *v;
1320 1a76625f 2018-10-22 stsp int ch, errcode;
1322 e5a0f69f 2018-08-18 stsp *new = NULL;
1324 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1325 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1326 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1327 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1328 07b0611c 2022-06-23 thomas view->count = 0;
1331 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1332 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1333 82954512 2020-02-03 stsp if (errcode)
1334 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1335 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1336 3da8ef85 2021-09-21 stsp sched_yield();
1337 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1338 82954512 2020-02-03 stsp if (errcode)
1339 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1340 82954512 2020-02-03 stsp "pthread_mutex_lock");
1341 60493ae3 2019-06-20 stsp view->search_next(view);
1342 60493ae3 2019-06-20 stsp return NULL;
1345 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1346 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1347 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1348 1a76625f 2018-10-22 stsp if (errcode)
1349 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1350 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1351 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1352 634cb454 2022-07-03 thomas cbreak();
1353 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1354 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1355 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1356 634cb454 2022-07-03 thomas view->count = 0;
1358 634cb454 2022-07-03 thomas ch = view->ch;
1359 634cb454 2022-07-03 thomas } else {
1360 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1361 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1362 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1364 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1365 1a76625f 2018-10-22 stsp if (errcode)
1366 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1367 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1369 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1370 25791caa 2018-10-24 stsp tog_resizeterm();
1371 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1372 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1373 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1374 25791caa 2018-10-24 stsp err = view_resize(v);
1376 25791caa 2018-10-24 stsp return err;
1377 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1379 25791caa 2018-10-24 stsp return err;
1380 cdfcfb03 2020-12-06 stsp if (v->child) {
1381 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1383 cdfcfb03 2020-12-06 stsp return err;
1384 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1385 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1387 cdfcfb03 2020-12-06 stsp return err;
1388 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1389 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1390 53d2bdd3 2022-07-10 thomas if (err)
1391 53d2bdd3 2022-07-10 thomas return err;
1397 e5a0f69f 2018-08-18 stsp switch (ch) {
1399 07b0611c 2022-06-23 thomas view->count = 0;
1400 1e37a5c2 2019-05-12 jcs if (view->child) {
1401 e78dc838 2020-12-04 stsp view->focussed = 0;
1402 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1403 e78dc838 2020-12-04 stsp view->focus_child = 1;
1404 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1405 e78dc838 2020-12-04 stsp view->focussed = 0;
1406 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1407 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1408 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1409 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1410 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1412 a5d43cac 2022-07-01 thomas if (err)
1413 a5d43cac 2022-07-01 thomas return err;
1415 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1416 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1417 a5d43cac 2022-07-01 thomas if (err)
1418 a5d43cac 2022-07-01 thomas return err;
1423 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1424 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1425 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1426 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1427 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1432 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1433 9970f7fc 2020-12-03 stsp view->dying = 1;
1438 1c5e5faa 2022-06-23 thomas case 'F':
1439 07b0611c 2022-06-23 thomas view->count = 0;
1440 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1441 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1443 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1444 e78dc838 2020-12-04 stsp view->focussed = 0;
1445 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1446 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1447 53d2bdd3 2022-07-10 thomas } else {
1448 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1449 53d2bdd3 2022-07-10 thomas if (!err)
1450 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1454 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1455 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1457 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1458 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1459 e78dc838 2020-12-04 stsp view->focussed = 1;
1460 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1462 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1463 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1464 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1465 53d2bdd3 2022-07-10 thomas if (!err)
1466 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1470 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1472 a5d43cac 2022-07-01 thomas if (err)
1474 3a0139e8 2022-07-22 thomas if (view->resize) {
1475 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1476 a5d43cac 2022-07-01 thomas if (err)
1479 a5d43cac 2022-07-01 thomas if (view->parent)
1480 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1481 a5d43cac 2022-07-01 thomas if (!err)
1482 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1484 64486692 2022-07-07 thomas case 'S':
1485 53d2bdd3 2022-07-10 thomas view->count = 0;
1486 64486692 2022-07-07 thomas err = switch_split(view);
1488 53d2bdd3 2022-07-10 thomas case '-':
1489 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1491 53d2bdd3 2022-07-10 thomas case '+':
1492 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1494 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1497 07b0611c 2022-06-23 thomas view->count = 0;
1498 60493ae3 2019-06-20 stsp if (view->search_start)
1499 2b49a8ae 2019-06-22 stsp view_search_start(view);
1501 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1505 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1506 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1507 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1508 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1509 60493ae3 2019-06-20 stsp view->search_next(view);
1511 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1513 adf4c9e0 2022-07-03 thomas case 'A':
1514 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1515 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1517 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1518 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1519 adf4c9e0 2022-07-03 thomas if (v->reset) {
1520 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1521 adf4c9e0 2022-07-03 thomas if (err)
1522 adf4c9e0 2022-07-03 thomas return err;
1524 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1525 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1526 adf4c9e0 2022-07-03 thomas if (err)
1527 adf4c9e0 2022-07-03 thomas return err;
1532 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1536 e5a0f69f 2018-08-18 stsp return err;
1539 ef20f542 2022-06-26 thomas static int
1540 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1542 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1543 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1545 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1547 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1550 669b5ffa 2018-10-07 stsp return view->focussed;
1553 bcbd79e2 2018-08-19 stsp static const struct got_error *
1554 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1556 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1557 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1558 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1559 64486692 2022-07-07 thomas char *mode;
1560 fd823528 2018-10-22 stsp int fast_refresh = 10;
1561 1a76625f 2018-10-22 stsp int done = 0, errcode;
1563 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1564 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1565 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1567 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1569 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1570 1a76625f 2018-10-22 stsp if (errcode)
1571 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1573 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1574 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1576 1004088d 2018-09-29 stsp view->focussed = 1;
1577 878940b7 2018-09-29 stsp err = view->show(view);
1579 0cf4efb1 2018-09-29 stsp return err;
1580 0cf4efb1 2018-09-29 stsp update_panels();
1581 0cf4efb1 2018-09-29 stsp doupdate();
1582 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1583 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1584 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1585 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1586 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1588 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1591 9970f7fc 2020-12-03 stsp if (view->dying) {
1592 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1594 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1595 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1597 e78dc838 2020-12-04 stsp else if (view->parent)
1598 669b5ffa 2018-10-07 stsp prev = view->parent;
1600 e78dc838 2020-12-04 stsp if (view->parent) {
1601 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1602 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1603 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1604 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1605 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1606 40236d76 2022-06-23 thomas if (err)
1608 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1609 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1611 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1613 9970f7fc 2020-12-03 stsp err = view_close(view);
1615 e5a0f69f 2018-08-18 stsp goto done;
1617 e78dc838 2020-12-04 stsp view = NULL;
1618 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1619 e78dc838 2020-12-04 stsp if (v->focussed)
1622 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1623 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1625 e78dc838 2020-12-04 stsp view = prev;
1626 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1627 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1628 e78dc838 2020-12-04 stsp tog_view_list_head);
1630 e78dc838 2020-12-04 stsp if (view) {
1631 e78dc838 2020-12-04 stsp if (view->focus_child) {
1632 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1633 e78dc838 2020-12-04 stsp view = view->child;
1635 e78dc838 2020-12-04 stsp view->focussed = 1;
1639 bcbd79e2 2018-08-19 stsp if (new_view) {
1640 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1641 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1642 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1643 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1645 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1646 86c66b02 2018-10-18 stsp err = view_close(v);
1648 86c66b02 2018-10-18 stsp goto done;
1651 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1652 fed7eaa8 2018-10-24 stsp view = new_view;
1654 669b5ffa 2018-10-07 stsp if (view) {
1655 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1656 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1657 e78dc838 2020-12-04 stsp view = view->child;
1659 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1660 e78dc838 2020-12-04 stsp view = view->parent;
1662 e78dc838 2020-12-04 stsp show_panel(view->panel);
1663 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1664 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1665 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1666 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1668 1a76625f 2018-10-22 stsp goto done;
1670 669b5ffa 2018-10-07 stsp err = view->show(view);
1672 1a76625f 2018-10-22 stsp goto done;
1673 669b5ffa 2018-10-07 stsp if (view->child) {
1674 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1676 1a76625f 2018-10-22 stsp goto done;
1678 1a76625f 2018-10-22 stsp update_panels();
1679 1a76625f 2018-10-22 stsp doupdate();
1683 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1684 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1685 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1686 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1687 f2d749db 2022-07-12 thomas close_err = view_close(view);
1688 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1689 f2d749db 2022-07-12 thomas err = close_err;
1692 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1693 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1694 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1696 e5a0f69f 2018-08-18 stsp return err;
1699 4ed7e80c 2018-05-20 stsp __dead static void
1700 9f7d7167 2018-04-29 stsp usage_log(void)
1703 c70c5802 2018-08-01 stsp fprintf(stderr,
1704 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1705 9f7d7167 2018-04-29 stsp getprogname());
1709 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1710 80ddbec8 2018-04-29 stsp static const struct got_error *
1711 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1713 00dfcb92 2018-06-11 stsp char *vis = NULL;
1714 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1716 963b370f 2018-05-20 stsp *ws = NULL;
1717 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1718 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1719 00dfcb92 2018-06-11 stsp int vislen;
1720 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1721 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1723 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1724 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1726 00dfcb92 2018-06-11 stsp return err;
1727 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1728 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1729 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1730 a7f50699 2018-06-11 stsp goto done;
1734 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1735 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1736 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1737 a7f50699 2018-06-11 stsp goto done;
1740 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1741 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1743 00dfcb92 2018-06-11 stsp free(vis);
1744 963b370f 2018-05-20 stsp if (err) {
1745 963b370f 2018-05-20 stsp free(*ws);
1746 963b370f 2018-05-20 stsp *ws = NULL;
1747 963b370f 2018-05-20 stsp *wlen = 0;
1749 963b370f 2018-05-20 stsp return err;
1752 05171be4 2022-06-23 thomas static const struct got_error *
1753 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1755 05171be4 2022-06-23 thomas char *dst;
1756 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1758 05171be4 2022-06-23 thomas *ptr = NULL;
1759 05171be4 2022-06-23 thomas n = len = strlen(src);
1760 83316834 2022-06-23 thomas dst = malloc(n + 1);
1761 05171be4 2022-06-23 thomas if (dst == NULL)
1762 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1764 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1765 05171be4 2022-06-23 thomas const char c = src[idx];
1767 05171be4 2022-06-23 thomas if (c == '\t') {
1768 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1769 b235ad5d 2022-06-23 thomas char *p;
1771 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1772 83316834 2022-06-23 thomas if (p == NULL) {
1773 83316834 2022-06-23 thomas free(dst);
1774 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1777 83316834 2022-06-23 thomas dst = p;
1778 05171be4 2022-06-23 thomas n += nb;
1779 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1780 05171be4 2022-06-23 thomas sz += nb;
1782 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1786 05171be4 2022-06-23 thomas dst[sz] = '\0';
1787 05171be4 2022-06-23 thomas *ptr = dst;
1788 05171be4 2022-06-23 thomas return NULL;
1792 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1793 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1794 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1795 8d208d34 2022-06-23 thomas * *rcol.
1797 8d208d34 2022-06-23 thomas static int
1798 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1800 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1802 8d208d34 2022-06-23 thomas if (n == 0) {
1803 8d208d34 2022-06-23 thomas *rcol = cols;
1804 8d208d34 2022-06-23 thomas return off;
1807 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1808 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1809 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1811 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1813 8d208d34 2022-06-23 thomas if (width == -1) {
1814 8d208d34 2022-06-23 thomas width = 1;
1815 8d208d34 2022-06-23 thomas wline[i] = L'.';
1818 8d208d34 2022-06-23 thomas if (cols + width > n)
1820 8d208d34 2022-06-23 thomas cols += width;
1823 8d208d34 2022-06-23 thomas *rcol = cols;
1824 8d208d34 2022-06-23 thomas return i;
1828 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1829 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1830 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1832 f91a2b48 2022-06-23 thomas static const struct got_error *
1833 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1834 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1836 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1837 8d208d34 2022-06-23 thomas int cols;
1838 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1839 05171be4 2022-06-23 thomas char *exstr = NULL;
1840 963b370f 2018-05-20 stsp size_t wlen;
1841 8d208d34 2022-06-23 thomas int i, scrollx;
1843 963b370f 2018-05-20 stsp *wlinep = NULL;
1844 b700b5d6 2018-07-10 stsp *widthp = 0;
1846 05171be4 2022-06-23 thomas if (expand) {
1847 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1848 05171be4 2022-06-23 thomas if (err)
1849 05171be4 2022-06-23 thomas return err;
1852 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1853 05171be4 2022-06-23 thomas free(exstr);
1855 963b370f 2018-05-20 stsp return err;
1857 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1859 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1860 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1863 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1864 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1868 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1869 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1871 b700b5d6 2018-07-10 stsp if (widthp)
1872 b700b5d6 2018-07-10 stsp *widthp = cols;
1873 f91a2b48 2022-06-23 thomas if (scrollxp)
1874 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1876 963b370f 2018-05-20 stsp free(wline);
1878 963b370f 2018-05-20 stsp *wlinep = wline;
1879 963b370f 2018-05-20 stsp return err;
1882 8b473291 2019-02-21 stsp static const struct got_error*
1883 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1884 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1886 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1887 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1889 8b473291 2019-02-21 stsp const char *name;
1891 8b473291 2019-02-21 stsp *refs_str = NULL;
1893 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1894 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1895 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1898 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1899 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1901 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1902 8b473291 2019-02-21 stsp name += 5;
1903 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1904 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1906 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1907 8b473291 2019-02-21 stsp name += 6;
1908 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1909 8b473291 2019-02-21 stsp name += 8;
1910 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1911 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1914 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1917 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1918 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1919 5d844a1e 2019-08-13 stsp if (err) {
1920 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1921 48cae60d 2020-09-22 stsp free(ref_id);
1924 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1925 5d844a1e 2019-08-13 stsp err = NULL;
1926 5d844a1e 2019-08-13 stsp tag = NULL;
1929 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1930 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1931 48cae60d 2020-09-22 stsp free(ref_id);
1933 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1934 52b5abe1 2019-08-13 stsp if (cmp != 0)
1936 8b473291 2019-02-21 stsp s = *refs_str;
1937 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1938 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1939 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1941 8b473291 2019-02-21 stsp *refs_str = NULL;
1947 8b473291 2019-02-21 stsp return err;
1950 963b370f 2018-05-20 stsp static const struct got_error *
1951 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1952 27a741e5 2019-09-11 stsp int col_tab_align)
1954 e6b8b890 2020-12-29 naddy char *smallerthan;
1956 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1957 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1958 5813d178 2019-03-09 stsp author = smallerthan + 1;
1959 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1960 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1961 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1964 5813d178 2019-03-09 stsp static const struct got_error *
1965 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1966 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1967 8fdc79fe 2020-12-01 naddy int author_display_cols)
1969 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1970 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1971 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1972 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1973 5813d178 2019-03-09 stsp char *author = NULL;
1974 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1975 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1976 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1977 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1978 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1979 ccb26ccd 2018-11-05 stsp struct tm tm;
1980 45d799e2 2018-12-23 stsp time_t committer_time;
1981 11b20872 2019-11-08 stsp struct tog_color *tc;
1983 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1984 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1985 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1986 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1987 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1989 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1990 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1992 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1993 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1995 11b20872 2019-11-08 stsp wattr_on(view->window,
1996 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1997 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1999 11b20872 2019-11-08 stsp wattr_off(view->window,
2000 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2001 27a741e5 2019-09-11 stsp col = limit;
2002 b39d25c7 2018-07-10 stsp if (col > avail)
2003 b39d25c7 2018-07-10 stsp goto done;
2005 6570a66d 2019-11-08 stsp if (avail >= 120) {
2006 6570a66d 2019-11-08 stsp char *id_str;
2007 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2009 6570a66d 2019-11-08 stsp goto done;
2010 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2012 11b20872 2019-11-08 stsp wattr_on(view->window,
2013 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2014 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2016 11b20872 2019-11-08 stsp wattr_off(view->window,
2017 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2018 6570a66d 2019-11-08 stsp free(id_str);
2020 6570a66d 2019-11-08 stsp if (col > avail)
2021 6570a66d 2019-11-08 stsp goto done;
2024 f69c5a46 2022-07-19 thomas if (s->use_committer)
2025 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2027 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2028 5813d178 2019-03-09 stsp if (author == NULL) {
2029 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2030 80ddbec8 2018-04-29 stsp goto done;
2032 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2034 bb737323 2018-05-20 stsp goto done;
2035 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2037 11b20872 2019-11-08 stsp wattr_on(view->window,
2038 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2039 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2041 11b20872 2019-11-08 stsp wattr_off(view->window,
2042 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2043 bb737323 2018-05-20 stsp col += author_width;
2044 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2045 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2047 bb737323 2018-05-20 stsp author_width++;
2049 9c2eaf34 2018-05-20 stsp if (col > avail)
2050 9c2eaf34 2018-05-20 stsp goto done;
2052 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2054 6d9fbc00 2018-04-29 stsp goto done;
2055 bb737323 2018-05-20 stsp logmsg = logmsg0;
2056 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2058 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2059 bb737323 2018-05-20 stsp if (newline)
2060 bb737323 2018-05-20 stsp *newline = '\0';
2061 f91a2b48 2022-06-23 thomas limit = avail - col;
2062 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2063 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2064 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2065 f91a2b48 2022-06-23 thomas limit, col, 1);
2066 331b1a16 2022-06-23 thomas if (err)
2067 331b1a16 2022-06-23 thomas goto done;
2068 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2069 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2070 27a741e5 2019-09-11 stsp while (col < avail) {
2071 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2075 80ddbec8 2018-04-29 stsp free(logmsg0);
2076 bb737323 2018-05-20 stsp free(wlogmsg);
2077 5813d178 2019-03-09 stsp free(author);
2078 bb737323 2018-05-20 stsp free(wauthor);
2079 80ddbec8 2018-04-29 stsp free(line);
2080 80ddbec8 2018-04-29 stsp return err;
2083 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2084 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2085 899d86c2 2018-05-10 stsp struct got_object_id *id)
2087 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2089 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2090 80ddbec8 2018-04-29 stsp if (entry == NULL)
2091 899d86c2 2018-05-10 stsp return NULL;
2093 899d86c2 2018-05-10 stsp entry->id = id;
2094 99db9666 2018-05-07 stsp entry->commit = commit;
2095 899d86c2 2018-05-10 stsp return entry;
2098 99db9666 2018-05-07 stsp static void
2099 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2101 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2103 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2104 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2105 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2106 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2107 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2108 99db9666 2018-05-07 stsp free(entry);
2111 99db9666 2018-05-07 stsp static void
2112 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2114 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2115 99db9666 2018-05-07 stsp pop_commit(commits);
2118 c4972b91 2018-05-07 stsp static const struct got_error *
2119 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2120 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2122 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2123 13add988 2019-10-15 stsp regmatch_t regmatch;
2124 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2126 13add988 2019-10-15 stsp *have_match = 0;
2128 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2130 13add988 2019-10-15 stsp return err;
2132 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2134 13add988 2019-10-15 stsp goto done;
2136 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2137 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2138 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2139 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2140 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2141 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2142 13add988 2019-10-15 stsp *have_match = 1;
2144 13add988 2019-10-15 stsp free(id_str);
2145 13add988 2019-10-15 stsp free(logmsg);
2146 13add988 2019-10-15 stsp return err;
2149 13add988 2019-10-15 stsp static const struct got_error *
2150 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2152 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2155 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2156 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2157 1a76625f 2018-10-22 stsp * while updating the display.
2160 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2161 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2162 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2163 1a76625f 2018-10-22 stsp int errcode;
2165 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2166 4e0d2870 2020-12-07 naddy NULL, NULL);
2167 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2170 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2173 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2174 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2175 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2179 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2180 1a76625f 2018-10-22 stsp if (errcode) {
2181 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2182 13add988 2019-10-15 stsp "pthread_mutex_lock");
2186 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2187 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2188 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2190 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2191 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2192 7c1452c1 2020-03-26 stsp int have_match;
2193 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2196 7c1452c1 2020-03-26 stsp if (have_match)
2197 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2200 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2201 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2202 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2203 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2206 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2208 9ba79e04 2018-06-11 stsp return err;
2211 2b779855 2020-12-05 naddy static void
2212 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2214 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2215 2b779855 2020-12-05 naddy int ncommits = 0;
2217 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2218 2b779855 2020-12-05 naddy while (entry) {
2219 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2220 2b779855 2020-12-05 naddy s->selected_entry = entry;
2223 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2224 2b779855 2020-12-05 naddy ncommits++;
2228 0553a4e3 2018-04-30 stsp static const struct got_error *
2229 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2231 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2232 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2233 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2234 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2235 60493ae3 2019-06-20 stsp int width;
2236 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2237 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2238 8b473291 2019-02-21 stsp char *refs_str = NULL;
2239 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2240 11b20872 2019-11-08 stsp struct tog_color *tc;
2241 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2243 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2244 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2245 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2246 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2248 ecb28ae0 2018-07-16 stsp return err;
2249 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2250 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2251 d2075bf3 2020-12-25 stsp if (refs) {
2252 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2253 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2255 d2075bf3 2020-12-25 stsp goto done;
2259 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2260 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2262 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2263 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2264 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2265 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2266 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2267 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2268 8f4ed634 2020-03-26 stsp goto done;
2271 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2273 f9686aa5 2020-03-27 stsp if (view->searching) {
2274 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2275 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2276 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2277 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2278 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2279 f9686aa5 2020-03-27 stsp search_str = "searching...";
2282 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2283 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2284 f9686aa5 2020-03-27 stsp search_str ? search_str :
2285 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2286 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2287 8f4ed634 2020-03-26 stsp goto done;
2291 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2292 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2293 91198554 2022-06-23 thomas "........................................",
2294 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2295 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2296 1a76625f 2018-10-22 stsp header = NULL;
2297 1a76625f 2018-10-22 stsp goto done;
2299 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2300 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2301 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2302 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2303 1a76625f 2018-10-22 stsp header = NULL;
2304 1a76625f 2018-10-22 stsp goto done;
2306 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2308 1a76625f 2018-10-22 stsp goto done;
2310 2814baeb 2018-08-01 stsp werase(view->window);
2312 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2313 a3404814 2018-09-02 stsp wstandout(view->window);
2314 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2316 11b20872 2019-11-08 stsp wattr_on(view->window,
2317 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2318 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2320 11b20872 2019-11-08 stsp wattr_off(view->window,
2321 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2322 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2323 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2326 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2327 a3404814 2018-09-02 stsp wstandend(view->window);
2328 ecb28ae0 2018-07-16 stsp free(wline);
2329 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2330 1a76625f 2018-10-22 stsp goto done;
2332 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2333 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2334 5813d178 2019-03-09 stsp ncommits = 0;
2335 05171be4 2022-06-23 thomas view->maxx = 0;
2336 5813d178 2019-03-09 stsp while (entry) {
2337 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2338 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2339 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2340 5813d178 2019-03-09 stsp int width;
2341 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2343 f69c5a46 2022-07-19 thomas if (s->use_committer)
2344 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2346 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2347 5813d178 2019-03-09 stsp if (author == NULL) {
2348 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2349 5813d178 2019-03-09 stsp goto done;
2351 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2352 27a741e5 2019-09-11 stsp date_display_cols);
2353 5813d178 2019-03-09 stsp if (author_cols < width)
2354 5813d178 2019-03-09 stsp author_cols = width;
2355 5813d178 2019-03-09 stsp free(wauthor);
2356 5813d178 2019-03-09 stsp free(author);
2357 ef944b8b 2022-07-21 thomas if (err)
2358 ef944b8b 2022-07-21 thomas goto done;
2359 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2360 05171be4 2022-06-23 thomas if (err)
2361 05171be4 2022-06-23 thomas goto done;
2362 05171be4 2022-06-23 thomas msg = msg0;
2363 05171be4 2022-06-23 thomas while (*msg == '\n')
2365 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2366 331b1a16 2022-06-23 thomas *eol = '\0';
2367 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2368 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2369 331b1a16 2022-06-23 thomas if (err)
2370 331b1a16 2022-06-23 thomas goto done;
2371 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2372 05171be4 2022-06-23 thomas free(msg0);
2373 331b1a16 2022-06-23 thomas free(wmsg);
2374 7ca04879 2019-10-19 stsp ncommits++;
2375 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2378 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2379 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2380 867c6645 2018-07-10 stsp ncommits = 0;
2381 899d86c2 2018-05-10 stsp while (entry) {
2382 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2384 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2385 2814baeb 2018-08-01 stsp wstandout(view->window);
2386 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2387 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2388 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2389 2814baeb 2018-08-01 stsp wstandend(view->window);
2391 60493ae3 2019-06-20 stsp goto done;
2392 0553a4e3 2018-04-30 stsp ncommits++;
2393 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2394 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2397 a5d43cac 2022-07-01 thomas view_border(view);
2399 1a76625f 2018-10-22 stsp free(id_str);
2400 8b473291 2019-02-21 stsp free(refs_str);
2401 1a76625f 2018-10-22 stsp free(ncommits_str);
2402 1a76625f 2018-10-22 stsp free(header);
2403 80ddbec8 2018-04-29 stsp return err;
2406 07b55e75 2018-05-10 stsp static void
2407 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2409 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2410 07b55e75 2018-05-10 stsp int nscrolled = 0;
2412 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2413 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2416 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2417 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2418 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2419 07b55e75 2018-05-10 stsp if (entry) {
2420 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2421 07b55e75 2018-05-10 stsp nscrolled++;
2426 aa075928 2018-05-10 stsp static const struct got_error *
2427 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2429 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2430 5e224a3e 2019-02-22 stsp int errcode;
2432 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2434 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2435 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2436 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2437 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2438 7aafa0d1 2019-02-22 stsp if (errcode)
2439 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2440 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2443 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2444 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2446 7c1452c1 2020-03-26 stsp if (!wait)
2449 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2450 ffe38506 2020-12-01 naddy show_log_view(view);
2451 7c1452c1 2020-03-26 stsp update_panels();
2452 7c1452c1 2020-03-26 stsp doupdate();
2454 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2455 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2456 82954512 2020-02-03 stsp if (errcode)
2457 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2458 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2460 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2461 ffe38506 2020-12-01 naddy show_log_view(view);
2462 7c1452c1 2020-03-26 stsp update_panels();
2463 7c1452c1 2020-03-26 stsp doupdate();
2466 5e224a3e 2019-02-22 stsp return NULL;
2469 a5d43cac 2022-07-01 thomas static const struct got_error *
2470 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2472 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2473 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2475 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2476 fe731b51 2022-07-14 thomas return NULL;
2478 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2479 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2480 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2482 a5d43cac 2022-07-01 thomas return err;
2485 5e224a3e 2019-02-22 stsp static const struct got_error *
2486 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2488 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2489 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2490 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2491 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2493 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2494 5e224a3e 2019-02-22 stsp return NULL;
2496 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2497 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2498 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2500 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2502 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2503 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2505 5e224a3e 2019-02-22 stsp return err;
2509 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2510 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2513 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2514 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2516 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2517 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2519 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2520 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2522 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2523 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2525 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2527 dd0a52c1 2018-05-20 stsp return err;
2530 cd0acaa7 2018-05-20 stsp static const struct got_error *
2531 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2532 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2533 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2535 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2536 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2537 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2539 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2540 15a94983 2018-12-23 stsp if (diff_view == NULL)
2541 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2543 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2544 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2545 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2546 e5a0f69f 2018-08-18 stsp if (err == NULL)
2547 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2548 cd0acaa7 2018-05-20 stsp return err;
2551 80ddbec8 2018-04-29 stsp static const struct got_error *
2552 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2553 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2555 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2557 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2558 941e9f74 2019-05-21 stsp if (parent == NULL)
2559 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2561 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2562 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2563 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2564 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2565 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2566 941e9f74 2019-05-21 stsp s->tree = subtree;
2567 941e9f74 2019-05-21 stsp s->selected = 0;
2568 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2569 941e9f74 2019-05-21 stsp return NULL;
2572 941e9f74 2019-05-21 stsp static const struct got_error *
2573 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2574 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2576 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2577 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2578 941e9f74 2019-05-21 stsp const char *p;
2579 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2581 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2583 941e9f74 2019-05-21 stsp while (*p) {
2584 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2585 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2586 56e0773d 2019-11-28 stsp char *te_name;
2588 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2591 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2592 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2593 941e9f74 2019-05-21 stsp if (slash == NULL)
2594 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2596 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2597 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2598 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2601 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2602 56e0773d 2019-11-28 stsp if (te == NULL) {
2603 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2604 56e0773d 2019-11-28 stsp free(te_name);
2607 56e0773d 2019-11-28 stsp free(te_name);
2608 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2610 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2611 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2613 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2614 941e9f74 2019-05-21 stsp if (slash)
2615 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2617 941e9f74 2019-05-21 stsp subpath = strdup(path);
2618 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2619 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2623 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2628 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2629 941e9f74 2019-05-21 stsp free(tree_id);
2633 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2634 941e9f74 2019-05-21 stsp if (err) {
2635 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2638 941e9f74 2019-05-21 stsp if (slash == NULL)
2640 941e9f74 2019-05-21 stsp free(subpath);
2641 941e9f74 2019-05-21 stsp subpath = NULL;
2642 941e9f74 2019-05-21 stsp p = slash;
2645 941e9f74 2019-05-21 stsp free(subpath);
2646 1a76625f 2018-10-22 stsp return err;
2649 61266923 2020-01-14 stsp static const struct got_error *
2650 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2651 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2652 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2654 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2655 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2656 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2658 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2659 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2660 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2662 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2664 55cccc34 2020-02-20 stsp return err;
2665 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2667 55cccc34 2020-02-20 stsp *new_view = tree_view;
2669 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2670 55cccc34 2020-02-20 stsp return NULL;
2672 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2675 55cccc34 2020-02-20 stsp static const struct got_error *
2676 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2678 61266923 2020-01-14 stsp sigset_t sigset;
2679 61266923 2020-01-14 stsp int errcode;
2681 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2682 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2684 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2685 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2686 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2687 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2688 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2689 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2690 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2691 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2692 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2694 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2695 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2696 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2698 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2699 61266923 2020-01-14 stsp if (errcode)
2700 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2702 61266923 2020-01-14 stsp return NULL;
2705 1a76625f 2018-10-22 stsp static void *
2706 1a76625f 2018-10-22 stsp log_thread(void *arg)
2708 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2709 1a76625f 2018-10-22 stsp int errcode = 0;
2710 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2711 1a76625f 2018-10-22 stsp int done = 0;
2714 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2715 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2717 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2718 f2d749db 2022-07-12 thomas if (errcode) {
2719 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2720 61266923 2020-01-14 stsp return (void *)err;
2723 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2724 f2d749db 2022-07-12 thomas if (err) {
2725 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2726 f2d749db 2022-07-12 thomas goto done;
2729 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2730 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2731 f2d749db 2022-07-12 thomas if (errcode) {
2732 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2733 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2734 f2d749db 2022-07-12 thomas goto done;
2736 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2737 1a76625f 2018-10-22 stsp if (err) {
2738 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2739 f2d749db 2022-07-12 thomas goto done;
2740 1a76625f 2018-10-22 stsp err = NULL;
2742 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2743 1a76625f 2018-10-22 stsp a->commits_needed--;
2745 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2746 3abe8080 2019-04-10 stsp if (errcode) {
2747 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2748 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2749 f2d749db 2022-07-12 thomas goto done;
2750 3abe8080 2019-04-10 stsp } else if (*a->quit)
2752 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2753 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2754 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2755 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2758 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2759 7c1452c1 2020-03-26 stsp if (errcode) {
2760 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2761 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2762 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2763 f2d749db 2022-07-12 thomas goto done;
2767 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2769 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2770 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2771 7c1452c1 2020-03-26 stsp &tog_mutex);
2772 f2d749db 2022-07-12 thomas if (errcode) {
2773 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2774 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2775 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2776 f2d749db 2022-07-12 thomas goto done;
2778 21355643 2020-12-06 stsp if (*a->quit)
2783 3abe8080 2019-04-10 stsp a->log_complete = 1;
2784 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2785 f2d749db 2022-07-12 thomas if (errcode)
2786 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2788 f2d749db 2022-07-12 thomas if (err) {
2789 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2790 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2792 1a76625f 2018-10-22 stsp return (void *)err;
2795 1a76625f 2018-10-22 stsp static const struct got_error *
2796 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2798 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2799 1a76625f 2018-10-22 stsp int errcode;
2801 1a76625f 2018-10-22 stsp if (s->thread) {
2802 1a76625f 2018-10-22 stsp s->quit = 1;
2803 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2804 1a76625f 2018-10-22 stsp if (errcode)
2805 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2806 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2807 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2808 1a76625f 2018-10-22 stsp if (errcode)
2809 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2810 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2811 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2812 1a76625f 2018-10-22 stsp if (errcode)
2813 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2814 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2815 1a76625f 2018-10-22 stsp if (errcode)
2816 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2817 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2818 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2821 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2822 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2823 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2826 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2827 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2828 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2829 1af5eddf 2022-06-23 thomas if (err == NULL)
2830 1af5eddf 2022-06-23 thomas err = pack_err;
2831 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2834 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2835 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2836 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2839 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2842 9343a5fb 2018-06-23 stsp static const struct got_error *
2843 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2845 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2846 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2847 276b94a1 2020-11-13 naddy int errcode;
2849 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2851 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2852 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2853 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2855 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2856 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2857 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2859 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2860 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2861 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2862 1a76625f 2018-10-22 stsp free(s->start_id);
2863 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2864 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2865 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2866 1a76625f 2018-10-22 stsp return err;
2869 1a76625f 2018-10-22 stsp static const struct got_error *
2870 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2872 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2874 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2875 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2876 60493ae3 2019-06-20 stsp return NULL;
2879 60493ae3 2019-06-20 stsp static const struct got_error *
2880 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2882 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2883 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2884 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2886 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2887 f9686aa5 2020-03-27 stsp show_log_view(view);
2888 f9686aa5 2020-03-27 stsp update_panels();
2889 f9686aa5 2020-03-27 stsp doupdate();
2891 96e2b566 2019-07-08 stsp if (s->search_entry) {
2892 13add988 2019-10-15 stsp int errcode, ch;
2893 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2894 13add988 2019-10-15 stsp if (errcode)
2895 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2896 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2897 13add988 2019-10-15 stsp ch = wgetch(view->window);
2898 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2899 13add988 2019-10-15 stsp if (errcode)
2900 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2901 13add988 2019-10-15 stsp "pthread_mutex_lock");
2902 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2903 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2904 678cbce5 2019-07-28 stsp return NULL;
2906 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2907 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2909 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2910 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2911 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2912 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2913 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2916 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2917 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2918 1f4d5162 2022-06-23 thomas * might have changed.
2920 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2921 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2922 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2924 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2925 b74feda6 2022-06-23 thomas } else {
2926 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2927 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2928 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2930 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2931 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2934 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2937 60493ae3 2019-06-20 stsp while (1) {
2938 13add988 2019-10-15 stsp int have_match = 0;
2940 60493ae3 2019-06-20 stsp if (entry == NULL) {
2941 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2942 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2943 f9967bca 2020-03-27 stsp view->search_next_done =
2944 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2945 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2946 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2947 f801134a 2019-06-25 stsp return NULL;
2950 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2951 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2952 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2954 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2955 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2958 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2959 13add988 2019-10-15 stsp &view->regex);
2962 13add988 2019-10-15 stsp if (have_match) {
2963 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2964 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2968 96e2b566 2019-07-08 stsp s->search_entry = entry;
2969 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2970 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2972 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2975 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2976 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2977 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2978 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2980 60493ae3 2019-06-20 stsp return err;
2983 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2984 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2986 60493ae3 2019-06-20 stsp return err;
2991 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2993 60493ae3 2019-06-20 stsp return NULL;
2996 60493ae3 2019-06-20 stsp static const struct got_error *
2997 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2998 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2999 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3001 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3002 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3003 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3004 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3005 1a76625f 2018-10-22 stsp int errcode;
3007 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3008 f135c941 2020-02-20 stsp free(s->in_repo_path);
3009 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3010 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3011 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3014 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3015 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3016 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3018 fb2756b9 2018-08-04 stsp s->repo = repo;
3019 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3020 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3021 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3022 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3023 9cd7cbd1 2020-12-07 stsp goto done;
3026 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3027 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3028 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3029 5036bf37 2018-09-24 stsp goto done;
3031 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3033 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3034 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3035 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3036 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3038 11b20872 2019-11-08 stsp goto done;
3039 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3040 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3041 11b20872 2019-11-08 stsp if (err) {
3042 11b20872 2019-11-08 stsp free_colors(&s->colors);
3043 11b20872 2019-11-08 stsp goto done;
3045 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3046 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3047 11b20872 2019-11-08 stsp if (err) {
3048 11b20872 2019-11-08 stsp free_colors(&s->colors);
3049 11b20872 2019-11-08 stsp goto done;
3053 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3054 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3055 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3056 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3057 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3058 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3060 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3061 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3062 1af5eddf 2022-06-23 thomas if (err)
3063 1af5eddf 2022-06-23 thomas goto done;
3065 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3066 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3067 7cd52833 2022-06-23 thomas if (err)
3068 7cd52833 2022-06-23 thomas goto done;
3069 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3070 b672a97a 2020-01-27 stsp !s->log_branches);
3072 1a76625f 2018-10-22 stsp goto done;
3073 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3074 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3076 c5b78334 2020-01-12 stsp goto done;
3078 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3079 1a76625f 2018-10-22 stsp if (errcode) {
3080 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3081 1a76625f 2018-10-22 stsp goto done;
3083 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3084 7c1452c1 2020-03-26 stsp if (errcode) {
3085 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3086 7c1452c1 2020-03-26 stsp goto done;
3089 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3090 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3091 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3092 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3093 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3094 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3095 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3096 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3097 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3098 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3099 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3100 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3101 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3104 1a76625f 2018-10-22 stsp close_log_view(view);
3105 ba4f502b 2018-08-04 stsp return err;
3108 e5a0f69f 2018-08-18 stsp static const struct got_error *
3109 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3111 f2f6d207 2020-11-24 stsp const struct got_error *err;
3112 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3114 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3115 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3116 2b380cc8 2018-10-24 stsp &s->thread_args);
3117 2b380cc8 2018-10-24 stsp if (errcode)
3118 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3119 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3120 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3122 f2f6d207 2020-11-24 stsp return err;
3126 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3129 b31f89ff 2022-07-01 thomas static void
3130 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3132 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3134 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3135 b31f89ff 2022-07-01 thomas view->count = 0;
3136 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3139 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3140 b31f89ff 2022-07-01 thomas || home)
3141 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3143 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3144 b31f89ff 2022-07-01 thomas --s->selected;
3146 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3148 b31f89ff 2022-07-01 thomas select_commit(s);
3152 b31f89ff 2022-07-01 thomas static const struct got_error *
3153 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3155 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3156 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3157 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3159 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3160 b31f89ff 2022-07-01 thomas if (first == NULL) {
3161 b31f89ff 2022-07-01 thomas view->count = 0;
3162 b31f89ff 2022-07-01 thomas return NULL;
3165 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3166 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3167 b31f89ff 2022-07-01 thomas return NULL;
3169 b31f89ff 2022-07-01 thomas if (!page) {
3170 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3172 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3173 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3174 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3175 b31f89ff 2022-07-01 thomas ++s->selected;
3177 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3178 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3179 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3180 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3181 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3183 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3184 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3185 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3186 b31f89ff 2022-07-01 thomas } else {
3187 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3188 b31f89ff 2022-07-01 thomas if (err)
3189 b31f89ff 2022-07-01 thomas return err;
3190 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3191 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3192 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3195 b31f89ff 2022-07-01 thomas if (err)
3196 b31f89ff 2022-07-01 thomas return err;
3199 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3200 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3202 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3203 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3205 b31f89ff 2022-07-01 thomas select_commit(s);
3207 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3208 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3209 b31f89ff 2022-07-01 thomas view->count = 0;
3211 b31f89ff 2022-07-01 thomas return NULL;
3214 a5d43cac 2022-07-01 thomas static void
3215 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3220 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3221 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3222 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3223 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3224 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3226 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3227 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3228 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3229 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3230 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3231 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3233 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3237 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3238 e5a0f69f 2018-08-18 stsp static const struct got_error *
3239 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3241 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3243 a5d43cac 2022-07-01 thomas view->nlines = y;
3244 64486692 2022-07-07 thomas view->ncols = COLS;
3245 a5d43cac 2022-07-01 thomas err = view_resize(view);
3246 a5d43cac 2022-07-01 thomas if (err)
3247 a5d43cac 2022-07-01 thomas return err;
3249 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3251 a5d43cac 2022-07-01 thomas return err;
3254 a5d43cac 2022-07-01 thomas static const struct got_error *
3255 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3257 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3258 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3259 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3260 2a31b33b 2022-07-23 thomas int eos, n, nscroll;
3262 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3263 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3264 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3265 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3266 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3267 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3269 b31f89ff 2022-07-01 thomas return err;
3272 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3273 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3274 068ab281 2022-07-01 thomas --eos; /* border */
3276 528dedf3 2021-08-30 stsp switch (ch) {
3278 1e37a5c2 2019-05-12 jcs s->quit = 1;
3280 05171be4 2022-06-23 thomas case '0':
3281 05171be4 2022-06-23 thomas view->x = 0;
3283 05171be4 2022-06-23 thomas case '$':
3284 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3285 07b0611c 2022-06-23 thomas view->count = 0;
3287 05171be4 2022-06-23 thomas case KEY_RIGHT:
3288 05171be4 2022-06-23 thomas case 'l':
3289 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3290 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3292 07b0611c 2022-06-23 thomas view->count = 0;
3294 05171be4 2022-06-23 thomas case KEY_LEFT:
3295 05171be4 2022-06-23 thomas case 'h':
3296 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3297 07b0611c 2022-06-23 thomas if (view->x <= 0)
3298 07b0611c 2022-06-23 thomas view->count = 0;
3301 1e37a5c2 2019-05-12 jcs case KEY_UP:
3304 f7140bf5 2021-10-17 thomas case CTRL('p'):
3305 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3308 912a3f79 2021-08-30 j case KEY_HOME:
3309 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3310 07b0611c 2022-06-23 thomas view->count = 0;
3312 70f17a53 2022-06-13 thomas case CTRL('u'):
3313 23427b14 2022-06-23 thomas case 'u':
3314 70f17a53 2022-06-13 thomas nscroll /= 2;
3315 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3316 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3317 a4292ac5 2019-05-12 jcs case CTRL('b'):
3318 1c5e5faa 2022-06-23 thomas case 'b':
3319 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3322 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3325 f7140bf5 2021-10-17 thomas case CTRL('n'):
3326 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3328 f69c5a46 2022-07-19 thomas case '@':
3329 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3332 912a3f79 2021-08-30 j case KEY_END: {
3333 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3334 912a3f79 2021-08-30 j * traverse them all. */
3335 07b0611c 2022-06-23 thomas view->count = 0;
3336 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3337 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3338 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3341 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3342 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3343 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3344 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3346 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3347 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3349 f3bc9f1d 2021-09-05 naddy if (n > 0)
3350 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3351 2b779855 2020-12-05 naddy select_commit(s);
3354 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3355 23427b14 2022-06-23 thomas case 'd':
3356 70f17a53 2022-06-13 thomas nscroll /= 2;
3357 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3358 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3359 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3360 4c2d69cb 2022-06-23 thomas case 'f':
3361 b31f89ff 2022-07-01 thomas case ' ':
3362 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3364 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3365 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3366 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3367 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3368 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3369 2b779855 2020-12-05 naddy select_commit(s);
3370 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3371 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3372 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3373 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3374 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3377 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3378 444d5325 2022-07-03 thomas case '\r':
3379 07b0611c 2022-06-23 thomas view->count = 0;
3380 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3382 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3384 1be4947a 2022-07-22 thomas case 'T':
3385 07b0611c 2022-06-23 thomas view->count = 0;
3386 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3388 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3390 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3391 21355643 2020-12-06 stsp case CTRL('l'):
3393 07b0611c 2022-06-23 thomas view->count = 0;
3394 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3395 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3397 21355643 2020-12-06 stsp err = stop_log_thread(s);
3399 74cfe85e 2020-10-20 stsp return err;
3400 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3401 21355643 2020-12-06 stsp char *parent_path;
3402 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3404 21355643 2020-12-06 stsp return err;
3405 21355643 2020-12-06 stsp free(s->in_repo_path);
3406 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3407 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3408 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3409 21355643 2020-12-06 stsp struct got_object_id *start_id;
3410 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3411 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3412 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3414 21355643 2020-12-06 stsp return err;
3415 21355643 2020-12-06 stsp free(s->start_id);
3416 21355643 2020-12-06 stsp s->start_id = start_id;
3417 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3418 21355643 2020-12-06 stsp } else /* 'B' */
3419 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3421 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3422 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3423 bf7e79b3 2022-06-23 thomas if (err)
3424 bf7e79b3 2022-06-23 thomas return err;
3426 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3427 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3428 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3430 21355643 2020-12-06 stsp return err;
3431 51a10b52 2020-12-26 stsp tog_free_refs();
3432 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3434 ca51c541 2020-12-07 stsp return err;
3435 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3436 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3438 d01904d4 2019-06-25 stsp return err;
3439 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3440 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3442 b672a97a 2020-01-27 stsp return err;
3443 21355643 2020-12-06 stsp free_commits(&s->commits);
3444 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3445 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3446 21355643 2020-12-06 stsp s->selected_entry = NULL;
3447 21355643 2020-12-06 stsp s->selected = 0;
3448 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3449 21355643 2020-12-06 stsp s->quit = 0;
3450 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3451 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3452 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3453 94ecf40d 2022-07-22 thomas view->offset = 0;
3455 1be4947a 2022-07-22 thomas case 'R':
3456 07b0611c 2022-06-23 thomas view->count = 0;
3457 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3460 07b0611c 2022-06-23 thomas view->count = 0;
3464 80ddbec8 2018-04-29 stsp return err;
3467 4ed7e80c 2018-05-20 stsp static const struct got_error *
3468 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3470 c2db6724 2019-01-04 stsp const struct got_error *error;
3472 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3473 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3474 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3476 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3477 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3479 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3480 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3482 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3483 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3485 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3486 c2db6724 2019-01-04 stsp if (error != NULL)
3487 c2db6724 2019-01-04 stsp return error;
3489 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3490 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3492 c2db6724 2019-01-04 stsp return NULL;
3495 a915003a 2019-02-05 stsp static void
3496 a915003a 2019-02-05 stsp init_curses(void)
3499 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3500 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3501 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3503 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3504 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3505 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3506 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3507 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3509 a915003a 2019-02-05 stsp initscr();
3511 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3514 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3515 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3516 a915003a 2019-02-05 stsp curs_set(0);
3517 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3518 6d17833f 2019-11-08 stsp start_color();
3519 6d17833f 2019-11-08 stsp use_default_colors();
3523 c2db6724 2019-01-04 stsp static const struct got_error *
3524 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3525 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3527 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3529 f135c941 2020-02-20 stsp if (argc == 0) {
3530 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3531 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3532 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3533 f135c941 2020-02-20 stsp return NULL;
3536 f135c941 2020-02-20 stsp if (worktree) {
3537 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3540 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3542 f135c941 2020-02-20 stsp return err;
3543 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3544 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3545 bfd61697 2020-11-14 stsp p) == -1) {
3546 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3547 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3551 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3553 f135c941 2020-02-20 stsp return err;
3556 f135c941 2020-02-20 stsp static const struct got_error *
3557 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3559 80ddbec8 2018-04-29 stsp const struct got_error *error;
3560 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3561 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3562 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3563 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3564 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3565 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3566 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3567 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3568 04cc582a 2018-08-01 stsp struct tog_view *view;
3569 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3571 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3572 80ddbec8 2018-04-29 stsp switch (ch) {
3574 b672a97a 2020-01-27 stsp log_branches = 1;
3577 80ddbec8 2018-04-29 stsp start_commit = optarg;
3580 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3581 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3582 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3586 17020d27 2019-03-07 stsp usage_log();
3587 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3591 80ddbec8 2018-04-29 stsp argc -= optind;
3592 80ddbec8 2018-04-29 stsp argv += optind;
3594 f135c941 2020-02-20 stsp if (argc > 1)
3595 f135c941 2020-02-20 stsp usage_log();
3597 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3598 7cd52833 2022-06-23 thomas if (error != NULL)
3599 7cd52833 2022-06-23 thomas goto done;
3601 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3602 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3603 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3604 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3605 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3606 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3607 c156c7a4 2020-12-18 stsp goto done;
3608 a1fbf39a 2019-08-11 stsp if (worktree)
3609 6962eb72 2020-02-20 stsp repo_path =
3610 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3612 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3613 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3614 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3615 c156c7a4 2020-12-18 stsp goto done;
3619 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3620 80ddbec8 2018-04-29 stsp if (error != NULL)
3621 ecb28ae0 2018-07-16 stsp goto done;
3623 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3624 f135c941 2020-02-20 stsp repo, worktree);
3625 f135c941 2020-02-20 stsp if (error)
3626 f135c941 2020-02-20 stsp goto done;
3628 f135c941 2020-02-20 stsp init_curses();
3630 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3631 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3632 c02c541e 2019-03-29 stsp if (error)
3633 c02c541e 2019-03-29 stsp goto done;
3635 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3636 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3637 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3638 87670572 2020-12-26 naddy if (error)
3639 87670572 2020-12-26 naddy goto done;
3642 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3643 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3644 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3645 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3646 d8f38dc4 2020-12-05 stsp if (error)
3647 d8f38dc4 2020-12-05 stsp goto done;
3648 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3650 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3651 d8f38dc4 2020-12-05 stsp if (error == NULL)
3652 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3653 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3654 d8f38dc4 2020-12-05 stsp goto done;
3655 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3656 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3657 d8f38dc4 2020-12-05 stsp if (error)
3658 d8f38dc4 2020-12-05 stsp goto done;
3661 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3662 04cc582a 2018-08-01 stsp if (view == NULL) {
3663 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3664 04cc582a 2018-08-01 stsp goto done;
3666 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3667 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3668 ba4f502b 2018-08-04 stsp if (error)
3669 ba4f502b 2018-08-04 stsp goto done;
3670 2fc00ff4 2019-08-31 stsp if (worktree) {
3671 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3672 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3673 2fc00ff4 2019-08-31 stsp worktree = NULL;
3675 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3677 f135c941 2020-02-20 stsp free(in_repo_path);
3678 ecb28ae0 2018-07-16 stsp free(repo_path);
3679 ecb28ae0 2018-07-16 stsp free(cwd);
3680 899d86c2 2018-05-10 stsp free(start_id);
3681 d8f38dc4 2020-12-05 stsp free(label);
3683 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3684 1d0f4054 2021-06-17 stsp if (repo) {
3685 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3686 1d0f4054 2021-06-17 stsp if (error == NULL)
3687 1d0f4054 2021-06-17 stsp error = close_err;
3689 ec142235 2019-03-07 stsp if (worktree)
3690 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3691 7cd52833 2022-06-23 thomas if (pack_fds) {
3692 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3693 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3694 7cd52833 2022-06-23 thomas if (error == NULL)
3695 7cd52833 2022-06-23 thomas error = pack_err;
3697 51a10b52 2020-12-26 stsp tog_free_refs();
3698 80ddbec8 2018-04-29 stsp return error;
3701 4ed7e80c 2018-05-20 stsp __dead static void
3702 9f7d7167 2018-04-29 stsp usage_diff(void)
3705 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3706 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3710 6d17833f 2019-11-08 stsp static int
3711 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3712 41605754 2020-11-12 stsp regmatch_t *regmatch)
3714 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3717 ef20f542 2022-06-26 thomas static struct tog_color *
3718 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3720 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3722 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3723 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3724 6d17833f 2019-11-08 stsp return tc;
3727 6d17833f 2019-11-08 stsp return NULL;
3730 4ed7e80c 2018-05-20 stsp static const struct got_error *
3731 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3732 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3734 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3735 666f7b10 2022-06-23 thomas char *exstr = NULL;
3736 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3737 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3738 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3739 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3741 41605754 2020-11-12 stsp *wtotal = 0;
3743 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3744 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3746 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3747 666f7b10 2022-06-23 thomas if (err)
3748 666f7b10 2022-06-23 thomas return err;
3750 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3751 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3752 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3753 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3754 666f7b10 2022-06-23 thomas goto done;
3756 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3757 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3758 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3759 cbea3800 2022-06-23 thomas goto done;
3761 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3762 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3763 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3764 cbea3800 2022-06-23 thomas goto done;
3767 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3768 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3769 cbea3800 2022-06-23 thomas col_tab_align, 1);
3770 cbea3800 2022-06-23 thomas if (err)
3771 cbea3800 2022-06-23 thomas goto done;
3772 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3773 05171be4 2022-06-23 thomas if (n) {
3774 cbea3800 2022-06-23 thomas free(wline);
3775 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3776 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3777 cbea3800 2022-06-23 thomas if (err)
3778 cbea3800 2022-06-23 thomas goto done;
3779 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3780 cbea3800 2022-06-23 thomas wlimit -= width;
3781 cbea3800 2022-06-23 thomas *wtotal += width;
3784 41605754 2020-11-12 stsp if (wlimit > 0) {
3785 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3786 cbea3800 2022-06-23 thomas size_t wlen;
3788 cbea3800 2022-06-23 thomas free(wline);
3789 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3790 cbea3800 2022-06-23 thomas col_tab_align, 1);
3791 cbea3800 2022-06-23 thomas if (err)
3792 cbea3800 2022-06-23 thomas goto done;
3793 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3794 cbea3800 2022-06-23 thomas while (i < wlen) {
3795 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3796 cbea3800 2022-06-23 thomas if (width == -1) {
3797 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3798 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3799 cbea3800 2022-06-23 thomas goto done;
3801 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3803 1c5e5faa 2022-06-23 thomas w += width;
3806 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3807 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3808 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3809 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3810 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3811 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3812 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3816 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3817 cbea3800 2022-06-23 thomas free(wline);
3818 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3819 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3820 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3821 cbea3800 2022-06-23 thomas col_tab_align, 1);
3822 cbea3800 2022-06-23 thomas if (err)
3823 cbea3800 2022-06-23 thomas goto done;
3824 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3825 cbea3800 2022-06-23 thomas } else {
3826 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3827 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3828 cbea3800 2022-06-23 thomas if (err)
3829 cbea3800 2022-06-23 thomas goto done;
3830 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3832 cbea3800 2022-06-23 thomas *wtotal += width2;
3835 05171be4 2022-06-23 thomas free(wline);
3836 666f7b10 2022-06-23 thomas free(exstr);
3837 cbea3800 2022-06-23 thomas free(seg0);
3838 cbea3800 2022-06-23 thomas free(seg1);
3839 cbea3800 2022-06-23 thomas free(seg2);
3840 cbea3800 2022-06-23 thomas return err;
3843 41605754 2020-11-12 stsp static const struct got_error *
3844 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3846 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3847 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3848 61e69b96 2018-05-20 stsp const struct got_error *err;
3849 fe621944 2020-11-10 stsp int nprinted = 0;
3850 b304db33 2018-05-20 stsp char *line;
3851 826082fe 2020-12-10 stsp size_t linesize = 0;
3852 826082fe 2020-12-10 stsp ssize_t linelen;
3853 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3854 61e69b96 2018-05-20 stsp wchar_t *wline;
3855 e0b650dd 2018-05-20 stsp int width;
3856 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3857 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3858 fe621944 2020-11-10 stsp off_t line_offset;
3860 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3861 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3862 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3864 f7d12f7e 2018-08-01 stsp werase(view->window);
3866 a3404814 2018-09-02 stsp if (header) {
3867 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3868 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3869 135a2da0 2020-11-11 stsp header) == -1)
3870 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3871 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3873 135a2da0 2020-11-11 stsp free(line);
3875 a3404814 2018-09-02 stsp return err;
3877 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3878 a3404814 2018-09-02 stsp wstandout(view->window);
3879 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3880 e54cc94a 2020-11-11 stsp free(wline);
3881 e54cc94a 2020-11-11 stsp wline = NULL;
3882 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3883 a3404814 2018-09-02 stsp wstandend(view->window);
3884 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3885 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3887 a3404814 2018-09-02 stsp if (max_lines <= 1)
3888 a3404814 2018-09-02 stsp return NULL;
3889 a3404814 2018-09-02 stsp max_lines--;
3892 89f1a395 2020-12-01 naddy s->eof = 0;
3893 05171be4 2022-06-23 thomas view->maxx = 0;
3894 826082fe 2020-12-10 stsp line = NULL;
3895 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3896 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3897 826082fe 2020-12-10 stsp if (linelen == -1) {
3898 826082fe 2020-12-10 stsp if (feof(s->f)) {
3899 826082fe 2020-12-10 stsp s->eof = 1;
3902 826082fe 2020-12-10 stsp free(line);
3903 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3906 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3907 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3908 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3909 cbea3800 2022-06-23 thomas if (err) {
3910 cbea3800 2022-06-23 thomas free(line);
3911 cbea3800 2022-06-23 thomas return err;
3913 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3914 cbea3800 2022-06-23 thomas free(wline);
3915 cbea3800 2022-06-23 thomas wline = NULL;
3917 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3919 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3920 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3921 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3922 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3923 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3924 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3925 41605754 2020-11-12 stsp if (err) {
3926 41605754 2020-11-12 stsp free(line);
3927 41605754 2020-11-12 stsp return err;
3930 f1c0ec19 2022-06-23 thomas int skip;
3931 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3932 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3933 f1c0ec19 2022-06-23 thomas if (err) {
3934 f1c0ec19 2022-06-23 thomas free(line);
3935 f1c0ec19 2022-06-23 thomas return err;
3937 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3938 f1c0ec19 2022-06-23 thomas free(wline);
3939 41605754 2020-11-12 stsp wline = NULL;
3942 6d17833f 2019-11-08 stsp wattr_off(view->window,
3943 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3944 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3945 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3946 fe621944 2020-11-10 stsp nprinted++;
3948 826082fe 2020-12-10 stsp free(line);
3949 fe621944 2020-11-10 stsp if (nprinted >= 1)
3950 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3951 89f1a395 2020-12-01 naddy (nprinted - 1);
3953 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3955 a5d43cac 2022-07-01 thomas view_border(view);
3957 89f1a395 2020-12-01 naddy if (s->eof) {
3958 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3959 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3960 c3e9aa98 2019-05-13 jcs nprinted++;
3963 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3964 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3966 c3e9aa98 2019-05-13 jcs return err;
3969 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3970 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3971 e54cc94a 2020-11-11 stsp free(wline);
3972 e54cc94a 2020-11-11 stsp wline = NULL;
3973 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3976 26ed57b2 2018-05-19 stsp return NULL;
3979 abd2672a 2018-12-23 stsp static char *
3980 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3982 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3983 09867e48 2019-08-13 stsp char *p, *s;
3985 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3986 09867e48 2019-08-13 stsp if (tm == NULL)
3987 09867e48 2019-08-13 stsp return NULL;
3988 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3989 09867e48 2019-08-13 stsp if (s == NULL)
3990 09867e48 2019-08-13 stsp return NULL;
3991 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3993 abd2672a 2018-12-23 stsp *p = '\0';
3997 4ed7e80c 2018-05-20 stsp static const struct got_error *
3998 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3999 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4001 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4002 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4003 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4004 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4006 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4007 0208f208 2020-05-05 stsp if (qid != NULL) {
4008 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4009 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4010 ec242592 2022-04-22 thomas &qid->id);
4012 0208f208 2020-05-05 stsp return err;
4014 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4015 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4016 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4017 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4018 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4020 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4024 0208f208 2020-05-05 stsp if (tree_id1) {
4025 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4027 0208f208 2020-05-05 stsp goto done;
4030 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4031 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4033 0208f208 2020-05-05 stsp goto done;
4035 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4036 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4038 0208f208 2020-05-05 stsp if (tree1)
4039 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4040 0208f208 2020-05-05 stsp if (tree2)
4041 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4042 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4043 0208f208 2020-05-05 stsp return err;
4046 0208f208 2020-05-05 stsp static const struct got_error *
4047 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4051 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4052 fe621944 2020-11-10 stsp if (p == NULL)
4053 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4054 fe621944 2020-11-10 stsp *line_offsets = p;
4055 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4056 fe621944 2020-11-10 stsp (*nlines)++;
4057 fe621944 2020-11-10 stsp return NULL;
4060 fe621944 2020-11-10 stsp static const struct got_error *
4061 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4062 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4063 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4065 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4066 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4067 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4068 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4069 45d799e2 2018-12-23 stsp time_t committer_time;
4070 45d799e2 2018-12-23 stsp const char *author, *committer;
4071 8b473291 2019-02-21 stsp char *refs_str = NULL;
4072 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4073 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4074 fe621944 2020-11-10 stsp off_t outoff = 0;
4077 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4079 8b473291 2019-02-21 stsp if (refs) {
4080 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4082 8b473291 2019-02-21 stsp return err;
4085 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4087 abd2672a 2018-12-23 stsp return err;
4089 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4090 15a94983 2018-12-23 stsp if (err) {
4091 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4092 15a94983 2018-12-23 stsp goto done;
4095 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4097 fe621944 2020-11-10 stsp goto done;
4099 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4100 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4101 fe621944 2020-11-10 stsp if (n < 0) {
4102 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4103 abd2672a 2018-12-23 stsp goto done;
4105 fe621944 2020-11-10 stsp outoff += n;
4106 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4108 fe621944 2020-11-10 stsp goto done;
4110 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4111 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4112 fe621944 2020-11-10 stsp if (n < 0) {
4113 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4114 abd2672a 2018-12-23 stsp goto done;
4116 fe621944 2020-11-10 stsp outoff += n;
4117 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4119 fe621944 2020-11-10 stsp goto done;
4121 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4122 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4123 fe621944 2020-11-10 stsp if (datestr) {
4124 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4125 fe621944 2020-11-10 stsp if (n < 0) {
4126 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4127 fe621944 2020-11-10 stsp goto done;
4129 fe621944 2020-11-10 stsp outoff += n;
4130 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4132 fe621944 2020-11-10 stsp goto done;
4134 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4135 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4136 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4137 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4138 fe621944 2020-11-10 stsp if (n < 0) {
4139 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4140 fe621944 2020-11-10 stsp goto done;
4142 fe621944 2020-11-10 stsp outoff += n;
4143 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4145 fe621944 2020-11-10 stsp goto done;
4147 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4148 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4149 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4150 ac4dc263 2021-09-24 thomas int pn = 1;
4151 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4152 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4153 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4154 ac4dc263 2021-09-24 thomas if (err)
4155 ac4dc263 2021-09-24 thomas goto done;
4156 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4157 ac4dc263 2021-09-24 thomas if (n < 0) {
4158 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4159 ac4dc263 2021-09-24 thomas goto done;
4161 ac4dc263 2021-09-24 thomas outoff += n;
4162 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4163 ac4dc263 2021-09-24 thomas if (err)
4164 ac4dc263 2021-09-24 thomas goto done;
4165 ac4dc263 2021-09-24 thomas free(id_str);
4166 ac4dc263 2021-09-24 thomas id_str = NULL;
4170 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4172 5943eee2 2019-08-13 stsp goto done;
4173 fe621944 2020-11-10 stsp s = logmsg;
4174 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4175 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4176 fe621944 2020-11-10 stsp if (n < 0) {
4177 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4178 fe621944 2020-11-10 stsp goto done;
4180 fe621944 2020-11-10 stsp outoff += n;
4181 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4183 fe621944 2020-11-10 stsp goto done;
4186 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4188 0208f208 2020-05-05 stsp goto done;
4189 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4190 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4191 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4192 fe621944 2020-11-10 stsp if (n < 0) {
4193 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4194 fe621944 2020-11-10 stsp goto done;
4196 fe621944 2020-11-10 stsp outoff += n;
4197 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4199 fe621944 2020-11-10 stsp goto done;
4200 0208f208 2020-05-05 stsp free((char *)pe->path);
4201 0208f208 2020-05-05 stsp free(pe->data);
4204 0208f208 2020-05-05 stsp fputc('\n', outfile);
4206 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4208 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4209 abd2672a 2018-12-23 stsp free(id_str);
4210 5943eee2 2019-08-13 stsp free(logmsg);
4211 8b473291 2019-02-21 stsp free(refs_str);
4212 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4213 7510f233 2020-08-09 stsp if (err) {
4214 ae6a6978 2020-08-09 stsp free(*line_offsets);
4215 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4216 ae6a6978 2020-08-09 stsp *nlines = 0;
4218 fe621944 2020-11-10 stsp return err;
4221 abd2672a 2018-12-23 stsp static const struct got_error *
4222 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4224 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4225 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4226 15a94983 2018-12-23 stsp int obj_type;
4228 fe621944 2020-11-10 stsp free(s->line_offsets);
4229 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4230 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4231 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4232 fe621944 2020-11-10 stsp s->nlines = 0;
4234 511a516b 2018-05-19 stsp f = got_opentemp();
4235 48ae06ee 2018-10-18 stsp if (f == NULL) {
4236 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4237 48ae06ee 2018-10-18 stsp goto done;
4239 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4240 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4241 fb43ecf1 2019-02-11 stsp goto done;
4245 15a94983 2018-12-23 stsp if (s->id1)
4246 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4248 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4250 15a94983 2018-12-23 stsp goto done;
4252 15a94983 2018-12-23 stsp switch (obj_type) {
4253 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4254 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4255 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4256 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4257 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4259 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4260 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4261 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4262 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4263 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4265 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4266 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4267 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4268 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4269 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4271 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4273 3ffacbe1 2020-02-02 tracey goto done;
4274 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4275 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4276 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4277 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4278 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4279 f44b1f58 2020-02-02 tracey if (err)
4280 f44b1f58 2020-02-02 tracey goto done;
4281 f44b1f58 2020-02-02 tracey } else {
4282 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4283 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4284 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4285 fe621944 2020-11-10 stsp err = write_commit_info(
4286 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4287 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4288 f44b1f58 2020-02-02 tracey if (err)
4289 f44b1f58 2020-02-02 tracey goto done;
4294 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4296 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4297 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4298 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4299 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4303 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4306 f44b1f58 2020-02-02 tracey if (err)
4307 f44b1f58 2020-02-02 tracey goto done;
4309 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4310 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4311 48ae06ee 2018-10-18 stsp return err;
4314 f5215bb9 2019-02-22 stsp static void
4315 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4317 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4318 f5215bb9 2019-02-22 stsp update_panels();
4319 f5215bb9 2019-02-22 stsp doupdate();
4322 f44b1f58 2020-02-02 tracey static const struct got_error *
4323 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4325 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4327 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4328 f44b1f58 2020-02-02 tracey return NULL;
4331 f44b1f58 2020-02-02 tracey static const struct got_error *
4332 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4334 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4335 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4336 f44b1f58 2020-02-02 tracey int lineno;
4337 78eecffc 2022-06-23 thomas char *line = NULL;
4338 826082fe 2020-12-10 stsp size_t linesize = 0;
4339 826082fe 2020-12-10 stsp ssize_t linelen;
4341 f44b1f58 2020-02-02 tracey if (!view->searching) {
4342 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4343 f44b1f58 2020-02-02 tracey return NULL;
4346 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4347 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4348 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4350 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4352 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4354 f44b1f58 2020-02-02 tracey while (1) {
4355 f44b1f58 2020-02-02 tracey off_t offset;
4357 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4358 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4359 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4363 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4364 f44b1f58 2020-02-02 tracey lineno = 1;
4366 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4369 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4370 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4371 f44b1f58 2020-02-02 tracey free(line);
4372 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4374 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4375 78eecffc 2022-06-23 thomas if (linelen != -1) {
4376 78eecffc 2022-06-23 thomas char *exstr;
4377 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4378 78eecffc 2022-06-23 thomas if (err)
4380 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4381 78eecffc 2022-06-23 thomas &view->regmatch)) {
4382 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4384 78eecffc 2022-06-23 thomas free(exstr);
4387 78eecffc 2022-06-23 thomas free(exstr);
4389 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4390 f44b1f58 2020-02-02 tracey lineno++;
4392 f44b1f58 2020-02-02 tracey lineno--;
4394 826082fe 2020-12-10 stsp free(line);
4396 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4397 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4398 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4401 05171be4 2022-06-23 thomas return err;
4404 48ae06ee 2018-10-18 stsp static const struct got_error *
4405 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4407 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4408 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4410 a0f32f33 2022-06-13 thomas free(s->id1);
4411 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4412 a0f32f33 2022-06-13 thomas free(s->id2);
4413 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4414 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4415 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4416 a0f32f33 2022-06-13 thomas s->f = NULL;
4417 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4418 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4419 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4420 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4421 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4422 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4423 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4424 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4425 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4426 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4427 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4428 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4429 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4430 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4431 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4432 a0f32f33 2022-06-13 thomas s->nlines = 0;
4433 a0f32f33 2022-06-13 thomas return err;
4436 a0f32f33 2022-06-13 thomas static const struct got_error *
4437 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4438 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4439 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4440 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4442 48ae06ee 2018-10-18 stsp const struct got_error *err;
4443 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4445 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4446 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4447 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4449 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4450 15a94983 2018-12-23 stsp int type1, type2;
4451 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4453 15a94983 2018-12-23 stsp return err;
4454 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4456 15a94983 2018-12-23 stsp return err;
4458 15a94983 2018-12-23 stsp if (type1 != type2)
4459 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4461 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4462 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4463 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4464 f44b1f58 2020-02-02 tracey s->repo = repo;
4465 f44b1f58 2020-02-02 tracey s->id1 = id1;
4466 f44b1f58 2020-02-02 tracey s->id2 = id2;
4467 3dbaef42 2020-11-24 stsp s->label1 = label1;
4468 3dbaef42 2020-11-24 stsp s->label2 = label2;
4470 15a94983 2018-12-23 stsp if (id1) {
4471 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4472 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4473 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4475 5465d566 2020-02-01 tracey s->id1 = NULL;
4477 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4478 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4479 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4480 a0f32f33 2022-06-13 thomas goto done;
4483 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4484 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4485 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4486 9a1d7435 2022-06-23 thomas goto done;
4489 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4490 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4491 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4492 a0f32f33 2022-06-13 thomas goto done;
4495 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4496 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4497 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4498 19a6a6b5 2022-07-01 thomas goto done;
4501 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4502 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4503 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4504 19a6a6b5 2022-07-01 thomas goto done;
4507 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4508 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4509 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4510 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4511 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4512 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4513 5465d566 2020-02-01 tracey s->repo = repo;
4515 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4516 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4517 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4518 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4519 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4521 a0f32f33 2022-06-13 thomas goto done;
4522 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4523 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4524 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4525 a0f32f33 2022-06-13 thomas if (err)
4526 a0f32f33 2022-06-13 thomas goto done;
4527 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4528 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4529 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4530 a0f32f33 2022-06-13 thomas if (err)
4531 a0f32f33 2022-06-13 thomas goto done;
4533 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4534 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4535 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4536 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4537 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4538 a0f32f33 2022-06-13 thomas if (err)
4539 a0f32f33 2022-06-13 thomas goto done;
4541 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4542 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4543 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4544 a0f32f33 2022-06-13 thomas if (err)
4545 a0f32f33 2022-06-13 thomas goto done;
4547 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4548 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4549 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4550 a0f32f33 2022-06-13 thomas if (err)
4551 a0f32f33 2022-06-13 thomas goto done;
4554 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4555 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4556 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4557 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4559 5465d566 2020-02-01 tracey err = create_diff(s);
4561 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4562 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4563 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4564 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4565 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4566 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4568 a0f32f33 2022-06-13 thomas if (err)
4569 a0f32f33 2022-06-13 thomas close_diff_view(view);
4570 e5a0f69f 2018-08-18 stsp return err;
4573 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4574 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4576 a3404814 2018-09-02 stsp const struct got_error *err;
4577 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4578 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4579 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4581 a3404814 2018-09-02 stsp if (s->id1) {
4582 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4584 a3404814 2018-09-02 stsp return err;
4585 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4587 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4589 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4591 a3404814 2018-09-02 stsp return err;
4592 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4594 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4595 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4596 a3404814 2018-09-02 stsp free(id_str1);
4597 a3404814 2018-09-02 stsp free(id_str2);
4598 a3404814 2018-09-02 stsp return err;
4600 a3404814 2018-09-02 stsp free(id_str1);
4601 a3404814 2018-09-02 stsp free(id_str2);
4603 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4604 267bb3b8 2021-08-01 stsp free(header);
4605 267bb3b8 2021-08-01 stsp return err;
4608 15a087fe 2019-02-21 stsp static const struct got_error *
4609 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4610 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4612 d7a04538 2019-02-21 stsp const struct got_error *err;
4613 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4614 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4615 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4617 15a087fe 2019-02-21 stsp free(s->id2);
4618 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4619 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4620 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4622 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4624 d7a04538 2019-02-21 stsp return err;
4625 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4626 15a087fe 2019-02-21 stsp free(s->id1);
4627 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4628 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4629 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4630 15a087fe 2019-02-21 stsp return NULL;
4633 0cf4efb1 2018-09-29 stsp static const struct got_error *
4634 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4636 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4638 adf4c9e0 2022-07-03 thomas view->count = 0;
4639 adf4c9e0 2022-07-03 thomas wclear(view->window);
4640 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4641 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4642 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4643 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4644 adf4c9e0 2022-07-03 thomas return create_diff(s);
4647 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4648 4fc71f3b 2022-07-12 thomas int, int, int);
4649 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4650 4fc71f3b 2022-07-12 thomas int, int);
4652 adf4c9e0 2022-07-03 thomas static const struct got_error *
4653 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4655 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4656 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4657 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4658 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4659 826082fe 2020-12-10 stsp char *line = NULL;
4660 826082fe 2020-12-10 stsp size_t linesize = 0;
4661 826082fe 2020-12-10 stsp ssize_t linelen;
4662 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4664 e5a0f69f 2018-08-18 stsp switch (ch) {
4665 05171be4 2022-06-23 thomas case '0':
4666 05171be4 2022-06-23 thomas view->x = 0;
4668 05171be4 2022-06-23 thomas case '$':
4669 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4670 07b0611c 2022-06-23 thomas view->count = 0;
4672 05171be4 2022-06-23 thomas case KEY_RIGHT:
4673 05171be4 2022-06-23 thomas case 'l':
4674 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4675 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4677 07b0611c 2022-06-23 thomas view->count = 0;
4679 05171be4 2022-06-23 thomas case KEY_LEFT:
4680 05171be4 2022-06-23 thomas case 'h':
4681 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4682 07b0611c 2022-06-23 thomas if (view->x <= 0)
4683 07b0611c 2022-06-23 thomas view->count = 0;
4687 3dbaef42 2020-11-24 stsp if (ch == 'a')
4688 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4689 3dbaef42 2020-11-24 stsp if (ch == 'w')
4690 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4691 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4694 912a3f79 2021-08-30 j case KEY_HOME:
4695 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4696 07b0611c 2022-06-23 thomas view->count = 0;
4699 912a3f79 2021-08-30 j case KEY_END:
4700 07b0611c 2022-06-23 thomas view->count = 0;
4704 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4708 1e37a5c2 2019-05-12 jcs case KEY_UP:
4709 f7140bf5 2021-10-17 thomas case CTRL('p'):
4710 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4711 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4713 07b0611c 2022-06-23 thomas view->count = 0;
4715 70f17a53 2022-06-13 thomas case CTRL('u'):
4716 23427b14 2022-06-23 thomas case 'u':
4717 70f17a53 2022-06-13 thomas nscroll /= 2;
4718 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4719 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4720 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4721 1c5e5faa 2022-06-23 thomas case 'b':
4722 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4723 07b0611c 2022-06-23 thomas view->count = 0;
4727 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4728 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4731 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4732 f7140bf5 2021-10-17 thomas case CTRL('n'):
4733 1e37a5c2 2019-05-12 jcs if (!s->eof)
4734 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4736 07b0611c 2022-06-23 thomas view->count = 0;
4738 70f17a53 2022-06-13 thomas case CTRL('d'):
4739 23427b14 2022-06-23 thomas case 'd':
4740 70f17a53 2022-06-13 thomas nscroll /= 2;
4741 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4742 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4743 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4744 1c5e5faa 2022-06-23 thomas case 'f':
4746 07b0611c 2022-06-23 thomas if (s->eof) {
4747 07b0611c 2022-06-23 thomas view->count = 0;
4751 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4752 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4753 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4754 826082fe 2020-12-10 stsp if (linelen == -1) {
4755 826082fe 2020-12-10 stsp if (feof(s->f)) {
4756 826082fe 2020-12-10 stsp s->eof = 1;
4758 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4762 826082fe 2020-12-10 stsp free(line);
4765 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4766 1e37a5c2 2019-05-12 jcs s->diff_context--;
4767 aa61903a 2021-12-31 thomas s->matched_line = 0;
4768 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4769 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4770 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4771 27829c9e 2020-11-21 stsp s->nlines) {
4772 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4773 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4776 07b0611c 2022-06-23 thomas view->count = 0;
4779 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4780 1e37a5c2 2019-05-12 jcs s->diff_context++;
4781 aa61903a 2021-12-31 thomas s->matched_line = 0;
4782 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4783 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4785 07b0611c 2022-06-23 thomas view->count = 0;
4789 777aae21 2022-07-20 thomas case 'K':
4791 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4792 4fc71f3b 2022-07-12 thomas case '>':
4793 4fc71f3b 2022-07-12 thomas case '.':
4794 777aae21 2022-07-20 thomas case 'J':
4795 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4796 07b0611c 2022-06-23 thomas view->count = 0;
4799 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4801 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4802 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4803 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4805 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4806 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4807 4fc71f3b 2022-07-12 thomas if (err)
4809 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4811 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4814 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4815 4fc71f3b 2022-07-12 thomas if (err)
4817 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4818 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4819 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4821 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4822 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4823 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4825 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4826 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4827 4fc71f3b 2022-07-12 thomas if (err)
4829 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4831 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4833 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4834 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4835 4fc71f3b 2022-07-12 thomas bs->selected_line);
4836 4fc71f3b 2022-07-12 thomas if (id == NULL)
4839 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4842 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4843 4fc71f3b 2022-07-12 thomas if (err)
4846 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4847 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4848 aa61903a 2021-12-31 thomas s->matched_line = 0;
4849 05171be4 2022-06-23 thomas view->x = 0;
4851 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4852 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4855 07b0611c 2022-06-23 thomas view->count = 0;
4859 bcbd79e2 2018-08-19 stsp return err;
4862 4ed7e80c 2018-05-20 stsp static const struct got_error *
4863 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4865 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4866 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4867 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4868 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4869 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4870 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4871 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4872 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4873 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4874 3dbaef42 2020-11-24 stsp const char *errstr;
4875 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4876 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4878 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4879 26ed57b2 2018-05-19 stsp switch (ch) {
4881 64453f7e 2020-11-21 stsp force_text_diff = 1;
4884 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4886 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4887 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4888 8f666e67 2022-02-12 thomas errstr, errstr);
4890 09b5bff8 2020-02-23 naddy case 'r':
4891 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4892 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4893 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4895 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4898 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4901 17020d27 2019-03-07 stsp usage_diff();
4902 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4906 26ed57b2 2018-05-19 stsp argc -= optind;
4907 26ed57b2 2018-05-19 stsp argv += optind;
4909 26ed57b2 2018-05-19 stsp if (argc == 0) {
4910 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4911 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4912 15a94983 2018-12-23 stsp id_str1 = argv[0];
4913 15a94983 2018-12-23 stsp id_str2 = argv[1];
4915 26ed57b2 2018-05-19 stsp usage_diff();
4917 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4918 7cd52833 2022-06-23 thomas if (error)
4919 7cd52833 2022-06-23 thomas goto done;
4921 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4922 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4923 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4924 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4925 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4926 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4927 c156c7a4 2020-12-18 stsp goto done;
4928 a273ac94 2020-02-23 naddy if (worktree)
4929 a273ac94 2020-02-23 naddy repo_path =
4930 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4932 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4933 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4934 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4935 c156c7a4 2020-12-18 stsp goto done;
4939 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4940 eb6600df 2019-01-04 stsp if (error)
4941 eb6600df 2019-01-04 stsp goto done;
4943 a273ac94 2020-02-23 naddy init_curses();
4945 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4946 51a10b52 2020-12-26 stsp if (error)
4947 51a10b52 2020-12-26 stsp goto done;
4949 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4950 26ed57b2 2018-05-19 stsp if (error)
4951 26ed57b2 2018-05-19 stsp goto done;
4953 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4954 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4955 26ed57b2 2018-05-19 stsp if (error)
4956 26ed57b2 2018-05-19 stsp goto done;
4958 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4959 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4960 26ed57b2 2018-05-19 stsp if (error)
4961 26ed57b2 2018-05-19 stsp goto done;
4963 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4964 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4965 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4966 ea5e7bb5 2018-08-01 stsp goto done;
4968 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4969 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4970 5dc9f4bc 2018-08-04 stsp if (error)
4971 5dc9f4bc 2018-08-04 stsp goto done;
4972 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4974 3dbaef42 2020-11-24 stsp free(label1);
4975 3dbaef42 2020-11-24 stsp free(label2);
4976 c02c541e 2019-03-29 stsp free(repo_path);
4977 a273ac94 2020-02-23 naddy free(cwd);
4978 1d0f4054 2021-06-17 stsp if (repo) {
4979 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4980 1d0f4054 2021-06-17 stsp if (error == NULL)
4981 1d0f4054 2021-06-17 stsp error = close_err;
4983 a273ac94 2020-02-23 naddy if (worktree)
4984 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4985 7cd52833 2022-06-23 thomas if (pack_fds) {
4986 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4987 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4988 7cd52833 2022-06-23 thomas if (error == NULL)
4989 7cd52833 2022-06-23 thomas error = pack_err;
4991 51a10b52 2020-12-26 stsp tog_free_refs();
4992 26ed57b2 2018-05-19 stsp return error;
4995 4ed7e80c 2018-05-20 stsp __dead static void
4996 9f7d7167 2018-04-29 stsp usage_blame(void)
4999 91198554 2022-06-23 thomas fprintf(stderr,
5000 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5001 9f7d7167 2018-04-29 stsp getprogname());
5005 84451b3e 2018-07-10 stsp struct tog_blame_line {
5006 84451b3e 2018-07-10 stsp int annotated;
5007 84451b3e 2018-07-10 stsp struct got_object_id *id;
5010 4ed7e80c 2018-05-20 stsp static const struct got_error *
5011 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5013 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5014 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5015 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5016 84451b3e 2018-07-10 stsp const struct got_error *err;
5017 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5018 826082fe 2020-12-10 stsp char *line = NULL;
5019 826082fe 2020-12-10 stsp size_t linesize = 0;
5020 826082fe 2020-12-10 stsp ssize_t linelen;
5021 84451b3e 2018-07-10 stsp wchar_t *wline;
5022 27a741e5 2019-09-11 stsp int width;
5023 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5024 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5025 ab089a2a 2018-07-12 stsp char *id_str;
5026 11b20872 2019-11-08 stsp struct tog_color *tc;
5028 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5030 ab089a2a 2018-07-12 stsp return err;
5032 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5033 f7d12f7e 2018-08-01 stsp werase(view->window);
5035 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5036 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5037 ab089a2a 2018-07-12 stsp free(id_str);
5038 ab089a2a 2018-07-12 stsp return err;
5041 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5042 ab089a2a 2018-07-12 stsp free(line);
5043 2550e4c3 2018-07-13 stsp line = NULL;
5045 1cae65b4 2019-09-22 stsp return err;
5046 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5047 a3404814 2018-09-02 stsp wstandout(view->window);
5048 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5050 11b20872 2019-11-08 stsp wattr_on(view->window,
5051 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5052 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5054 11b20872 2019-11-08 stsp wattr_off(view->window,
5055 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5056 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5057 a3404814 2018-09-02 stsp wstandend(view->window);
5058 2550e4c3 2018-07-13 stsp free(wline);
5059 2550e4c3 2018-07-13 stsp wline = NULL;
5060 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5061 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5063 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5064 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5065 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5066 ab089a2a 2018-07-12 stsp free(id_str);
5067 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5069 ab089a2a 2018-07-12 stsp free(id_str);
5070 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5071 3f60a8ef 2018-07-10 stsp free(line);
5072 2550e4c3 2018-07-13 stsp line = NULL;
5074 3f60a8ef 2018-07-10 stsp return err;
5075 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5076 2550e4c3 2018-07-13 stsp free(wline);
5077 2550e4c3 2018-07-13 stsp wline = NULL;
5078 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5079 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5081 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5082 05171be4 2022-06-23 thomas view->maxx = 0;
5083 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5084 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5085 826082fe 2020-12-10 stsp if (linelen == -1) {
5086 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5087 826082fe 2020-12-10 stsp s->eof = 1;
5090 84451b3e 2018-07-10 stsp free(line);
5091 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5093 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5096 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5097 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5098 cbea3800 2022-06-23 thomas if (err) {
5099 cbea3800 2022-06-23 thomas free(line);
5100 cbea3800 2022-06-23 thomas return err;
5102 cbea3800 2022-06-23 thomas free(wline);
5103 cbea3800 2022-06-23 thomas wline = NULL;
5104 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5106 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5107 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5109 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5110 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5111 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5112 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5113 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5114 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5115 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5116 8d0fe45a 2019-08-12 stsp char *id_str;
5117 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5118 91198554 2022-06-23 thomas blame_line->id);
5119 8d0fe45a 2019-08-12 stsp if (err) {
5120 8d0fe45a 2019-08-12 stsp free(line);
5121 8d0fe45a 2019-08-12 stsp return err;
5123 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5125 11b20872 2019-11-08 stsp wattr_on(view->window,
5126 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5127 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5129 11b20872 2019-11-08 stsp wattr_off(view->window,
5130 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5131 8d0fe45a 2019-08-12 stsp free(id_str);
5132 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5134 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5135 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5138 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5139 ee41ec32 2018-07-10 stsp prev_id = NULL;
5142 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5143 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5144 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5146 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5147 41605754 2020-11-12 stsp width = 9;
5148 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5149 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5150 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5151 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5152 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5153 41605754 2020-11-12 stsp if (err) {
5154 41605754 2020-11-12 stsp free(line);
5155 41605754 2020-11-12 stsp return err;
5157 41605754 2020-11-12 stsp width += 9;
5159 923086fd 2022-06-23 thomas int skip;
5160 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5161 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5162 923086fd 2022-06-23 thomas if (err) {
5163 923086fd 2022-06-23 thomas free(line);
5164 923086fd 2022-06-23 thomas return err;
5166 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5167 02bce7e2 2022-06-23 thomas width += 9;
5168 41605754 2020-11-12 stsp free(wline);
5169 41605754 2020-11-12 stsp wline = NULL;
5172 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5173 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5174 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5175 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5177 826082fe 2020-12-10 stsp free(line);
5178 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5180 a5d43cac 2022-07-01 thomas view_border(view);
5182 84451b3e 2018-07-10 stsp return NULL;
5185 84451b3e 2018-07-10 stsp static const struct got_error *
5186 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5187 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5189 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5190 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5191 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5192 1a76625f 2018-10-22 stsp int errcode;
5194 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5195 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5196 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5198 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5199 1a76625f 2018-10-22 stsp if (errcode)
5200 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5202 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5203 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5204 d68a0a7d 2018-07-10 stsp goto done;
5207 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5208 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5210 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5211 d68a0a7d 2018-07-10 stsp if (line->annotated)
5212 d68a0a7d 2018-07-10 stsp goto done;
5214 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5215 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5216 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5217 84451b3e 2018-07-10 stsp goto done;
5219 84451b3e 2018-07-10 stsp line->annotated = 1;
5221 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5222 1a76625f 2018-10-22 stsp if (errcode)
5223 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5224 84451b3e 2018-07-10 stsp return err;
5227 84451b3e 2018-07-10 stsp static void *
5228 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5230 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5231 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5232 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5233 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5234 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5236 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5237 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5238 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5240 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5241 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5242 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5243 9117a7b7 2022-07-01 thomas goto done;
5246 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5247 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5248 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5249 9117a7b7 2022-07-01 thomas goto done;
5251 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5252 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5253 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5254 9117a7b7 2022-07-01 thomas goto done;
5257 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5259 9117a7b7 2022-07-01 thomas goto done;
5261 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5262 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5263 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5264 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5265 fc06ba56 2019-08-22 stsp err = NULL;
5267 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5268 9117a7b7 2022-07-01 thomas if (errcode) {
5269 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5270 9117a7b7 2022-07-01 thomas goto done;
5273 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5274 1d0f4054 2021-06-17 stsp if (err == NULL)
5275 1d0f4054 2021-06-17 stsp err = close_err;
5276 c9beca56 2018-07-22 stsp ta->repo = NULL;
5277 c9beca56 2018-07-22 stsp *ta->complete = 1;
5279 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5280 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5281 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5284 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5285 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5286 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5287 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5288 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5289 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5290 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5291 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5293 18430de3 2018-07-10 stsp return (void *)err;
5296 245d91c1 2018-07-12 stsp static struct got_object_id *
5297 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5298 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5300 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5302 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5303 8d0fe45a 2019-08-12 stsp return NULL;
5305 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5306 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5307 4fc71f3b 2022-07-12 thomas return NULL;
5309 4fc71f3b 2022-07-12 thomas return line->id;
5312 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5313 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5314 4fc71f3b 2022-07-12 thomas int lineno)
5316 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5318 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5319 4fc71f3b 2022-07-12 thomas return NULL;
5321 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5322 245d91c1 2018-07-12 stsp if (!line->annotated)
5323 245d91c1 2018-07-12 stsp return NULL;
5325 245d91c1 2018-07-12 stsp return line->id;
5328 b880a918 2018-07-10 stsp static const struct got_error *
5329 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5331 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5334 245d91c1 2018-07-12 stsp if (blame->thread) {
5335 1a76625f 2018-10-22 stsp int errcode;
5336 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5337 1a76625f 2018-10-22 stsp if (errcode)
5338 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5339 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5340 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5341 1a76625f 2018-10-22 stsp if (errcode)
5342 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5343 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5344 1a76625f 2018-10-22 stsp if (errcode)
5345 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5346 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5347 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5348 245d91c1 2018-07-12 stsp err = NULL;
5349 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5351 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5352 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5353 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5354 1d0f4054 2021-06-17 stsp if (err == NULL)
5355 1d0f4054 2021-06-17 stsp err = close_err;
5356 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5358 245d91c1 2018-07-12 stsp if (blame->f) {
5359 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5360 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5361 245d91c1 2018-07-12 stsp blame->f = NULL;
5363 57670559 2018-12-23 stsp if (blame->lines) {
5364 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5365 57670559 2018-12-23 stsp free(blame->lines[i].id);
5366 57670559 2018-12-23 stsp free(blame->lines);
5367 57670559 2018-12-23 stsp blame->lines = NULL;
5369 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5370 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5371 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5372 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5373 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5374 7cd52833 2022-06-23 thomas if (err == NULL)
5375 7cd52833 2022-06-23 thomas err = pack_err;
5376 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5378 245d91c1 2018-07-12 stsp return err;
5381 245d91c1 2018-07-12 stsp static const struct got_error *
5382 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5384 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5385 fc06ba56 2019-08-22 stsp int *done = arg;
5386 fc06ba56 2019-08-22 stsp int errcode;
5388 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5389 fc06ba56 2019-08-22 stsp if (errcode)
5390 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5391 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5393 fc06ba56 2019-08-22 stsp if (*done)
5394 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5396 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5397 fc06ba56 2019-08-22 stsp if (errcode)
5398 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5399 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5401 fc06ba56 2019-08-22 stsp return err;
5404 fc06ba56 2019-08-22 stsp static const struct got_error *
5405 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5407 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5408 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5409 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5410 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5411 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5412 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5413 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5414 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5415 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5417 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5418 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5420 15a94983 2018-12-23 stsp return err;
5422 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5423 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5424 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5425 f4ae6ddb 2022-07-01 thomas goto done;
5428 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5429 945f9229 2022-04-16 thomas if (err)
5430 945f9229 2022-04-16 thomas goto done;
5432 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5434 84451b3e 2018-07-10 stsp goto done;
5436 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5437 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5438 84451b3e 2018-07-10 stsp goto done;
5441 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5443 a70480e0 2018-06-23 stsp goto done;
5444 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5445 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5446 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5447 84451b3e 2018-07-10 stsp goto done;
5449 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5450 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5452 1fddf795 2021-01-20 stsp goto done;
5453 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5454 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5455 84451b3e 2018-07-10 stsp goto done;
5458 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5459 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5460 b02560ec 2019-08-19 stsp blame->nlines--;
5462 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5463 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5464 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5465 84451b3e 2018-07-10 stsp goto done;
5468 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5470 bd24772e 2018-07-11 stsp goto done;
5471 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5472 7cd52833 2022-06-23 thomas pack_fds);
5473 7cd52833 2022-06-23 thomas if (err)
5474 7cd52833 2022-06-23 thomas goto done;
5476 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5477 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5478 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5479 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5480 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5481 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5482 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5483 245d91c1 2018-07-12 stsp goto done;
5485 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5487 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5488 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5489 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5490 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5491 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5492 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5493 a5388363 2020-12-01 naddy s->blame_complete = 0;
5495 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5496 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5497 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5498 f5a09613 2020-12-13 naddy s->selected_line = 1;
5500 aa61903a 2021-12-31 thomas s->matched_line = 0;
5503 945f9229 2022-04-16 thomas if (commit)
5504 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5505 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5506 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5508 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5509 27d434c2 2018-09-15 stsp free(obj_id);
5511 245d91c1 2018-07-12 stsp stop_blame(blame);
5512 245d91c1 2018-07-12 stsp return err;
5515 245d91c1 2018-07-12 stsp static const struct got_error *
5516 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5517 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5519 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5520 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5522 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5524 c4843652 2019-08-12 stsp s->path = strdup(path);
5525 c4843652 2019-08-12 stsp if (s->path == NULL)
5526 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5528 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5529 c4843652 2019-08-12 stsp if (err) {
5530 c4843652 2019-08-12 stsp free(s->path);
5531 7cbe629d 2018-08-04 stsp return err;
5534 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5535 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5536 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5537 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5538 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5539 fb2756b9 2018-08-04 stsp s->repo = repo;
5540 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5541 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5543 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5544 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5545 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5546 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5548 11b20872 2019-11-08 stsp return err;
5551 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5552 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5553 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5554 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5555 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5556 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5558 a5388363 2020-12-01 naddy return run_blame(view);
5561 e5a0f69f 2018-08-18 stsp static const struct got_error *
5562 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5564 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5565 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5567 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5568 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5570 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5571 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5572 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5573 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5574 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5577 e5a0f69f 2018-08-18 stsp free(s->path);
5578 11b20872 2019-11-08 stsp free_colors(&s->colors);
5579 e5a0f69f 2018-08-18 stsp return err;
5582 7cbe629d 2018-08-04 stsp static const struct got_error *
5583 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5585 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5587 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5588 6c4c42e0 2019-06-24 stsp return NULL;
5591 6c4c42e0 2019-06-24 stsp static const struct got_error *
5592 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5594 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5595 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5596 6c4c42e0 2019-06-24 stsp int lineno;
5597 78eecffc 2022-06-23 thomas char *line = NULL;
5598 826082fe 2020-12-10 stsp size_t linesize = 0;
5599 826082fe 2020-12-10 stsp ssize_t linelen;
5601 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5602 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5603 6c4c42e0 2019-06-24 stsp return NULL;
5606 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5607 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5608 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5610 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5612 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5614 6c4c42e0 2019-06-24 stsp while (1) {
5615 6c4c42e0 2019-06-24 stsp off_t offset;
5617 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5618 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5619 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5623 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5624 6c4c42e0 2019-06-24 stsp lineno = 1;
5626 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5629 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5630 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5631 6c4c42e0 2019-06-24 stsp free(line);
5632 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5634 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5635 78eecffc 2022-06-23 thomas if (linelen != -1) {
5636 78eecffc 2022-06-23 thomas char *exstr;
5637 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5638 78eecffc 2022-06-23 thomas if (err)
5640 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5641 78eecffc 2022-06-23 thomas &view->regmatch)) {
5642 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5643 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5644 78eecffc 2022-06-23 thomas free(exstr);
5647 78eecffc 2022-06-23 thomas free(exstr);
5649 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5654 826082fe 2020-12-10 stsp free(line);
5656 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5657 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5658 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5661 05171be4 2022-06-23 thomas return err;
5664 6c4c42e0 2019-06-24 stsp static const struct got_error *
5665 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5667 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5668 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5669 2b380cc8 2018-10-24 stsp int errcode;
5671 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5672 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5673 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5674 2b380cc8 2018-10-24 stsp if (errcode)
5675 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5677 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5680 51fe7530 2019-08-19 stsp if (s->blame_complete)
5681 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5683 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5685 a5d43cac 2022-07-01 thomas view_border(view);
5686 e5a0f69f 2018-08-18 stsp return err;
5689 e5a0f69f 2018-08-18 stsp static const struct got_error *
5690 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5691 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5693 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5694 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5696 eaeaa612 2022-07-20 thomas *new_view = NULL;
5698 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5699 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5700 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5702 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5703 eaeaa612 2022-07-20 thomas if (err)
5704 eaeaa612 2022-07-20 thomas view_close(log_view);
5706 eaeaa612 2022-07-20 thomas *new_view = log_view;
5708 eaeaa612 2022-07-20 thomas return err;
5711 eaeaa612 2022-07-20 thomas static const struct got_error *
5712 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5714 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5715 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
5716 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5717 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5719 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5720 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5721 a5d43cac 2022-07-01 thomas --eos; /* border */
5723 e5a0f69f 2018-08-18 stsp switch (ch) {
5724 05171be4 2022-06-23 thomas case '0':
5725 05171be4 2022-06-23 thomas view->x = 0;
5727 05171be4 2022-06-23 thomas case '$':
5728 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5729 07b0611c 2022-06-23 thomas view->count = 0;
5731 05171be4 2022-06-23 thomas case KEY_RIGHT:
5732 05171be4 2022-06-23 thomas case 'l':
5733 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5734 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5736 07b0611c 2022-06-23 thomas view->count = 0;
5738 05171be4 2022-06-23 thomas case KEY_LEFT:
5739 05171be4 2022-06-23 thomas case 'h':
5740 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5741 07b0611c 2022-06-23 thomas if (view->x <= 0)
5742 07b0611c 2022-06-23 thomas view->count = 0;
5745 1e37a5c2 2019-05-12 jcs s->done = 1;
5747 4deef56f 2021-09-02 naddy case 'g':
5748 4deef56f 2021-09-02 naddy case KEY_HOME:
5749 4deef56f 2021-09-02 naddy s->selected_line = 1;
5750 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5751 07b0611c 2022-06-23 thomas view->count = 0;
5753 4deef56f 2021-09-02 naddy case 'G':
5754 4deef56f 2021-09-02 naddy case KEY_END:
5755 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5756 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5757 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5759 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5760 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5762 07b0611c 2022-06-23 thomas view->count = 0;
5765 1e37a5c2 2019-05-12 jcs case KEY_UP:
5766 f7140bf5 2021-10-17 thomas case CTRL('p'):
5767 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5768 1e37a5c2 2019-05-12 jcs s->selected_line--;
5769 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5770 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5771 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5773 07b0611c 2022-06-23 thomas view->count = 0;
5775 70f17a53 2022-06-13 thomas case CTRL('u'):
5776 23427b14 2022-06-23 thomas case 'u':
5777 70f17a53 2022-06-13 thomas nscroll /= 2;
5778 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5779 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5780 ea025d1d 2020-02-22 naddy case CTRL('b'):
5781 1c5e5faa 2022-06-23 thomas case 'b':
5782 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5783 07b0611c 2022-06-23 thomas if (view->count > 1)
5784 07b0611c 2022-06-23 thomas nscroll += nscroll;
5785 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5786 07b0611c 2022-06-23 thomas view->count = 0;
5789 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5790 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5792 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5795 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5796 f7140bf5 2021-10-17 thomas case CTRL('n'):
5797 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5798 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5799 1e37a5c2 2019-05-12 jcs s->selected_line++;
5800 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5801 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5803 07b0611c 2022-06-23 thomas view->count = 0;
5805 1c5e5faa 2022-06-23 thomas case 'c':
5806 1e37a5c2 2019-05-12 jcs case 'p': {
5807 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5809 07b0611c 2022-06-23 thomas view->count = 0;
5810 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5811 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5812 1e37a5c2 2019-05-12 jcs if (id == NULL)
5814 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5815 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5816 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5817 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5818 1e37a5c2 2019-05-12 jcs int obj_type;
5819 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5820 1e37a5c2 2019-05-12 jcs s->repo, id);
5823 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5824 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5825 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5826 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5829 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5830 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5831 ec242592 2022-04-22 thomas s->repo, &pid->id);
5832 945f9229 2022-04-16 thomas if (err)
5834 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5835 945f9229 2022-04-16 thomas pcommit, s->path);
5836 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5837 e5a0f69f 2018-08-18 stsp if (err) {
5838 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5839 1e37a5c2 2019-05-12 jcs err = NULL;
5840 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5843 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5845 1e37a5c2 2019-05-12 jcs free(blob_id);
5846 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5847 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5848 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5851 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5852 ec242592 2022-04-22 thomas &pid->id);
5853 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5855 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5856 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5858 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5863 1e37a5c2 2019-05-12 jcs s->done = 1;
5864 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5865 1e37a5c2 2019-05-12 jcs s->done = 0;
5866 1e37a5c2 2019-05-12 jcs if (thread_err)
5868 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5869 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5870 a5388363 2020-12-01 naddy err = run_blame(view);
5875 1c5e5faa 2022-06-23 thomas case 'C': {
5876 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5878 07b0611c 2022-06-23 thomas view->count = 0;
5879 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5880 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5882 1e37a5c2 2019-05-12 jcs s->done = 1;
5883 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5884 1e37a5c2 2019-05-12 jcs s->done = 0;
5885 1e37a5c2 2019-05-12 jcs if (thread_err)
5887 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5888 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5889 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5890 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5891 a5388363 2020-12-01 naddy err = run_blame(view);
5896 2a31b33b 2022-07-23 thomas case 'L':
5897 eaeaa612 2022-07-20 thomas view->count = 0;
5898 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
5899 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
5900 2a31b33b 2022-07-23 thomas if (s->id_to_log)
5901 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
5903 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5904 1e37a5c2 2019-05-12 jcs case '\r': {
5905 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5906 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5907 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5909 07b0611c 2022-06-23 thomas view->count = 0;
5910 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5911 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5912 1e37a5c2 2019-05-12 jcs if (id == NULL)
5914 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5917 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5918 4fc71f3b 2022-07-12 thomas if (*new_view) {
5919 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5920 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5921 4fc71f3b 2022-07-12 thomas if (err)
5923 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
5924 4fc71f3b 2022-07-12 thomas } else {
5925 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
5926 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
5928 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
5929 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
5930 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
5931 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
5932 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
5936 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5937 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
5938 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5940 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5943 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
5944 4fc71f3b 2022-07-12 thomas s->selected_line;
5945 4fc71f3b 2022-07-12 thomas if (*new_view)
5946 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
5947 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
5948 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5949 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
5950 a5d43cac 2022-07-01 thomas if (err)
5954 e78dc838 2020-12-04 stsp view->focussed = 0;
5955 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5956 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
5957 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
5958 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5959 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
5960 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5963 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
5964 40236d76 2022-06-23 thomas if (err)
5966 e78dc838 2020-12-04 stsp view->focus_child = 1;
5968 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5973 70f17a53 2022-06-13 thomas case CTRL('d'):
5974 23427b14 2022-06-23 thomas case 'd':
5975 70f17a53 2022-06-13 thomas nscroll /= 2;
5976 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5977 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5978 ea025d1d 2020-02-22 naddy case CTRL('f'):
5979 1c5e5faa 2022-06-23 thomas case 'f':
5981 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5982 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5983 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5984 07b0611c 2022-06-23 thomas view->count = 0;
5987 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5988 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5989 70f17a53 2022-06-13 thomas s->selected_line +=
5990 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5991 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5993 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5994 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5996 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5997 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5999 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6000 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6001 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6002 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6006 07b0611c 2022-06-23 thomas view->count = 0;
6009 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6012 adf4c9e0 2022-07-03 thomas static const struct got_error *
6013 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6015 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6016 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6018 adf4c9e0 2022-07-03 thomas view->count = 0;
6019 adf4c9e0 2022-07-03 thomas s->done = 1;
6020 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6021 adf4c9e0 2022-07-03 thomas s->done = 0;
6022 adf4c9e0 2022-07-03 thomas if (err)
6023 adf4c9e0 2022-07-03 thomas return err;
6024 adf4c9e0 2022-07-03 thomas return run_blame(view);
6027 a70480e0 2018-06-23 stsp static const struct got_error *
6028 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6030 a70480e0 2018-06-23 stsp const struct got_error *error;
6031 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6032 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6033 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6034 0587e10c 2020-07-23 stsp char *link_target = NULL;
6035 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6036 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6037 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6039 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6040 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6042 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6043 a70480e0 2018-06-23 stsp switch (ch) {
6045 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6048 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6049 69069811 2018-08-02 stsp if (repo_path == NULL)
6050 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6054 17020d27 2019-03-07 stsp usage_blame();
6055 a70480e0 2018-06-23 stsp /* NOTREACHED */
6059 a70480e0 2018-06-23 stsp argc -= optind;
6060 a70480e0 2018-06-23 stsp argv += optind;
6062 f135c941 2020-02-20 stsp if (argc != 1)
6063 a70480e0 2018-06-23 stsp usage_blame();
6065 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6066 7cd52833 2022-06-23 thomas if (error != NULL)
6067 7cd52833 2022-06-23 thomas goto done;
6069 69069811 2018-08-02 stsp if (repo_path == NULL) {
6070 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6071 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6072 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6073 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6074 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6075 c156c7a4 2020-12-18 stsp goto done;
6076 f135c941 2020-02-20 stsp if (worktree)
6077 eb41ed75 2019-02-05 stsp repo_path =
6078 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6080 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6081 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6082 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6083 c156c7a4 2020-12-18 stsp goto done;
6087 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6088 c02c541e 2019-03-29 stsp if (error != NULL)
6089 8e94dd5b 2019-01-04 stsp goto done;
6091 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6092 f135c941 2020-02-20 stsp worktree);
6093 c02c541e 2019-03-29 stsp if (error)
6094 92205607 2019-01-04 stsp goto done;
6096 f135c941 2020-02-20 stsp init_curses();
6098 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6099 51a10b52 2020-12-26 stsp if (error)
6100 51a10b52 2020-12-26 stsp goto done;
6102 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6103 eb41ed75 2019-02-05 stsp if (error)
6104 69069811 2018-08-02 stsp goto done;
6106 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6107 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6108 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6109 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6110 a70480e0 2018-06-23 stsp if (error != NULL)
6111 66b4983c 2018-06-23 stsp goto done;
6112 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6113 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6115 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6116 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6118 a19e88aa 2018-06-23 stsp if (error != NULL)
6119 8b473291 2019-02-21 stsp goto done;
6121 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6122 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6123 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6124 e1cd8fed 2018-08-01 stsp goto done;
6127 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6128 945f9229 2022-04-16 thomas if (error)
6129 945f9229 2022-04-16 thomas goto done;
6131 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6132 945f9229 2022-04-16 thomas commit, repo);
6133 7cbe629d 2018-08-04 stsp if (error)
6134 7cbe629d 2018-08-04 stsp goto done;
6136 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6137 78756c87 2020-11-24 stsp commit_id, repo);
6138 0587e10c 2020-07-23 stsp if (error)
6139 0587e10c 2020-07-23 stsp goto done;
6140 12314ad4 2019-08-31 stsp if (worktree) {
6141 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6142 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6143 12314ad4 2019-08-31 stsp worktree = NULL;
6145 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6147 69069811 2018-08-02 stsp free(repo_path);
6148 f135c941 2020-02-20 stsp free(in_repo_path);
6149 0587e10c 2020-07-23 stsp free(link_target);
6150 69069811 2018-08-02 stsp free(cwd);
6151 a70480e0 2018-06-23 stsp free(commit_id);
6152 945f9229 2022-04-16 thomas if (commit)
6153 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6154 eb41ed75 2019-02-05 stsp if (worktree)
6155 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6156 1d0f4054 2021-06-17 stsp if (repo) {
6157 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6158 1d0f4054 2021-06-17 stsp if (error == NULL)
6159 1d0f4054 2021-06-17 stsp error = close_err;
6161 7cd52833 2022-06-23 thomas if (pack_fds) {
6162 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6163 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6164 7cd52833 2022-06-23 thomas if (error == NULL)
6165 7cd52833 2022-06-23 thomas error = pack_err;
6167 51a10b52 2020-12-26 stsp tog_free_refs();
6168 a70480e0 2018-06-23 stsp return error;
6171 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6172 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6174 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6175 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6176 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6177 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6178 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6179 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6180 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6182 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6183 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6184 a5d43cac 2022-07-01 thomas --limit; /* border */
6186 f7d12f7e 2018-08-01 stsp werase(view->window);
6188 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6189 ffd1d5e5 2018-06-23 stsp return NULL;
6191 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6194 ffd1d5e5 2018-06-23 stsp return err;
6195 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6196 a3404814 2018-09-02 stsp wstandout(view->window);
6197 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6199 11b20872 2019-11-08 stsp wattr_on(view->window,
6200 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6201 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6203 11b20872 2019-11-08 stsp wattr_off(view->window,
6204 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6205 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6206 a3404814 2018-09-02 stsp wstandend(view->window);
6207 2550e4c3 2018-07-13 stsp free(wline);
6208 2550e4c3 2018-07-13 stsp wline = NULL;
6209 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6210 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6211 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6212 ffd1d5e5 2018-06-23 stsp return NULL;
6213 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6216 ce52c690 2018-06-23 stsp return err;
6217 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6218 2550e4c3 2018-07-13 stsp free(wline);
6219 2550e4c3 2018-07-13 stsp wline = NULL;
6220 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6221 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6222 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6223 ffd1d5e5 2018-06-23 stsp return NULL;
6224 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6225 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6226 a1eca9bb 2018-06-23 stsp return NULL;
6228 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6229 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6230 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6231 0cf4efb1 2018-09-29 stsp if (view->focussed)
6232 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6233 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6235 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6236 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6237 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6238 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6239 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6240 ffd1d5e5 2018-06-23 stsp return NULL;
6244 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6247 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6248 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6249 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6250 848d6979 2019-08-12 stsp const char *modestr = "";
6251 56e0773d 2019-11-28 stsp mode_t mode;
6253 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6254 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6256 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6257 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6258 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6260 638f9024 2019-05-13 stsp return got_error_from_errno(
6261 230a42bd 2019-05-11 jcs "got_object_id_str");
6263 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6264 63c5ca5d 2019-08-24 stsp modestr = "$";
6265 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6268 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6269 d86d3b18 2020-12-01 naddy te, s->repo);
6270 0d6c6ee3 2020-05-20 stsp if (err) {
6271 0d6c6ee3 2020-05-20 stsp free(id_str);
6272 0d6c6ee3 2020-05-20 stsp return err;
6274 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6275 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6276 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6278 848d6979 2019-08-12 stsp modestr = "@";
6280 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6281 848d6979 2019-08-12 stsp modestr = "/";
6282 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6283 848d6979 2019-08-12 stsp modestr = "*";
6284 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6285 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6286 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6287 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6288 1d13200f 2018-07-12 stsp free(id_str);
6289 0d6c6ee3 2020-05-20 stsp free(link_target);
6290 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6292 1d13200f 2018-07-12 stsp free(id_str);
6293 0d6c6ee3 2020-05-20 stsp free(link_target);
6294 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6296 ffd1d5e5 2018-06-23 stsp if (err) {
6297 ffd1d5e5 2018-06-23 stsp free(line);
6300 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6301 0cf4efb1 2018-09-29 stsp if (view->focussed)
6302 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6303 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6305 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6307 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6308 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6309 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6311 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6312 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6313 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6314 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6315 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6316 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6317 ffd1d5e5 2018-06-23 stsp free(line);
6318 2550e4c3 2018-07-13 stsp free(wline);
6319 2550e4c3 2018-07-13 stsp wline = NULL;
6321 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6322 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6323 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6327 ffd1d5e5 2018-06-23 stsp return err;
6330 ffd1d5e5 2018-06-23 stsp static void
6331 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6333 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6334 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6335 fa86c4bf 2020-11-29 stsp int i = 0;
6337 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6340 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6341 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6342 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6343 fa86c4bf 2020-11-29 stsp if (!isroot)
6344 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6347 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6348 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6352 a5d43cac 2022-07-01 thomas static const struct got_error *
6353 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6355 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6356 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6357 ffd1d5e5 2018-06-23 stsp int n = 0;
6359 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6360 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6361 694d3271 2020-12-01 naddy s->first_displayed_entry);
6363 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6365 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6366 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6367 a5d43cac 2022-07-01 thomas if (last)
6368 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6369 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6370 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6371 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6375 a5d43cac 2022-07-01 thomas return NULL;
6378 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6379 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6380 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6382 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6383 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6384 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6386 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6387 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6388 56e0773d 2019-11-28 stsp + 1 /* slash */;
6390 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6392 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6393 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6394 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6396 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6397 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6398 d9765a41 2018-06-23 stsp while (pt) {
6399 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6400 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6401 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6402 cb2ebc8a 2018-06-23 stsp goto done;
6404 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6405 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6406 cb2ebc8a 2018-06-23 stsp goto done;
6408 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6411 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6412 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6413 ce52c690 2018-06-23 stsp goto done;
6417 ce52c690 2018-06-23 stsp if (err) {
6418 ce52c690 2018-06-23 stsp free(*path);
6419 ce52c690 2018-06-23 stsp *path = NULL;
6421 ce52c690 2018-06-23 stsp return err;
6424 ce52c690 2018-06-23 stsp static const struct got_error *
6425 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6426 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6427 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6429 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6430 ce52c690 2018-06-23 stsp char *path;
6431 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6433 a0de39f3 2019-08-09 stsp *new_view = NULL;
6435 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6437 ce52c690 2018-06-23 stsp return err;
6439 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6440 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6441 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6442 83ce39e3 2019-08-12 stsp goto done;
6445 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6446 e5a0f69f 2018-08-18 stsp if (err) {
6447 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6448 fc06ba56 2019-08-22 stsp err = NULL;
6449 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6451 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6453 83ce39e3 2019-08-12 stsp free(path);
6454 69efd4c4 2018-07-18 stsp return err;
6457 69efd4c4 2018-07-18 stsp static const struct got_error *
6458 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6459 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6461 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6462 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6463 69efd4c4 2018-07-18 stsp char *path;
6465 a0de39f3 2019-08-09 stsp *new_view = NULL;
6467 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6468 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6469 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6471 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6473 69efd4c4 2018-07-18 stsp return err;
6475 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6478 e5a0f69f 2018-08-18 stsp view_close(log_view);
6480 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6481 cb2ebc8a 2018-06-23 stsp free(path);
6482 cb2ebc8a 2018-06-23 stsp return err;
6485 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6486 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6487 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6489 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6490 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6491 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6492 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6494 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6495 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6497 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6498 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6499 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6501 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6503 bc573f3b 2021-07-10 stsp goto done;
6506 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6507 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6508 bc573f3b 2021-07-10 stsp * closed on demand.
6510 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6511 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6513 bc573f3b 2021-07-10 stsp goto done;
6514 bc573f3b 2021-07-10 stsp s->tree = s->root;
6516 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6517 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6518 ffd1d5e5 2018-06-23 stsp goto done;
6520 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6521 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6522 ffd1d5e5 2018-06-23 stsp goto done;
6525 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6526 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6527 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6528 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6529 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6530 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6531 9cd7cbd1 2020-12-07 stsp goto done;
6534 fb2756b9 2018-08-04 stsp s->repo = repo;
6536 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6537 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6538 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6539 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6541 c0b01bdb 2019-11-08 stsp goto done;
6542 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6543 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6545 c0b01bdb 2019-11-08 stsp goto done;
6546 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6547 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6548 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6550 c0b01bdb 2019-11-08 stsp goto done;
6552 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6553 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6554 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6556 11b20872 2019-11-08 stsp goto done;
6558 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6559 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6561 c0b01bdb 2019-11-08 stsp goto done;
6564 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6565 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6566 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6567 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6568 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6570 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6571 bc573f3b 2021-07-10 stsp if (commit)
6572 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6574 bc573f3b 2021-07-10 stsp close_tree_view(view);
6575 ad80ab7b 2018-08-04 stsp return err;
6578 e5a0f69f 2018-08-18 stsp static const struct got_error *
6579 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6581 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6583 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6584 fb2756b9 2018-08-04 stsp free(s->tree_label);
6585 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6586 6484ec90 2018-09-29 stsp free(s->commit_id);
6587 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6588 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6589 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6590 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6591 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6592 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6593 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6594 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6595 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6596 ad80ab7b 2018-08-04 stsp free(parent);
6599 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6600 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6601 bc573f3b 2021-07-10 stsp if (s->root)
6602 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6603 7c32bd05 2019-06-22 stsp return NULL;
6606 7c32bd05 2019-06-22 stsp static const struct got_error *
6607 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6609 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6611 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6612 7c32bd05 2019-06-22 stsp return NULL;
6615 7c32bd05 2019-06-22 stsp static int
6616 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6618 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6620 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6624 7c32bd05 2019-06-22 stsp static const struct got_error *
6625 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6627 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6628 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6630 7c32bd05 2019-06-22 stsp if (!view->searching) {
6631 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6632 7c32bd05 2019-06-22 stsp return NULL;
6635 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6636 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6637 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6638 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6639 56e0773d 2019-11-28 stsp s->selected_entry);
6641 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6643 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6644 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6646 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6647 56e0773d 2019-11-28 stsp s->selected_entry);
6650 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6651 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6652 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6653 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6655 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6658 7c32bd05 2019-06-22 stsp while (1) {
6659 56e0773d 2019-11-28 stsp if (te == NULL) {
6660 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6661 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6662 ac66afb8 2019-06-24 stsp return NULL;
6664 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6665 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6667 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6670 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6671 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6672 56e0773d 2019-11-28 stsp s->matched_entry = te;
6676 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6677 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6679 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6682 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6683 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6684 7c32bd05 2019-06-22 stsp s->selected = 0;
6687 e5a0f69f 2018-08-18 stsp return NULL;
6690 ad80ab7b 2018-08-04 stsp static const struct got_error *
6691 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6693 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6694 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6695 e5a0f69f 2018-08-18 stsp char *parent_path;
6697 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6699 e5a0f69f 2018-08-18 stsp return err;
6701 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6702 e5a0f69f 2018-08-18 stsp free(parent_path);
6704 a5d43cac 2022-07-01 thomas view_border(view);
6705 e5a0f69f 2018-08-18 stsp return err;
6708 e5a0f69f 2018-08-18 stsp static const struct got_error *
6709 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6711 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6712 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6713 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6714 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
6716 e5a0f69f 2018-08-18 stsp switch (ch) {
6718 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6719 07b0611c 2022-06-23 thomas view->count = 0;
6721 1be4947a 2022-07-22 thomas case 'L':
6722 07b0611c 2022-06-23 thomas view->count = 0;
6723 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6725 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6727 1be4947a 2022-07-22 thomas case 'R':
6728 07b0611c 2022-06-23 thomas view->count = 0;
6729 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
6731 e4526bf5 2021-09-03 naddy case 'g':
6732 e4526bf5 2021-09-03 naddy case KEY_HOME:
6733 e4526bf5 2021-09-03 naddy s->selected = 0;
6734 07b0611c 2022-06-23 thomas view->count = 0;
6735 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6736 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6737 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6739 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6741 e4526bf5 2021-09-03 naddy case 'G':
6742 a5d43cac 2022-07-01 thomas case KEY_END: {
6743 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6745 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6746 a5d43cac 2022-07-01 thomas --eos; /* border */
6747 e4526bf5 2021-09-03 naddy s->selected = 0;
6748 07b0611c 2022-06-23 thomas view->count = 0;
6749 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6750 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6751 e4526bf5 2021-09-03 naddy if (te == NULL) {
6752 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6753 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6758 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6759 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6761 e4526bf5 2021-09-03 naddy if (n > 0)
6762 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6766 1e37a5c2 2019-05-12 jcs case KEY_UP:
6767 f7140bf5 2021-10-17 thomas case CTRL('p'):
6768 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6769 1e37a5c2 2019-05-12 jcs s->selected--;
6772 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6773 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6774 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6775 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6776 07b0611c 2022-06-23 thomas view->count = 0;
6778 70f17a53 2022-06-13 thomas case CTRL('u'):
6779 23427b14 2022-06-23 thomas case 'u':
6780 70f17a53 2022-06-13 thomas nscroll /= 2;
6781 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6782 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6783 ea025d1d 2020-02-22 naddy case CTRL('b'):
6784 1c5e5faa 2022-06-23 thomas case 'b':
6785 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6786 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6787 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6788 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6790 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6791 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6793 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6794 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6795 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6796 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6797 07b0611c 2022-06-23 thomas view->count = 0;
6800 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6801 f7140bf5 2021-10-17 thomas case CTRL('n'):
6802 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6803 1e37a5c2 2019-05-12 jcs s->selected++;
6806 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6807 07b0611c 2022-06-23 thomas == NULL) {
6808 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6809 07b0611c 2022-06-23 thomas view->count = 0;
6812 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6814 70f17a53 2022-06-13 thomas case CTRL('d'):
6815 23427b14 2022-06-23 thomas case 'd':
6816 70f17a53 2022-06-13 thomas nscroll /= 2;
6817 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6818 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6819 ea025d1d 2020-02-22 naddy case CTRL('f'):
6820 1c5e5faa 2022-06-23 thomas case 'f':
6821 4c2d69cb 2022-06-23 thomas case ' ':
6822 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6824 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6825 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6826 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6827 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6829 07b0611c 2022-06-23 thomas view->count = 0;
6832 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6834 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6836 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6837 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6838 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6839 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6840 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6841 07b0611c 2022-06-23 thomas view->count = 0;
6844 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6845 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6847 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6848 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6849 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6850 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6851 1e37a5c2 2019-05-12 jcs s->selected_entry =
6852 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6853 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6854 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6855 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6856 a5d43cac 2022-07-01 thomas if (err)
6859 1e37a5c2 2019-05-12 jcs free(parent);
6860 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6861 56e0773d 2019-11-28 stsp s->selected_entry))) {
6862 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6863 07b0611c 2022-06-23 thomas view->count = 0;
6864 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6865 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6868 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6869 941e9f74 2019-05-21 stsp if (err) {
6870 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6873 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
6874 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
6876 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6877 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6878 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6879 07b0611c 2022-06-23 thomas view->count = 0;
6882 07b0611c 2022-06-23 thomas view->count = 0;
6886 ffd1d5e5 2018-06-23 stsp return err;
6889 ffd1d5e5 2018-06-23 stsp __dead static void
6890 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6893 91198554 2022-06-23 thomas fprintf(stderr,
6894 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6895 ffd1d5e5 2018-06-23 stsp getprogname());
6899 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6900 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6902 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6903 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6904 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6905 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6906 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6907 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6908 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6909 4e97c21c 2020-12-06 stsp char *label = NULL;
6910 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6911 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6913 5221c383 2018-08-01 stsp struct tog_view *view;
6914 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6916 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6917 ffd1d5e5 2018-06-23 stsp switch (ch) {
6919 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6922 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6923 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6924 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6928 e99e2d15 2020-11-24 naddy usage_tree();
6929 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6933 ffd1d5e5 2018-06-23 stsp argc -= optind;
6934 ffd1d5e5 2018-06-23 stsp argv += optind;
6936 55cccc34 2020-02-20 stsp if (argc > 1)
6937 e99e2d15 2020-11-24 naddy usage_tree();
6939 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6940 7cd52833 2022-06-23 thomas if (error != NULL)
6941 7cd52833 2022-06-23 thomas goto done;
6943 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6944 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6945 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6946 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6947 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6948 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6949 c156c7a4 2020-12-18 stsp goto done;
6950 55cccc34 2020-02-20 stsp if (worktree)
6951 52185f70 2019-02-05 stsp repo_path =
6952 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6954 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6955 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6956 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6957 c156c7a4 2020-12-18 stsp goto done;
6961 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6962 c02c541e 2019-03-29 stsp if (error != NULL)
6963 52185f70 2019-02-05 stsp goto done;
6965 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6966 55cccc34 2020-02-20 stsp repo, worktree);
6967 55cccc34 2020-02-20 stsp if (error)
6968 55cccc34 2020-02-20 stsp goto done;
6970 55cccc34 2020-02-20 stsp init_curses();
6972 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6973 c02c541e 2019-03-29 stsp if (error)
6974 52185f70 2019-02-05 stsp goto done;
6976 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6977 51a10b52 2020-12-26 stsp if (error)
6978 51a10b52 2020-12-26 stsp goto done;
6980 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6981 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6982 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6983 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6984 4e97c21c 2020-12-06 stsp if (error)
6985 4e97c21c 2020-12-06 stsp goto done;
6986 4e97c21c 2020-12-06 stsp head_ref_name = label;
6988 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6989 4e97c21c 2020-12-06 stsp if (error == NULL)
6990 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6991 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6992 4e97c21c 2020-12-06 stsp goto done;
6993 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6994 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6995 4e97c21c 2020-12-06 stsp if (error)
6996 4e97c21c 2020-12-06 stsp goto done;
6999 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7000 945f9229 2022-04-16 thomas if (error)
7001 945f9229 2022-04-16 thomas goto done;
7003 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7004 5221c383 2018-08-01 stsp if (view == NULL) {
7005 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7006 5221c383 2018-08-01 stsp goto done;
7008 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7009 ad80ab7b 2018-08-04 stsp if (error)
7010 ad80ab7b 2018-08-04 stsp goto done;
7011 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7012 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7013 d91faf3b 2020-12-01 naddy in_repo_path);
7014 55cccc34 2020-02-20 stsp if (error)
7015 55cccc34 2020-02-20 stsp goto done;
7018 55cccc34 2020-02-20 stsp if (worktree) {
7019 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7020 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7021 55cccc34 2020-02-20 stsp worktree = NULL;
7023 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7025 52185f70 2019-02-05 stsp free(repo_path);
7026 e4a0e26d 2020-02-20 stsp free(cwd);
7027 ffd1d5e5 2018-06-23 stsp free(commit_id);
7028 4e97c21c 2020-12-06 stsp free(label);
7030 486cd271 2020-12-06 stsp got_ref_close(ref);
7031 1d0f4054 2021-06-17 stsp if (repo) {
7032 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7033 1d0f4054 2021-06-17 stsp if (error == NULL)
7034 1d0f4054 2021-06-17 stsp error = close_err;
7036 7cd52833 2022-06-23 thomas if (pack_fds) {
7037 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7038 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7039 7cd52833 2022-06-23 thomas if (error == NULL)
7040 7cd52833 2022-06-23 thomas error = pack_err;
7042 51a10b52 2020-12-26 stsp tog_free_refs();
7043 ffd1d5e5 2018-06-23 stsp return error;
7046 6458efa5 2020-11-24 stsp static const struct got_error *
7047 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7049 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7050 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7052 6458efa5 2020-11-24 stsp s->nrefs = 0;
7053 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7054 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7055 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7056 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7057 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7060 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7061 6458efa5 2020-11-24 stsp if (re == NULL)
7062 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7064 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7065 8924d611 2020-12-26 stsp if (re->ref == NULL)
7066 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7067 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7068 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7071 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7072 6458efa5 2020-11-24 stsp return NULL;
7075 ef20f542 2022-06-26 thomas static void
7076 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7078 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7080 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7081 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7082 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7083 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7088 6458efa5 2020-11-24 stsp static const struct got_error *
7089 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7091 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7092 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7094 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7095 6458efa5 2020-11-24 stsp s->repo = repo;
7097 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7098 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7100 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7102 6458efa5 2020-11-24 stsp return err;
7104 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7105 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7106 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7107 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7109 6458efa5 2020-11-24 stsp goto done;
7111 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7112 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7113 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7115 6458efa5 2020-11-24 stsp goto done;
7117 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7118 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7119 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7120 2183bbf6 2022-01-23 thomas if (err)
7121 2183bbf6 2022-01-23 thomas goto done;
7123 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7124 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7125 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7127 6458efa5 2020-11-24 stsp goto done;
7130 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7131 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7132 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7133 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7134 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7137 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7138 6458efa5 2020-11-24 stsp return err;
7141 6458efa5 2020-11-24 stsp static const struct got_error *
7142 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7144 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7146 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7147 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7149 6458efa5 2020-11-24 stsp return NULL;
7152 6458efa5 2020-11-24 stsp static const struct got_error *
7153 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7154 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7156 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7157 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7158 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7159 6458efa5 2020-11-24 stsp int obj_type;
7161 c42c9805 2020-11-24 stsp *commit_id = NULL;
7163 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7165 6458efa5 2020-11-24 stsp return err;
7167 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7169 6458efa5 2020-11-24 stsp goto done;
7171 6458efa5 2020-11-24 stsp switch (obj_type) {
7172 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7173 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7175 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7176 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7178 6458efa5 2020-11-24 stsp goto done;
7179 c42c9805 2020-11-24 stsp free(obj_id);
7180 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7181 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7183 6458efa5 2020-11-24 stsp goto done;
7184 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7185 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7186 c42c9805 2020-11-24 stsp goto done;
7188 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7189 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7190 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7191 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7192 c42c9805 2020-11-24 stsp goto done;
7196 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7202 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7203 c42c9805 2020-11-24 stsp if (err) {
7204 c42c9805 2020-11-24 stsp free(*commit_id);
7205 c42c9805 2020-11-24 stsp *commit_id = NULL;
7207 c42c9805 2020-11-24 stsp return err;
7210 c42c9805 2020-11-24 stsp static const struct got_error *
7211 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7212 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7214 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7215 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7216 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7218 c42c9805 2020-11-24 stsp *new_view = NULL;
7220 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7221 c42c9805 2020-11-24 stsp if (err) {
7222 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7223 c42c9805 2020-11-24 stsp return err;
7225 c42c9805 2020-11-24 stsp return NULL;
7228 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7229 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7230 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7231 6458efa5 2020-11-24 stsp goto done;
7234 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7235 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7238 6458efa5 2020-11-24 stsp view_close(log_view);
7240 6458efa5 2020-11-24 stsp *new_view = log_view;
7241 c42c9805 2020-11-24 stsp free(commit_id);
7242 6458efa5 2020-11-24 stsp return err;
7245 ce5b7c56 2019-07-09 stsp static void
7246 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7248 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7249 34ba6917 2020-11-30 stsp int i = 0;
7251 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7254 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7255 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7256 34ba6917 2020-11-30 stsp if (re == NULL)
7258 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7259 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7263 a5d43cac 2022-07-01 thomas static const struct got_error *
7264 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7266 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7267 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7268 6458efa5 2020-11-24 stsp int n = 0;
7270 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7271 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7273 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7275 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7276 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7277 a5d43cac 2022-07-01 thomas if (last)
7278 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7279 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7280 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7281 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7285 a5d43cac 2022-07-01 thomas return NULL;
7288 6458efa5 2020-11-24 stsp static const struct got_error *
7289 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7291 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7293 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7294 6458efa5 2020-11-24 stsp return NULL;
7297 6458efa5 2020-11-24 stsp static int
7298 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7300 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7302 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7306 6458efa5 2020-11-24 stsp static const struct got_error *
7307 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7309 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7310 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7312 6458efa5 2020-11-24 stsp if (!view->searching) {
7313 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7314 6458efa5 2020-11-24 stsp return NULL;
7317 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7318 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7319 6458efa5 2020-11-24 stsp if (s->selected_entry)
7320 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7322 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7323 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7325 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7326 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7328 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7329 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7332 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7333 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7334 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7335 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7337 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7340 6458efa5 2020-11-24 stsp while (1) {
7341 6458efa5 2020-11-24 stsp if (re == NULL) {
7342 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7343 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7344 6458efa5 2020-11-24 stsp return NULL;
7346 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7347 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7349 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7352 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7353 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7354 6458efa5 2020-11-24 stsp s->matched_entry = re;
7358 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7359 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7361 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7364 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7365 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7366 6458efa5 2020-11-24 stsp s->selected = 0;
7369 6458efa5 2020-11-24 stsp return NULL;
7372 6458efa5 2020-11-24 stsp static const struct got_error *
7373 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7375 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7376 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7377 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7378 6458efa5 2020-11-24 stsp char *line = NULL;
7379 6458efa5 2020-11-24 stsp wchar_t *wline;
7380 6458efa5 2020-11-24 stsp struct tog_color *tc;
7381 6458efa5 2020-11-24 stsp int width, n;
7382 6458efa5 2020-11-24 stsp int limit = view->nlines;
7384 6458efa5 2020-11-24 stsp werase(view->window);
7386 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7387 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7388 a5d43cac 2022-07-01 thomas --limit; /* border */
7390 6458efa5 2020-11-24 stsp if (limit == 0)
7391 6458efa5 2020-11-24 stsp return NULL;
7393 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7395 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7396 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7397 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7399 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7400 6458efa5 2020-11-24 stsp if (err) {
7401 6458efa5 2020-11-24 stsp free(line);
7402 6458efa5 2020-11-24 stsp return err;
7404 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7405 6458efa5 2020-11-24 stsp wstandout(view->window);
7406 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7407 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7408 6458efa5 2020-11-24 stsp wstandend(view->window);
7409 6458efa5 2020-11-24 stsp free(wline);
7410 6458efa5 2020-11-24 stsp wline = NULL;
7411 6458efa5 2020-11-24 stsp free(line);
7412 6458efa5 2020-11-24 stsp line = NULL;
7413 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7414 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7415 6458efa5 2020-11-24 stsp if (--limit <= 0)
7416 6458efa5 2020-11-24 stsp return NULL;
7419 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7420 6458efa5 2020-11-24 stsp char *line = NULL;
7421 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7423 84227eb1 2022-06-23 thomas if (s->show_date) {
7424 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7425 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7426 84227eb1 2022-06-23 thomas struct got_object_id *id;
7427 84227eb1 2022-06-23 thomas struct tm tm;
7428 84227eb1 2022-06-23 thomas time_t t;
7430 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7431 84227eb1 2022-06-23 thomas if (err)
7432 84227eb1 2022-06-23 thomas return err;
7433 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7434 84227eb1 2022-06-23 thomas if (err) {
7435 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7436 84227eb1 2022-06-23 thomas free(id);
7437 84227eb1 2022-06-23 thomas return err;
7439 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7441 84227eb1 2022-06-23 thomas if (err) {
7442 84227eb1 2022-06-23 thomas free(id);
7443 84227eb1 2022-06-23 thomas return err;
7445 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7446 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7447 84227eb1 2022-06-23 thomas } else {
7448 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7449 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7451 84227eb1 2022-06-23 thomas free(id);
7452 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7453 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7454 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7455 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7457 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7458 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7459 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7460 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7461 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7462 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7463 6458efa5 2020-11-24 stsp struct got_object_id *id;
7464 6458efa5 2020-11-24 stsp char *id_str;
7465 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7467 6458efa5 2020-11-24 stsp return err;
7468 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7469 6458efa5 2020-11-24 stsp if (err) {
7471 6458efa5 2020-11-24 stsp return err;
7473 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7474 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7475 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7477 6458efa5 2020-11-24 stsp free(id_str);
7478 6458efa5 2020-11-24 stsp return err;
7481 6458efa5 2020-11-24 stsp free(id_str);
7482 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7483 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7484 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7486 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7488 6458efa5 2020-11-24 stsp if (err) {
7489 6458efa5 2020-11-24 stsp free(line);
7490 6458efa5 2020-11-24 stsp return err;
7492 6458efa5 2020-11-24 stsp if (n == s->selected) {
7493 6458efa5 2020-11-24 stsp if (view->focussed)
7494 6458efa5 2020-11-24 stsp wstandout(view->window);
7495 6458efa5 2020-11-24 stsp s->selected_entry = re;
7497 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7499 6458efa5 2020-11-24 stsp wattr_on(view->window,
7500 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7501 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7503 6458efa5 2020-11-24 stsp wattr_off(view->window,
7504 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7505 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7506 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7507 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7508 6458efa5 2020-11-24 stsp wstandend(view->window);
7509 6458efa5 2020-11-24 stsp free(line);
7510 6458efa5 2020-11-24 stsp free(wline);
7511 6458efa5 2020-11-24 stsp wline = NULL;
7513 6458efa5 2020-11-24 stsp s->ndisplayed++;
7514 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7517 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7520 a5d43cac 2022-07-01 thomas view_border(view);
7521 6458efa5 2020-11-24 stsp return err;
7524 6458efa5 2020-11-24 stsp static const struct got_error *
7525 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7526 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7528 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7529 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7530 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7532 c42c9805 2020-11-24 stsp *new_view = NULL;
7534 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7535 c42c9805 2020-11-24 stsp if (err) {
7536 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7537 c42c9805 2020-11-24 stsp return err;
7539 c42c9805 2020-11-24 stsp return NULL;
7543 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7544 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7545 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7546 c42c9805 2020-11-24 stsp goto done;
7549 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7550 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7552 c42c9805 2020-11-24 stsp goto done;
7554 c42c9805 2020-11-24 stsp *new_view = tree_view;
7556 c42c9805 2020-11-24 stsp free(commit_id);
7557 c42c9805 2020-11-24 stsp return err;
7559 c42c9805 2020-11-24 stsp static const struct got_error *
7560 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7562 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7563 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7564 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7565 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
7567 6458efa5 2020-11-24 stsp switch (ch) {
7569 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7570 07b0611c 2022-06-23 thomas view->count = 0;
7572 84227eb1 2022-06-23 thomas case 'm':
7573 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7574 07b0611c 2022-06-23 thomas view->count = 0;
7576 98182bd0 2021-11-20 thomas case 'o':
7577 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7578 07b0611c 2022-06-23 thomas view->count = 0;
7579 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7580 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7581 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7582 c591440f 2021-11-20 thomas if (err)
7584 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7585 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7586 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7587 3bfadbd4 2021-11-20 thomas if (err)
7589 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7590 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7592 6458efa5 2020-11-24 stsp case KEY_ENTER:
7593 6458efa5 2020-11-24 stsp case '\r':
7594 07b0611c 2022-06-23 thomas view->count = 0;
7595 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7597 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7599 1be4947a 2022-07-22 thomas case 'T':
7600 07b0611c 2022-06-23 thomas view->count = 0;
7601 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7603 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
7605 e4526bf5 2021-09-03 naddy case 'g':
7606 e4526bf5 2021-09-03 naddy case KEY_HOME:
7607 e4526bf5 2021-09-03 naddy s->selected = 0;
7608 07b0611c 2022-06-23 thomas view->count = 0;
7609 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7611 e4526bf5 2021-09-03 naddy case 'G':
7612 a5d43cac 2022-07-01 thomas case KEY_END: {
7613 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7615 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7616 a5d43cac 2022-07-01 thomas --eos; /* border */
7617 e4526bf5 2021-09-03 naddy s->selected = 0;
7618 07b0611c 2022-06-23 thomas view->count = 0;
7619 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7620 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7621 e4526bf5 2021-09-03 naddy if (re == NULL)
7623 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7624 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7626 e4526bf5 2021-09-03 naddy if (n > 0)
7627 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7631 6458efa5 2020-11-24 stsp case KEY_UP:
7632 f7140bf5 2021-10-17 thomas case CTRL('p'):
7633 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7634 6458efa5 2020-11-24 stsp s->selected--;
7637 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7638 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7639 07b0611c 2022-06-23 thomas view->count = 0;
7641 70f17a53 2022-06-13 thomas case CTRL('u'):
7642 23427b14 2022-06-23 thomas case 'u':
7643 70f17a53 2022-06-13 thomas nscroll /= 2;
7644 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7645 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7646 6458efa5 2020-11-24 stsp case CTRL('b'):
7647 1c5e5faa 2022-06-23 thomas case 'b':
7648 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7649 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7650 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7651 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7652 07b0611c 2022-06-23 thomas view->count = 0;
7655 6458efa5 2020-11-24 stsp case KEY_DOWN:
7656 f7140bf5 2021-10-17 thomas case CTRL('n'):
7657 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7658 6458efa5 2020-11-24 stsp s->selected++;
7661 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7662 6458efa5 2020-11-24 stsp /* can't scroll any further */
7663 07b0611c 2022-06-23 thomas view->count = 0;
7666 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7668 70f17a53 2022-06-13 thomas case CTRL('d'):
7669 23427b14 2022-06-23 thomas case 'd':
7670 70f17a53 2022-06-13 thomas nscroll /= 2;
7671 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7672 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7673 6458efa5 2020-11-24 stsp case CTRL('f'):
7674 1c5e5faa 2022-06-23 thomas case 'f':
7675 4c2d69cb 2022-06-23 thomas case ' ':
7676 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7677 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7678 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7679 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7680 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7681 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7682 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7683 07b0611c 2022-06-23 thomas view->count = 0;
7686 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7688 6458efa5 2020-11-24 stsp case CTRL('l'):
7689 07b0611c 2022-06-23 thomas view->count = 0;
7690 8924d611 2020-12-26 stsp tog_free_refs();
7691 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7694 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7695 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7697 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7698 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7699 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7702 07b0611c 2022-06-23 thomas view->count = 0;
7706 6458efa5 2020-11-24 stsp return err;
7709 6458efa5 2020-11-24 stsp __dead static void
7710 6458efa5 2020-11-24 stsp usage_ref(void)
7713 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7714 6458efa5 2020-11-24 stsp getprogname());
7718 6458efa5 2020-11-24 stsp static const struct got_error *
7719 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7721 6458efa5 2020-11-24 stsp const struct got_error *error;
7722 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7723 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7724 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7726 6458efa5 2020-11-24 stsp struct tog_view *view;
7727 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7729 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7730 6458efa5 2020-11-24 stsp switch (ch) {
7732 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7733 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7734 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7738 e99e2d15 2020-11-24 naddy usage_ref();
7739 6458efa5 2020-11-24 stsp /* NOTREACHED */
7743 6458efa5 2020-11-24 stsp argc -= optind;
7744 6458efa5 2020-11-24 stsp argv += optind;
7746 6458efa5 2020-11-24 stsp if (argc > 1)
7747 e99e2d15 2020-11-24 naddy usage_ref();
7749 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7750 7cd52833 2022-06-23 thomas if (error != NULL)
7751 7cd52833 2022-06-23 thomas goto done;
7753 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7754 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7755 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7756 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7757 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7758 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7759 c156c7a4 2020-12-18 stsp goto done;
7760 6458efa5 2020-11-24 stsp if (worktree)
7761 6458efa5 2020-11-24 stsp repo_path =
7762 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7764 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7765 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7766 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7767 c156c7a4 2020-12-18 stsp goto done;
7771 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7772 6458efa5 2020-11-24 stsp if (error != NULL)
7773 6458efa5 2020-11-24 stsp goto done;
7775 6458efa5 2020-11-24 stsp init_curses();
7777 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7778 51a10b52 2020-12-26 stsp if (error)
7779 51a10b52 2020-12-26 stsp goto done;
7781 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7782 6458efa5 2020-11-24 stsp if (error)
7783 6458efa5 2020-11-24 stsp goto done;
7785 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7786 6458efa5 2020-11-24 stsp if (view == NULL) {
7787 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7788 6458efa5 2020-11-24 stsp goto done;
7791 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7792 6458efa5 2020-11-24 stsp if (error)
7793 6458efa5 2020-11-24 stsp goto done;
7795 6458efa5 2020-11-24 stsp if (worktree) {
7796 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7797 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7798 6458efa5 2020-11-24 stsp worktree = NULL;
7800 6458efa5 2020-11-24 stsp error = view_loop(view);
7802 6458efa5 2020-11-24 stsp free(repo_path);
7803 6458efa5 2020-11-24 stsp free(cwd);
7804 1d0f4054 2021-06-17 stsp if (repo) {
7805 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7806 1d0f4054 2021-06-17 stsp if (close_err)
7807 1d0f4054 2021-06-17 stsp error = close_err;
7809 7cd52833 2022-06-23 thomas if (pack_fds) {
7810 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7811 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7812 7cd52833 2022-06-23 thomas if (error == NULL)
7813 7cd52833 2022-06-23 thomas error = pack_err;
7815 51a10b52 2020-12-26 stsp tog_free_refs();
7816 6458efa5 2020-11-24 stsp return error;
7819 2a31b33b 2022-07-23 thomas static const struct got_error *
7820 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
7821 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
7823 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
7825 2a31b33b 2022-07-23 thomas *new_view = NULL;
7827 2a31b33b 2022-07-23 thomas switch (request) {
7828 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
7829 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
7830 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
7832 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
7833 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
7834 2a31b33b 2022-07-23 thomas view, s->repo);
7836 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
7837 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7839 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
7840 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
7841 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
7843 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
7844 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
7845 2a31b33b 2022-07-23 thomas s->repo);
7847 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
7848 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7850 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
7851 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
7852 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
7853 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
7854 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
7855 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
7856 2a31b33b 2022-07-23 thomas &view->state.tree);
7857 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
7858 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
7859 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
7860 2a31b33b 2022-07-23 thomas view->state.ref.repo);
7862 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
7863 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7865 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
7866 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
7867 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
7868 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
7869 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
7870 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
7871 2a31b33b 2022-07-23 thomas view->state.log.repo);
7872 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
7873 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
7874 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
7875 2a31b33b 2022-07-23 thomas view->state.ref.repo);
7877 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
7878 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7880 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
7881 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
7882 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
7883 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
7884 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
7885 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
7886 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
7887 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
7889 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
7890 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7891 2a31b33b 2022-07-23 thomas if (err)
7892 2a31b33b 2022-07-23 thomas view_close(*new_view);
7894 2a31b33b 2022-07-23 thomas default:
7895 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
7898 2a31b33b 2022-07-23 thomas return err;
7902 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
7903 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
7905 6458efa5 2020-11-24 stsp static void
7906 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
7908 a5d43cac 2022-07-01 thomas switch (view->type) {
7909 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7910 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7911 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
7912 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
7916 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
7917 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
7919 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
7920 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
7923 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
7924 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
7925 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
7927 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
7928 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
7929 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
7931 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
7932 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
7933 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
7935 a5d43cac 2022-07-01 thomas default:
7939 a5d43cac 2022-07-01 thomas view->offset = 0;
7943 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
7944 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
7946 a5d43cac 2022-07-01 thomas static const struct got_error *
7947 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
7949 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
7950 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
7951 a5d43cac 2022-07-01 thomas int *selected = NULL;
7952 a5d43cac 2022-07-01 thomas int header, offset;
7954 a5d43cac 2022-07-01 thomas switch (view->type) {
7955 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7956 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7957 a5d43cac 2022-07-01 thomas header = 3;
7958 a5d43cac 2022-07-01 thomas scrolld = NULL;
7959 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
7960 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
7961 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
7962 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
7963 a5d43cac 2022-07-01 thomas view->offset = offset;
7967 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
7968 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
7969 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
7970 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
7971 a5d43cac 2022-07-01 thomas selected = &s->selected;
7974 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
7975 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7976 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
7977 a5d43cac 2022-07-01 thomas header = 3;
7978 a5d43cac 2022-07-01 thomas selected = &s->selected;
7981 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
7982 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7983 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
7984 a5d43cac 2022-07-01 thomas header = 5;
7985 a5d43cac 2022-07-01 thomas selected = &s->selected;
7988 a5d43cac 2022-07-01 thomas default:
7989 a5d43cac 2022-07-01 thomas selected = NULL;
7990 a5d43cac 2022-07-01 thomas scrolld = NULL;
7991 a5d43cac 2022-07-01 thomas header = 0;
7995 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
7996 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
7997 a5d43cac 2022-07-01 thomas view->offset = offset;
7998 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
7999 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8000 a5d43cac 2022-07-01 thomas *selected -= offset;
8004 a5d43cac 2022-07-01 thomas return err;
8007 a5d43cac 2022-07-01 thomas static void
8008 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8012 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8013 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8014 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8015 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8017 6879ba42 2020-10-01 naddy fputc('\n', fp);
8020 4ed7e80c 2018-05-20 stsp __dead static void
8021 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8023 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8025 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8026 6879ba42 2020-10-01 naddy getprogname());
8027 6879ba42 2020-10-01 naddy if (hflag) {
8028 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8029 6879ba42 2020-10-01 naddy list_commands(fp);
8031 6879ba42 2020-10-01 naddy exit(status);
8034 c2301be8 2018-04-30 stsp static char **
8035 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8037 ee85c5e8 2020-02-29 stsp va_list ap;
8038 c2301be8 2018-04-30 stsp char **argv;
8041 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8043 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8044 c2301be8 2018-04-30 stsp if (argv == NULL)
8045 c2301be8 2018-04-30 stsp err(1, "calloc");
8046 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8047 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8048 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8049 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8052 ee85c5e8 2020-02-29 stsp va_end(ap);
8053 c2301be8 2018-04-30 stsp return argv;
8057 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8058 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8059 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8060 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8061 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8063 ee85c5e8 2020-02-29 stsp static const struct got_error *
8064 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8066 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8067 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8068 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8069 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8070 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8071 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8072 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8073 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8074 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8076 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8077 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8078 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8080 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8081 7cd52833 2022-06-23 thomas if (error != NULL)
8082 7cd52833 2022-06-23 thomas goto done;
8084 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8085 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8086 ee85c5e8 2020-02-29 stsp goto done;
8088 ee85c5e8 2020-02-29 stsp if (worktree)
8089 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8091 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8092 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8093 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8094 ee85c5e8 2020-02-29 stsp goto done;
8097 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8098 ee85c5e8 2020-02-29 stsp if (error != NULL)
8099 ee85c5e8 2020-02-29 stsp goto done;
8101 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8102 ee85c5e8 2020-02-29 stsp repo, worktree);
8103 ee85c5e8 2020-02-29 stsp if (error)
8104 ee85c5e8 2020-02-29 stsp goto done;
8106 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8107 84de9106 2020-12-26 stsp if (error)
8108 84de9106 2020-12-26 stsp goto done;
8109 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8110 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8111 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8112 ee85c5e8 2020-02-29 stsp if (error)
8113 ee85c5e8 2020-02-29 stsp goto done;
8115 ee85c5e8 2020-02-29 stsp if (worktree) {
8116 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8117 ee85c5e8 2020-02-29 stsp worktree = NULL;
8120 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8121 945f9229 2022-04-16 thomas if (error)
8122 945f9229 2022-04-16 thomas goto done;
8124 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8125 ee85c5e8 2020-02-29 stsp if (error) {
8126 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8127 ee85c5e8 2020-02-29 stsp goto done;
8128 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8129 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8130 6879ba42 2020-10-01 naddy usage(1, 1);
8131 ee85c5e8 2020-02-29 stsp /* not reached */
8134 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8135 1d0f4054 2021-06-17 stsp if (error == NULL)
8136 1d0f4054 2021-06-17 stsp error = close_err;
8137 ee85c5e8 2020-02-29 stsp repo = NULL;
8139 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8140 ee85c5e8 2020-02-29 stsp if (error)
8141 ee85c5e8 2020-02-29 stsp goto done;
8143 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8145 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8146 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8148 1d0f4054 2021-06-17 stsp if (repo) {
8149 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8150 1d0f4054 2021-06-17 stsp if (error == NULL)
8151 1d0f4054 2021-06-17 stsp error = close_err;
8153 945f9229 2022-04-16 thomas if (commit)
8154 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8155 ee85c5e8 2020-02-29 stsp if (worktree)
8156 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8157 7cd52833 2022-06-23 thomas if (pack_fds) {
8158 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8159 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8160 7cd52833 2022-06-23 thomas if (error == NULL)
8161 7cd52833 2022-06-23 thomas error = pack_err;
8164 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8165 ee85c5e8 2020-02-29 stsp free(commit_id);
8166 ee85c5e8 2020-02-29 stsp free(cwd);
8167 ee85c5e8 2020-02-29 stsp free(repo_path);
8168 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8169 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8171 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8172 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8173 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8175 87670572 2020-12-26 naddy tog_free_refs();
8176 ee85c5e8 2020-02-29 stsp return error;
8180 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8182 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8183 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8184 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8185 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8186 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8187 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8188 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8190 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8192 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8194 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8195 9f7d7167 2018-04-29 stsp switch (ch) {
8197 9f7d7167 2018-04-29 stsp hflag = 1;
8200 53ccebc2 2019-07-30 stsp Vflag = 1;
8203 6879ba42 2020-10-01 naddy usage(hflag, 1);
8204 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8208 9f7d7167 2018-04-29 stsp argc -= optind;
8209 9f7d7167 2018-04-29 stsp argv += optind;
8210 9814e6a3 2020-09-27 naddy optind = 1;
8211 c2301be8 2018-04-30 stsp optreset = 1;
8213 53ccebc2 2019-07-30 stsp if (Vflag) {
8214 53ccebc2 2019-07-30 stsp got_version_print_str();
8215 6879ba42 2020-10-01 naddy return 0;
8218 4010e238 2020-12-04 stsp #ifndef PROFILE
8219 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8220 4010e238 2020-12-04 stsp NULL) == -1)
8221 4010e238 2020-12-04 stsp err(1, "pledge");
8224 c2301be8 2018-04-30 stsp if (argc == 0) {
8225 f29d3e89 2018-06-23 stsp if (hflag)
8226 6879ba42 2020-10-01 naddy usage(hflag, 0);
8227 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8228 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8230 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8234 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8235 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8236 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8237 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8238 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8244 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8245 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8246 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8247 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8248 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8249 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8252 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8253 ee85c5e8 2020-02-29 stsp if (argc != 1)
8254 6879ba42 2020-10-01 naddy usage(0, 1);
8255 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8256 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8258 ee85c5e8 2020-02-29 stsp if (hflag)
8259 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8261 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8265 b46c1e04 2020-09-20 naddy putchar('\n');
8266 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8268 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8269 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8270 a2f4a359 2020-02-28 stsp free(cmd_argv);
8273 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8274 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);