2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
172 51a10b52 2020-12-26 stsp const struct got_error *err;
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
178 51a10b52 2020-12-26 stsp return err;
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
221 11b20872 2019-11-08 stsp return err;
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
231 11b20872 2019-11-08 stsp struct tog_color *tc;
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
251 11b20872 2019-11-08 stsp return NULL;
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
322 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
323 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
324 3dbaef42 2020-11-24 stsp const char *label1, *label2;
325 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
326 19a6a6b5 2022-07-01 thomas int fd1, fd2;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 bddb1296 2019-11-08 stsp struct tog_colors colors;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey off_t *line_offsets;
337 f44b1f58 2020-02-02 tracey int matched_line;
338 f44b1f58 2020-02-02 tracey int selected_line;
340 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
341 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
344 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
347 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
348 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
349 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
350 1a76625f 2018-10-22 stsp int commits_needed;
351 fb280deb 2021-08-30 stsp int load_all;
352 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
353 1a76625f 2018-10-22 stsp struct commit_queue *commits;
354 1a76625f 2018-10-22 stsp const char *in_repo_path;
355 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
356 1a76625f 2018-10-22 stsp struct got_repository *repo;
357 1af5eddf 2022-06-23 thomas int *pack_fds;
358 1a76625f 2018-10-22 stsp int log_complete;
359 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
361 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
362 13add988 2019-10-15 stsp int *searching;
363 13add988 2019-10-15 stsp int *search_next_done;
364 13add988 2019-10-15 stsp regex_t *regex;
367 1a76625f 2018-10-22 stsp struct tog_log_view_state {
368 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
371 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
372 b01e7d3b 2018-08-04 stsp int selected;
373 b01e7d3b 2018-08-04 stsp char *in_repo_path;
374 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
375 b672a97a 2020-01-27 stsp int log_branches;
376 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
377 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
378 1a76625f 2018-10-22 stsp sig_atomic_t quit;
379 1a76625f 2018-10-22 stsp pthread_t thread;
380 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
381 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
382 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
383 11b20872 2019-11-08 stsp struct tog_colors colors;
384 f69c5a46 2022-07-19 thomas int use_committer;
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
394 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
395 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
396 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
397 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
400 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
401 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
403 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
404 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
405 e9424729 2018-08-04 stsp int nlines;
407 e9424729 2018-08-04 stsp struct tog_view *view;
408 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
412 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
413 e9424729 2018-08-04 stsp const char *path;
414 e9424729 2018-08-04 stsp struct got_repository *repo;
415 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
416 e9424729 2018-08-04 stsp int *complete;
417 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
418 fc06ba56 2019-08-22 stsp void *cancel_arg;
421 e9424729 2018-08-04 stsp struct tog_blame {
423 be659d10 2020-11-18 stsp off_t filesize;
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
425 6fcac457 2018-11-19 stsp int nlines;
426 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
427 e9424729 2018-08-04 stsp pthread_t thread;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
429 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
430 e9424729 2018-08-04 stsp const char *path;
431 7cd52833 2022-06-23 thomas int *pack_fds;
434 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
435 7cbe629d 2018-08-04 stsp int first_displayed_line;
436 7cbe629d 2018-08-04 stsp int last_displayed_line;
437 7cbe629d 2018-08-04 stsp int selected_line;
438 4fc71f3b 2022-07-12 thomas int last_diffed_line;
439 7cbe629d 2018-08-04 stsp int blame_complete;
442 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
443 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
444 e5a0f69f 2018-08-18 stsp char *path;
445 7cbe629d 2018-08-04 stsp struct got_repository *repo;
446 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
447 e9424729 2018-08-04 stsp struct tog_blame blame;
448 6c4c42e0 2019-06-24 stsp int matched_line;
449 11b20872 2019-11-08 stsp struct tog_colors colors;
452 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
453 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 ad80ab7b 2018-08-04 stsp int selected;
460 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
462 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
463 ad80ab7b 2018-08-04 stsp char *tree_label;
464 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
470 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
471 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
473 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
474 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
475 6458efa5 2020-11-24 stsp struct tog_colors colors;
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
479 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
480 6458efa5 2020-11-24 stsp struct got_reference *ref;
484 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
486 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
487 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
491 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 6458efa5 2020-11-24 stsp struct got_repository *repo;
493 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
494 bddb1296 2019-11-08 stsp struct tog_colors colors;
498 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
500 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
501 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
502 669b5ffa 2018-10-07 stsp * there is enough screen estate.
504 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
505 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
507 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
508 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
509 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
511 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
512 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
514 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
516 cc3c9aac 2018-08-01 stsp struct tog_view {
517 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
518 26ed57b2 2018-05-19 stsp WINDOW *window;
519 26ed57b2 2018-05-19 stsp PANEL *panel;
520 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
522 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
523 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
524 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
526 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
527 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
529 669b5ffa 2018-10-07 stsp struct tog_view *parent;
530 669b5ffa 2018-10-07 stsp struct tog_view *child;
533 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
534 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
535 e78dc838 2020-12-04 stsp * between parent and child.
536 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
537 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
538 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
539 e78dc838 2020-12-04 stsp * situations.
541 e78dc838 2020-12-04 stsp int focus_child;
543 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
544 5dc9f4bc 2018-08-04 stsp /* type-specific state */
545 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
547 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
548 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
549 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
550 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
551 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
554 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
556 e78dc838 2020-12-04 stsp struct tog_view *, int);
557 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
558 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
559 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
561 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
563 c0c4acc8 2021-01-24 stsp int search_started;
564 60493ae3 2019-06-20 stsp int searching;
565 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
567 60493ae3 2019-06-20 stsp int search_next_done;
568 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
570 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
571 1803e47f 2019-06-22 stsp regex_t regex;
572 41605754 2020-11-12 stsp regmatch_t regmatch;
575 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
576 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
577 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
578 78756c87 2020-11-24 stsp struct got_repository *);
579 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
584 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
587 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
588 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
589 78756c87 2020-11-24 stsp const char *, const char *, int);
590 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
591 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
592 e78dc838 2020-12-04 stsp struct tog_view *, int);
593 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
594 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
595 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
598 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
599 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
600 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
601 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
602 e78dc838 2020-12-04 stsp struct tog_view *, int);
603 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
604 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
605 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
608 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
609 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
610 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
611 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
612 e78dc838 2020-12-04 stsp struct tog_view *, int);
613 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
614 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
617 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
618 6458efa5 2020-11-24 stsp struct got_repository *);
619 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
621 e78dc838 2020-12-04 stsp struct tog_view *, int);
622 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
623 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
626 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
627 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
628 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
629 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
632 25791caa 2018-10-24 stsp static void
633 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
635 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
638 83baff54 2019-08-12 stsp static void
639 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
641 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
644 61266923 2020-01-14 stsp static void
645 61266923 2020-01-14 stsp tog_sigcont(int signo)
647 61266923 2020-01-14 stsp tog_sigcont_received = 1;
650 296152d1 2022-05-31 thomas static void
651 296152d1 2022-05-31 thomas tog_sigint(int signo)
653 296152d1 2022-05-31 thomas tog_sigint_received = 1;
656 296152d1 2022-05-31 thomas static void
657 296152d1 2022-05-31 thomas tog_sigterm(int signo)
659 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
662 296152d1 2022-05-31 thomas static int
663 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
665 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
666 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
669 e5a0f69f 2018-08-18 stsp static const struct got_error *
670 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
672 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
674 669b5ffa 2018-10-07 stsp if (view->child) {
675 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
676 669b5ffa 2018-10-07 stsp view->child = NULL;
678 e5a0f69f 2018-08-18 stsp if (view->close)
679 e5a0f69f 2018-08-18 stsp err = view->close(view);
680 ea5e7bb5 2018-08-01 stsp if (view->panel)
681 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
682 ea5e7bb5 2018-08-01 stsp if (view->window)
683 ea5e7bb5 2018-08-01 stsp delwin(view->window);
684 ea5e7bb5 2018-08-01 stsp free(view);
685 f2d749db 2022-07-12 thomas return err ? err : child_err;
688 ea5e7bb5 2018-08-01 stsp static struct tog_view *
689 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
690 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
692 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
694 ea5e7bb5 2018-08-01 stsp if (view == NULL)
695 ea5e7bb5 2018-08-01 stsp return NULL;
697 d6b05b5b 2018-08-04 stsp view->type = type;
698 f7d12f7e 2018-08-01 stsp view->lines = LINES;
699 f7d12f7e 2018-08-01 stsp view->cols = COLS;
700 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
701 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
702 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
703 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
704 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
705 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
706 96a765a8 2018-08-04 stsp view_close(view);
707 ea5e7bb5 2018-08-01 stsp return NULL;
709 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
710 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
711 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
712 96a765a8 2018-08-04 stsp view_close(view);
713 ea5e7bb5 2018-08-01 stsp return NULL;
716 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
717 ea5e7bb5 2018-08-01 stsp return view;
721 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
723 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
725 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
728 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
729 a5d43cac 2022-07-01 thomas static int
730 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
732 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
735 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
737 5c60c32a 2018-10-18 stsp static const struct got_error *
738 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
740 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
742 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
743 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
744 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
746 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
747 a5d43cac 2022-07-01 thomas view->begin_x = 0;
748 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
749 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
750 53d2bdd3 2022-07-10 thomas view->cols > 119)
751 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
753 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
754 a5d43cac 2022-07-01 thomas view->begin_y = 0;
756 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
757 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
758 5c60c32a 2018-10-18 stsp view->lines = LINES;
759 5c60c32a 2018-10-18 stsp view->cols = COLS;
760 5c60c32a 2018-10-18 stsp err = view_resize(view);
762 5c60c32a 2018-10-18 stsp return err;
764 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
765 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
767 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
768 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
770 5c60c32a 2018-10-18 stsp return NULL;
773 5c60c32a 2018-10-18 stsp static const struct got_error *
774 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
776 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
778 5c60c32a 2018-10-18 stsp view->begin_x = 0;
779 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
780 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
781 5c60c32a 2018-10-18 stsp view->ncols = COLS;
782 5c60c32a 2018-10-18 stsp view->lines = LINES;
783 5c60c32a 2018-10-18 stsp view->cols = COLS;
784 5c60c32a 2018-10-18 stsp err = view_resize(view);
786 5c60c32a 2018-10-18 stsp return err;
788 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
789 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
791 5c60c32a 2018-10-18 stsp return NULL;
795 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
797 5c60c32a 2018-10-18 stsp return view->parent == NULL;
800 b65b3ea0 2022-06-23 thomas static int
801 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
803 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
806 e44940c3 2022-07-01 thomas static int
807 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
809 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
812 444d5325 2022-07-03 thomas static int
813 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
815 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
816 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
819 a5d43cac 2022-07-01 thomas static void
820 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
822 a5d43cac 2022-07-01 thomas PANEL *panel;
823 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
825 a5d43cac 2022-07-01 thomas if (view->parent)
826 a5d43cac 2022-07-01 thomas return view_border(view->parent);
828 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
829 a5d43cac 2022-07-01 thomas if (panel == NULL)
832 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
833 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
834 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
835 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
836 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
838 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
839 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
842 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
843 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
844 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
846 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
848 4d8c2215 2018-08-19 stsp static const struct got_error *
849 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
851 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
852 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
854 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
856 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
857 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
859 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
860 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
861 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
863 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
865 4918811f 2022-07-01 thomas if (view->child) {
866 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
868 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
869 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
870 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
871 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
872 40236d76 2022-06-23 thomas ncols = COLS;
874 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
875 5c60c32a 2018-10-18 stsp if (view->child->focussed)
876 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
878 5c60c32a 2018-10-18 stsp show_panel(view->panel);
880 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
882 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
883 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
886 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
887 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
888 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
890 a5d43cac 2022-07-01 thomas if (hs) {
891 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
893 a5d43cac 2022-07-01 thomas return err;
894 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
895 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
897 a5d43cac 2022-07-01 thomas return err;
899 a5d43cac 2022-07-01 thomas view_border(view);
900 a5d43cac 2022-07-01 thomas update_panels();
901 a5d43cac 2022-07-01 thomas doupdate();
902 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
903 a5d43cac 2022-07-01 thomas nlines = view->nlines;
905 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
906 40236d76 2022-06-23 thomas ncols = COLS;
908 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
909 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
911 ea0bff04 2022-07-19 thomas return err;
914 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
915 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
916 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
917 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
918 40236d76 2022-06-23 thomas wclear(view->window);
920 40236d76 2022-06-23 thomas view->nlines = nlines;
921 40236d76 2022-06-23 thomas view->ncols = ncols;
922 40236d76 2022-06-23 thomas view->lines = LINES;
923 40236d76 2022-06-23 thomas view->cols = COLS;
925 5c60c32a 2018-10-18 stsp return NULL;
928 ea0bff04 2022-07-19 thomas static const struct got_error *
929 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
931 ea0bff04 2022-07-19 thomas struct tog_log_view_state *s = &view->state.log;
932 ea0bff04 2022-07-19 thomas const struct got_error *err = NULL;
933 ea0bff04 2022-07-19 thomas int n = s->selected_entry->idx + view->lines - s->selected;
936 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
937 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
939 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
940 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
941 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
944 ea0bff04 2022-07-19 thomas return err;
947 97cb21cd 2022-07-12 thomas static void
948 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
950 97cb21cd 2022-07-12 thomas if (n == 0)
953 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
954 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
955 97cb21cd 2022-07-12 thomas view->parent->offset += n;
957 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
958 97cb21cd 2022-07-12 thomas } else if (view->offset) {
959 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
960 97cb21cd 2022-07-12 thomas view->offset -= n;
962 97cb21cd 2022-07-12 thomas view->offset = 0;
966 53d2bdd3 2022-07-10 thomas static const struct got_error *
967 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
969 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
970 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
972 53d2bdd3 2022-07-10 thomas if (view->parent)
973 53d2bdd3 2022-07-10 thomas v = view->parent;
975 53d2bdd3 2022-07-10 thomas v = view;
977 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
978 53d2bdd3 2022-07-10 thomas return NULL;
980 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
982 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 ae98518f 2022-07-12 thomas int y = v->child->begin_y;
985 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
986 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
987 53d2bdd3 2022-07-10 thomas if (view->parent)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
990 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
991 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
992 53d2bdd3 2022-07-10 thomas view->count = 0;
993 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
994 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
995 53d2bdd3 2022-07-10 thomas view->count = 0;
996 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
998 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
999 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1000 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1001 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1002 53d2bdd3 2022-07-10 thomas if (err)
1003 53d2bdd3 2022-07-10 thomas return err;
1004 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1005 fe731b51 2022-07-14 thomas if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1006 ae98518f 2022-07-12 thomas v->child->nscrolled = y - v->child->begin_y;
1007 fe731b51 2022-07-14 thomas else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1008 fe731b51 2022-07-14 thomas v->nscrolled = v->child->begin_y - y;
1009 53d2bdd3 2022-07-10 thomas } else {
1010 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1012 53d2bdd3 2022-07-10 thomas if (view->parent)
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1015 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1016 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1017 53d2bdd3 2022-07-10 thomas view->count = 0;
1018 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1019 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1020 53d2bdd3 2022-07-10 thomas view->count = 0;
1021 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1023 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1026 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1027 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1028 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1029 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1031 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1032 53d2bdd3 2022-07-10 thomas if (err)
1033 53d2bdd3 2022-07-10 thomas return err;
1034 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1035 53d2bdd3 2022-07-10 thomas if (err)
1036 53d2bdd3 2022-07-10 thomas return err;
1038 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1039 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1040 53d2bdd3 2022-07-10 thomas if (err)
1041 53d2bdd3 2022-07-10 thomas return err;
1044 fe731b51 2022-07-14 thomas if (v->nscrolled)
1045 53d2bdd3 2022-07-10 thomas err = request_log_commits(v);
1046 fe731b51 2022-07-14 thomas else if (v->child->nscrolled)
1047 53d2bdd3 2022-07-10 thomas err = request_log_commits(v->child);
1049 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1051 53d2bdd3 2022-07-10 thomas return err;
1054 53d2bdd3 2022-07-10 thomas static void
1055 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1057 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1059 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1060 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1063 669b5ffa 2018-10-07 stsp static const struct got_error *
1064 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1066 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1068 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1069 669b5ffa 2018-10-07 stsp return NULL;
1071 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1072 669b5ffa 2018-10-07 stsp view->child = NULL;
1073 669b5ffa 2018-10-07 stsp return err;
1076 40236d76 2022-06-23 thomas static const struct got_error *
1077 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1079 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1081 669b5ffa 2018-10-07 stsp view->child = child;
1082 669b5ffa 2018-10-07 stsp child->parent = view;
1084 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1085 53d2bdd3 2022-07-10 thomas if (err)
1086 53d2bdd3 2022-07-10 thomas return err;
1088 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1089 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1091 53d2bdd3 2022-07-10 thomas return err;
1094 34bc9ec9 2019-02-22 stsp static void
1095 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1097 25791caa 2018-10-24 stsp int cols, lines;
1098 25791caa 2018-10-24 stsp struct winsize size;
1100 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1101 25791caa 2018-10-24 stsp cols = 80; /* Default */
1102 25791caa 2018-10-24 stsp lines = 24;
1104 25791caa 2018-10-24 stsp cols = size.ws_col;
1105 25791caa 2018-10-24 stsp lines = size.ws_row;
1107 25791caa 2018-10-24 stsp resize_term(lines, cols);
1110 2b49a8ae 2019-06-22 stsp static const struct got_error *
1111 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1113 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1114 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1115 2b49a8ae 2019-06-22 stsp char pattern[1024];
1118 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1119 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1120 c0c4acc8 2021-01-24 stsp view->searching = 0;
1121 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1123 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1125 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1126 2b49a8ae 2019-06-22 stsp return NULL;
1128 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1129 a5d43cac 2022-07-01 thomas v = view->child;
1131 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1132 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1134 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1135 2b49a8ae 2019-06-22 stsp nocbreak();
1137 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1138 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1141 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1142 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1143 2b49a8ae 2019-06-22 stsp return NULL;
1145 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1146 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1147 7c32bd05 2019-06-22 stsp if (err) {
1148 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1149 7c32bd05 2019-06-22 stsp return err;
1151 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1152 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1153 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1154 2b49a8ae 2019-06-22 stsp view->search_next(view);
1157 2b49a8ae 2019-06-22 stsp return NULL;
1160 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1161 64486692 2022-07-07 thomas static const struct got_error *
1162 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1164 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1165 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1167 64486692 2022-07-07 thomas if (view->parent)
1168 64486692 2022-07-07 thomas v = view->parent;
1170 64486692 2022-07-07 thomas v = view;
1172 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1175 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1177 ddbc4d37 2022-07-12 thomas if (!v->child)
1178 ddbc4d37 2022-07-12 thomas return NULL;
1179 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1180 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1182 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1183 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1184 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1185 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1186 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1189 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1190 64486692 2022-07-07 thomas v->ncols = COLS;
1191 64486692 2022-07-07 thomas v->child->ncols = COLS;
1192 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1194 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1195 64486692 2022-07-07 thomas if (err)
1196 64486692 2022-07-07 thomas return err;
1198 64486692 2022-07-07 thomas v->child->mode = v->mode;
1199 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1200 64486692 2022-07-07 thomas v->focus_child = 1;
1202 64486692 2022-07-07 thomas err = view_fullscreen(v);
1203 64486692 2022-07-07 thomas if (err)
1204 64486692 2022-07-07 thomas return err;
1205 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1206 64486692 2022-07-07 thomas if (err)
1207 64486692 2022-07-07 thomas return err;
1209 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1210 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1211 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1212 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1213 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1214 ddbc4d37 2022-07-12 thomas } else {
1215 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1216 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1218 ae98518f 2022-07-12 thomas if (v->type == TOG_VIEW_LOG && v->nscrolled)
1219 ddbc4d37 2022-07-12 thomas err = request_log_commits(v);
1220 ae98518f 2022-07-12 thomas else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1221 ddbc4d37 2022-07-12 thomas err = request_log_commits(v->child);
1223 64486692 2022-07-07 thomas return err;
1227 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1228 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1230 07b0611c 2022-06-23 thomas static int
1231 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1233 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1234 a5d43cac 2022-07-01 thomas int x, n = 0;
1236 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1237 a5d43cac 2022-07-01 thomas v = view->child;
1238 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1239 a5d43cac 2022-07-01 thomas v = view->parent;
1241 07b0611c 2022-06-23 thomas view->count = 0;
1242 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1243 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1244 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1245 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1248 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1249 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1250 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1251 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1255 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1256 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1258 07b0611c 2022-06-23 thomas if (n >= 9999999)
1259 07b0611c 2022-06-23 thomas n = 9999999;
1261 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1262 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1264 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1265 07b0611c 2022-06-23 thomas view->count = n;
1267 07b0611c 2022-06-23 thomas return c;
1270 0cf4efb1 2018-09-29 stsp static const struct got_error *
1271 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1272 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1274 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1275 669b5ffa 2018-10-07 stsp struct tog_view *v;
1276 1a76625f 2018-10-22 stsp int ch, errcode;
1278 e5a0f69f 2018-08-18 stsp *new = NULL;
1280 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1281 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1282 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1283 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1284 07b0611c 2022-06-23 thomas view->count = 0;
1287 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1288 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1289 82954512 2020-02-03 stsp if (errcode)
1290 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1291 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1292 3da8ef85 2021-09-21 stsp sched_yield();
1293 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1294 82954512 2020-02-03 stsp if (errcode)
1295 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1296 82954512 2020-02-03 stsp "pthread_mutex_lock");
1297 60493ae3 2019-06-20 stsp view->search_next(view);
1298 60493ae3 2019-06-20 stsp return NULL;
1301 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1302 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1303 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1304 1a76625f 2018-10-22 stsp if (errcode)
1305 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1306 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1307 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1308 634cb454 2022-07-03 thomas cbreak();
1309 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1310 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1311 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1312 634cb454 2022-07-03 thomas view->count = 0;
1314 634cb454 2022-07-03 thomas ch = view->ch;
1315 634cb454 2022-07-03 thomas } else {
1316 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1317 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1318 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1320 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1321 1a76625f 2018-10-22 stsp if (errcode)
1322 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1323 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1325 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1326 25791caa 2018-10-24 stsp tog_resizeterm();
1327 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1328 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1329 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1330 25791caa 2018-10-24 stsp err = view_resize(v);
1332 25791caa 2018-10-24 stsp return err;
1333 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1335 25791caa 2018-10-24 stsp return err;
1336 cdfcfb03 2020-12-06 stsp if (v->child) {
1337 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1339 cdfcfb03 2020-12-06 stsp return err;
1340 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1341 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1343 cdfcfb03 2020-12-06 stsp return err;
1344 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1345 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1346 53d2bdd3 2022-07-10 thomas if (err)
1347 53d2bdd3 2022-07-10 thomas return err;
1353 e5a0f69f 2018-08-18 stsp switch (ch) {
1355 07b0611c 2022-06-23 thomas view->count = 0;
1356 1e37a5c2 2019-05-12 jcs if (view->child) {
1357 e78dc838 2020-12-04 stsp view->focussed = 0;
1358 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1359 e78dc838 2020-12-04 stsp view->focus_child = 1;
1360 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1361 e78dc838 2020-12-04 stsp view->focussed = 0;
1362 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1363 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1364 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1365 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1366 a5d43cac 2022-07-01 thomas view->parent->type == TOG_VIEW_LOG) {
1367 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1368 a5d43cac 2022-07-01 thomas if (err)
1369 a5d43cac 2022-07-01 thomas return err;
1371 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1372 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1373 a5d43cac 2022-07-01 thomas if (err)
1374 a5d43cac 2022-07-01 thomas return err;
1379 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1380 a5d43cac 2022-07-01 thomas if (view->parent->type == TOG_VIEW_LOG) {
1381 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1382 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1383 a5d43cac 2022-07-01 thomas if (err)
1386 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1388 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1389 9970f7fc 2020-12-03 stsp view->dying = 1;
1394 1c5e5faa 2022-06-23 thomas case 'F':
1395 07b0611c 2022-06-23 thomas view->count = 0;
1396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1397 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1399 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1400 e78dc838 2020-12-04 stsp view->focussed = 0;
1401 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1402 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1403 53d2bdd3 2022-07-10 thomas } else {
1404 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1405 53d2bdd3 2022-07-10 thomas if (!err)
1406 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1410 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1411 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1413 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1414 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1415 e78dc838 2020-12-04 stsp view->focussed = 1;
1416 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1418 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1419 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1420 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1421 53d2bdd3 2022-07-10 thomas if (!err)
1422 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1426 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1428 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG) {
1431 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
1432 a5d43cac 2022-07-01 thomas if (err)
1435 a5d43cac 2022-07-01 thomas if (view->parent)
1436 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1437 a5d43cac 2022-07-01 thomas if (!err)
1438 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1440 64486692 2022-07-07 thomas case 'S':
1441 53d2bdd3 2022-07-10 thomas view->count = 0;
1442 64486692 2022-07-07 thomas err = switch_split(view);
1444 53d2bdd3 2022-07-10 thomas case '-':
1445 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1447 53d2bdd3 2022-07-10 thomas case '+':
1448 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1450 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1453 07b0611c 2022-06-23 thomas view->count = 0;
1454 60493ae3 2019-06-20 stsp if (view->search_start)
1455 2b49a8ae 2019-06-22 stsp view_search_start(view);
1457 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1461 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1462 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1463 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1464 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1465 60493ae3 2019-06-20 stsp view->search_next(view);
1467 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1469 adf4c9e0 2022-07-03 thomas case 'A':
1470 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1471 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1473 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1474 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1475 adf4c9e0 2022-07-03 thomas if (v->reset) {
1476 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1477 adf4c9e0 2022-07-03 thomas if (err)
1478 adf4c9e0 2022-07-03 thomas return err;
1480 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1481 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1482 adf4c9e0 2022-07-03 thomas if (err)
1483 adf4c9e0 2022-07-03 thomas return err;
1488 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1492 e5a0f69f 2018-08-18 stsp return err;
1495 ef20f542 2022-06-26 thomas static int
1496 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1498 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1499 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1501 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1503 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1506 669b5ffa 2018-10-07 stsp return view->focussed;
1509 bcbd79e2 2018-08-19 stsp static const struct got_error *
1510 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1512 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1513 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1514 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1515 64486692 2022-07-07 thomas char *mode;
1516 fd823528 2018-10-22 stsp int fast_refresh = 10;
1517 1a76625f 2018-10-22 stsp int done = 0, errcode;
1519 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1520 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1521 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1523 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1525 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1526 1a76625f 2018-10-22 stsp if (errcode)
1527 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1529 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1530 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1532 1004088d 2018-09-29 stsp view->focussed = 1;
1533 878940b7 2018-09-29 stsp err = view->show(view);
1535 0cf4efb1 2018-09-29 stsp return err;
1536 0cf4efb1 2018-09-29 stsp update_panels();
1537 0cf4efb1 2018-09-29 stsp doupdate();
1538 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1539 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1540 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1541 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1542 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1544 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1547 9970f7fc 2020-12-03 stsp if (view->dying) {
1548 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1550 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1551 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1553 e78dc838 2020-12-04 stsp else if (view->parent)
1554 669b5ffa 2018-10-07 stsp prev = view->parent;
1556 e78dc838 2020-12-04 stsp if (view->parent) {
1557 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1558 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1559 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1560 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1561 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1562 40236d76 2022-06-23 thomas if (err)
1564 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1565 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1567 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1569 9970f7fc 2020-12-03 stsp err = view_close(view);
1571 e5a0f69f 2018-08-18 stsp goto done;
1573 e78dc838 2020-12-04 stsp view = NULL;
1574 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1575 e78dc838 2020-12-04 stsp if (v->focussed)
1578 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1579 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1581 e78dc838 2020-12-04 stsp view = prev;
1582 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1583 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1584 e78dc838 2020-12-04 stsp tog_view_list_head);
1586 e78dc838 2020-12-04 stsp if (view) {
1587 e78dc838 2020-12-04 stsp if (view->focus_child) {
1588 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1589 e78dc838 2020-12-04 stsp view = view->child;
1591 e78dc838 2020-12-04 stsp view->focussed = 1;
1595 bcbd79e2 2018-08-19 stsp if (new_view) {
1596 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1597 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1598 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1599 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1601 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1602 86c66b02 2018-10-18 stsp err = view_close(v);
1604 86c66b02 2018-10-18 stsp goto done;
1607 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1608 fed7eaa8 2018-10-24 stsp view = new_view;
1610 669b5ffa 2018-10-07 stsp if (view) {
1611 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1612 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1613 e78dc838 2020-12-04 stsp view = view->child;
1615 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1616 e78dc838 2020-12-04 stsp view = view->parent;
1618 e78dc838 2020-12-04 stsp show_panel(view->panel);
1619 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1620 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1621 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1622 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1624 1a76625f 2018-10-22 stsp goto done;
1626 669b5ffa 2018-10-07 stsp err = view->show(view);
1628 1a76625f 2018-10-22 stsp goto done;
1629 669b5ffa 2018-10-07 stsp if (view->child) {
1630 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1632 1a76625f 2018-10-22 stsp goto done;
1634 1a76625f 2018-10-22 stsp update_panels();
1635 1a76625f 2018-10-22 stsp doupdate();
1639 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1640 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1641 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1642 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1643 f2d749db 2022-07-12 thomas close_err = view_close(view);
1644 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1645 f2d749db 2022-07-12 thomas err = close_err;
1648 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1649 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1650 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1652 e5a0f69f 2018-08-18 stsp return err;
1655 4ed7e80c 2018-05-20 stsp __dead static void
1656 9f7d7167 2018-04-29 stsp usage_log(void)
1659 c70c5802 2018-08-01 stsp fprintf(stderr,
1660 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1661 9f7d7167 2018-04-29 stsp getprogname());
1665 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1666 80ddbec8 2018-04-29 stsp static const struct got_error *
1667 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1669 00dfcb92 2018-06-11 stsp char *vis = NULL;
1670 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1672 963b370f 2018-05-20 stsp *ws = NULL;
1673 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1674 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1675 00dfcb92 2018-06-11 stsp int vislen;
1676 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1677 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1679 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1680 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1682 00dfcb92 2018-06-11 stsp return err;
1683 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1684 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1685 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1686 a7f50699 2018-06-11 stsp goto done;
1690 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1691 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1693 a7f50699 2018-06-11 stsp goto done;
1696 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1697 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1699 00dfcb92 2018-06-11 stsp free(vis);
1700 963b370f 2018-05-20 stsp if (err) {
1701 963b370f 2018-05-20 stsp free(*ws);
1702 963b370f 2018-05-20 stsp *ws = NULL;
1703 963b370f 2018-05-20 stsp *wlen = 0;
1705 963b370f 2018-05-20 stsp return err;
1708 05171be4 2022-06-23 thomas static const struct got_error *
1709 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1711 05171be4 2022-06-23 thomas char *dst;
1712 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1714 05171be4 2022-06-23 thomas *ptr = NULL;
1715 05171be4 2022-06-23 thomas n = len = strlen(src);
1716 83316834 2022-06-23 thomas dst = malloc(n + 1);
1717 05171be4 2022-06-23 thomas if (dst == NULL)
1718 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1720 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1721 05171be4 2022-06-23 thomas const char c = src[idx];
1723 05171be4 2022-06-23 thomas if (c == '\t') {
1724 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1725 b235ad5d 2022-06-23 thomas char *p;
1727 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1728 83316834 2022-06-23 thomas if (p == NULL) {
1729 83316834 2022-06-23 thomas free(dst);
1730 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1733 83316834 2022-06-23 thomas dst = p;
1734 05171be4 2022-06-23 thomas n += nb;
1735 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1736 05171be4 2022-06-23 thomas sz += nb;
1738 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1742 05171be4 2022-06-23 thomas dst[sz] = '\0';
1743 05171be4 2022-06-23 thomas *ptr = dst;
1744 05171be4 2022-06-23 thomas return NULL;
1748 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1749 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1750 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1751 8d208d34 2022-06-23 thomas * *rcol.
1753 8d208d34 2022-06-23 thomas static int
1754 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1756 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1758 8d208d34 2022-06-23 thomas if (n == 0) {
1759 8d208d34 2022-06-23 thomas *rcol = cols;
1760 8d208d34 2022-06-23 thomas return off;
1763 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1764 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1765 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1767 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1769 8d208d34 2022-06-23 thomas if (width == -1) {
1770 8d208d34 2022-06-23 thomas width = 1;
1771 8d208d34 2022-06-23 thomas wline[i] = L'.';
1774 8d208d34 2022-06-23 thomas if (cols + width > n)
1776 8d208d34 2022-06-23 thomas cols += width;
1779 8d208d34 2022-06-23 thomas *rcol = cols;
1780 8d208d34 2022-06-23 thomas return i;
1784 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1785 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1786 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1788 f91a2b48 2022-06-23 thomas static const struct got_error *
1789 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1790 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1792 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1793 8d208d34 2022-06-23 thomas int cols;
1794 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1795 05171be4 2022-06-23 thomas char *exstr = NULL;
1796 963b370f 2018-05-20 stsp size_t wlen;
1797 8d208d34 2022-06-23 thomas int i, scrollx;
1799 963b370f 2018-05-20 stsp *wlinep = NULL;
1800 b700b5d6 2018-07-10 stsp *widthp = 0;
1802 05171be4 2022-06-23 thomas if (expand) {
1803 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1804 05171be4 2022-06-23 thomas if (err)
1805 05171be4 2022-06-23 thomas return err;
1808 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1809 05171be4 2022-06-23 thomas free(exstr);
1811 963b370f 2018-05-20 stsp return err;
1813 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1815 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1816 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1819 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1820 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1824 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1825 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1827 b700b5d6 2018-07-10 stsp if (widthp)
1828 b700b5d6 2018-07-10 stsp *widthp = cols;
1829 f91a2b48 2022-06-23 thomas if (scrollxp)
1830 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1832 963b370f 2018-05-20 stsp free(wline);
1834 963b370f 2018-05-20 stsp *wlinep = wline;
1835 963b370f 2018-05-20 stsp return err;
1838 8b473291 2019-02-21 stsp static const struct got_error*
1839 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1840 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1842 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1843 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1845 8b473291 2019-02-21 stsp const char *name;
1847 8b473291 2019-02-21 stsp *refs_str = NULL;
1849 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1850 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1851 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1854 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1855 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1857 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1858 8b473291 2019-02-21 stsp name += 5;
1859 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1860 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1862 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1863 8b473291 2019-02-21 stsp name += 6;
1864 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1865 8b473291 2019-02-21 stsp name += 8;
1866 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1867 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1870 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1873 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1874 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1875 5d844a1e 2019-08-13 stsp if (err) {
1876 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1877 48cae60d 2020-09-22 stsp free(ref_id);
1880 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1881 5d844a1e 2019-08-13 stsp err = NULL;
1882 5d844a1e 2019-08-13 stsp tag = NULL;
1885 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1886 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1887 48cae60d 2020-09-22 stsp free(ref_id);
1889 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1890 52b5abe1 2019-08-13 stsp if (cmp != 0)
1892 8b473291 2019-02-21 stsp s = *refs_str;
1893 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1894 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1895 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1897 8b473291 2019-02-21 stsp *refs_str = NULL;
1903 8b473291 2019-02-21 stsp return err;
1906 963b370f 2018-05-20 stsp static const struct got_error *
1907 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1908 27a741e5 2019-09-11 stsp int col_tab_align)
1910 e6b8b890 2020-12-29 naddy char *smallerthan;
1912 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1913 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1914 5813d178 2019-03-09 stsp author = smallerthan + 1;
1915 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1916 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1917 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1920 5813d178 2019-03-09 stsp static const struct got_error *
1921 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1922 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1923 8fdc79fe 2020-12-01 naddy int author_display_cols)
1925 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1926 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1927 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1928 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1929 5813d178 2019-03-09 stsp char *author = NULL;
1930 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1931 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1932 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1933 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1934 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1935 ccb26ccd 2018-11-05 stsp struct tm tm;
1936 45d799e2 2018-12-23 stsp time_t committer_time;
1937 11b20872 2019-11-08 stsp struct tog_color *tc;
1939 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1940 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1941 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1942 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1943 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1945 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1946 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1948 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1949 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1951 11b20872 2019-11-08 stsp wattr_on(view->window,
1952 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1953 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1955 11b20872 2019-11-08 stsp wattr_off(view->window,
1956 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1957 27a741e5 2019-09-11 stsp col = limit;
1958 b39d25c7 2018-07-10 stsp if (col > avail)
1959 b39d25c7 2018-07-10 stsp goto done;
1961 6570a66d 2019-11-08 stsp if (avail >= 120) {
1962 6570a66d 2019-11-08 stsp char *id_str;
1963 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1965 6570a66d 2019-11-08 stsp goto done;
1966 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1968 11b20872 2019-11-08 stsp wattr_on(view->window,
1969 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1970 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1972 11b20872 2019-11-08 stsp wattr_off(view->window,
1973 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1974 6570a66d 2019-11-08 stsp free(id_str);
1976 6570a66d 2019-11-08 stsp if (col > avail)
1977 6570a66d 2019-11-08 stsp goto done;
1980 f69c5a46 2022-07-19 thomas if (s->use_committer)
1981 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
1983 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
1984 5813d178 2019-03-09 stsp if (author == NULL) {
1985 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1986 80ddbec8 2018-04-29 stsp goto done;
1988 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1990 bb737323 2018-05-20 stsp goto done;
1991 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1993 11b20872 2019-11-08 stsp wattr_on(view->window,
1994 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1995 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1997 11b20872 2019-11-08 stsp wattr_off(view->window,
1998 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1999 bb737323 2018-05-20 stsp col += author_width;
2000 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2001 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2003 bb737323 2018-05-20 stsp author_width++;
2005 9c2eaf34 2018-05-20 stsp if (col > avail)
2006 9c2eaf34 2018-05-20 stsp goto done;
2008 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2010 6d9fbc00 2018-04-29 stsp goto done;
2011 bb737323 2018-05-20 stsp logmsg = logmsg0;
2012 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2014 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2015 bb737323 2018-05-20 stsp if (newline)
2016 bb737323 2018-05-20 stsp *newline = '\0';
2017 f91a2b48 2022-06-23 thomas limit = avail - col;
2018 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2019 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2020 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2021 f91a2b48 2022-06-23 thomas limit, col, 1);
2022 331b1a16 2022-06-23 thomas if (err)
2023 331b1a16 2022-06-23 thomas goto done;
2024 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2025 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2026 27a741e5 2019-09-11 stsp while (col < avail) {
2027 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2031 80ddbec8 2018-04-29 stsp free(logmsg0);
2032 bb737323 2018-05-20 stsp free(wlogmsg);
2033 5813d178 2019-03-09 stsp free(author);
2034 bb737323 2018-05-20 stsp free(wauthor);
2035 80ddbec8 2018-04-29 stsp free(line);
2036 80ddbec8 2018-04-29 stsp return err;
2039 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2040 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2041 899d86c2 2018-05-10 stsp struct got_object_id *id)
2043 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2045 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2046 80ddbec8 2018-04-29 stsp if (entry == NULL)
2047 899d86c2 2018-05-10 stsp return NULL;
2049 899d86c2 2018-05-10 stsp entry->id = id;
2050 99db9666 2018-05-07 stsp entry->commit = commit;
2051 899d86c2 2018-05-10 stsp return entry;
2054 99db9666 2018-05-07 stsp static void
2055 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2057 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2059 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2060 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2061 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2062 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2063 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2064 99db9666 2018-05-07 stsp free(entry);
2067 99db9666 2018-05-07 stsp static void
2068 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2070 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2071 99db9666 2018-05-07 stsp pop_commit(commits);
2074 c4972b91 2018-05-07 stsp static const struct got_error *
2075 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2076 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2078 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2079 13add988 2019-10-15 stsp regmatch_t regmatch;
2080 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2082 13add988 2019-10-15 stsp *have_match = 0;
2084 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2086 13add988 2019-10-15 stsp return err;
2088 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2090 13add988 2019-10-15 stsp goto done;
2092 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2093 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2094 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2095 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2096 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2097 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2098 13add988 2019-10-15 stsp *have_match = 1;
2100 13add988 2019-10-15 stsp free(id_str);
2101 13add988 2019-10-15 stsp free(logmsg);
2102 13add988 2019-10-15 stsp return err;
2105 13add988 2019-10-15 stsp static const struct got_error *
2106 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2108 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2111 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2112 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2113 1a76625f 2018-10-22 stsp * while updating the display.
2116 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2117 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2118 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2119 1a76625f 2018-10-22 stsp int errcode;
2121 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2122 4e0d2870 2020-12-07 naddy NULL, NULL);
2123 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2126 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2129 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2130 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2131 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2135 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2136 1a76625f 2018-10-22 stsp if (errcode) {
2137 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2138 13add988 2019-10-15 stsp "pthread_mutex_lock");
2142 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2143 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2144 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2146 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2147 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2148 7c1452c1 2020-03-26 stsp int have_match;
2149 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2152 7c1452c1 2020-03-26 stsp if (have_match)
2153 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2156 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2157 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2158 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2159 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2162 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2164 9ba79e04 2018-06-11 stsp return err;
2167 2b779855 2020-12-05 naddy static void
2168 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2170 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2171 2b779855 2020-12-05 naddy int ncommits = 0;
2173 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2174 2b779855 2020-12-05 naddy while (entry) {
2175 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2176 2b779855 2020-12-05 naddy s->selected_entry = entry;
2179 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2180 2b779855 2020-12-05 naddy ncommits++;
2184 0553a4e3 2018-04-30 stsp static const struct got_error *
2185 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2187 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2188 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2189 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2190 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2191 60493ae3 2019-06-20 stsp int width;
2192 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2193 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2194 8b473291 2019-02-21 stsp char *refs_str = NULL;
2195 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2196 11b20872 2019-11-08 stsp struct tog_color *tc;
2197 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2199 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2200 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2201 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2202 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2204 ecb28ae0 2018-07-16 stsp return err;
2205 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2206 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2207 d2075bf3 2020-12-25 stsp if (refs) {
2208 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2209 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2211 d2075bf3 2020-12-25 stsp goto done;
2215 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2216 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2218 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2219 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2220 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2221 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2222 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2223 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2224 8f4ed634 2020-03-26 stsp goto done;
2227 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2229 f9686aa5 2020-03-27 stsp if (view->searching) {
2230 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2231 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2232 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2233 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2234 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2235 f9686aa5 2020-03-27 stsp search_str = "searching...";
2238 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2239 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2240 f9686aa5 2020-03-27 stsp search_str ? search_str :
2241 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2242 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2243 8f4ed634 2020-03-26 stsp goto done;
2247 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2248 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2249 91198554 2022-06-23 thomas "........................................",
2250 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2251 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2252 1a76625f 2018-10-22 stsp header = NULL;
2253 1a76625f 2018-10-22 stsp goto done;
2255 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2256 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2257 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2258 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2259 1a76625f 2018-10-22 stsp header = NULL;
2260 1a76625f 2018-10-22 stsp goto done;
2262 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2264 1a76625f 2018-10-22 stsp goto done;
2266 2814baeb 2018-08-01 stsp werase(view->window);
2268 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2269 a3404814 2018-09-02 stsp wstandout(view->window);
2270 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2272 11b20872 2019-11-08 stsp wattr_on(view->window,
2273 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2274 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2276 11b20872 2019-11-08 stsp wattr_off(view->window,
2277 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2278 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2279 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2282 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2283 a3404814 2018-09-02 stsp wstandend(view->window);
2284 ecb28ae0 2018-07-16 stsp free(wline);
2285 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2286 1a76625f 2018-10-22 stsp goto done;
2288 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2289 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2290 5813d178 2019-03-09 stsp ncommits = 0;
2291 05171be4 2022-06-23 thomas view->maxx = 0;
2292 5813d178 2019-03-09 stsp while (entry) {
2293 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2294 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2295 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2296 5813d178 2019-03-09 stsp int width;
2297 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2299 f69c5a46 2022-07-19 thomas if (s->use_committer)
2300 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2302 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2303 5813d178 2019-03-09 stsp if (author == NULL) {
2304 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2305 5813d178 2019-03-09 stsp goto done;
2307 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2308 27a741e5 2019-09-11 stsp date_display_cols);
2309 5813d178 2019-03-09 stsp if (author_cols < width)
2310 5813d178 2019-03-09 stsp author_cols = width;
2311 5813d178 2019-03-09 stsp free(wauthor);
2312 5813d178 2019-03-09 stsp free(author);
2313 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2314 05171be4 2022-06-23 thomas if (err)
2315 05171be4 2022-06-23 thomas goto done;
2316 05171be4 2022-06-23 thomas msg = msg0;
2317 05171be4 2022-06-23 thomas while (*msg == '\n')
2319 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2320 331b1a16 2022-06-23 thomas *eol = '\0';
2321 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2322 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2323 331b1a16 2022-06-23 thomas if (err)
2324 331b1a16 2022-06-23 thomas goto done;
2325 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2326 05171be4 2022-06-23 thomas free(msg0);
2327 331b1a16 2022-06-23 thomas free(wmsg);
2328 7ca04879 2019-10-19 stsp ncommits++;
2329 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2332 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2333 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2334 867c6645 2018-07-10 stsp ncommits = 0;
2335 899d86c2 2018-05-10 stsp while (entry) {
2336 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2338 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2339 2814baeb 2018-08-01 stsp wstandout(view->window);
2340 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2341 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2342 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2343 2814baeb 2018-08-01 stsp wstandend(view->window);
2345 60493ae3 2019-06-20 stsp goto done;
2346 0553a4e3 2018-04-30 stsp ncommits++;
2347 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2348 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2351 a5d43cac 2022-07-01 thomas view_border(view);
2353 1a76625f 2018-10-22 stsp free(id_str);
2354 8b473291 2019-02-21 stsp free(refs_str);
2355 1a76625f 2018-10-22 stsp free(ncommits_str);
2356 1a76625f 2018-10-22 stsp free(header);
2357 80ddbec8 2018-04-29 stsp return err;
2360 07b55e75 2018-05-10 stsp static void
2361 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2363 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2364 07b55e75 2018-05-10 stsp int nscrolled = 0;
2366 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2367 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2370 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2371 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2372 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2373 07b55e75 2018-05-10 stsp if (entry) {
2374 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2375 07b55e75 2018-05-10 stsp nscrolled++;
2380 aa075928 2018-05-10 stsp static const struct got_error *
2381 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2383 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2384 5e224a3e 2019-02-22 stsp int errcode;
2386 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2388 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2389 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2390 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2391 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2392 7aafa0d1 2019-02-22 stsp if (errcode)
2393 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2394 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2397 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2398 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2400 7c1452c1 2020-03-26 stsp if (!wait)
2403 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2404 ffe38506 2020-12-01 naddy show_log_view(view);
2405 7c1452c1 2020-03-26 stsp update_panels();
2406 7c1452c1 2020-03-26 stsp doupdate();
2408 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2409 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2410 82954512 2020-02-03 stsp if (errcode)
2411 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2412 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2414 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2415 ffe38506 2020-12-01 naddy show_log_view(view);
2416 7c1452c1 2020-03-26 stsp update_panels();
2417 7c1452c1 2020-03-26 stsp doupdate();
2420 5e224a3e 2019-02-22 stsp return NULL;
2423 a5d43cac 2022-07-01 thomas static const struct got_error *
2424 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2426 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2427 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2429 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2430 fe731b51 2022-07-14 thomas return NULL;
2432 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2433 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2434 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2436 a5d43cac 2022-07-01 thomas return err;
2439 5e224a3e 2019-02-22 stsp static const struct got_error *
2440 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2442 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2443 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2444 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2445 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2447 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2448 5e224a3e 2019-02-22 stsp return NULL;
2450 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2451 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2452 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2454 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2456 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2457 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2459 5e224a3e 2019-02-22 stsp return err;
2463 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2464 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2467 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2468 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2470 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2471 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2473 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2474 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2476 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2477 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2479 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2481 dd0a52c1 2018-05-20 stsp return err;
2484 cd0acaa7 2018-05-20 stsp static const struct got_error *
2485 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2486 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2487 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2489 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2490 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2491 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2493 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2494 15a94983 2018-12-23 stsp if (diff_view == NULL)
2495 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2497 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2498 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2499 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2500 e5a0f69f 2018-08-18 stsp if (err == NULL)
2501 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2502 cd0acaa7 2018-05-20 stsp return err;
2505 80ddbec8 2018-04-29 stsp static const struct got_error *
2506 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2507 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2509 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2511 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2512 941e9f74 2019-05-21 stsp if (parent == NULL)
2513 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2515 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2516 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2517 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2518 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2519 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2520 941e9f74 2019-05-21 stsp s->tree = subtree;
2521 941e9f74 2019-05-21 stsp s->selected = 0;
2522 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2523 941e9f74 2019-05-21 stsp return NULL;
2526 941e9f74 2019-05-21 stsp static const struct got_error *
2527 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2528 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2530 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2531 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2532 941e9f74 2019-05-21 stsp const char *p;
2533 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2535 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2537 941e9f74 2019-05-21 stsp while (*p) {
2538 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2539 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2540 56e0773d 2019-11-28 stsp char *te_name;
2542 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2545 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2546 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2547 941e9f74 2019-05-21 stsp if (slash == NULL)
2548 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2550 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2551 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2552 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2555 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2556 56e0773d 2019-11-28 stsp if (te == NULL) {
2557 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2558 56e0773d 2019-11-28 stsp free(te_name);
2561 56e0773d 2019-11-28 stsp free(te_name);
2562 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2564 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2565 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2567 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2568 941e9f74 2019-05-21 stsp if (slash)
2569 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2571 941e9f74 2019-05-21 stsp subpath = strdup(path);
2572 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2573 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2577 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2582 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2583 941e9f74 2019-05-21 stsp free(tree_id);
2587 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2588 941e9f74 2019-05-21 stsp if (err) {
2589 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2592 941e9f74 2019-05-21 stsp if (slash == NULL)
2594 941e9f74 2019-05-21 stsp free(subpath);
2595 941e9f74 2019-05-21 stsp subpath = NULL;
2596 941e9f74 2019-05-21 stsp p = slash;
2599 941e9f74 2019-05-21 stsp free(subpath);
2600 1a76625f 2018-10-22 stsp return err;
2603 61266923 2020-01-14 stsp static const struct got_error *
2604 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2605 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2606 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2608 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2609 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2610 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2612 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2613 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2614 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2616 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2618 55cccc34 2020-02-20 stsp return err;
2619 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2621 55cccc34 2020-02-20 stsp *new_view = tree_view;
2623 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2624 55cccc34 2020-02-20 stsp return NULL;
2626 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2629 55cccc34 2020-02-20 stsp static const struct got_error *
2630 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2632 61266923 2020-01-14 stsp sigset_t sigset;
2633 61266923 2020-01-14 stsp int errcode;
2635 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2636 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2638 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2639 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2640 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2641 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2642 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2643 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2644 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2645 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2646 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2648 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2649 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2650 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2652 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2653 61266923 2020-01-14 stsp if (errcode)
2654 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2656 61266923 2020-01-14 stsp return NULL;
2659 1a76625f 2018-10-22 stsp static void *
2660 1a76625f 2018-10-22 stsp log_thread(void *arg)
2662 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2663 1a76625f 2018-10-22 stsp int errcode = 0;
2664 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2665 1a76625f 2018-10-22 stsp int done = 0;
2668 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2669 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2671 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2672 f2d749db 2022-07-12 thomas if (errcode) {
2673 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2674 61266923 2020-01-14 stsp return (void *)err;
2677 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2678 f2d749db 2022-07-12 thomas if (err) {
2679 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2680 f2d749db 2022-07-12 thomas goto done;
2683 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2684 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2685 f2d749db 2022-07-12 thomas if (errcode) {
2686 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2687 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2688 f2d749db 2022-07-12 thomas goto done;
2690 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2691 1a76625f 2018-10-22 stsp if (err) {
2692 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2693 f2d749db 2022-07-12 thomas goto done;
2694 1a76625f 2018-10-22 stsp err = NULL;
2696 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2697 1a76625f 2018-10-22 stsp a->commits_needed--;
2699 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2700 3abe8080 2019-04-10 stsp if (errcode) {
2701 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2702 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2703 f2d749db 2022-07-12 thomas goto done;
2704 3abe8080 2019-04-10 stsp } else if (*a->quit)
2706 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2707 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2708 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2709 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2712 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2713 7c1452c1 2020-03-26 stsp if (errcode) {
2714 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2715 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2716 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2717 f2d749db 2022-07-12 thomas goto done;
2721 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2723 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2724 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2725 7c1452c1 2020-03-26 stsp &tog_mutex);
2726 f2d749db 2022-07-12 thomas if (errcode) {
2727 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2728 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2729 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2730 f2d749db 2022-07-12 thomas goto done;
2732 21355643 2020-12-06 stsp if (*a->quit)
2737 3abe8080 2019-04-10 stsp a->log_complete = 1;
2738 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2739 f2d749db 2022-07-12 thomas if (errcode)
2740 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2742 f2d749db 2022-07-12 thomas if (err) {
2743 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2744 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2746 1a76625f 2018-10-22 stsp return (void *)err;
2749 1a76625f 2018-10-22 stsp static const struct got_error *
2750 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2752 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2753 1a76625f 2018-10-22 stsp int errcode;
2755 1a76625f 2018-10-22 stsp if (s->thread) {
2756 1a76625f 2018-10-22 stsp s->quit = 1;
2757 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2758 1a76625f 2018-10-22 stsp if (errcode)
2759 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2760 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2761 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2762 1a76625f 2018-10-22 stsp if (errcode)
2763 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2764 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2765 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2766 1a76625f 2018-10-22 stsp if (errcode)
2767 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2768 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2769 1a76625f 2018-10-22 stsp if (errcode)
2770 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2771 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2772 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2775 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2776 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2777 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2780 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2781 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2782 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2783 1af5eddf 2022-06-23 thomas if (err == NULL)
2784 1af5eddf 2022-06-23 thomas err = pack_err;
2785 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2788 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2789 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2790 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2793 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2796 9343a5fb 2018-06-23 stsp static const struct got_error *
2797 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2799 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2800 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2801 276b94a1 2020-11-13 naddy int errcode;
2803 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2805 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2806 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2807 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2809 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2810 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2811 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2813 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2814 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2815 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2816 1a76625f 2018-10-22 stsp free(s->start_id);
2817 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2818 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2819 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2820 1a76625f 2018-10-22 stsp return err;
2823 1a76625f 2018-10-22 stsp static const struct got_error *
2824 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2826 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2828 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2829 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2830 60493ae3 2019-06-20 stsp return NULL;
2833 60493ae3 2019-06-20 stsp static const struct got_error *
2834 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2836 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2837 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2838 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2840 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2841 f9686aa5 2020-03-27 stsp show_log_view(view);
2842 f9686aa5 2020-03-27 stsp update_panels();
2843 f9686aa5 2020-03-27 stsp doupdate();
2845 96e2b566 2019-07-08 stsp if (s->search_entry) {
2846 13add988 2019-10-15 stsp int errcode, ch;
2847 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2848 13add988 2019-10-15 stsp if (errcode)
2849 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2850 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2851 13add988 2019-10-15 stsp ch = wgetch(view->window);
2852 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2853 13add988 2019-10-15 stsp if (errcode)
2854 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2855 13add988 2019-10-15 stsp "pthread_mutex_lock");
2856 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2857 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2858 678cbce5 2019-07-28 stsp return NULL;
2860 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2861 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2863 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2864 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2865 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2866 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2867 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2870 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2871 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2872 1f4d5162 2022-06-23 thomas * might have changed.
2874 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2875 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2876 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2878 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2879 b74feda6 2022-06-23 thomas } else {
2880 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2881 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2882 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2884 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2885 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2888 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2891 60493ae3 2019-06-20 stsp while (1) {
2892 13add988 2019-10-15 stsp int have_match = 0;
2894 60493ae3 2019-06-20 stsp if (entry == NULL) {
2895 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2896 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2897 f9967bca 2020-03-27 stsp view->search_next_done =
2898 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2899 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2900 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2901 f801134a 2019-06-25 stsp return NULL;
2904 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2905 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2906 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2908 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2909 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2912 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2913 13add988 2019-10-15 stsp &view->regex);
2916 13add988 2019-10-15 stsp if (have_match) {
2917 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2918 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2922 96e2b566 2019-07-08 stsp s->search_entry = entry;
2923 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2924 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2926 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2929 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2930 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2931 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2932 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2934 60493ae3 2019-06-20 stsp return err;
2937 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2938 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2940 60493ae3 2019-06-20 stsp return err;
2945 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2947 60493ae3 2019-06-20 stsp return NULL;
2950 60493ae3 2019-06-20 stsp static const struct got_error *
2951 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2952 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2953 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2955 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2956 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2957 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2958 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2959 1a76625f 2018-10-22 stsp int errcode;
2961 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2962 f135c941 2020-02-20 stsp free(s->in_repo_path);
2963 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2964 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2965 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2968 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2969 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2970 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2972 fb2756b9 2018-08-04 stsp s->repo = repo;
2973 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2974 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2975 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2976 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2977 9cd7cbd1 2020-12-07 stsp goto done;
2980 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2981 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2982 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2983 5036bf37 2018-09-24 stsp goto done;
2985 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2987 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2988 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2989 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2990 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2992 11b20872 2019-11-08 stsp goto done;
2993 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2994 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2995 11b20872 2019-11-08 stsp if (err) {
2996 11b20872 2019-11-08 stsp free_colors(&s->colors);
2997 11b20872 2019-11-08 stsp goto done;
2999 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3000 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3001 11b20872 2019-11-08 stsp if (err) {
3002 11b20872 2019-11-08 stsp free_colors(&s->colors);
3003 11b20872 2019-11-08 stsp goto done;
3007 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3008 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3009 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3010 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3011 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3012 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3014 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3015 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3016 1af5eddf 2022-06-23 thomas if (err)
3017 1af5eddf 2022-06-23 thomas goto done;
3019 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3020 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3021 7cd52833 2022-06-23 thomas if (err)
3022 7cd52833 2022-06-23 thomas goto done;
3023 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3024 b672a97a 2020-01-27 stsp !s->log_branches);
3026 1a76625f 2018-10-22 stsp goto done;
3027 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3028 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3030 c5b78334 2020-01-12 stsp goto done;
3032 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3033 1a76625f 2018-10-22 stsp if (errcode) {
3034 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3035 1a76625f 2018-10-22 stsp goto done;
3037 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3038 7c1452c1 2020-03-26 stsp if (errcode) {
3039 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3040 7c1452c1 2020-03-26 stsp goto done;
3043 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3044 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3045 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3046 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3047 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3048 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3049 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3050 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3051 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3052 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3053 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3054 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3055 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3058 1a76625f 2018-10-22 stsp close_log_view(view);
3059 ba4f502b 2018-08-04 stsp return err;
3062 e5a0f69f 2018-08-18 stsp static const struct got_error *
3063 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3065 f2f6d207 2020-11-24 stsp const struct got_error *err;
3066 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3068 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3069 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3070 2b380cc8 2018-10-24 stsp &s->thread_args);
3071 2b380cc8 2018-10-24 stsp if (errcode)
3072 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3073 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3074 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3076 f2f6d207 2020-11-24 stsp return err;
3080 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3083 b31f89ff 2022-07-01 thomas static void
3084 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3086 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3088 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3089 b31f89ff 2022-07-01 thomas view->count = 0;
3090 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3093 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3094 b31f89ff 2022-07-01 thomas || home)
3095 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3097 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3098 b31f89ff 2022-07-01 thomas --s->selected;
3100 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3102 b31f89ff 2022-07-01 thomas select_commit(s);
3106 b31f89ff 2022-07-01 thomas static const struct got_error *
3107 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3109 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3110 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3111 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3113 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3114 b31f89ff 2022-07-01 thomas if (first == NULL) {
3115 b31f89ff 2022-07-01 thomas view->count = 0;
3116 b31f89ff 2022-07-01 thomas return NULL;
3119 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3120 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3121 b31f89ff 2022-07-01 thomas return NULL;
3123 b31f89ff 2022-07-01 thomas if (!page) {
3124 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3126 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3127 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3128 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3129 b31f89ff 2022-07-01 thomas ++s->selected;
3131 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3132 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3133 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3134 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3135 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3137 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3138 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3139 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3140 b31f89ff 2022-07-01 thomas } else {
3141 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3142 b31f89ff 2022-07-01 thomas if (err)
3143 b31f89ff 2022-07-01 thomas return err;
3144 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3145 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3146 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3149 b31f89ff 2022-07-01 thomas if (err)
3150 b31f89ff 2022-07-01 thomas return err;
3153 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3154 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3156 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3157 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3159 b31f89ff 2022-07-01 thomas select_commit(s);
3161 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3162 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3163 b31f89ff 2022-07-01 thomas view->count = 0;
3165 b31f89ff 2022-07-01 thomas return NULL;
3168 a5d43cac 2022-07-01 thomas static void
3169 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3174 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3175 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3176 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3177 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3178 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3180 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3181 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3182 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3183 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3184 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3185 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3187 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3191 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3192 e5a0f69f 2018-08-18 stsp static const struct got_error *
3193 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3195 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3197 a5d43cac 2022-07-01 thomas view->nlines = y;
3198 64486692 2022-07-07 thomas view->ncols = COLS;
3199 a5d43cac 2022-07-01 thomas err = view_resize(view);
3200 a5d43cac 2022-07-01 thomas if (err)
3201 a5d43cac 2022-07-01 thomas return err;
3203 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3205 a5d43cac 2022-07-01 thomas return err;
3208 a5d43cac 2022-07-01 thomas static const struct got_error *
3209 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3211 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3212 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3213 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3214 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3215 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3216 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3218 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3219 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3220 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3221 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3222 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3223 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3225 b31f89ff 2022-07-01 thomas return err;
3228 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3229 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3230 068ab281 2022-07-01 thomas --eos; /* border */
3232 528dedf3 2021-08-30 stsp switch (ch) {
3234 1e37a5c2 2019-05-12 jcs s->quit = 1;
3236 05171be4 2022-06-23 thomas case '0':
3237 05171be4 2022-06-23 thomas view->x = 0;
3239 05171be4 2022-06-23 thomas case '$':
3240 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3241 07b0611c 2022-06-23 thomas view->count = 0;
3243 05171be4 2022-06-23 thomas case KEY_RIGHT:
3244 05171be4 2022-06-23 thomas case 'l':
3245 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3246 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3248 07b0611c 2022-06-23 thomas view->count = 0;
3250 05171be4 2022-06-23 thomas case KEY_LEFT:
3251 05171be4 2022-06-23 thomas case 'h':
3252 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3253 07b0611c 2022-06-23 thomas if (view->x <= 0)
3254 07b0611c 2022-06-23 thomas view->count = 0;
3257 1e37a5c2 2019-05-12 jcs case KEY_UP:
3260 f7140bf5 2021-10-17 thomas case CTRL('p'):
3261 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3264 912a3f79 2021-08-30 j case KEY_HOME:
3265 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3266 07b0611c 2022-06-23 thomas view->count = 0;
3268 70f17a53 2022-06-13 thomas case CTRL('u'):
3269 23427b14 2022-06-23 thomas case 'u':
3270 70f17a53 2022-06-13 thomas nscroll /= 2;
3271 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3272 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3273 a4292ac5 2019-05-12 jcs case CTRL('b'):
3274 1c5e5faa 2022-06-23 thomas case 'b':
3275 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3278 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3281 f7140bf5 2021-10-17 thomas case CTRL('n'):
3282 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3284 f69c5a46 2022-07-19 thomas case '@':
3285 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3288 912a3f79 2021-08-30 j case KEY_END: {
3289 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3290 912a3f79 2021-08-30 j * traverse them all. */
3291 07b0611c 2022-06-23 thomas view->count = 0;
3292 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3293 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3294 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3297 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3298 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3299 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3300 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3302 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3303 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3305 f3bc9f1d 2021-09-05 naddy if (n > 0)
3306 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3307 2b779855 2020-12-05 naddy select_commit(s);
3310 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3311 23427b14 2022-06-23 thomas case 'd':
3312 70f17a53 2022-06-13 thomas nscroll /= 2;
3313 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3314 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3315 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3316 4c2d69cb 2022-06-23 thomas case 'f':
3317 b31f89ff 2022-07-01 thomas case ' ':
3318 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3320 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3321 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3322 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3323 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3324 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3325 2b779855 2020-12-05 naddy select_commit(s);
3326 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3327 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3328 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3329 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3330 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3333 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3334 444d5325 2022-07-03 thomas case '\r':
3335 07b0611c 2022-06-23 thomas view->count = 0;
3336 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3339 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3340 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3341 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3343 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3344 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3345 78756c87 2020-11-24 stsp view, s->repo);
3349 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3350 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3351 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3352 a5d43cac 2022-07-01 thomas if (err)
3356 e78dc838 2020-12-04 stsp view->focussed = 0;
3357 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3358 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3359 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3361 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3362 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3363 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3365 1e37a5c2 2019-05-12 jcs return err;
3366 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3367 40236d76 2022-06-23 thomas if (err)
3368 40236d76 2022-06-23 thomas return err;
3369 e78dc838 2020-12-04 stsp view->focus_child = 1;
3371 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3374 07b0611c 2022-06-23 thomas view->count = 0;
3375 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3377 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3378 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3379 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3380 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3384 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3385 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3386 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3387 444d5325 2022-07-03 thomas if (err)
3390 e78dc838 2020-12-04 stsp view->focussed = 0;
3391 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3392 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3393 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3394 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3395 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3396 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3398 1e37a5c2 2019-05-12 jcs return err;
3399 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3400 40236d76 2022-06-23 thomas if (err)
3401 40236d76 2022-06-23 thomas return err;
3402 e78dc838 2020-12-04 stsp view->focus_child = 1;
3404 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3406 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3407 21355643 2020-12-06 stsp case CTRL('l'):
3409 07b0611c 2022-06-23 thomas view->count = 0;
3410 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3411 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3413 21355643 2020-12-06 stsp err = stop_log_thread(s);
3415 74cfe85e 2020-10-20 stsp return err;
3416 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3417 21355643 2020-12-06 stsp char *parent_path;
3418 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3420 21355643 2020-12-06 stsp return err;
3421 21355643 2020-12-06 stsp free(s->in_repo_path);
3422 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3423 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3424 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3425 21355643 2020-12-06 stsp struct got_object_id *start_id;
3426 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3427 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3428 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3430 21355643 2020-12-06 stsp return err;
3431 21355643 2020-12-06 stsp free(s->start_id);
3432 21355643 2020-12-06 stsp s->start_id = start_id;
3433 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3434 21355643 2020-12-06 stsp } else /* 'B' */
3435 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3437 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3438 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3439 bf7e79b3 2022-06-23 thomas if (err)
3440 bf7e79b3 2022-06-23 thomas return err;
3442 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3443 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3444 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3446 21355643 2020-12-06 stsp return err;
3447 51a10b52 2020-12-26 stsp tog_free_refs();
3448 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3450 ca51c541 2020-12-07 stsp return err;
3451 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3452 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3454 d01904d4 2019-06-25 stsp return err;
3455 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3456 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3458 b672a97a 2020-01-27 stsp return err;
3459 21355643 2020-12-06 stsp free_commits(&s->commits);
3460 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3461 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3462 21355643 2020-12-06 stsp s->selected_entry = NULL;
3463 21355643 2020-12-06 stsp s->selected = 0;
3464 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3465 21355643 2020-12-06 stsp s->quit = 0;
3466 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3467 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3468 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3471 07b0611c 2022-06-23 thomas view->count = 0;
3472 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3473 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3474 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3475 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3476 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3477 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3478 6458efa5 2020-11-24 stsp if (err) {
3479 6458efa5 2020-11-24 stsp view_close(ref_view);
3480 6458efa5 2020-11-24 stsp return err;
3482 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3483 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3484 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3485 444d5325 2022-07-03 thomas if (err)
3488 e78dc838 2020-12-04 stsp view->focussed = 0;
3489 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3490 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3491 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3492 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3493 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3494 6458efa5 2020-11-24 stsp err = view_close_child(view);
3496 6458efa5 2020-11-24 stsp return err;
3497 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3498 40236d76 2022-06-23 thomas if (err)
3499 40236d76 2022-06-23 thomas return err;
3500 e78dc838 2020-12-04 stsp view->focus_child = 1;
3502 6458efa5 2020-11-24 stsp *new_view = ref_view;
3505 07b0611c 2022-06-23 thomas view->count = 0;
3509 80ddbec8 2018-04-29 stsp return err;
3512 4ed7e80c 2018-05-20 stsp static const struct got_error *
3513 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3515 c2db6724 2019-01-04 stsp const struct got_error *error;
3517 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3518 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3519 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3521 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3522 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3524 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3525 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3527 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3528 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3530 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3531 c2db6724 2019-01-04 stsp if (error != NULL)
3532 c2db6724 2019-01-04 stsp return error;
3534 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3535 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3537 c2db6724 2019-01-04 stsp return NULL;
3540 a915003a 2019-02-05 stsp static void
3541 a915003a 2019-02-05 stsp init_curses(void)
3544 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3545 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3546 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3548 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3549 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3550 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3551 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3552 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3554 a915003a 2019-02-05 stsp initscr();
3556 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3559 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3560 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3561 a915003a 2019-02-05 stsp curs_set(0);
3562 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3563 6d17833f 2019-11-08 stsp start_color();
3564 6d17833f 2019-11-08 stsp use_default_colors();
3568 c2db6724 2019-01-04 stsp static const struct got_error *
3569 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3570 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3572 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3574 f135c941 2020-02-20 stsp if (argc == 0) {
3575 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3576 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3577 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3578 f135c941 2020-02-20 stsp return NULL;
3581 f135c941 2020-02-20 stsp if (worktree) {
3582 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3585 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3587 f135c941 2020-02-20 stsp return err;
3588 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3589 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3590 bfd61697 2020-11-14 stsp p) == -1) {
3591 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3592 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3596 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3598 f135c941 2020-02-20 stsp return err;
3601 f135c941 2020-02-20 stsp static const struct got_error *
3602 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3604 80ddbec8 2018-04-29 stsp const struct got_error *error;
3605 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3606 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3607 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3608 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3609 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3610 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3611 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3612 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3613 04cc582a 2018-08-01 stsp struct tog_view *view;
3614 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3616 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3617 80ddbec8 2018-04-29 stsp switch (ch) {
3619 b672a97a 2020-01-27 stsp log_branches = 1;
3622 80ddbec8 2018-04-29 stsp start_commit = optarg;
3625 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3626 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3627 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3631 17020d27 2019-03-07 stsp usage_log();
3632 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3636 80ddbec8 2018-04-29 stsp argc -= optind;
3637 80ddbec8 2018-04-29 stsp argv += optind;
3639 f135c941 2020-02-20 stsp if (argc > 1)
3640 f135c941 2020-02-20 stsp usage_log();
3642 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3643 7cd52833 2022-06-23 thomas if (error != NULL)
3644 7cd52833 2022-06-23 thomas goto done;
3646 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3647 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3648 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3649 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3650 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3651 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3652 c156c7a4 2020-12-18 stsp goto done;
3653 a1fbf39a 2019-08-11 stsp if (worktree)
3654 6962eb72 2020-02-20 stsp repo_path =
3655 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3657 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3658 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3659 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3660 c156c7a4 2020-12-18 stsp goto done;
3664 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3665 80ddbec8 2018-04-29 stsp if (error != NULL)
3666 ecb28ae0 2018-07-16 stsp goto done;
3668 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3669 f135c941 2020-02-20 stsp repo, worktree);
3670 f135c941 2020-02-20 stsp if (error)
3671 f135c941 2020-02-20 stsp goto done;
3673 f135c941 2020-02-20 stsp init_curses();
3675 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3676 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3677 c02c541e 2019-03-29 stsp if (error)
3678 c02c541e 2019-03-29 stsp goto done;
3680 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3681 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3682 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3683 87670572 2020-12-26 naddy if (error)
3684 87670572 2020-12-26 naddy goto done;
3687 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3688 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3689 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3690 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3691 d8f38dc4 2020-12-05 stsp if (error)
3692 d8f38dc4 2020-12-05 stsp goto done;
3693 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3695 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3696 d8f38dc4 2020-12-05 stsp if (error == NULL)
3697 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3698 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3699 d8f38dc4 2020-12-05 stsp goto done;
3700 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3701 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3702 d8f38dc4 2020-12-05 stsp if (error)
3703 d8f38dc4 2020-12-05 stsp goto done;
3706 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3707 04cc582a 2018-08-01 stsp if (view == NULL) {
3708 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3709 04cc582a 2018-08-01 stsp goto done;
3711 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3712 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3713 ba4f502b 2018-08-04 stsp if (error)
3714 ba4f502b 2018-08-04 stsp goto done;
3715 2fc00ff4 2019-08-31 stsp if (worktree) {
3716 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3717 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3718 2fc00ff4 2019-08-31 stsp worktree = NULL;
3720 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3722 f135c941 2020-02-20 stsp free(in_repo_path);
3723 ecb28ae0 2018-07-16 stsp free(repo_path);
3724 ecb28ae0 2018-07-16 stsp free(cwd);
3725 899d86c2 2018-05-10 stsp free(start_id);
3726 d8f38dc4 2020-12-05 stsp free(label);
3728 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3729 1d0f4054 2021-06-17 stsp if (repo) {
3730 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3731 1d0f4054 2021-06-17 stsp if (error == NULL)
3732 1d0f4054 2021-06-17 stsp error = close_err;
3734 ec142235 2019-03-07 stsp if (worktree)
3735 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3736 7cd52833 2022-06-23 thomas if (pack_fds) {
3737 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3738 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3739 7cd52833 2022-06-23 thomas if (error == NULL)
3740 7cd52833 2022-06-23 thomas error = pack_err;
3742 51a10b52 2020-12-26 stsp tog_free_refs();
3743 80ddbec8 2018-04-29 stsp return error;
3746 4ed7e80c 2018-05-20 stsp __dead static void
3747 9f7d7167 2018-04-29 stsp usage_diff(void)
3750 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3751 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3755 6d17833f 2019-11-08 stsp static int
3756 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3757 41605754 2020-11-12 stsp regmatch_t *regmatch)
3759 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3762 ef20f542 2022-06-26 thomas static struct tog_color *
3763 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3765 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3767 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3768 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3769 6d17833f 2019-11-08 stsp return tc;
3772 6d17833f 2019-11-08 stsp return NULL;
3775 4ed7e80c 2018-05-20 stsp static const struct got_error *
3776 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3777 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3779 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3780 666f7b10 2022-06-23 thomas char *exstr = NULL;
3781 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3782 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3783 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3784 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3786 41605754 2020-11-12 stsp *wtotal = 0;
3788 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3789 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3791 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3792 666f7b10 2022-06-23 thomas if (err)
3793 666f7b10 2022-06-23 thomas return err;
3795 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3796 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3797 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3798 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3799 666f7b10 2022-06-23 thomas goto done;
3801 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3802 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3803 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3804 cbea3800 2022-06-23 thomas goto done;
3806 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3807 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3808 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3809 cbea3800 2022-06-23 thomas goto done;
3812 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3813 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3814 cbea3800 2022-06-23 thomas col_tab_align, 1);
3815 cbea3800 2022-06-23 thomas if (err)
3816 cbea3800 2022-06-23 thomas goto done;
3817 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3818 05171be4 2022-06-23 thomas if (n) {
3819 cbea3800 2022-06-23 thomas free(wline);
3820 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3821 cbea3800 2022-06-23 thomas wlimit, 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 wlimit -= width;
3826 cbea3800 2022-06-23 thomas *wtotal += width;
3829 41605754 2020-11-12 stsp if (wlimit > 0) {
3830 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3831 cbea3800 2022-06-23 thomas size_t wlen;
3833 cbea3800 2022-06-23 thomas free(wline);
3834 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3835 cbea3800 2022-06-23 thomas col_tab_align, 1);
3836 cbea3800 2022-06-23 thomas if (err)
3837 cbea3800 2022-06-23 thomas goto done;
3838 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3839 cbea3800 2022-06-23 thomas while (i < wlen) {
3840 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3841 cbea3800 2022-06-23 thomas if (width == -1) {
3842 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3843 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3844 cbea3800 2022-06-23 thomas goto done;
3846 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3848 1c5e5faa 2022-06-23 thomas w += width;
3851 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3852 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3853 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3854 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3855 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3856 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3857 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3861 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3862 cbea3800 2022-06-23 thomas free(wline);
3863 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3864 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3865 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3866 cbea3800 2022-06-23 thomas col_tab_align, 1);
3867 cbea3800 2022-06-23 thomas if (err)
3868 cbea3800 2022-06-23 thomas goto done;
3869 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3870 cbea3800 2022-06-23 thomas } else {
3871 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3872 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3873 cbea3800 2022-06-23 thomas if (err)
3874 cbea3800 2022-06-23 thomas goto done;
3875 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3877 cbea3800 2022-06-23 thomas *wtotal += width2;
3880 05171be4 2022-06-23 thomas free(wline);
3881 666f7b10 2022-06-23 thomas free(exstr);
3882 cbea3800 2022-06-23 thomas free(seg0);
3883 cbea3800 2022-06-23 thomas free(seg1);
3884 cbea3800 2022-06-23 thomas free(seg2);
3885 cbea3800 2022-06-23 thomas return err;
3888 41605754 2020-11-12 stsp static const struct got_error *
3889 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3891 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3892 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3893 61e69b96 2018-05-20 stsp const struct got_error *err;
3894 fe621944 2020-11-10 stsp int nprinted = 0;
3895 b304db33 2018-05-20 stsp char *line;
3896 826082fe 2020-12-10 stsp size_t linesize = 0;
3897 826082fe 2020-12-10 stsp ssize_t linelen;
3898 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3899 61e69b96 2018-05-20 stsp wchar_t *wline;
3900 e0b650dd 2018-05-20 stsp int width;
3901 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3902 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3903 fe621944 2020-11-10 stsp off_t line_offset;
3905 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3906 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3907 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3909 f7d12f7e 2018-08-01 stsp werase(view->window);
3911 a3404814 2018-09-02 stsp if (header) {
3912 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3913 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3914 135a2da0 2020-11-11 stsp header) == -1)
3915 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3916 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3918 135a2da0 2020-11-11 stsp free(line);
3920 a3404814 2018-09-02 stsp return err;
3922 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3923 a3404814 2018-09-02 stsp wstandout(view->window);
3924 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3925 e54cc94a 2020-11-11 stsp free(wline);
3926 e54cc94a 2020-11-11 stsp wline = NULL;
3927 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3928 a3404814 2018-09-02 stsp wstandend(view->window);
3929 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3930 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3932 a3404814 2018-09-02 stsp if (max_lines <= 1)
3933 a3404814 2018-09-02 stsp return NULL;
3934 a3404814 2018-09-02 stsp max_lines--;
3937 89f1a395 2020-12-01 naddy s->eof = 0;
3938 05171be4 2022-06-23 thomas view->maxx = 0;
3939 826082fe 2020-12-10 stsp line = NULL;
3940 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3941 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3942 826082fe 2020-12-10 stsp if (linelen == -1) {
3943 826082fe 2020-12-10 stsp if (feof(s->f)) {
3944 826082fe 2020-12-10 stsp s->eof = 1;
3947 826082fe 2020-12-10 stsp free(line);
3948 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3951 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3952 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3953 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3954 cbea3800 2022-06-23 thomas if (err) {
3955 cbea3800 2022-06-23 thomas free(line);
3956 cbea3800 2022-06-23 thomas return err;
3958 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3959 cbea3800 2022-06-23 thomas free(wline);
3960 cbea3800 2022-06-23 thomas wline = NULL;
3962 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3964 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3965 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3966 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3967 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3968 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3969 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3970 41605754 2020-11-12 stsp if (err) {
3971 41605754 2020-11-12 stsp free(line);
3972 41605754 2020-11-12 stsp return err;
3975 f1c0ec19 2022-06-23 thomas int skip;
3976 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3977 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3978 f1c0ec19 2022-06-23 thomas if (err) {
3979 f1c0ec19 2022-06-23 thomas free(line);
3980 f1c0ec19 2022-06-23 thomas return err;
3982 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3983 f1c0ec19 2022-06-23 thomas free(wline);
3984 41605754 2020-11-12 stsp wline = NULL;
3987 6d17833f 2019-11-08 stsp wattr_off(view->window,
3988 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3989 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3990 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3991 fe621944 2020-11-10 stsp nprinted++;
3993 826082fe 2020-12-10 stsp free(line);
3994 fe621944 2020-11-10 stsp if (nprinted >= 1)
3995 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3996 89f1a395 2020-12-01 naddy (nprinted - 1);
3998 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4000 a5d43cac 2022-07-01 thomas view_border(view);
4002 89f1a395 2020-12-01 naddy if (s->eof) {
4003 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4004 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4005 c3e9aa98 2019-05-13 jcs nprinted++;
4008 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4009 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4011 c3e9aa98 2019-05-13 jcs return err;
4014 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4015 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4016 e54cc94a 2020-11-11 stsp free(wline);
4017 e54cc94a 2020-11-11 stsp wline = NULL;
4018 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4021 26ed57b2 2018-05-19 stsp return NULL;
4024 abd2672a 2018-12-23 stsp static char *
4025 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4027 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4028 09867e48 2019-08-13 stsp char *p, *s;
4030 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4031 09867e48 2019-08-13 stsp if (tm == NULL)
4032 09867e48 2019-08-13 stsp return NULL;
4033 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4034 09867e48 2019-08-13 stsp if (s == NULL)
4035 09867e48 2019-08-13 stsp return NULL;
4036 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4038 abd2672a 2018-12-23 stsp *p = '\0';
4042 4ed7e80c 2018-05-20 stsp static const struct got_error *
4043 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4044 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4046 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4047 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4048 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4049 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4051 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4052 0208f208 2020-05-05 stsp if (qid != NULL) {
4053 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4054 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4055 ec242592 2022-04-22 thomas &qid->id);
4057 0208f208 2020-05-05 stsp return err;
4059 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4060 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4061 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4062 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4063 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4065 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4069 0208f208 2020-05-05 stsp if (tree_id1) {
4070 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4072 0208f208 2020-05-05 stsp goto done;
4075 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4076 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4078 0208f208 2020-05-05 stsp goto done;
4080 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4081 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4083 0208f208 2020-05-05 stsp if (tree1)
4084 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4085 0208f208 2020-05-05 stsp if (tree2)
4086 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4087 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4088 0208f208 2020-05-05 stsp return err;
4091 0208f208 2020-05-05 stsp static const struct got_error *
4092 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4096 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4097 fe621944 2020-11-10 stsp if (p == NULL)
4098 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4099 fe621944 2020-11-10 stsp *line_offsets = p;
4100 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4101 fe621944 2020-11-10 stsp (*nlines)++;
4102 fe621944 2020-11-10 stsp return NULL;
4105 fe621944 2020-11-10 stsp static const struct got_error *
4106 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4107 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4108 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4110 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4111 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4112 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4113 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4114 45d799e2 2018-12-23 stsp time_t committer_time;
4115 45d799e2 2018-12-23 stsp const char *author, *committer;
4116 8b473291 2019-02-21 stsp char *refs_str = NULL;
4117 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4118 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4119 fe621944 2020-11-10 stsp off_t outoff = 0;
4122 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4124 8b473291 2019-02-21 stsp if (refs) {
4125 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4127 8b473291 2019-02-21 stsp return err;
4130 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4132 abd2672a 2018-12-23 stsp return err;
4134 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4135 15a94983 2018-12-23 stsp if (err) {
4136 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4137 15a94983 2018-12-23 stsp goto done;
4140 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4142 fe621944 2020-11-10 stsp goto done;
4144 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4145 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4146 fe621944 2020-11-10 stsp if (n < 0) {
4147 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4148 abd2672a 2018-12-23 stsp goto done;
4150 fe621944 2020-11-10 stsp outoff += n;
4151 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4153 fe621944 2020-11-10 stsp goto done;
4155 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4156 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4157 fe621944 2020-11-10 stsp if (n < 0) {
4158 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4159 abd2672a 2018-12-23 stsp goto done;
4161 fe621944 2020-11-10 stsp outoff += n;
4162 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4164 fe621944 2020-11-10 stsp goto done;
4166 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4167 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4168 fe621944 2020-11-10 stsp if (datestr) {
4169 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4170 fe621944 2020-11-10 stsp if (n < 0) {
4171 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4172 fe621944 2020-11-10 stsp goto done;
4174 fe621944 2020-11-10 stsp outoff += n;
4175 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4177 fe621944 2020-11-10 stsp goto done;
4179 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4180 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4181 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4182 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4183 fe621944 2020-11-10 stsp if (n < 0) {
4184 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4185 fe621944 2020-11-10 stsp goto done;
4187 fe621944 2020-11-10 stsp outoff += n;
4188 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4190 fe621944 2020-11-10 stsp goto done;
4192 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4193 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4194 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4195 ac4dc263 2021-09-24 thomas int pn = 1;
4196 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4197 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4198 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4199 ac4dc263 2021-09-24 thomas if (err)
4200 ac4dc263 2021-09-24 thomas goto done;
4201 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4202 ac4dc263 2021-09-24 thomas if (n < 0) {
4203 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4204 ac4dc263 2021-09-24 thomas goto done;
4206 ac4dc263 2021-09-24 thomas outoff += n;
4207 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4208 ac4dc263 2021-09-24 thomas if (err)
4209 ac4dc263 2021-09-24 thomas goto done;
4210 ac4dc263 2021-09-24 thomas free(id_str);
4211 ac4dc263 2021-09-24 thomas id_str = NULL;
4215 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4217 5943eee2 2019-08-13 stsp goto done;
4218 fe621944 2020-11-10 stsp s = logmsg;
4219 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4220 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4221 fe621944 2020-11-10 stsp if (n < 0) {
4222 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4223 fe621944 2020-11-10 stsp goto done;
4225 fe621944 2020-11-10 stsp outoff += n;
4226 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4228 fe621944 2020-11-10 stsp goto done;
4231 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4233 0208f208 2020-05-05 stsp goto done;
4234 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4235 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4236 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4237 fe621944 2020-11-10 stsp if (n < 0) {
4238 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4239 fe621944 2020-11-10 stsp goto done;
4241 fe621944 2020-11-10 stsp outoff += n;
4242 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4244 fe621944 2020-11-10 stsp goto done;
4245 0208f208 2020-05-05 stsp free((char *)pe->path);
4246 0208f208 2020-05-05 stsp free(pe->data);
4249 0208f208 2020-05-05 stsp fputc('\n', outfile);
4251 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4253 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4254 abd2672a 2018-12-23 stsp free(id_str);
4255 5943eee2 2019-08-13 stsp free(logmsg);
4256 8b473291 2019-02-21 stsp free(refs_str);
4257 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4258 7510f233 2020-08-09 stsp if (err) {
4259 ae6a6978 2020-08-09 stsp free(*line_offsets);
4260 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4261 ae6a6978 2020-08-09 stsp *nlines = 0;
4263 fe621944 2020-11-10 stsp return err;
4266 abd2672a 2018-12-23 stsp static const struct got_error *
4267 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4269 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4270 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4271 15a94983 2018-12-23 stsp int obj_type;
4273 fe621944 2020-11-10 stsp free(s->line_offsets);
4274 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4275 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4276 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4277 fe621944 2020-11-10 stsp s->nlines = 0;
4279 511a516b 2018-05-19 stsp f = got_opentemp();
4280 48ae06ee 2018-10-18 stsp if (f == NULL) {
4281 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4282 48ae06ee 2018-10-18 stsp goto done;
4284 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4285 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4286 fb43ecf1 2019-02-11 stsp goto done;
4290 15a94983 2018-12-23 stsp if (s->id1)
4291 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4293 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4295 15a94983 2018-12-23 stsp goto done;
4297 15a94983 2018-12-23 stsp switch (obj_type) {
4298 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4299 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4300 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4301 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4302 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4304 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4305 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4306 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4307 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4308 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4310 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4311 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4312 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4313 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4314 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4316 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4318 3ffacbe1 2020-02-02 tracey goto done;
4319 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4320 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4321 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4322 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4323 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4324 f44b1f58 2020-02-02 tracey if (err)
4325 f44b1f58 2020-02-02 tracey goto done;
4326 f44b1f58 2020-02-02 tracey } else {
4327 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4328 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4329 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4330 fe621944 2020-11-10 stsp err = write_commit_info(
4331 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4332 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4333 f44b1f58 2020-02-02 tracey if (err)
4334 f44b1f58 2020-02-02 tracey goto done;
4339 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4341 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4342 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4343 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4344 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4348 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4351 f44b1f58 2020-02-02 tracey if (err)
4352 f44b1f58 2020-02-02 tracey goto done;
4354 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4355 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4356 48ae06ee 2018-10-18 stsp return err;
4359 f5215bb9 2019-02-22 stsp static void
4360 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4362 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4363 f5215bb9 2019-02-22 stsp update_panels();
4364 f5215bb9 2019-02-22 stsp doupdate();
4367 f44b1f58 2020-02-02 tracey static const struct got_error *
4368 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4370 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4372 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4373 f44b1f58 2020-02-02 tracey return NULL;
4376 f44b1f58 2020-02-02 tracey static const struct got_error *
4377 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4379 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4380 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4381 f44b1f58 2020-02-02 tracey int lineno;
4382 78eecffc 2022-06-23 thomas char *line = NULL;
4383 826082fe 2020-12-10 stsp size_t linesize = 0;
4384 826082fe 2020-12-10 stsp ssize_t linelen;
4386 f44b1f58 2020-02-02 tracey if (!view->searching) {
4387 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4388 f44b1f58 2020-02-02 tracey return NULL;
4391 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4392 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4393 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4395 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4397 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4399 f44b1f58 2020-02-02 tracey while (1) {
4400 f44b1f58 2020-02-02 tracey off_t offset;
4402 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4403 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4404 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4408 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4409 f44b1f58 2020-02-02 tracey lineno = 1;
4411 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4414 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4415 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4416 f44b1f58 2020-02-02 tracey free(line);
4417 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4419 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4420 78eecffc 2022-06-23 thomas if (linelen != -1) {
4421 78eecffc 2022-06-23 thomas char *exstr;
4422 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4423 78eecffc 2022-06-23 thomas if (err)
4425 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4426 78eecffc 2022-06-23 thomas &view->regmatch)) {
4427 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4428 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4429 78eecffc 2022-06-23 thomas free(exstr);
4432 78eecffc 2022-06-23 thomas free(exstr);
4434 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4435 f44b1f58 2020-02-02 tracey lineno++;
4437 f44b1f58 2020-02-02 tracey lineno--;
4439 826082fe 2020-12-10 stsp free(line);
4441 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4442 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4443 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4446 05171be4 2022-06-23 thomas return err;
4449 48ae06ee 2018-10-18 stsp static const struct got_error *
4450 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4452 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4453 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4455 a0f32f33 2022-06-13 thomas free(s->id1);
4456 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4457 a0f32f33 2022-06-13 thomas free(s->id2);
4458 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4459 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4460 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4461 a0f32f33 2022-06-13 thomas s->f = NULL;
4462 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4463 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4464 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4465 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4466 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4467 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4468 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4469 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4470 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4471 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4472 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4473 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4474 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4475 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4476 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4477 a0f32f33 2022-06-13 thomas s->nlines = 0;
4478 a0f32f33 2022-06-13 thomas return err;
4481 a0f32f33 2022-06-13 thomas static const struct got_error *
4482 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4483 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4484 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4485 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4487 48ae06ee 2018-10-18 stsp const struct got_error *err;
4488 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4490 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4491 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4492 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4494 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4495 15a94983 2018-12-23 stsp int type1, type2;
4496 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4498 15a94983 2018-12-23 stsp return err;
4499 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4501 15a94983 2018-12-23 stsp return err;
4503 15a94983 2018-12-23 stsp if (type1 != type2)
4504 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4506 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4507 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4508 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4509 f44b1f58 2020-02-02 tracey s->repo = repo;
4510 f44b1f58 2020-02-02 tracey s->id1 = id1;
4511 f44b1f58 2020-02-02 tracey s->id2 = id2;
4512 3dbaef42 2020-11-24 stsp s->label1 = label1;
4513 3dbaef42 2020-11-24 stsp s->label2 = label2;
4515 15a94983 2018-12-23 stsp if (id1) {
4516 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4517 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4518 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4520 5465d566 2020-02-01 tracey s->id1 = NULL;
4522 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4523 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4524 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4525 a0f32f33 2022-06-13 thomas goto done;
4528 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4529 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4530 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4531 9a1d7435 2022-06-23 thomas goto done;
4534 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4535 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4536 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4537 a0f32f33 2022-06-13 thomas goto done;
4540 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4541 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4542 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4543 19a6a6b5 2022-07-01 thomas goto done;
4546 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4547 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4548 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4549 19a6a6b5 2022-07-01 thomas goto done;
4552 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4553 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4554 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4555 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4556 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4557 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4558 5465d566 2020-02-01 tracey s->repo = repo;
4560 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4561 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4562 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4563 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4564 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4566 a0f32f33 2022-06-13 thomas goto done;
4567 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4568 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4569 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4570 a0f32f33 2022-06-13 thomas if (err)
4571 a0f32f33 2022-06-13 thomas goto done;
4572 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4573 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4574 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4575 a0f32f33 2022-06-13 thomas if (err)
4576 a0f32f33 2022-06-13 thomas goto done;
4578 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4579 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4580 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4581 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4582 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4583 a0f32f33 2022-06-13 thomas if (err)
4584 a0f32f33 2022-06-13 thomas goto done;
4586 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4587 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4588 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4589 a0f32f33 2022-06-13 thomas if (err)
4590 a0f32f33 2022-06-13 thomas goto done;
4592 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4593 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4594 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4595 a0f32f33 2022-06-13 thomas if (err)
4596 a0f32f33 2022-06-13 thomas goto done;
4599 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4600 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4601 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4602 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4604 5465d566 2020-02-01 tracey err = create_diff(s);
4606 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4607 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4608 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4609 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4610 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4611 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4613 a0f32f33 2022-06-13 thomas if (err)
4614 a0f32f33 2022-06-13 thomas close_diff_view(view);
4615 e5a0f69f 2018-08-18 stsp return err;
4618 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4619 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4621 a3404814 2018-09-02 stsp const struct got_error *err;
4622 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4623 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4624 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4626 a3404814 2018-09-02 stsp if (s->id1) {
4627 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4629 a3404814 2018-09-02 stsp return err;
4630 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4632 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4634 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4636 a3404814 2018-09-02 stsp return err;
4637 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4639 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4640 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4641 a3404814 2018-09-02 stsp free(id_str1);
4642 a3404814 2018-09-02 stsp free(id_str2);
4643 a3404814 2018-09-02 stsp return err;
4645 a3404814 2018-09-02 stsp free(id_str1);
4646 a3404814 2018-09-02 stsp free(id_str2);
4648 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4649 267bb3b8 2021-08-01 stsp free(header);
4650 267bb3b8 2021-08-01 stsp return err;
4653 15a087fe 2019-02-21 stsp static const struct got_error *
4654 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4655 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4657 d7a04538 2019-02-21 stsp const struct got_error *err;
4658 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4659 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4660 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4662 15a087fe 2019-02-21 stsp free(s->id2);
4663 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4664 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4665 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4667 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4669 d7a04538 2019-02-21 stsp return err;
4670 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4671 15a087fe 2019-02-21 stsp free(s->id1);
4672 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4673 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4674 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4675 15a087fe 2019-02-21 stsp return NULL;
4678 0cf4efb1 2018-09-29 stsp static const struct got_error *
4679 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4681 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4683 adf4c9e0 2022-07-03 thomas view->count = 0;
4684 adf4c9e0 2022-07-03 thomas wclear(view->window);
4685 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4686 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4687 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4688 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4689 adf4c9e0 2022-07-03 thomas return create_diff(s);
4692 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4693 4fc71f3b 2022-07-12 thomas int, int, int);
4694 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4695 4fc71f3b 2022-07-12 thomas int, int);
4697 adf4c9e0 2022-07-03 thomas static const struct got_error *
4698 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4700 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4701 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4702 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4703 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4704 826082fe 2020-12-10 stsp char *line = NULL;
4705 826082fe 2020-12-10 stsp size_t linesize = 0;
4706 826082fe 2020-12-10 stsp ssize_t linelen;
4707 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4709 e5a0f69f 2018-08-18 stsp switch (ch) {
4710 05171be4 2022-06-23 thomas case '0':
4711 05171be4 2022-06-23 thomas view->x = 0;
4713 05171be4 2022-06-23 thomas case '$':
4714 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4715 07b0611c 2022-06-23 thomas view->count = 0;
4717 05171be4 2022-06-23 thomas case KEY_RIGHT:
4718 05171be4 2022-06-23 thomas case 'l':
4719 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4720 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4722 07b0611c 2022-06-23 thomas view->count = 0;
4724 05171be4 2022-06-23 thomas case KEY_LEFT:
4725 05171be4 2022-06-23 thomas case 'h':
4726 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4727 07b0611c 2022-06-23 thomas if (view->x <= 0)
4728 07b0611c 2022-06-23 thomas view->count = 0;
4732 3dbaef42 2020-11-24 stsp if (ch == 'a')
4733 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4734 3dbaef42 2020-11-24 stsp if (ch == 'w')
4735 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4736 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4739 912a3f79 2021-08-30 j case KEY_HOME:
4740 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4741 07b0611c 2022-06-23 thomas view->count = 0;
4744 912a3f79 2021-08-30 j case KEY_END:
4745 07b0611c 2022-06-23 thomas view->count = 0;
4749 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4753 1e37a5c2 2019-05-12 jcs case KEY_UP:
4754 f7140bf5 2021-10-17 thomas case CTRL('p'):
4755 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4756 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4758 07b0611c 2022-06-23 thomas view->count = 0;
4760 70f17a53 2022-06-13 thomas case CTRL('u'):
4761 23427b14 2022-06-23 thomas case 'u':
4762 70f17a53 2022-06-13 thomas nscroll /= 2;
4763 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4764 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4765 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4766 1c5e5faa 2022-06-23 thomas case 'b':
4767 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4768 07b0611c 2022-06-23 thomas view->count = 0;
4772 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4773 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4776 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4777 f7140bf5 2021-10-17 thomas case CTRL('n'):
4778 1e37a5c2 2019-05-12 jcs if (!s->eof)
4779 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4781 07b0611c 2022-06-23 thomas view->count = 0;
4783 70f17a53 2022-06-13 thomas case CTRL('d'):
4784 23427b14 2022-06-23 thomas case 'd':
4785 70f17a53 2022-06-13 thomas nscroll /= 2;
4786 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4787 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4788 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4789 1c5e5faa 2022-06-23 thomas case 'f':
4791 07b0611c 2022-06-23 thomas if (s->eof) {
4792 07b0611c 2022-06-23 thomas view->count = 0;
4796 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4797 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4798 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4799 826082fe 2020-12-10 stsp if (linelen == -1) {
4800 826082fe 2020-12-10 stsp if (feof(s->f)) {
4801 826082fe 2020-12-10 stsp s->eof = 1;
4803 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4807 826082fe 2020-12-10 stsp free(line);
4810 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4811 1e37a5c2 2019-05-12 jcs s->diff_context--;
4812 aa61903a 2021-12-31 thomas s->matched_line = 0;
4813 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4814 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4815 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4816 27829c9e 2020-11-21 stsp s->nlines) {
4817 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4818 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4821 07b0611c 2022-06-23 thomas view->count = 0;
4824 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4825 1e37a5c2 2019-05-12 jcs s->diff_context++;
4826 aa61903a 2021-12-31 thomas s->matched_line = 0;
4827 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4828 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4830 07b0611c 2022-06-23 thomas view->count = 0;
4834 777aae21 2022-07-20 thomas case 'K':
4836 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4837 4fc71f3b 2022-07-12 thomas case '>':
4838 4fc71f3b 2022-07-12 thomas case '.':
4839 777aae21 2022-07-20 thomas case 'J':
4840 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4841 07b0611c 2022-06-23 thomas view->count = 0;
4844 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4846 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4847 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4848 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4850 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4851 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4852 4fc71f3b 2022-07-12 thomas if (err)
4854 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4856 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4859 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4860 4fc71f3b 2022-07-12 thomas if (err)
4862 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4863 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4864 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4866 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4867 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4868 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4870 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4871 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4872 4fc71f3b 2022-07-12 thomas if (err)
4874 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4876 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4878 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4879 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4880 4fc71f3b 2022-07-12 thomas bs->selected_line);
4881 4fc71f3b 2022-07-12 thomas if (id == NULL)
4884 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4887 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4888 4fc71f3b 2022-07-12 thomas if (err)
4891 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4892 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4893 aa61903a 2021-12-31 thomas s->matched_line = 0;
4894 05171be4 2022-06-23 thomas view->x = 0;
4896 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4897 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4900 07b0611c 2022-06-23 thomas view->count = 0;
4904 bcbd79e2 2018-08-19 stsp return err;
4907 4ed7e80c 2018-05-20 stsp static const struct got_error *
4908 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4910 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4911 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4912 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4913 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4914 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4915 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4916 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4917 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4918 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4919 3dbaef42 2020-11-24 stsp const char *errstr;
4920 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4921 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4923 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4924 26ed57b2 2018-05-19 stsp switch (ch) {
4926 64453f7e 2020-11-21 stsp force_text_diff = 1;
4929 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4931 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4932 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4933 8f666e67 2022-02-12 thomas errstr, errstr);
4935 09b5bff8 2020-02-23 naddy case 'r':
4936 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4937 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4938 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4940 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4943 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4946 17020d27 2019-03-07 stsp usage_diff();
4947 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4951 26ed57b2 2018-05-19 stsp argc -= optind;
4952 26ed57b2 2018-05-19 stsp argv += optind;
4954 26ed57b2 2018-05-19 stsp if (argc == 0) {
4955 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4956 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4957 15a94983 2018-12-23 stsp id_str1 = argv[0];
4958 15a94983 2018-12-23 stsp id_str2 = argv[1];
4960 26ed57b2 2018-05-19 stsp usage_diff();
4962 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4963 7cd52833 2022-06-23 thomas if (error)
4964 7cd52833 2022-06-23 thomas goto done;
4966 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4967 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4968 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4969 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4970 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4971 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4972 c156c7a4 2020-12-18 stsp goto done;
4973 a273ac94 2020-02-23 naddy if (worktree)
4974 a273ac94 2020-02-23 naddy repo_path =
4975 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4977 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4978 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4979 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4980 c156c7a4 2020-12-18 stsp goto done;
4984 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4985 eb6600df 2019-01-04 stsp if (error)
4986 eb6600df 2019-01-04 stsp goto done;
4988 a273ac94 2020-02-23 naddy init_curses();
4990 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4991 51a10b52 2020-12-26 stsp if (error)
4992 51a10b52 2020-12-26 stsp goto done;
4994 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4995 26ed57b2 2018-05-19 stsp if (error)
4996 26ed57b2 2018-05-19 stsp goto done;
4998 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4999 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5000 26ed57b2 2018-05-19 stsp if (error)
5001 26ed57b2 2018-05-19 stsp goto done;
5003 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5004 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5005 26ed57b2 2018-05-19 stsp if (error)
5006 26ed57b2 2018-05-19 stsp goto done;
5008 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5009 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5010 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5011 ea5e7bb5 2018-08-01 stsp goto done;
5013 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5014 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5015 5dc9f4bc 2018-08-04 stsp if (error)
5016 5dc9f4bc 2018-08-04 stsp goto done;
5017 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5019 3dbaef42 2020-11-24 stsp free(label1);
5020 3dbaef42 2020-11-24 stsp free(label2);
5021 c02c541e 2019-03-29 stsp free(repo_path);
5022 a273ac94 2020-02-23 naddy free(cwd);
5023 1d0f4054 2021-06-17 stsp if (repo) {
5024 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5025 1d0f4054 2021-06-17 stsp if (error == NULL)
5026 1d0f4054 2021-06-17 stsp error = close_err;
5028 a273ac94 2020-02-23 naddy if (worktree)
5029 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5030 7cd52833 2022-06-23 thomas if (pack_fds) {
5031 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5032 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5033 7cd52833 2022-06-23 thomas if (error == NULL)
5034 7cd52833 2022-06-23 thomas error = pack_err;
5036 51a10b52 2020-12-26 stsp tog_free_refs();
5037 26ed57b2 2018-05-19 stsp return error;
5040 4ed7e80c 2018-05-20 stsp __dead static void
5041 9f7d7167 2018-04-29 stsp usage_blame(void)
5044 91198554 2022-06-23 thomas fprintf(stderr,
5045 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5046 9f7d7167 2018-04-29 stsp getprogname());
5050 84451b3e 2018-07-10 stsp struct tog_blame_line {
5051 84451b3e 2018-07-10 stsp int annotated;
5052 84451b3e 2018-07-10 stsp struct got_object_id *id;
5055 4ed7e80c 2018-05-20 stsp static const struct got_error *
5056 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5058 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5059 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5060 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5061 84451b3e 2018-07-10 stsp const struct got_error *err;
5062 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5063 826082fe 2020-12-10 stsp char *line = NULL;
5064 826082fe 2020-12-10 stsp size_t linesize = 0;
5065 826082fe 2020-12-10 stsp ssize_t linelen;
5066 84451b3e 2018-07-10 stsp wchar_t *wline;
5067 27a741e5 2019-09-11 stsp int width;
5068 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5069 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5070 ab089a2a 2018-07-12 stsp char *id_str;
5071 11b20872 2019-11-08 stsp struct tog_color *tc;
5073 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5075 ab089a2a 2018-07-12 stsp return err;
5077 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5078 f7d12f7e 2018-08-01 stsp werase(view->window);
5080 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5081 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5082 ab089a2a 2018-07-12 stsp free(id_str);
5083 ab089a2a 2018-07-12 stsp return err;
5086 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5087 ab089a2a 2018-07-12 stsp free(line);
5088 2550e4c3 2018-07-13 stsp line = NULL;
5090 1cae65b4 2019-09-22 stsp return err;
5091 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5092 a3404814 2018-09-02 stsp wstandout(view->window);
5093 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5095 11b20872 2019-11-08 stsp wattr_on(view->window,
5096 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5097 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5099 11b20872 2019-11-08 stsp wattr_off(view->window,
5100 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5101 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5102 a3404814 2018-09-02 stsp wstandend(view->window);
5103 2550e4c3 2018-07-13 stsp free(wline);
5104 2550e4c3 2018-07-13 stsp wline = NULL;
5105 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5106 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5108 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5109 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5110 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5111 ab089a2a 2018-07-12 stsp free(id_str);
5112 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5114 ab089a2a 2018-07-12 stsp free(id_str);
5115 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5116 3f60a8ef 2018-07-10 stsp free(line);
5117 2550e4c3 2018-07-13 stsp line = NULL;
5119 3f60a8ef 2018-07-10 stsp return err;
5120 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5121 2550e4c3 2018-07-13 stsp free(wline);
5122 2550e4c3 2018-07-13 stsp wline = NULL;
5123 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5124 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5126 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5127 05171be4 2022-06-23 thomas view->maxx = 0;
5128 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5129 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5130 826082fe 2020-12-10 stsp if (linelen == -1) {
5131 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5132 826082fe 2020-12-10 stsp s->eof = 1;
5135 84451b3e 2018-07-10 stsp free(line);
5136 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5138 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5141 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5142 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5143 cbea3800 2022-06-23 thomas if (err) {
5144 cbea3800 2022-06-23 thomas free(line);
5145 cbea3800 2022-06-23 thomas return err;
5147 cbea3800 2022-06-23 thomas free(wline);
5148 cbea3800 2022-06-23 thomas wline = NULL;
5149 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5151 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5152 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5154 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5155 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5156 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5157 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5158 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5159 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5160 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5161 8d0fe45a 2019-08-12 stsp char *id_str;
5162 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5163 91198554 2022-06-23 thomas blame_line->id);
5164 8d0fe45a 2019-08-12 stsp if (err) {
5165 8d0fe45a 2019-08-12 stsp free(line);
5166 8d0fe45a 2019-08-12 stsp return err;
5168 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5170 11b20872 2019-11-08 stsp wattr_on(view->window,
5171 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5172 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5174 11b20872 2019-11-08 stsp wattr_off(view->window,
5175 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5176 8d0fe45a 2019-08-12 stsp free(id_str);
5177 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5179 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5180 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5183 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5184 ee41ec32 2018-07-10 stsp prev_id = NULL;
5187 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5188 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5189 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5191 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5192 41605754 2020-11-12 stsp width = 9;
5193 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5194 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5195 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5196 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5197 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5198 41605754 2020-11-12 stsp if (err) {
5199 41605754 2020-11-12 stsp free(line);
5200 41605754 2020-11-12 stsp return err;
5202 41605754 2020-11-12 stsp width += 9;
5204 923086fd 2022-06-23 thomas int skip;
5205 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5206 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5207 923086fd 2022-06-23 thomas if (err) {
5208 923086fd 2022-06-23 thomas free(line);
5209 923086fd 2022-06-23 thomas return err;
5211 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5212 02bce7e2 2022-06-23 thomas width += 9;
5213 41605754 2020-11-12 stsp free(wline);
5214 41605754 2020-11-12 stsp wline = NULL;
5217 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5218 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5219 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5220 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5222 826082fe 2020-12-10 stsp free(line);
5223 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5225 a5d43cac 2022-07-01 thomas view_border(view);
5227 84451b3e 2018-07-10 stsp return NULL;
5230 84451b3e 2018-07-10 stsp static const struct got_error *
5231 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5232 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5234 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5235 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5236 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5237 1a76625f 2018-10-22 stsp int errcode;
5239 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5240 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5241 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5243 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5244 1a76625f 2018-10-22 stsp if (errcode)
5245 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5247 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5248 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5249 d68a0a7d 2018-07-10 stsp goto done;
5252 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5253 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5255 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5256 d68a0a7d 2018-07-10 stsp if (line->annotated)
5257 d68a0a7d 2018-07-10 stsp goto done;
5259 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5260 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5261 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5262 84451b3e 2018-07-10 stsp goto done;
5264 84451b3e 2018-07-10 stsp line->annotated = 1;
5266 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5267 1a76625f 2018-10-22 stsp if (errcode)
5268 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5269 84451b3e 2018-07-10 stsp return err;
5272 84451b3e 2018-07-10 stsp static void *
5273 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5275 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5276 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5277 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5278 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5279 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5281 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5282 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5283 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5285 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5286 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5287 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5288 9117a7b7 2022-07-01 thomas goto done;
5291 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5292 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5293 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5294 9117a7b7 2022-07-01 thomas goto done;
5296 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5297 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5298 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5299 9117a7b7 2022-07-01 thomas goto done;
5302 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5304 9117a7b7 2022-07-01 thomas goto done;
5306 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5307 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5308 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5309 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5310 fc06ba56 2019-08-22 stsp err = NULL;
5312 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5313 9117a7b7 2022-07-01 thomas if (errcode) {
5314 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5315 9117a7b7 2022-07-01 thomas goto done;
5318 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5319 1d0f4054 2021-06-17 stsp if (err == NULL)
5320 1d0f4054 2021-06-17 stsp err = close_err;
5321 c9beca56 2018-07-22 stsp ta->repo = NULL;
5322 c9beca56 2018-07-22 stsp *ta->complete = 1;
5324 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5325 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5326 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5329 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5330 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5331 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5332 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5333 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5334 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5335 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5336 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5338 18430de3 2018-07-10 stsp return (void *)err;
5341 245d91c1 2018-07-12 stsp static struct got_object_id *
5342 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5343 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5345 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5347 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5348 8d0fe45a 2019-08-12 stsp return NULL;
5350 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5351 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5352 4fc71f3b 2022-07-12 thomas return NULL;
5354 4fc71f3b 2022-07-12 thomas return line->id;
5357 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5358 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5359 4fc71f3b 2022-07-12 thomas int lineno)
5361 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5363 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5364 4fc71f3b 2022-07-12 thomas return NULL;
5366 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5367 245d91c1 2018-07-12 stsp if (!line->annotated)
5368 245d91c1 2018-07-12 stsp return NULL;
5370 245d91c1 2018-07-12 stsp return line->id;
5373 b880a918 2018-07-10 stsp static const struct got_error *
5374 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5376 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5379 245d91c1 2018-07-12 stsp if (blame->thread) {
5380 1a76625f 2018-10-22 stsp int errcode;
5381 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5382 1a76625f 2018-10-22 stsp if (errcode)
5383 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5384 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5385 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5386 1a76625f 2018-10-22 stsp if (errcode)
5387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5388 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5389 1a76625f 2018-10-22 stsp if (errcode)
5390 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5391 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5392 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5393 245d91c1 2018-07-12 stsp err = NULL;
5394 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5396 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5397 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5398 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5399 1d0f4054 2021-06-17 stsp if (err == NULL)
5400 1d0f4054 2021-06-17 stsp err = close_err;
5401 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5403 245d91c1 2018-07-12 stsp if (blame->f) {
5404 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5405 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5406 245d91c1 2018-07-12 stsp blame->f = NULL;
5408 57670559 2018-12-23 stsp if (blame->lines) {
5409 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5410 57670559 2018-12-23 stsp free(blame->lines[i].id);
5411 57670559 2018-12-23 stsp free(blame->lines);
5412 57670559 2018-12-23 stsp blame->lines = NULL;
5414 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5415 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5416 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5417 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5418 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5419 7cd52833 2022-06-23 thomas if (err == NULL)
5420 7cd52833 2022-06-23 thomas err = pack_err;
5421 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5423 245d91c1 2018-07-12 stsp return err;
5426 245d91c1 2018-07-12 stsp static const struct got_error *
5427 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5429 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5430 fc06ba56 2019-08-22 stsp int *done = arg;
5431 fc06ba56 2019-08-22 stsp int errcode;
5433 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5434 fc06ba56 2019-08-22 stsp if (errcode)
5435 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5436 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5438 fc06ba56 2019-08-22 stsp if (*done)
5439 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5441 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5442 fc06ba56 2019-08-22 stsp if (errcode)
5443 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5444 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5446 fc06ba56 2019-08-22 stsp return err;
5449 fc06ba56 2019-08-22 stsp static const struct got_error *
5450 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5452 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5453 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5454 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5455 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5456 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5457 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5458 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5459 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5460 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5462 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5463 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5465 15a94983 2018-12-23 stsp return err;
5467 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5468 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5469 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5470 f4ae6ddb 2022-07-01 thomas goto done;
5473 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5474 945f9229 2022-04-16 thomas if (err)
5475 945f9229 2022-04-16 thomas goto done;
5477 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5479 84451b3e 2018-07-10 stsp goto done;
5481 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5482 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5483 84451b3e 2018-07-10 stsp goto done;
5486 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5488 a70480e0 2018-06-23 stsp goto done;
5489 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5490 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5491 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5492 84451b3e 2018-07-10 stsp goto done;
5494 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5495 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5497 1fddf795 2021-01-20 stsp goto done;
5498 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5499 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5500 84451b3e 2018-07-10 stsp goto done;
5503 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5504 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5505 b02560ec 2019-08-19 stsp blame->nlines--;
5507 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5508 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5509 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5510 84451b3e 2018-07-10 stsp goto done;
5513 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5515 bd24772e 2018-07-11 stsp goto done;
5516 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5517 7cd52833 2022-06-23 thomas pack_fds);
5518 7cd52833 2022-06-23 thomas if (err)
5519 7cd52833 2022-06-23 thomas goto done;
5521 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5522 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5523 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5524 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5525 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5526 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5527 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5528 245d91c1 2018-07-12 stsp goto done;
5530 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5532 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5533 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5534 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5535 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5536 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5537 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5538 a5388363 2020-12-01 naddy s->blame_complete = 0;
5540 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5541 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5542 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5543 f5a09613 2020-12-13 naddy s->selected_line = 1;
5545 aa61903a 2021-12-31 thomas s->matched_line = 0;
5548 945f9229 2022-04-16 thomas if (commit)
5549 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5550 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5551 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5553 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5554 27d434c2 2018-09-15 stsp free(obj_id);
5556 245d91c1 2018-07-12 stsp stop_blame(blame);
5557 245d91c1 2018-07-12 stsp return err;
5560 245d91c1 2018-07-12 stsp static const struct got_error *
5561 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5562 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5564 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5565 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5567 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5569 c4843652 2019-08-12 stsp s->path = strdup(path);
5570 c4843652 2019-08-12 stsp if (s->path == NULL)
5571 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5573 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5574 c4843652 2019-08-12 stsp if (err) {
5575 c4843652 2019-08-12 stsp free(s->path);
5576 7cbe629d 2018-08-04 stsp return err;
5579 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5580 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5581 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5582 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5583 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5584 fb2756b9 2018-08-04 stsp s->repo = repo;
5585 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5586 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5588 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5589 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5590 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5591 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5593 11b20872 2019-11-08 stsp return err;
5596 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5597 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5598 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5599 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5600 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5601 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5603 a5388363 2020-12-01 naddy return run_blame(view);
5606 e5a0f69f 2018-08-18 stsp static const struct got_error *
5607 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5609 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5610 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5612 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5613 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5615 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5616 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5617 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5618 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5619 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5622 e5a0f69f 2018-08-18 stsp free(s->path);
5623 11b20872 2019-11-08 stsp free_colors(&s->colors);
5624 e5a0f69f 2018-08-18 stsp return err;
5627 7cbe629d 2018-08-04 stsp static const struct got_error *
5628 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5630 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5632 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5633 6c4c42e0 2019-06-24 stsp return NULL;
5636 6c4c42e0 2019-06-24 stsp static const struct got_error *
5637 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5639 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5640 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5641 6c4c42e0 2019-06-24 stsp int lineno;
5642 78eecffc 2022-06-23 thomas char *line = NULL;
5643 826082fe 2020-12-10 stsp size_t linesize = 0;
5644 826082fe 2020-12-10 stsp ssize_t linelen;
5646 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5647 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5648 6c4c42e0 2019-06-24 stsp return NULL;
5651 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5652 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5653 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5655 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5657 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5659 6c4c42e0 2019-06-24 stsp while (1) {
5660 6c4c42e0 2019-06-24 stsp off_t offset;
5662 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5663 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5664 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5668 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5669 6c4c42e0 2019-06-24 stsp lineno = 1;
5671 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5674 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5675 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5676 6c4c42e0 2019-06-24 stsp free(line);
5677 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5679 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5680 78eecffc 2022-06-23 thomas if (linelen != -1) {
5681 78eecffc 2022-06-23 thomas char *exstr;
5682 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5683 78eecffc 2022-06-23 thomas if (err)
5685 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5686 78eecffc 2022-06-23 thomas &view->regmatch)) {
5687 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5688 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5689 78eecffc 2022-06-23 thomas free(exstr);
5692 78eecffc 2022-06-23 thomas free(exstr);
5694 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5699 826082fe 2020-12-10 stsp free(line);
5701 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5702 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5703 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5706 05171be4 2022-06-23 thomas return err;
5709 6c4c42e0 2019-06-24 stsp static const struct got_error *
5710 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5712 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5713 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5714 2b380cc8 2018-10-24 stsp int errcode;
5716 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5717 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5718 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5719 2b380cc8 2018-10-24 stsp if (errcode)
5720 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5722 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5725 51fe7530 2019-08-19 stsp if (s->blame_complete)
5726 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5728 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5730 a5d43cac 2022-07-01 thomas view_border(view);
5731 e5a0f69f 2018-08-18 stsp return err;
5734 e5a0f69f 2018-08-18 stsp static const struct got_error *
5735 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5736 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5738 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5739 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5741 eaeaa612 2022-07-20 thomas *new_view = NULL;
5743 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5744 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5745 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5747 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5748 eaeaa612 2022-07-20 thomas if (err)
5749 eaeaa612 2022-07-20 thomas view_close(log_view);
5751 eaeaa612 2022-07-20 thomas *new_view = log_view;
5753 eaeaa612 2022-07-20 thomas return err;
5756 eaeaa612 2022-07-20 thomas static const struct got_error *
5757 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5759 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5760 eaeaa612 2022-07-20 thomas struct tog_view *diff_view, *log_view;
5761 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5762 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5764 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5765 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5766 a5d43cac 2022-07-01 thomas --eos; /* border */
5768 e5a0f69f 2018-08-18 stsp switch (ch) {
5769 05171be4 2022-06-23 thomas case '0':
5770 05171be4 2022-06-23 thomas view->x = 0;
5772 05171be4 2022-06-23 thomas case '$':
5773 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5774 07b0611c 2022-06-23 thomas view->count = 0;
5776 05171be4 2022-06-23 thomas case KEY_RIGHT:
5777 05171be4 2022-06-23 thomas case 'l':
5778 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5779 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5781 07b0611c 2022-06-23 thomas view->count = 0;
5783 05171be4 2022-06-23 thomas case KEY_LEFT:
5784 05171be4 2022-06-23 thomas case 'h':
5785 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5786 07b0611c 2022-06-23 thomas if (view->x <= 0)
5787 07b0611c 2022-06-23 thomas view->count = 0;
5790 1e37a5c2 2019-05-12 jcs s->done = 1;
5792 4deef56f 2021-09-02 naddy case 'g':
5793 4deef56f 2021-09-02 naddy case KEY_HOME:
5794 4deef56f 2021-09-02 naddy s->selected_line = 1;
5795 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5796 07b0611c 2022-06-23 thomas view->count = 0;
5798 4deef56f 2021-09-02 naddy case 'G':
5799 4deef56f 2021-09-02 naddy case KEY_END:
5800 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5801 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5802 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5804 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5805 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5807 07b0611c 2022-06-23 thomas view->count = 0;
5810 1e37a5c2 2019-05-12 jcs case KEY_UP:
5811 f7140bf5 2021-10-17 thomas case CTRL('p'):
5812 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5813 1e37a5c2 2019-05-12 jcs s->selected_line--;
5814 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5815 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5816 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5818 07b0611c 2022-06-23 thomas view->count = 0;
5820 70f17a53 2022-06-13 thomas case CTRL('u'):
5821 23427b14 2022-06-23 thomas case 'u':
5822 70f17a53 2022-06-13 thomas nscroll /= 2;
5823 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5824 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5825 ea025d1d 2020-02-22 naddy case CTRL('b'):
5826 1c5e5faa 2022-06-23 thomas case 'b':
5827 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5828 07b0611c 2022-06-23 thomas if (view->count > 1)
5829 07b0611c 2022-06-23 thomas nscroll += nscroll;
5830 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5831 07b0611c 2022-06-23 thomas view->count = 0;
5834 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5835 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5837 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5840 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5841 f7140bf5 2021-10-17 thomas case CTRL('n'):
5842 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5843 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5844 1e37a5c2 2019-05-12 jcs s->selected_line++;
5845 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5846 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5848 07b0611c 2022-06-23 thomas view->count = 0;
5850 1c5e5faa 2022-06-23 thomas case 'c':
5851 1e37a5c2 2019-05-12 jcs case 'p': {
5852 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5854 07b0611c 2022-06-23 thomas view->count = 0;
5855 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5856 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5857 1e37a5c2 2019-05-12 jcs if (id == NULL)
5859 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5860 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5861 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5862 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5863 1e37a5c2 2019-05-12 jcs int obj_type;
5864 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5865 1e37a5c2 2019-05-12 jcs s->repo, id);
5868 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5869 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5870 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5871 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5874 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5875 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5876 ec242592 2022-04-22 thomas s->repo, &pid->id);
5877 945f9229 2022-04-16 thomas if (err)
5879 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5880 945f9229 2022-04-16 thomas pcommit, s->path);
5881 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5882 e5a0f69f 2018-08-18 stsp if (err) {
5883 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5884 1e37a5c2 2019-05-12 jcs err = NULL;
5885 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5888 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5890 1e37a5c2 2019-05-12 jcs free(blob_id);
5891 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5892 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5893 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5896 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5897 ec242592 2022-04-22 thomas &pid->id);
5898 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5900 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5901 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5903 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5908 1e37a5c2 2019-05-12 jcs s->done = 1;
5909 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5910 1e37a5c2 2019-05-12 jcs s->done = 0;
5911 1e37a5c2 2019-05-12 jcs if (thread_err)
5913 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5914 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5915 a5388363 2020-12-01 naddy err = run_blame(view);
5920 1c5e5faa 2022-06-23 thomas case 'C': {
5921 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5923 07b0611c 2022-06-23 thomas view->count = 0;
5924 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5925 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5927 1e37a5c2 2019-05-12 jcs s->done = 1;
5928 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5929 1e37a5c2 2019-05-12 jcs s->done = 0;
5930 1e37a5c2 2019-05-12 jcs if (thread_err)
5932 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5933 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5934 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5935 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5936 a5388363 2020-12-01 naddy err = run_blame(view);
5941 eaeaa612 2022-07-20 thomas case 'L': {
5942 eaeaa612 2022-07-20 thomas struct got_object_id *id = NULL;
5944 eaeaa612 2022-07-20 thomas view->count = 0;
5945 eaeaa612 2022-07-20 thomas id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5946 eaeaa612 2022-07-20 thomas s->first_displayed_line, s->selected_line);
5947 eaeaa612 2022-07-20 thomas if (id == NULL)
5950 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view))
5951 eaeaa612 2022-07-20 thomas view_get_split(view, &begin_y, &begin_x);
5952 eaeaa612 2022-07-20 thomas err = log_annotated_line(&log_view, begin_y, begin_x,
5953 eaeaa612 2022-07-20 thomas s->repo, id);
5954 eaeaa612 2022-07-20 thomas if (err)
5956 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view) &&
5957 eaeaa612 2022-07-20 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5958 eaeaa612 2022-07-20 thomas err = view_init_hsplit(view, begin_y);
5959 eaeaa612 2022-07-20 thomas if (err)
5963 eaeaa612 2022-07-20 thomas view->focussed = 0;
5964 eaeaa612 2022-07-20 thomas log_view->focussed = 1;
5965 eaeaa612 2022-07-20 thomas log_view->mode = view->mode;
5966 eaeaa612 2022-07-20 thomas log_view->nlines = view->lines - begin_y;
5967 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view)) {
5968 eaeaa612 2022-07-20 thomas view_transfer_size(log_view, view);
5969 eaeaa612 2022-07-20 thomas err = view_close_child(view);
5970 eaeaa612 2022-07-20 thomas if (err)
5971 eaeaa612 2022-07-20 thomas return err;
5972 eaeaa612 2022-07-20 thomas err = view_set_child(view, log_view);
5973 eaeaa612 2022-07-20 thomas if (err)
5974 eaeaa612 2022-07-20 thomas return err;
5975 eaeaa612 2022-07-20 thomas view->focus_child = 1;
5977 eaeaa612 2022-07-20 thomas *new_view = log_view;
5980 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5981 1e37a5c2 2019-05-12 jcs case '\r': {
5982 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5983 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5984 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5986 07b0611c 2022-06-23 thomas view->count = 0;
5987 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5988 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5989 1e37a5c2 2019-05-12 jcs if (id == NULL)
5991 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5994 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5995 4fc71f3b 2022-07-12 thomas if (*new_view) {
5996 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5997 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5998 4fc71f3b 2022-07-12 thomas if (err)
6000 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6001 4fc71f3b 2022-07-12 thomas } else {
6002 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6003 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6005 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6006 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6007 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6008 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6009 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6013 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6014 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6015 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6017 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6020 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6021 4fc71f3b 2022-07-12 thomas s->selected_line;
6022 4fc71f3b 2022-07-12 thomas if (*new_view)
6023 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6024 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6025 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6026 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6027 a5d43cac 2022-07-01 thomas if (err)
6031 e78dc838 2020-12-04 stsp view->focussed = 0;
6032 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6033 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6034 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6035 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6036 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6037 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6040 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6041 40236d76 2022-06-23 thomas if (err)
6043 e78dc838 2020-12-04 stsp view->focus_child = 1;
6045 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6050 70f17a53 2022-06-13 thomas case CTRL('d'):
6051 23427b14 2022-06-23 thomas case 'd':
6052 70f17a53 2022-06-13 thomas nscroll /= 2;
6053 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6054 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6055 ea025d1d 2020-02-22 naddy case CTRL('f'):
6056 1c5e5faa 2022-06-23 thomas case 'f':
6058 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6059 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6060 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6061 07b0611c 2022-06-23 thomas view->count = 0;
6064 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6065 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6066 70f17a53 2022-06-13 thomas s->selected_line +=
6067 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6068 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6070 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6071 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6073 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6074 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6076 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6077 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6078 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6079 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6083 07b0611c 2022-06-23 thomas view->count = 0;
6086 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6089 adf4c9e0 2022-07-03 thomas static const struct got_error *
6090 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6092 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6093 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6095 adf4c9e0 2022-07-03 thomas view->count = 0;
6096 adf4c9e0 2022-07-03 thomas s->done = 1;
6097 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6098 adf4c9e0 2022-07-03 thomas s->done = 0;
6099 adf4c9e0 2022-07-03 thomas if (err)
6100 adf4c9e0 2022-07-03 thomas return err;
6101 adf4c9e0 2022-07-03 thomas return run_blame(view);
6104 a70480e0 2018-06-23 stsp static const struct got_error *
6105 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6107 a70480e0 2018-06-23 stsp const struct got_error *error;
6108 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6109 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6110 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6111 0587e10c 2020-07-23 stsp char *link_target = NULL;
6112 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6113 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6114 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6116 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6117 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6119 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6120 a70480e0 2018-06-23 stsp switch (ch) {
6122 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6125 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6126 69069811 2018-08-02 stsp if (repo_path == NULL)
6127 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6131 17020d27 2019-03-07 stsp usage_blame();
6132 a70480e0 2018-06-23 stsp /* NOTREACHED */
6136 a70480e0 2018-06-23 stsp argc -= optind;
6137 a70480e0 2018-06-23 stsp argv += optind;
6139 f135c941 2020-02-20 stsp if (argc != 1)
6140 a70480e0 2018-06-23 stsp usage_blame();
6142 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6143 7cd52833 2022-06-23 thomas if (error != NULL)
6144 7cd52833 2022-06-23 thomas goto done;
6146 69069811 2018-08-02 stsp if (repo_path == NULL) {
6147 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6148 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6149 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6150 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6151 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6152 c156c7a4 2020-12-18 stsp goto done;
6153 f135c941 2020-02-20 stsp if (worktree)
6154 eb41ed75 2019-02-05 stsp repo_path =
6155 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6157 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6158 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6159 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6160 c156c7a4 2020-12-18 stsp goto done;
6164 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6165 c02c541e 2019-03-29 stsp if (error != NULL)
6166 8e94dd5b 2019-01-04 stsp goto done;
6168 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6169 f135c941 2020-02-20 stsp worktree);
6170 c02c541e 2019-03-29 stsp if (error)
6171 92205607 2019-01-04 stsp goto done;
6173 f135c941 2020-02-20 stsp init_curses();
6175 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6176 51a10b52 2020-12-26 stsp if (error)
6177 51a10b52 2020-12-26 stsp goto done;
6179 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6180 eb41ed75 2019-02-05 stsp if (error)
6181 69069811 2018-08-02 stsp goto done;
6183 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6184 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6185 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6186 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6187 a70480e0 2018-06-23 stsp if (error != NULL)
6188 66b4983c 2018-06-23 stsp goto done;
6189 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6190 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6192 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6193 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6195 a19e88aa 2018-06-23 stsp if (error != NULL)
6196 8b473291 2019-02-21 stsp goto done;
6198 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6199 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6200 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6201 e1cd8fed 2018-08-01 stsp goto done;
6204 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6205 945f9229 2022-04-16 thomas if (error)
6206 945f9229 2022-04-16 thomas goto done;
6208 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6209 945f9229 2022-04-16 thomas commit, repo);
6210 7cbe629d 2018-08-04 stsp if (error)
6211 7cbe629d 2018-08-04 stsp goto done;
6213 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6214 78756c87 2020-11-24 stsp commit_id, repo);
6215 0587e10c 2020-07-23 stsp if (error)
6216 0587e10c 2020-07-23 stsp goto done;
6217 12314ad4 2019-08-31 stsp if (worktree) {
6218 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6219 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6220 12314ad4 2019-08-31 stsp worktree = NULL;
6222 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6224 69069811 2018-08-02 stsp free(repo_path);
6225 f135c941 2020-02-20 stsp free(in_repo_path);
6226 0587e10c 2020-07-23 stsp free(link_target);
6227 69069811 2018-08-02 stsp free(cwd);
6228 a70480e0 2018-06-23 stsp free(commit_id);
6229 945f9229 2022-04-16 thomas if (commit)
6230 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6231 eb41ed75 2019-02-05 stsp if (worktree)
6232 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6233 1d0f4054 2021-06-17 stsp if (repo) {
6234 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6235 1d0f4054 2021-06-17 stsp if (error == NULL)
6236 1d0f4054 2021-06-17 stsp error = close_err;
6238 7cd52833 2022-06-23 thomas if (pack_fds) {
6239 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6240 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6241 7cd52833 2022-06-23 thomas if (error == NULL)
6242 7cd52833 2022-06-23 thomas error = pack_err;
6244 51a10b52 2020-12-26 stsp tog_free_refs();
6245 a70480e0 2018-06-23 stsp return error;
6248 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6249 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6251 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6252 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6253 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6254 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6255 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6256 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6257 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6259 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6260 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6261 a5d43cac 2022-07-01 thomas --limit; /* border */
6263 f7d12f7e 2018-08-01 stsp werase(view->window);
6265 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6266 ffd1d5e5 2018-06-23 stsp return NULL;
6268 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6271 ffd1d5e5 2018-06-23 stsp return err;
6272 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6273 a3404814 2018-09-02 stsp wstandout(view->window);
6274 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6276 11b20872 2019-11-08 stsp wattr_on(view->window,
6277 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6278 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6280 11b20872 2019-11-08 stsp wattr_off(view->window,
6281 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6282 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6283 a3404814 2018-09-02 stsp wstandend(view->window);
6284 2550e4c3 2018-07-13 stsp free(wline);
6285 2550e4c3 2018-07-13 stsp wline = NULL;
6286 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6287 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6288 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6289 ffd1d5e5 2018-06-23 stsp return NULL;
6290 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6293 ce52c690 2018-06-23 stsp return err;
6294 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6295 2550e4c3 2018-07-13 stsp free(wline);
6296 2550e4c3 2018-07-13 stsp wline = NULL;
6297 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6298 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6299 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6300 ffd1d5e5 2018-06-23 stsp return NULL;
6301 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6302 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6303 a1eca9bb 2018-06-23 stsp return NULL;
6305 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6306 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6307 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6308 0cf4efb1 2018-09-29 stsp if (view->focussed)
6309 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6310 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6312 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6313 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6314 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6315 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6316 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6317 ffd1d5e5 2018-06-23 stsp return NULL;
6321 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6324 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6325 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6326 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6327 848d6979 2019-08-12 stsp const char *modestr = "";
6328 56e0773d 2019-11-28 stsp mode_t mode;
6330 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6331 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6333 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6334 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6335 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6337 638f9024 2019-05-13 stsp return got_error_from_errno(
6338 230a42bd 2019-05-11 jcs "got_object_id_str");
6340 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6341 63c5ca5d 2019-08-24 stsp modestr = "$";
6342 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6345 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6346 d86d3b18 2020-12-01 naddy te, s->repo);
6347 0d6c6ee3 2020-05-20 stsp if (err) {
6348 0d6c6ee3 2020-05-20 stsp free(id_str);
6349 0d6c6ee3 2020-05-20 stsp return err;
6351 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6352 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6353 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6355 848d6979 2019-08-12 stsp modestr = "@";
6357 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6358 848d6979 2019-08-12 stsp modestr = "/";
6359 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6360 848d6979 2019-08-12 stsp modestr = "*";
6361 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6362 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6363 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6364 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6365 1d13200f 2018-07-12 stsp free(id_str);
6366 0d6c6ee3 2020-05-20 stsp free(link_target);
6367 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6369 1d13200f 2018-07-12 stsp free(id_str);
6370 0d6c6ee3 2020-05-20 stsp free(link_target);
6371 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6373 ffd1d5e5 2018-06-23 stsp if (err) {
6374 ffd1d5e5 2018-06-23 stsp free(line);
6377 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6378 0cf4efb1 2018-09-29 stsp if (view->focussed)
6379 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6380 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6382 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6384 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6385 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6386 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6388 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6389 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6390 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6391 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6392 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6393 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6394 ffd1d5e5 2018-06-23 stsp free(line);
6395 2550e4c3 2018-07-13 stsp free(wline);
6396 2550e4c3 2018-07-13 stsp wline = NULL;
6398 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6399 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6400 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6404 ffd1d5e5 2018-06-23 stsp return err;
6407 ffd1d5e5 2018-06-23 stsp static void
6408 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6410 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6411 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6412 fa86c4bf 2020-11-29 stsp int i = 0;
6414 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6417 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6418 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6419 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6420 fa86c4bf 2020-11-29 stsp if (!isroot)
6421 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6424 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6425 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6429 a5d43cac 2022-07-01 thomas static const struct got_error *
6430 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6432 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6433 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6434 ffd1d5e5 2018-06-23 stsp int n = 0;
6436 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6437 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6438 694d3271 2020-12-01 naddy s->first_displayed_entry);
6440 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6442 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6443 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6444 a5d43cac 2022-07-01 thomas if (last)
6445 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6446 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6447 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6448 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6452 a5d43cac 2022-07-01 thomas return NULL;
6455 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6456 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6457 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6459 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6460 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6461 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6463 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6464 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6465 56e0773d 2019-11-28 stsp + 1 /* slash */;
6467 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6469 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6470 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6471 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6473 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6474 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6475 d9765a41 2018-06-23 stsp while (pt) {
6476 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6477 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6478 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6479 cb2ebc8a 2018-06-23 stsp goto done;
6481 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6482 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6483 cb2ebc8a 2018-06-23 stsp goto done;
6485 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6488 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6489 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6490 ce52c690 2018-06-23 stsp goto done;
6494 ce52c690 2018-06-23 stsp if (err) {
6495 ce52c690 2018-06-23 stsp free(*path);
6496 ce52c690 2018-06-23 stsp *path = NULL;
6498 ce52c690 2018-06-23 stsp return err;
6501 ce52c690 2018-06-23 stsp static const struct got_error *
6502 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6503 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6504 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6506 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6507 ce52c690 2018-06-23 stsp char *path;
6508 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6510 a0de39f3 2019-08-09 stsp *new_view = NULL;
6512 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6514 ce52c690 2018-06-23 stsp return err;
6516 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6517 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6518 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6519 83ce39e3 2019-08-12 stsp goto done;
6522 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6523 e5a0f69f 2018-08-18 stsp if (err) {
6524 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6525 fc06ba56 2019-08-22 stsp err = NULL;
6526 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6528 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6530 83ce39e3 2019-08-12 stsp free(path);
6531 69efd4c4 2018-07-18 stsp return err;
6534 69efd4c4 2018-07-18 stsp static const struct got_error *
6535 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6536 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6538 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6539 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6540 69efd4c4 2018-07-18 stsp char *path;
6542 a0de39f3 2019-08-09 stsp *new_view = NULL;
6544 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6545 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6546 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6548 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6550 69efd4c4 2018-07-18 stsp return err;
6552 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6555 e5a0f69f 2018-08-18 stsp view_close(log_view);
6557 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6558 cb2ebc8a 2018-06-23 stsp free(path);
6559 cb2ebc8a 2018-06-23 stsp return err;
6562 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6563 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6564 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6566 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6567 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6568 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6569 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6571 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6572 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6574 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6575 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6576 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6578 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6580 bc573f3b 2021-07-10 stsp goto done;
6583 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6584 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6585 bc573f3b 2021-07-10 stsp * closed on demand.
6587 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6588 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6590 bc573f3b 2021-07-10 stsp goto done;
6591 bc573f3b 2021-07-10 stsp s->tree = s->root;
6593 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6594 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6595 ffd1d5e5 2018-06-23 stsp goto done;
6597 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6598 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6599 ffd1d5e5 2018-06-23 stsp goto done;
6602 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6603 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6604 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6605 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6606 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6607 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6608 9cd7cbd1 2020-12-07 stsp goto done;
6611 fb2756b9 2018-08-04 stsp s->repo = repo;
6613 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6614 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6615 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6616 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6618 c0b01bdb 2019-11-08 stsp goto done;
6619 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6620 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6622 c0b01bdb 2019-11-08 stsp goto done;
6623 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6624 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6625 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6627 c0b01bdb 2019-11-08 stsp goto done;
6629 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6630 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6631 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6633 11b20872 2019-11-08 stsp goto done;
6635 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6636 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6638 c0b01bdb 2019-11-08 stsp goto done;
6641 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6642 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6643 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6644 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6645 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6647 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6648 bc573f3b 2021-07-10 stsp if (commit)
6649 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6651 bc573f3b 2021-07-10 stsp close_tree_view(view);
6652 ad80ab7b 2018-08-04 stsp return err;
6655 e5a0f69f 2018-08-18 stsp static const struct got_error *
6656 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6658 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6660 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6661 fb2756b9 2018-08-04 stsp free(s->tree_label);
6662 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6663 6484ec90 2018-09-29 stsp free(s->commit_id);
6664 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6665 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6666 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6667 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6668 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6669 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6670 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6671 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6672 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6673 ad80ab7b 2018-08-04 stsp free(parent);
6676 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6677 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6678 bc573f3b 2021-07-10 stsp if (s->root)
6679 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6680 7c32bd05 2019-06-22 stsp return NULL;
6683 7c32bd05 2019-06-22 stsp static const struct got_error *
6684 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6686 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6688 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6689 7c32bd05 2019-06-22 stsp return NULL;
6692 7c32bd05 2019-06-22 stsp static int
6693 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6695 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6697 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6701 7c32bd05 2019-06-22 stsp static const struct got_error *
6702 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6704 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6705 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6707 7c32bd05 2019-06-22 stsp if (!view->searching) {
6708 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6709 7c32bd05 2019-06-22 stsp return NULL;
6712 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6713 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6714 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6715 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6716 56e0773d 2019-11-28 stsp s->selected_entry);
6718 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6720 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6721 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6723 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6724 56e0773d 2019-11-28 stsp s->selected_entry);
6727 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6728 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6729 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6730 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6732 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6735 7c32bd05 2019-06-22 stsp while (1) {
6736 56e0773d 2019-11-28 stsp if (te == NULL) {
6737 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6738 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6739 ac66afb8 2019-06-24 stsp return NULL;
6741 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6742 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6744 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6747 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6748 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6749 56e0773d 2019-11-28 stsp s->matched_entry = te;
6753 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6754 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6756 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6759 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6760 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6761 7c32bd05 2019-06-22 stsp s->selected = 0;
6764 e5a0f69f 2018-08-18 stsp return NULL;
6767 ad80ab7b 2018-08-04 stsp static const struct got_error *
6768 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6770 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6771 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6772 e5a0f69f 2018-08-18 stsp char *parent_path;
6774 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6776 e5a0f69f 2018-08-18 stsp return err;
6778 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6779 e5a0f69f 2018-08-18 stsp free(parent_path);
6781 a5d43cac 2022-07-01 thomas view_border(view);
6782 e5a0f69f 2018-08-18 stsp return err;
6785 e5a0f69f 2018-08-18 stsp static const struct got_error *
6786 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6788 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6789 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6790 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6791 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6792 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6794 e5a0f69f 2018-08-18 stsp switch (ch) {
6796 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6797 07b0611c 2022-06-23 thomas view->count = 0;
6800 07b0611c 2022-06-23 thomas view->count = 0;
6801 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6803 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6804 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6805 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6806 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6807 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6808 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6809 444d5325 2022-07-03 thomas if (err)
6812 e78dc838 2020-12-04 stsp view->focussed = 0;
6813 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6814 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6815 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6816 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6817 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6818 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6820 1e37a5c2 2019-05-12 jcs return err;
6821 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6822 40236d76 2022-06-23 thomas if (err)
6823 40236d76 2022-06-23 thomas return err;
6824 e78dc838 2020-12-04 stsp view->focus_child = 1;
6826 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6829 07b0611c 2022-06-23 thomas view->count = 0;
6830 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6831 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6832 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6833 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6834 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6835 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6836 152c1c93 2020-11-29 stsp if (err) {
6837 152c1c93 2020-11-29 stsp view_close(ref_view);
6838 152c1c93 2020-11-29 stsp return err;
6840 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6841 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6842 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6843 444d5325 2022-07-03 thomas if (err)
6846 e78dc838 2020-12-04 stsp view->focussed = 0;
6847 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6848 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6849 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6850 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6851 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6852 152c1c93 2020-11-29 stsp err = view_close_child(view);
6854 152c1c93 2020-11-29 stsp return err;
6855 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6856 40236d76 2022-06-23 thomas if (err)
6857 40236d76 2022-06-23 thomas return err;
6858 e78dc838 2020-12-04 stsp view->focus_child = 1;
6860 152c1c93 2020-11-29 stsp *new_view = ref_view;
6862 e4526bf5 2021-09-03 naddy case 'g':
6863 e4526bf5 2021-09-03 naddy case KEY_HOME:
6864 e4526bf5 2021-09-03 naddy s->selected = 0;
6865 07b0611c 2022-06-23 thomas view->count = 0;
6866 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6867 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6868 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6870 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6872 e4526bf5 2021-09-03 naddy case 'G':
6873 a5d43cac 2022-07-01 thomas case KEY_END: {
6874 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6876 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6877 a5d43cac 2022-07-01 thomas --eos; /* border */
6878 e4526bf5 2021-09-03 naddy s->selected = 0;
6879 07b0611c 2022-06-23 thomas view->count = 0;
6880 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6881 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6882 e4526bf5 2021-09-03 naddy if (te == NULL) {
6883 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6884 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6889 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6890 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6892 e4526bf5 2021-09-03 naddy if (n > 0)
6893 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6897 1e37a5c2 2019-05-12 jcs case KEY_UP:
6898 f7140bf5 2021-10-17 thomas case CTRL('p'):
6899 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6900 1e37a5c2 2019-05-12 jcs s->selected--;
6903 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6904 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6905 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6906 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6907 07b0611c 2022-06-23 thomas view->count = 0;
6909 70f17a53 2022-06-13 thomas case CTRL('u'):
6910 23427b14 2022-06-23 thomas case 'u':
6911 70f17a53 2022-06-13 thomas nscroll /= 2;
6912 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6913 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6914 ea025d1d 2020-02-22 naddy case CTRL('b'):
6915 1c5e5faa 2022-06-23 thomas case 'b':
6916 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6917 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6918 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6919 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6921 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6922 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6924 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6925 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6926 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6927 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6928 07b0611c 2022-06-23 thomas view->count = 0;
6931 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6932 f7140bf5 2021-10-17 thomas case CTRL('n'):
6933 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6934 1e37a5c2 2019-05-12 jcs s->selected++;
6937 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6938 07b0611c 2022-06-23 thomas == NULL) {
6939 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6940 07b0611c 2022-06-23 thomas view->count = 0;
6943 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6945 70f17a53 2022-06-13 thomas case CTRL('d'):
6946 23427b14 2022-06-23 thomas case 'd':
6947 70f17a53 2022-06-13 thomas nscroll /= 2;
6948 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6949 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6950 ea025d1d 2020-02-22 naddy case CTRL('f'):
6951 1c5e5faa 2022-06-23 thomas case 'f':
6952 4c2d69cb 2022-06-23 thomas case ' ':
6953 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6955 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6956 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6957 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6958 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6960 07b0611c 2022-06-23 thomas view->count = 0;
6963 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6965 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6967 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6968 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6969 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6970 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6971 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6972 07b0611c 2022-06-23 thomas view->count = 0;
6975 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6976 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6978 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6979 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6980 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6981 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6982 1e37a5c2 2019-05-12 jcs s->selected_entry =
6983 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6984 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6985 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6986 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6987 a5d43cac 2022-07-01 thomas if (err)
6990 1e37a5c2 2019-05-12 jcs free(parent);
6991 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6992 56e0773d 2019-11-28 stsp s->selected_entry))) {
6993 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6994 07b0611c 2022-06-23 thomas view->count = 0;
6995 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6996 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6999 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7000 941e9f74 2019-05-21 stsp if (err) {
7001 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7004 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7005 56e0773d 2019-11-28 stsp s->selected_entry))) {
7006 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7007 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
7009 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
7010 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7012 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
7013 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7014 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7018 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7019 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7020 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7021 a5d43cac 2022-07-01 thomas if (err)
7025 07b0611c 2022-06-23 thomas view->count = 0;
7026 e78dc838 2020-12-04 stsp view->focussed = 0;
7027 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7028 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
7029 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
7030 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7031 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
7032 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7034 669b5ffa 2018-10-07 stsp return err;
7035 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
7036 40236d76 2022-06-23 thomas if (err)
7037 40236d76 2022-06-23 thomas return err;
7038 e78dc838 2020-12-04 stsp view->focus_child = 1;
7040 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7043 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7044 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7045 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7046 07b0611c 2022-06-23 thomas view->count = 0;
7049 07b0611c 2022-06-23 thomas view->count = 0;
7053 ffd1d5e5 2018-06-23 stsp return err;
7056 ffd1d5e5 2018-06-23 stsp __dead static void
7057 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7060 91198554 2022-06-23 thomas fprintf(stderr,
7061 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7062 ffd1d5e5 2018-06-23 stsp getprogname());
7066 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7067 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7069 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7070 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7071 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7072 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7073 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7074 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7075 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7076 4e97c21c 2020-12-06 stsp char *label = NULL;
7077 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7078 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7080 5221c383 2018-08-01 stsp struct tog_view *view;
7081 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7083 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7084 ffd1d5e5 2018-06-23 stsp switch (ch) {
7086 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7089 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7090 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7091 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7095 e99e2d15 2020-11-24 naddy usage_tree();
7096 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7100 ffd1d5e5 2018-06-23 stsp argc -= optind;
7101 ffd1d5e5 2018-06-23 stsp argv += optind;
7103 55cccc34 2020-02-20 stsp if (argc > 1)
7104 e99e2d15 2020-11-24 naddy usage_tree();
7106 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7107 7cd52833 2022-06-23 thomas if (error != NULL)
7108 7cd52833 2022-06-23 thomas goto done;
7110 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7111 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7112 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7113 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7114 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7115 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7116 c156c7a4 2020-12-18 stsp goto done;
7117 55cccc34 2020-02-20 stsp if (worktree)
7118 52185f70 2019-02-05 stsp repo_path =
7119 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7121 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7122 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7123 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7124 c156c7a4 2020-12-18 stsp goto done;
7128 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7129 c02c541e 2019-03-29 stsp if (error != NULL)
7130 52185f70 2019-02-05 stsp goto done;
7132 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7133 55cccc34 2020-02-20 stsp repo, worktree);
7134 55cccc34 2020-02-20 stsp if (error)
7135 55cccc34 2020-02-20 stsp goto done;
7137 55cccc34 2020-02-20 stsp init_curses();
7139 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7140 c02c541e 2019-03-29 stsp if (error)
7141 52185f70 2019-02-05 stsp goto done;
7143 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7144 51a10b52 2020-12-26 stsp if (error)
7145 51a10b52 2020-12-26 stsp goto done;
7147 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7148 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7149 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7150 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7151 4e97c21c 2020-12-06 stsp if (error)
7152 4e97c21c 2020-12-06 stsp goto done;
7153 4e97c21c 2020-12-06 stsp head_ref_name = label;
7155 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7156 4e97c21c 2020-12-06 stsp if (error == NULL)
7157 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7158 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7159 4e97c21c 2020-12-06 stsp goto done;
7160 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7161 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7162 4e97c21c 2020-12-06 stsp if (error)
7163 4e97c21c 2020-12-06 stsp goto done;
7166 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7167 945f9229 2022-04-16 thomas if (error)
7168 945f9229 2022-04-16 thomas goto done;
7170 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7171 5221c383 2018-08-01 stsp if (view == NULL) {
7172 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7173 5221c383 2018-08-01 stsp goto done;
7175 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7176 ad80ab7b 2018-08-04 stsp if (error)
7177 ad80ab7b 2018-08-04 stsp goto done;
7178 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7179 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7180 d91faf3b 2020-12-01 naddy in_repo_path);
7181 55cccc34 2020-02-20 stsp if (error)
7182 55cccc34 2020-02-20 stsp goto done;
7185 55cccc34 2020-02-20 stsp if (worktree) {
7186 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7187 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7188 55cccc34 2020-02-20 stsp worktree = NULL;
7190 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7192 52185f70 2019-02-05 stsp free(repo_path);
7193 e4a0e26d 2020-02-20 stsp free(cwd);
7194 ffd1d5e5 2018-06-23 stsp free(commit_id);
7195 4e97c21c 2020-12-06 stsp free(label);
7197 486cd271 2020-12-06 stsp got_ref_close(ref);
7198 1d0f4054 2021-06-17 stsp if (repo) {
7199 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7200 1d0f4054 2021-06-17 stsp if (error == NULL)
7201 1d0f4054 2021-06-17 stsp error = close_err;
7203 7cd52833 2022-06-23 thomas if (pack_fds) {
7204 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7205 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7206 7cd52833 2022-06-23 thomas if (error == NULL)
7207 7cd52833 2022-06-23 thomas error = pack_err;
7209 51a10b52 2020-12-26 stsp tog_free_refs();
7210 ffd1d5e5 2018-06-23 stsp return error;
7213 6458efa5 2020-11-24 stsp static const struct got_error *
7214 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7216 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7217 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7219 6458efa5 2020-11-24 stsp s->nrefs = 0;
7220 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7221 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7222 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7223 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7224 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7227 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7228 6458efa5 2020-11-24 stsp if (re == NULL)
7229 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7231 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7232 8924d611 2020-12-26 stsp if (re->ref == NULL)
7233 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7234 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7235 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7238 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7239 6458efa5 2020-11-24 stsp return NULL;
7242 ef20f542 2022-06-26 thomas static void
7243 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7245 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7247 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7248 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7249 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7250 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7255 6458efa5 2020-11-24 stsp static const struct got_error *
7256 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7258 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7259 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7261 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7262 6458efa5 2020-11-24 stsp s->repo = repo;
7264 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7265 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7267 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7269 6458efa5 2020-11-24 stsp return err;
7271 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7272 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7273 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7274 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7276 6458efa5 2020-11-24 stsp goto done;
7278 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7279 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7280 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7282 6458efa5 2020-11-24 stsp goto done;
7284 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7285 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7286 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7287 2183bbf6 2022-01-23 thomas if (err)
7288 2183bbf6 2022-01-23 thomas goto done;
7290 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7291 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7292 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7294 6458efa5 2020-11-24 stsp goto done;
7297 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7298 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7299 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7300 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7301 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7304 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7305 6458efa5 2020-11-24 stsp return err;
7308 6458efa5 2020-11-24 stsp static const struct got_error *
7309 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7311 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7313 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7314 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7316 6458efa5 2020-11-24 stsp return NULL;
7319 6458efa5 2020-11-24 stsp static const struct got_error *
7320 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7321 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7323 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7324 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7325 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7326 6458efa5 2020-11-24 stsp int obj_type;
7328 c42c9805 2020-11-24 stsp *commit_id = NULL;
7330 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7332 6458efa5 2020-11-24 stsp return err;
7334 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7336 6458efa5 2020-11-24 stsp goto done;
7338 6458efa5 2020-11-24 stsp switch (obj_type) {
7339 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7340 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7342 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7343 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7345 6458efa5 2020-11-24 stsp goto done;
7346 c42c9805 2020-11-24 stsp free(obj_id);
7347 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7348 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7350 6458efa5 2020-11-24 stsp goto done;
7351 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7352 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7353 c42c9805 2020-11-24 stsp goto done;
7355 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7356 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7357 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7358 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7359 c42c9805 2020-11-24 stsp goto done;
7363 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7369 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7370 c42c9805 2020-11-24 stsp if (err) {
7371 c42c9805 2020-11-24 stsp free(*commit_id);
7372 c42c9805 2020-11-24 stsp *commit_id = NULL;
7374 c42c9805 2020-11-24 stsp return err;
7377 c42c9805 2020-11-24 stsp static const struct got_error *
7378 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7379 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7381 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7382 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7383 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7385 c42c9805 2020-11-24 stsp *new_view = NULL;
7387 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7388 c42c9805 2020-11-24 stsp if (err) {
7389 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7390 c42c9805 2020-11-24 stsp return err;
7392 c42c9805 2020-11-24 stsp return NULL;
7395 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7396 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7397 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7398 6458efa5 2020-11-24 stsp goto done;
7401 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7402 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7405 6458efa5 2020-11-24 stsp view_close(log_view);
7407 6458efa5 2020-11-24 stsp *new_view = log_view;
7408 c42c9805 2020-11-24 stsp free(commit_id);
7409 6458efa5 2020-11-24 stsp return err;
7412 ce5b7c56 2019-07-09 stsp static void
7413 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7415 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7416 34ba6917 2020-11-30 stsp int i = 0;
7418 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7421 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7422 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7423 34ba6917 2020-11-30 stsp if (re == NULL)
7425 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7426 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7430 a5d43cac 2022-07-01 thomas static const struct got_error *
7431 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7433 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7434 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7435 6458efa5 2020-11-24 stsp int n = 0;
7437 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7438 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7440 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7442 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7443 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7444 a5d43cac 2022-07-01 thomas if (last)
7445 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7446 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7447 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7448 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7452 a5d43cac 2022-07-01 thomas return NULL;
7455 6458efa5 2020-11-24 stsp static const struct got_error *
7456 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7458 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7460 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7461 6458efa5 2020-11-24 stsp return NULL;
7464 6458efa5 2020-11-24 stsp static int
7465 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7467 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7469 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7473 6458efa5 2020-11-24 stsp static const struct got_error *
7474 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7476 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7479 6458efa5 2020-11-24 stsp if (!view->searching) {
7480 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7481 6458efa5 2020-11-24 stsp return NULL;
7484 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7485 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7486 6458efa5 2020-11-24 stsp if (s->selected_entry)
7487 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7489 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7490 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7492 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7493 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7495 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7496 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7499 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7500 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7501 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7502 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7504 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7507 6458efa5 2020-11-24 stsp while (1) {
7508 6458efa5 2020-11-24 stsp if (re == NULL) {
7509 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7510 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7511 6458efa5 2020-11-24 stsp return NULL;
7513 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7514 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7516 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7519 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7520 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7521 6458efa5 2020-11-24 stsp s->matched_entry = re;
7525 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7526 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7528 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7531 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7532 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7533 6458efa5 2020-11-24 stsp s->selected = 0;
7536 6458efa5 2020-11-24 stsp return NULL;
7539 6458efa5 2020-11-24 stsp static const struct got_error *
7540 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7542 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7543 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7544 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7545 6458efa5 2020-11-24 stsp char *line = NULL;
7546 6458efa5 2020-11-24 stsp wchar_t *wline;
7547 6458efa5 2020-11-24 stsp struct tog_color *tc;
7548 6458efa5 2020-11-24 stsp int width, n;
7549 6458efa5 2020-11-24 stsp int limit = view->nlines;
7551 6458efa5 2020-11-24 stsp werase(view->window);
7553 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7554 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7555 a5d43cac 2022-07-01 thomas --limit; /* border */
7557 6458efa5 2020-11-24 stsp if (limit == 0)
7558 6458efa5 2020-11-24 stsp return NULL;
7560 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7562 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7563 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7564 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7566 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7567 6458efa5 2020-11-24 stsp if (err) {
7568 6458efa5 2020-11-24 stsp free(line);
7569 6458efa5 2020-11-24 stsp return err;
7571 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7572 6458efa5 2020-11-24 stsp wstandout(view->window);
7573 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7574 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7575 6458efa5 2020-11-24 stsp wstandend(view->window);
7576 6458efa5 2020-11-24 stsp free(wline);
7577 6458efa5 2020-11-24 stsp wline = NULL;
7578 6458efa5 2020-11-24 stsp free(line);
7579 6458efa5 2020-11-24 stsp line = NULL;
7580 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7581 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7582 6458efa5 2020-11-24 stsp if (--limit <= 0)
7583 6458efa5 2020-11-24 stsp return NULL;
7586 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7587 6458efa5 2020-11-24 stsp char *line = NULL;
7588 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7590 84227eb1 2022-06-23 thomas if (s->show_date) {
7591 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7592 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7593 84227eb1 2022-06-23 thomas struct got_object_id *id;
7594 84227eb1 2022-06-23 thomas struct tm tm;
7595 84227eb1 2022-06-23 thomas time_t t;
7597 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7598 84227eb1 2022-06-23 thomas if (err)
7599 84227eb1 2022-06-23 thomas return err;
7600 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7601 84227eb1 2022-06-23 thomas if (err) {
7602 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7603 84227eb1 2022-06-23 thomas free(id);
7604 84227eb1 2022-06-23 thomas return err;
7606 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7608 84227eb1 2022-06-23 thomas if (err) {
7609 84227eb1 2022-06-23 thomas free(id);
7610 84227eb1 2022-06-23 thomas return err;
7612 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7613 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7614 84227eb1 2022-06-23 thomas } else {
7615 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7616 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7618 84227eb1 2022-06-23 thomas free(id);
7619 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7620 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7621 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7622 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7624 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7625 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7626 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7627 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7628 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7629 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7630 6458efa5 2020-11-24 stsp struct got_object_id *id;
7631 6458efa5 2020-11-24 stsp char *id_str;
7632 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7634 6458efa5 2020-11-24 stsp return err;
7635 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7636 6458efa5 2020-11-24 stsp if (err) {
7638 6458efa5 2020-11-24 stsp return err;
7640 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7641 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7642 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7644 6458efa5 2020-11-24 stsp free(id_str);
7645 6458efa5 2020-11-24 stsp return err;
7648 6458efa5 2020-11-24 stsp free(id_str);
7649 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7650 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7651 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7653 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7655 6458efa5 2020-11-24 stsp if (err) {
7656 6458efa5 2020-11-24 stsp free(line);
7657 6458efa5 2020-11-24 stsp return err;
7659 6458efa5 2020-11-24 stsp if (n == s->selected) {
7660 6458efa5 2020-11-24 stsp if (view->focussed)
7661 6458efa5 2020-11-24 stsp wstandout(view->window);
7662 6458efa5 2020-11-24 stsp s->selected_entry = re;
7664 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7666 6458efa5 2020-11-24 stsp wattr_on(view->window,
7667 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7668 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7670 6458efa5 2020-11-24 stsp wattr_off(view->window,
7671 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7672 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7673 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7674 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7675 6458efa5 2020-11-24 stsp wstandend(view->window);
7676 6458efa5 2020-11-24 stsp free(line);
7677 6458efa5 2020-11-24 stsp free(wline);
7678 6458efa5 2020-11-24 stsp wline = NULL;
7680 6458efa5 2020-11-24 stsp s->ndisplayed++;
7681 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7684 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7687 a5d43cac 2022-07-01 thomas view_border(view);
7688 6458efa5 2020-11-24 stsp return err;
7691 6458efa5 2020-11-24 stsp static const struct got_error *
7692 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7693 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7695 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7696 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7697 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7699 c42c9805 2020-11-24 stsp *new_view = NULL;
7701 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7702 c42c9805 2020-11-24 stsp if (err) {
7703 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7704 c42c9805 2020-11-24 stsp return err;
7706 c42c9805 2020-11-24 stsp return NULL;
7710 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7711 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7712 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7713 c42c9805 2020-11-24 stsp goto done;
7716 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7717 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7719 c42c9805 2020-11-24 stsp goto done;
7721 c42c9805 2020-11-24 stsp *new_view = tree_view;
7723 c42c9805 2020-11-24 stsp free(commit_id);
7724 c42c9805 2020-11-24 stsp return err;
7726 c42c9805 2020-11-24 stsp static const struct got_error *
7727 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7729 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7730 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7731 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7732 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7733 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7735 6458efa5 2020-11-24 stsp switch (ch) {
7737 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7738 07b0611c 2022-06-23 thomas view->count = 0;
7740 84227eb1 2022-06-23 thomas case 'm':
7741 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7742 07b0611c 2022-06-23 thomas view->count = 0;
7744 98182bd0 2021-11-20 thomas case 'o':
7745 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7746 07b0611c 2022-06-23 thomas view->count = 0;
7747 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7748 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7749 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7750 c591440f 2021-11-20 thomas if (err)
7752 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7753 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7754 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7755 3bfadbd4 2021-11-20 thomas if (err)
7757 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7758 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7760 6458efa5 2020-11-24 stsp case KEY_ENTER:
7761 6458efa5 2020-11-24 stsp case '\r':
7762 07b0611c 2022-06-23 thomas view->count = 0;
7763 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7765 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7766 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7768 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7769 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7770 a5d43cac 2022-07-01 thomas if (err)
7773 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7774 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7775 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7776 a5d43cac 2022-07-01 thomas if (err)
7780 e78dc838 2020-12-04 stsp view->focussed = 0;
7781 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7782 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7783 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7784 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7785 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7786 6458efa5 2020-11-24 stsp err = view_close_child(view);
7788 6458efa5 2020-11-24 stsp return err;
7789 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7790 40236d76 2022-06-23 thomas if (err)
7791 40236d76 2022-06-23 thomas return err;
7792 e78dc838 2020-12-04 stsp view->focus_child = 1;
7794 6458efa5 2020-11-24 stsp *new_view = log_view;
7797 07b0611c 2022-06-23 thomas view->count = 0;
7798 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7800 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7801 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7802 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7803 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7804 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7806 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7807 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7808 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7809 444d5325 2022-07-03 thomas if (err)
7812 e78dc838 2020-12-04 stsp view->focussed = 0;
7813 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7814 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7815 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7816 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7817 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7818 c42c9805 2020-11-24 stsp err = view_close_child(view);
7820 c42c9805 2020-11-24 stsp return err;
7821 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7822 40236d76 2022-06-23 thomas if (err)
7823 40236d76 2022-06-23 thomas return err;
7824 e78dc838 2020-12-04 stsp view->focus_child = 1;
7826 c42c9805 2020-11-24 stsp *new_view = tree_view;
7828 e4526bf5 2021-09-03 naddy case 'g':
7829 e4526bf5 2021-09-03 naddy case KEY_HOME:
7830 e4526bf5 2021-09-03 naddy s->selected = 0;
7831 07b0611c 2022-06-23 thomas view->count = 0;
7832 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7834 e4526bf5 2021-09-03 naddy case 'G':
7835 a5d43cac 2022-07-01 thomas case KEY_END: {
7836 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7838 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7839 a5d43cac 2022-07-01 thomas --eos; /* border */
7840 e4526bf5 2021-09-03 naddy s->selected = 0;
7841 07b0611c 2022-06-23 thomas view->count = 0;
7842 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7843 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7844 e4526bf5 2021-09-03 naddy if (re == NULL)
7846 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7847 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7849 e4526bf5 2021-09-03 naddy if (n > 0)
7850 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7854 6458efa5 2020-11-24 stsp case KEY_UP:
7855 f7140bf5 2021-10-17 thomas case CTRL('p'):
7856 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7857 6458efa5 2020-11-24 stsp s->selected--;
7860 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7861 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7862 07b0611c 2022-06-23 thomas view->count = 0;
7864 70f17a53 2022-06-13 thomas case CTRL('u'):
7865 23427b14 2022-06-23 thomas case 'u':
7866 70f17a53 2022-06-13 thomas nscroll /= 2;
7867 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7868 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7869 6458efa5 2020-11-24 stsp case CTRL('b'):
7870 1c5e5faa 2022-06-23 thomas case 'b':
7871 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7872 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7873 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7874 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7875 07b0611c 2022-06-23 thomas view->count = 0;
7878 6458efa5 2020-11-24 stsp case KEY_DOWN:
7879 f7140bf5 2021-10-17 thomas case CTRL('n'):
7880 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7881 6458efa5 2020-11-24 stsp s->selected++;
7884 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7885 6458efa5 2020-11-24 stsp /* can't scroll any further */
7886 07b0611c 2022-06-23 thomas view->count = 0;
7889 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7891 70f17a53 2022-06-13 thomas case CTRL('d'):
7892 23427b14 2022-06-23 thomas case 'd':
7893 70f17a53 2022-06-13 thomas nscroll /= 2;
7894 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7895 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7896 6458efa5 2020-11-24 stsp case CTRL('f'):
7897 1c5e5faa 2022-06-23 thomas case 'f':
7898 4c2d69cb 2022-06-23 thomas case ' ':
7899 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7900 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7901 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7902 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7903 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7904 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7905 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7906 07b0611c 2022-06-23 thomas view->count = 0;
7909 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7911 6458efa5 2020-11-24 stsp case CTRL('l'):
7912 07b0611c 2022-06-23 thomas view->count = 0;
7913 8924d611 2020-12-26 stsp tog_free_refs();
7914 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7917 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7918 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7920 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7921 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7922 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7925 07b0611c 2022-06-23 thomas view->count = 0;
7929 6458efa5 2020-11-24 stsp return err;
7932 6458efa5 2020-11-24 stsp __dead static void
7933 6458efa5 2020-11-24 stsp usage_ref(void)
7936 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7937 6458efa5 2020-11-24 stsp getprogname());
7941 6458efa5 2020-11-24 stsp static const struct got_error *
7942 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7944 6458efa5 2020-11-24 stsp const struct got_error *error;
7945 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7946 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7947 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7949 6458efa5 2020-11-24 stsp struct tog_view *view;
7950 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7952 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7953 6458efa5 2020-11-24 stsp switch (ch) {
7955 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7956 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7957 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7961 e99e2d15 2020-11-24 naddy usage_ref();
7962 6458efa5 2020-11-24 stsp /* NOTREACHED */
7966 6458efa5 2020-11-24 stsp argc -= optind;
7967 6458efa5 2020-11-24 stsp argv += optind;
7969 6458efa5 2020-11-24 stsp if (argc > 1)
7970 e99e2d15 2020-11-24 naddy usage_ref();
7972 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7973 7cd52833 2022-06-23 thomas if (error != NULL)
7974 7cd52833 2022-06-23 thomas goto done;
7976 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7977 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7978 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7979 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7980 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7981 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7982 c156c7a4 2020-12-18 stsp goto done;
7983 6458efa5 2020-11-24 stsp if (worktree)
7984 6458efa5 2020-11-24 stsp repo_path =
7985 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7987 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7988 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7989 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7990 c156c7a4 2020-12-18 stsp goto done;
7994 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7995 6458efa5 2020-11-24 stsp if (error != NULL)
7996 6458efa5 2020-11-24 stsp goto done;
7998 6458efa5 2020-11-24 stsp init_curses();
8000 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8001 51a10b52 2020-12-26 stsp if (error)
8002 51a10b52 2020-12-26 stsp goto done;
8004 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8005 6458efa5 2020-11-24 stsp if (error)
8006 6458efa5 2020-11-24 stsp goto done;
8008 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8009 6458efa5 2020-11-24 stsp if (view == NULL) {
8010 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8011 6458efa5 2020-11-24 stsp goto done;
8014 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8015 6458efa5 2020-11-24 stsp if (error)
8016 6458efa5 2020-11-24 stsp goto done;
8018 6458efa5 2020-11-24 stsp if (worktree) {
8019 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8020 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8021 6458efa5 2020-11-24 stsp worktree = NULL;
8023 6458efa5 2020-11-24 stsp error = view_loop(view);
8025 6458efa5 2020-11-24 stsp free(repo_path);
8026 6458efa5 2020-11-24 stsp free(cwd);
8027 1d0f4054 2021-06-17 stsp if (repo) {
8028 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8029 1d0f4054 2021-06-17 stsp if (close_err)
8030 1d0f4054 2021-06-17 stsp error = close_err;
8032 7cd52833 2022-06-23 thomas if (pack_fds) {
8033 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8034 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8035 7cd52833 2022-06-23 thomas if (error == NULL)
8036 7cd52833 2022-06-23 thomas error = pack_err;
8038 51a10b52 2020-12-26 stsp tog_free_refs();
8039 6458efa5 2020-11-24 stsp return error;
8043 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8044 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8046 6458efa5 2020-11-24 stsp static void
8047 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8049 a5d43cac 2022-07-01 thomas switch (view->type) {
8050 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8051 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8052 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8053 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8057 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8058 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8060 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8061 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8064 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8065 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8066 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8068 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8069 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8070 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8072 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8073 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8074 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8076 a5d43cac 2022-07-01 thomas default:
8080 a5d43cac 2022-07-01 thomas view->offset = 0;
8084 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8085 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8087 a5d43cac 2022-07-01 thomas static const struct got_error *
8088 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8090 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8091 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8092 a5d43cac 2022-07-01 thomas int *selected = NULL;
8093 a5d43cac 2022-07-01 thomas int header, offset;
8095 a5d43cac 2022-07-01 thomas switch (view->type) {
8096 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8097 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8098 a5d43cac 2022-07-01 thomas header = 3;
8099 a5d43cac 2022-07-01 thomas scrolld = NULL;
8100 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8101 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8102 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8103 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8104 a5d43cac 2022-07-01 thomas view->offset = offset;
8108 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8109 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8110 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8111 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8112 a5d43cac 2022-07-01 thomas selected = &s->selected;
8115 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8116 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8117 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8118 a5d43cac 2022-07-01 thomas header = 3;
8119 a5d43cac 2022-07-01 thomas selected = &s->selected;
8122 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8123 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8124 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8125 a5d43cac 2022-07-01 thomas header = 5;
8126 a5d43cac 2022-07-01 thomas selected = &s->selected;
8129 a5d43cac 2022-07-01 thomas default:
8130 a5d43cac 2022-07-01 thomas selected = NULL;
8131 a5d43cac 2022-07-01 thomas scrolld = NULL;
8132 a5d43cac 2022-07-01 thomas header = 0;
8136 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8137 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8138 a5d43cac 2022-07-01 thomas view->offset = offset;
8139 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8140 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8141 a5d43cac 2022-07-01 thomas *selected -= offset;
8145 a5d43cac 2022-07-01 thomas return err;
8148 a5d43cac 2022-07-01 thomas static void
8149 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8153 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8154 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8155 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8156 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8158 6879ba42 2020-10-01 naddy fputc('\n', fp);
8161 4ed7e80c 2018-05-20 stsp __dead static void
8162 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8164 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8166 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8167 6879ba42 2020-10-01 naddy getprogname());
8168 6879ba42 2020-10-01 naddy if (hflag) {
8169 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8170 6879ba42 2020-10-01 naddy list_commands(fp);
8172 6879ba42 2020-10-01 naddy exit(status);
8175 c2301be8 2018-04-30 stsp static char **
8176 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8178 ee85c5e8 2020-02-29 stsp va_list ap;
8179 c2301be8 2018-04-30 stsp char **argv;
8182 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8184 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8185 c2301be8 2018-04-30 stsp if (argv == NULL)
8186 c2301be8 2018-04-30 stsp err(1, "calloc");
8187 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8188 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8189 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8190 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8193 ee85c5e8 2020-02-29 stsp va_end(ap);
8194 c2301be8 2018-04-30 stsp return argv;
8198 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8199 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8200 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8201 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8202 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8204 ee85c5e8 2020-02-29 stsp static const struct got_error *
8205 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8207 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8208 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8209 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8210 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8211 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8212 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8213 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8214 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8215 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8217 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8218 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8219 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8221 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8222 7cd52833 2022-06-23 thomas if (error != NULL)
8223 7cd52833 2022-06-23 thomas goto done;
8225 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8226 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8227 ee85c5e8 2020-02-29 stsp goto done;
8229 ee85c5e8 2020-02-29 stsp if (worktree)
8230 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8232 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8233 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8234 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8235 ee85c5e8 2020-02-29 stsp goto done;
8238 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8239 ee85c5e8 2020-02-29 stsp if (error != NULL)
8240 ee85c5e8 2020-02-29 stsp goto done;
8242 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8243 ee85c5e8 2020-02-29 stsp repo, worktree);
8244 ee85c5e8 2020-02-29 stsp if (error)
8245 ee85c5e8 2020-02-29 stsp goto done;
8247 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8248 84de9106 2020-12-26 stsp if (error)
8249 84de9106 2020-12-26 stsp goto done;
8250 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8251 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8252 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8253 ee85c5e8 2020-02-29 stsp if (error)
8254 ee85c5e8 2020-02-29 stsp goto done;
8256 ee85c5e8 2020-02-29 stsp if (worktree) {
8257 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8258 ee85c5e8 2020-02-29 stsp worktree = NULL;
8261 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8262 945f9229 2022-04-16 thomas if (error)
8263 945f9229 2022-04-16 thomas goto done;
8265 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8266 ee85c5e8 2020-02-29 stsp if (error) {
8267 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8268 ee85c5e8 2020-02-29 stsp goto done;
8269 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8270 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8271 6879ba42 2020-10-01 naddy usage(1, 1);
8272 ee85c5e8 2020-02-29 stsp /* not reached */
8275 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8276 1d0f4054 2021-06-17 stsp if (error == NULL)
8277 1d0f4054 2021-06-17 stsp error = close_err;
8278 ee85c5e8 2020-02-29 stsp repo = NULL;
8280 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8281 ee85c5e8 2020-02-29 stsp if (error)
8282 ee85c5e8 2020-02-29 stsp goto done;
8284 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8286 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8287 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8289 1d0f4054 2021-06-17 stsp if (repo) {
8290 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8291 1d0f4054 2021-06-17 stsp if (error == NULL)
8292 1d0f4054 2021-06-17 stsp error = close_err;
8294 945f9229 2022-04-16 thomas if (commit)
8295 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8296 ee85c5e8 2020-02-29 stsp if (worktree)
8297 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8298 7cd52833 2022-06-23 thomas if (pack_fds) {
8299 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8300 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8301 7cd52833 2022-06-23 thomas if (error == NULL)
8302 7cd52833 2022-06-23 thomas error = pack_err;
8305 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8306 ee85c5e8 2020-02-29 stsp free(commit_id);
8307 ee85c5e8 2020-02-29 stsp free(cwd);
8308 ee85c5e8 2020-02-29 stsp free(repo_path);
8309 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8310 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8312 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8313 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8314 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8316 87670572 2020-12-26 naddy tog_free_refs();
8317 ee85c5e8 2020-02-29 stsp return error;
8321 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8323 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8324 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8325 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8326 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8327 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8328 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8329 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8331 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8333 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8335 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8336 9f7d7167 2018-04-29 stsp switch (ch) {
8338 9f7d7167 2018-04-29 stsp hflag = 1;
8341 53ccebc2 2019-07-30 stsp Vflag = 1;
8344 6879ba42 2020-10-01 naddy usage(hflag, 1);
8345 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8349 9f7d7167 2018-04-29 stsp argc -= optind;
8350 9f7d7167 2018-04-29 stsp argv += optind;
8351 9814e6a3 2020-09-27 naddy optind = 1;
8352 c2301be8 2018-04-30 stsp optreset = 1;
8354 53ccebc2 2019-07-30 stsp if (Vflag) {
8355 53ccebc2 2019-07-30 stsp got_version_print_str();
8356 6879ba42 2020-10-01 naddy return 0;
8359 4010e238 2020-12-04 stsp #ifndef PROFILE
8360 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8361 4010e238 2020-12-04 stsp NULL) == -1)
8362 4010e238 2020-12-04 stsp err(1, "pledge");
8365 c2301be8 2018-04-30 stsp if (argc == 0) {
8366 f29d3e89 2018-06-23 stsp if (hflag)
8367 6879ba42 2020-10-01 naddy usage(hflag, 0);
8368 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8369 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8371 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8375 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8376 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8377 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8378 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8379 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8385 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8386 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8387 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8388 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8389 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8390 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8393 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8394 ee85c5e8 2020-02-29 stsp if (argc != 1)
8395 6879ba42 2020-10-01 naddy usage(0, 1);
8396 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8397 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8399 ee85c5e8 2020-02-29 stsp if (hflag)
8400 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8402 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8406 b46c1e04 2020-09-20 naddy putchar('\n');
8407 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8409 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8410 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8411 a2f4a359 2020-02-28 stsp free(cmd_argv);
8414 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8415 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);