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);
321 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
322 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
323 3dbaef42 2020-11-24 stsp const char *label1, *label2;
324 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
325 19a6a6b5 2022-07-01 thomas int fd1, fd2;
326 82c78e96 2022-08-06 thomas int lineno;
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 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey int matched_line;
337 f44b1f58 2020-02-02 tracey int selected_line;
339 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
340 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
343 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
346 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
347 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
348 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
349 1a76625f 2018-10-22 stsp int commits_needed;
350 fb280deb 2021-08-30 stsp int load_all;
351 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
352 1a76625f 2018-10-22 stsp struct commit_queue *commits;
353 1a76625f 2018-10-22 stsp const char *in_repo_path;
354 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
355 1a76625f 2018-10-22 stsp struct got_repository *repo;
356 1af5eddf 2022-06-23 thomas int *pack_fds;
357 1a76625f 2018-10-22 stsp int log_complete;
358 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
359 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
361 13add988 2019-10-15 stsp int *searching;
362 13add988 2019-10-15 stsp int *search_next_done;
363 13add988 2019-10-15 stsp regex_t *regex;
366 1a76625f 2018-10-22 stsp struct tog_log_view_state {
367 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
368 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
371 b01e7d3b 2018-08-04 stsp int selected;
372 b01e7d3b 2018-08-04 stsp char *in_repo_path;
373 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
374 b672a97a 2020-01-27 stsp int log_branches;
375 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
376 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
377 1a76625f 2018-10-22 stsp sig_atomic_t quit;
378 1a76625f 2018-10-22 stsp pthread_t thread;
379 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
380 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
381 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
382 11b20872 2019-11-08 stsp struct tog_colors colors;
383 f69c5a46 2022-07-19 thomas int use_committer;
386 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
390 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
394 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
395 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
396 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
397 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
400 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
402 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
403 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
404 e9424729 2018-08-04 stsp int nlines;
406 e9424729 2018-08-04 stsp struct tog_view *view;
407 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
412 e9424729 2018-08-04 stsp const char *path;
413 e9424729 2018-08-04 stsp struct got_repository *repo;
414 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
415 e9424729 2018-08-04 stsp int *complete;
416 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
417 fc06ba56 2019-08-22 stsp void *cancel_arg;
420 e9424729 2018-08-04 stsp struct tog_blame {
422 be659d10 2020-11-18 stsp off_t filesize;
423 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
424 6fcac457 2018-11-19 stsp int nlines;
425 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
426 e9424729 2018-08-04 stsp pthread_t thread;
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
428 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
429 e9424729 2018-08-04 stsp const char *path;
430 7cd52833 2022-06-23 thomas int *pack_fds;
433 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
434 7cbe629d 2018-08-04 stsp int first_displayed_line;
435 7cbe629d 2018-08-04 stsp int last_displayed_line;
436 7cbe629d 2018-08-04 stsp int selected_line;
437 4fc71f3b 2022-07-12 thomas int last_diffed_line;
438 7cbe629d 2018-08-04 stsp int blame_complete;
441 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
442 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
443 e5a0f69f 2018-08-18 stsp char *path;
444 7cbe629d 2018-08-04 stsp struct got_repository *repo;
445 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
446 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
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 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
526 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
527 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
528 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
530 669b5ffa 2018-10-07 stsp struct tog_view *parent;
531 669b5ffa 2018-10-07 stsp struct tog_view *child;
534 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
535 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
536 e78dc838 2020-12-04 stsp * between parent and child.
537 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
538 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
539 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
540 e78dc838 2020-12-04 stsp * situations.
542 e78dc838 2020-12-04 stsp int focus_child;
544 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
545 5dc9f4bc 2018-08-04 stsp /* type-specific state */
546 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
548 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
549 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
550 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
551 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
552 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
557 e78dc838 2020-12-04 stsp struct tog_view *, int);
558 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
559 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
560 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
563 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
564 c0c4acc8 2021-01-24 stsp int search_started;
565 60493ae3 2019-06-20 stsp int searching;
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
567 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
568 60493ae3 2019-06-20 stsp int search_next_done;
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
570 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
571 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
572 1803e47f 2019-06-22 stsp regex_t regex;
573 41605754 2020-11-12 stsp regmatch_t regmatch;
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
577 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
578 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
579 78756c87 2020-11-24 stsp struct got_repository *);
580 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
582 e78dc838 2020-12-04 stsp struct tog_view *, int);
583 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
584 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
586 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
588 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
589 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
590 78756c87 2020-11-24 stsp const char *, const char *, int);
591 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
593 e78dc838 2020-12-04 stsp struct tog_view *, int);
594 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
597 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
600 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
601 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
602 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
603 e78dc838 2020-12-04 stsp struct tog_view *, int);
604 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
605 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
607 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
609 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
610 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
611 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
612 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
613 e78dc838 2020-12-04 stsp struct tog_view *, int);
614 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
616 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
618 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
619 6458efa5 2020-11-24 stsp struct got_repository *);
620 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
621 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
622 e78dc838 2020-12-04 stsp struct tog_view *, int);
623 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
625 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
627 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
628 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
629 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
631 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
633 25791caa 2018-10-24 stsp static void
634 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
636 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
639 83baff54 2019-08-12 stsp static void
640 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
642 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
645 61266923 2020-01-14 stsp static void
646 61266923 2020-01-14 stsp tog_sigcont(int signo)
648 61266923 2020-01-14 stsp tog_sigcont_received = 1;
651 296152d1 2022-05-31 thomas static void
652 296152d1 2022-05-31 thomas tog_sigint(int signo)
654 296152d1 2022-05-31 thomas tog_sigint_received = 1;
657 296152d1 2022-05-31 thomas static void
658 296152d1 2022-05-31 thomas tog_sigterm(int signo)
660 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
663 296152d1 2022-05-31 thomas static int
664 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
666 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
667 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
670 e5a0f69f 2018-08-18 stsp static const struct got_error *
671 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
673 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
675 669b5ffa 2018-10-07 stsp if (view->child) {
676 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
677 669b5ffa 2018-10-07 stsp view->child = NULL;
679 e5a0f69f 2018-08-18 stsp if (view->close)
680 e5a0f69f 2018-08-18 stsp err = view->close(view);
681 ea5e7bb5 2018-08-01 stsp if (view->panel)
682 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
683 ea5e7bb5 2018-08-01 stsp if (view->window)
684 ea5e7bb5 2018-08-01 stsp delwin(view->window);
685 ea5e7bb5 2018-08-01 stsp free(view);
686 f2d749db 2022-07-12 thomas return err ? err : child_err;
689 ea5e7bb5 2018-08-01 stsp static struct tog_view *
690 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
691 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
693 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
695 ea5e7bb5 2018-08-01 stsp if (view == NULL)
696 ea5e7bb5 2018-08-01 stsp return NULL;
698 d6b05b5b 2018-08-04 stsp view->type = type;
699 f7d12f7e 2018-08-01 stsp view->lines = LINES;
700 f7d12f7e 2018-08-01 stsp view->cols = COLS;
701 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
702 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
703 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
704 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
705 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
706 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
710 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
711 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
712 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
713 96a765a8 2018-08-04 stsp view_close(view);
714 ea5e7bb5 2018-08-01 stsp return NULL;
717 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
718 ea5e7bb5 2018-08-01 stsp return view;
722 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
724 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
726 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
729 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
730 a5d43cac 2022-07-01 thomas static int
731 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
733 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
736 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
738 5c60c32a 2018-10-18 stsp static const struct got_error *
739 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
741 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
743 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
745 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
747 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
748 a5d43cac 2022-07-01 thomas view->begin_x = 0;
749 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
750 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
751 53d2bdd3 2022-07-10 thomas view->cols > 119)
752 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
754 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
755 a5d43cac 2022-07-01 thomas view->begin_y = 0;
757 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
758 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
759 5c60c32a 2018-10-18 stsp view->lines = LINES;
760 5c60c32a 2018-10-18 stsp view->cols = COLS;
761 5c60c32a 2018-10-18 stsp err = view_resize(view);
763 5c60c32a 2018-10-18 stsp return err;
765 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
768 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
771 5c60c32a 2018-10-18 stsp return NULL;
774 5c60c32a 2018-10-18 stsp static const struct got_error *
775 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
777 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
779 5c60c32a 2018-10-18 stsp view->begin_x = 0;
780 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
781 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
782 5c60c32a 2018-10-18 stsp view->ncols = COLS;
783 5c60c32a 2018-10-18 stsp view->lines = LINES;
784 5c60c32a 2018-10-18 stsp view->cols = COLS;
785 5c60c32a 2018-10-18 stsp err = view_resize(view);
787 5c60c32a 2018-10-18 stsp return err;
789 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
792 5c60c32a 2018-10-18 stsp return NULL;
796 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
798 5c60c32a 2018-10-18 stsp return view->parent == NULL;
801 b65b3ea0 2022-06-23 thomas static int
802 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
804 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
807 e44940c3 2022-07-01 thomas static int
808 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
810 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
813 444d5325 2022-07-03 thomas static int
814 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
816 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
820 a5d43cac 2022-07-01 thomas static void
821 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
823 a5d43cac 2022-07-01 thomas PANEL *panel;
824 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
826 a5d43cac 2022-07-01 thomas if (view->parent)
827 a5d43cac 2022-07-01 thomas return view_border(view->parent);
829 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
830 a5d43cac 2022-07-01 thomas if (panel == NULL)
833 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
834 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
836 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
837 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
839 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
843 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
846 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
847 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
849 4d8c2215 2018-08-19 stsp static const struct got_error *
850 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
852 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
853 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
855 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
857 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
858 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
860 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
861 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
862 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
864 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
866 4918811f 2022-07-01 thomas if (view->child) {
867 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
869 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
870 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
871 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
873 40236d76 2022-06-23 thomas ncols = COLS;
875 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
876 5c60c32a 2018-10-18 stsp if (view->child->focussed)
877 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
879 5c60c32a 2018-10-18 stsp show_panel(view->panel);
881 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
883 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
884 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
887 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
888 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
889 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
891 a5d43cac 2022-07-01 thomas if (hs) {
892 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
894 a5d43cac 2022-07-01 thomas return err;
895 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
896 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
898 a5d43cac 2022-07-01 thomas return err;
900 a5d43cac 2022-07-01 thomas view_border(view);
901 a5d43cac 2022-07-01 thomas update_panels();
902 a5d43cac 2022-07-01 thomas doupdate();
903 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
904 a5d43cac 2022-07-01 thomas nlines = view->nlines;
906 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
907 40236d76 2022-06-23 thomas ncols = COLS;
909 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
910 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
912 ea0bff04 2022-07-19 thomas return err;
915 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
916 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
917 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
918 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
919 40236d76 2022-06-23 thomas wclear(view->window);
921 40236d76 2022-06-23 thomas view->nlines = nlines;
922 40236d76 2022-06-23 thomas view->ncols = ncols;
923 40236d76 2022-06-23 thomas view->lines = LINES;
924 40236d76 2022-06-23 thomas view->cols = COLS;
926 5c60c32a 2018-10-18 stsp return NULL;
929 ea0bff04 2022-07-19 thomas static const struct got_error *
930 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
932 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
933 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
934 3a0139e8 2022-07-22 thomas int n = 0;
936 3a0139e8 2022-07-22 thomas if (s->selected_entry)
937 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
940 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
941 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
943 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
944 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
945 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
948 ea0bff04 2022-07-19 thomas return err;
951 97cb21cd 2022-07-12 thomas static void
952 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
954 97cb21cd 2022-07-12 thomas if (n == 0)
957 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
958 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
959 97cb21cd 2022-07-12 thomas view->parent->offset += n;
961 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
962 97cb21cd 2022-07-12 thomas } else if (view->offset) {
963 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
964 97cb21cd 2022-07-12 thomas view->offset -= n;
966 97cb21cd 2022-07-12 thomas view->offset = 0;
970 53d2bdd3 2022-07-10 thomas static const struct got_error *
971 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
973 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
974 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
976 53d2bdd3 2022-07-10 thomas if (view->parent)
977 53d2bdd3 2022-07-10 thomas v = view->parent;
979 53d2bdd3 2022-07-10 thomas v = view;
981 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
982 53d2bdd3 2022-07-10 thomas return NULL;
984 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
986 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
989 53d2bdd3 2022-07-10 thomas if (view->parent)
990 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
992 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
993 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
994 53d2bdd3 2022-07-10 thomas view->count = 0;
995 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
996 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
997 53d2bdd3 2022-07-10 thomas view->count = 0;
998 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1000 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1001 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1002 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1003 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1004 53d2bdd3 2022-07-10 thomas if (err)
1005 53d2bdd3 2022-07-10 thomas return err;
1006 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1007 53d2bdd3 2022-07-10 thomas } else {
1008 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1009 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1010 53d2bdd3 2022-07-10 thomas if (view->parent)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1014 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1015 53d2bdd3 2022-07-10 thomas view->count = 0;
1016 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1017 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1018 53d2bdd3 2022-07-10 thomas view->count = 0;
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1021 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1024 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1025 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1026 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1027 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1029 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1030 53d2bdd3 2022-07-10 thomas if (err)
1031 53d2bdd3 2022-07-10 thomas return err;
1032 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1033 53d2bdd3 2022-07-10 thomas if (err)
1034 53d2bdd3 2022-07-10 thomas return err;
1036 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1042 3a0139e8 2022-07-22 thomas if (v->resize)
1043 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1044 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1045 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1047 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1049 53d2bdd3 2022-07-10 thomas return err;
1052 53d2bdd3 2022-07-10 thomas static void
1053 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1055 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1057 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1058 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1061 669b5ffa 2018-10-07 stsp static const struct got_error *
1062 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1064 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1066 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1067 669b5ffa 2018-10-07 stsp return NULL;
1069 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1070 669b5ffa 2018-10-07 stsp view->child = NULL;
1071 669b5ffa 2018-10-07 stsp return err;
1074 40236d76 2022-06-23 thomas static const struct got_error *
1075 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1077 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1079 669b5ffa 2018-10-07 stsp view->child = child;
1080 669b5ffa 2018-10-07 stsp child->parent = view;
1082 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1083 53d2bdd3 2022-07-10 thomas if (err)
1084 53d2bdd3 2022-07-10 thomas return err;
1086 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1087 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1089 53d2bdd3 2022-07-10 thomas return err;
1092 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1093 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1095 2a31b33b 2022-07-23 thomas static const struct got_error *
1096 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1097 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1099 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1100 2a31b33b 2022-07-23 thomas const struct got_error *err;
1101 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1103 2a31b33b 2022-07-23 thomas *requested = NULL;
1105 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1106 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1108 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1109 2a31b33b 2022-07-23 thomas if (err)
1110 2a31b33b 2022-07-23 thomas return err;
1112 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1114 2a31b33b 2022-07-23 thomas if (err)
1115 2a31b33b 2022-07-23 thomas return err;
1118 2a31b33b 2022-07-23 thomas view->focussed = 0;
1119 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1120 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1121 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1123 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1124 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1125 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1126 2a31b33b 2022-07-23 thomas if (err)
1127 2a31b33b 2022-07-23 thomas return err;
1128 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1129 2a31b33b 2022-07-23 thomas if (err)
1130 2a31b33b 2022-07-23 thomas return err;
1131 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1133 2a31b33b 2022-07-23 thomas *requested = new_view;
1135 2a31b33b 2022-07-23 thomas return NULL;
1138 34bc9ec9 2019-02-22 stsp static void
1139 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1141 25791caa 2018-10-24 stsp int cols, lines;
1142 25791caa 2018-10-24 stsp struct winsize size;
1144 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 25791caa 2018-10-24 stsp cols = 80; /* Default */
1146 25791caa 2018-10-24 stsp lines = 24;
1148 25791caa 2018-10-24 stsp cols = size.ws_col;
1149 25791caa 2018-10-24 stsp lines = size.ws_row;
1151 25791caa 2018-10-24 stsp resize_term(lines, cols);
1154 2b49a8ae 2019-06-22 stsp static const struct got_error *
1155 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1157 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1158 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1159 2b49a8ae 2019-06-22 stsp char pattern[1024];
1162 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1163 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1164 c0c4acc8 2021-01-24 stsp view->searching = 0;
1165 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1167 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1169 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1170 2b49a8ae 2019-06-22 stsp return NULL;
1172 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1173 a5d43cac 2022-07-01 thomas v = view->child;
1175 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1178 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1179 2b49a8ae 2019-06-22 stsp nocbreak();
1181 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1185 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1186 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1187 2b49a8ae 2019-06-22 stsp return NULL;
1189 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1191 7c32bd05 2019-06-22 stsp if (err) {
1192 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1193 7c32bd05 2019-06-22 stsp return err;
1195 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1196 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1197 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1198 2b49a8ae 2019-06-22 stsp view->search_next(view);
1201 2b49a8ae 2019-06-22 stsp return NULL;
1204 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 64486692 2022-07-07 thomas static const struct got_error *
1206 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1208 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1209 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1211 64486692 2022-07-07 thomas if (view->parent)
1212 64486692 2022-07-07 thomas v = view->parent;
1214 64486692 2022-07-07 thomas v = view;
1216 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1219 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1221 ddbc4d37 2022-07-12 thomas if (!v->child)
1222 ddbc4d37 2022-07-12 thomas return NULL;
1223 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1226 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1229 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1233 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1234 64486692 2022-07-07 thomas v->ncols = COLS;
1235 64486692 2022-07-07 thomas v->child->ncols = COLS;
1236 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1238 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1239 64486692 2022-07-07 thomas if (err)
1240 64486692 2022-07-07 thomas return err;
1242 64486692 2022-07-07 thomas v->child->mode = v->mode;
1243 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1244 64486692 2022-07-07 thomas v->focus_child = 1;
1246 64486692 2022-07-07 thomas err = view_fullscreen(v);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1249 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1250 64486692 2022-07-07 thomas if (err)
1251 64486692 2022-07-07 thomas return err;
1253 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1255 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1257 cf1fe301 2022-07-29 thomas if (err)
1258 cf1fe301 2022-07-29 thomas return err;
1259 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1260 cf1fe301 2022-07-29 thomas if (err)
1261 cf1fe301 2022-07-29 thomas return err;
1262 ddbc4d37 2022-07-12 thomas } else {
1263 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1264 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1266 f4e6231a 2022-07-22 thomas if (v->resize)
1267 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1268 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1269 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1271 64486692 2022-07-07 thomas return err;
1275 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1276 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1278 07b0611c 2022-06-23 thomas static int
1279 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1281 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1282 a5d43cac 2022-07-01 thomas int x, n = 0;
1284 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1285 a5d43cac 2022-07-01 thomas v = view->child;
1286 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1287 a5d43cac 2022-07-01 thomas v = view->parent;
1289 07b0611c 2022-06-23 thomas view->count = 0;
1290 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1291 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1292 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1293 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1294 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1297 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1298 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1299 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1300 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1304 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1305 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1307 07b0611c 2022-06-23 thomas if (n >= 9999999)
1308 07b0611c 2022-06-23 thomas n = 9999999;
1310 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1311 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1313 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1314 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1319 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1320 07b0611c 2022-06-23 thomas view->count = n;
1322 07b0611c 2022-06-23 thomas return c;
1325 0cf4efb1 2018-09-29 stsp static const struct got_error *
1326 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1327 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1329 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1330 669b5ffa 2018-10-07 stsp struct tog_view *v;
1331 1a76625f 2018-10-22 stsp int ch, errcode;
1333 e5a0f69f 2018-08-18 stsp *new = NULL;
1335 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1336 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1337 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1338 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1339 07b0611c 2022-06-23 thomas view->count = 0;
1342 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1343 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1344 82954512 2020-02-03 stsp if (errcode)
1345 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1346 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1347 3da8ef85 2021-09-21 stsp sched_yield();
1348 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1349 82954512 2020-02-03 stsp if (errcode)
1350 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1351 82954512 2020-02-03 stsp "pthread_mutex_lock");
1352 60493ae3 2019-06-20 stsp view->search_next(view);
1353 60493ae3 2019-06-20 stsp return NULL;
1356 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1357 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1358 1a76625f 2018-10-22 stsp if (errcode)
1359 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1360 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1361 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1362 634cb454 2022-07-03 thomas cbreak();
1363 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1364 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1365 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1366 634cb454 2022-07-03 thomas view->count = 0;
1368 634cb454 2022-07-03 thomas ch = view->ch;
1369 634cb454 2022-07-03 thomas } else {
1370 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1371 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1372 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1374 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1375 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1376 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1377 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1378 1a76625f 2018-10-22 stsp if (errcode)
1379 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1381 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1382 25791caa 2018-10-24 stsp tog_resizeterm();
1383 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1384 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1385 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1386 25791caa 2018-10-24 stsp err = view_resize(v);
1388 25791caa 2018-10-24 stsp return err;
1389 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1391 25791caa 2018-10-24 stsp return err;
1392 cdfcfb03 2020-12-06 stsp if (v->child) {
1393 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1395 cdfcfb03 2020-12-06 stsp return err;
1396 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1397 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1399 cdfcfb03 2020-12-06 stsp return err;
1400 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1401 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1402 53d2bdd3 2022-07-10 thomas if (err)
1403 53d2bdd3 2022-07-10 thomas return err;
1409 e5a0f69f 2018-08-18 stsp switch (ch) {
1411 07b0611c 2022-06-23 thomas view->count = 0;
1412 1e37a5c2 2019-05-12 jcs if (view->child) {
1413 e78dc838 2020-12-04 stsp view->focussed = 0;
1414 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1415 e78dc838 2020-12-04 stsp view->focus_child = 1;
1416 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1417 e78dc838 2020-12-04 stsp view->focussed = 0;
1418 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1419 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1420 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1421 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1422 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1424 a5d43cac 2022-07-01 thomas if (err)
1425 a5d43cac 2022-07-01 thomas return err;
1427 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1428 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1429 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas return err;
1435 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1436 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1437 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1438 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1439 a5d43cac 2022-07-01 thomas if (err)
1442 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1444 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1445 9970f7fc 2020-12-03 stsp view->dying = 1;
1450 1c5e5faa 2022-06-23 thomas case 'F':
1451 07b0611c 2022-06-23 thomas view->count = 0;
1452 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1453 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1455 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1456 e78dc838 2020-12-04 stsp view->focussed = 0;
1457 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1458 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1459 53d2bdd3 2022-07-10 thomas } else {
1460 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1461 53d2bdd3 2022-07-10 thomas if (!err)
1462 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1466 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1467 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1469 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1470 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1471 e78dc838 2020-12-04 stsp view->focussed = 1;
1472 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1474 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1475 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1476 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1477 53d2bdd3 2022-07-10 thomas if (!err)
1478 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1482 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1484 a5d43cac 2022-07-01 thomas if (err)
1486 3a0139e8 2022-07-22 thomas if (view->resize) {
1487 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1488 a5d43cac 2022-07-01 thomas if (err)
1491 a5d43cac 2022-07-01 thomas if (view->parent)
1492 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1493 a5d43cac 2022-07-01 thomas if (!err)
1494 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1496 64486692 2022-07-07 thomas case 'S':
1497 53d2bdd3 2022-07-10 thomas view->count = 0;
1498 64486692 2022-07-07 thomas err = switch_split(view);
1500 53d2bdd3 2022-07-10 thomas case '-':
1501 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1503 53d2bdd3 2022-07-10 thomas case '+':
1504 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1506 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1509 07b0611c 2022-06-23 thomas view->count = 0;
1510 60493ae3 2019-06-20 stsp if (view->search_start)
1511 2b49a8ae 2019-06-22 stsp view_search_start(view);
1513 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1517 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1518 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1519 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1520 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1521 60493ae3 2019-06-20 stsp view->search_next(view);
1523 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1525 adf4c9e0 2022-07-03 thomas case 'A':
1526 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1527 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1529 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1530 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1531 adf4c9e0 2022-07-03 thomas if (v->reset) {
1532 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1533 adf4c9e0 2022-07-03 thomas if (err)
1534 adf4c9e0 2022-07-03 thomas return err;
1536 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1537 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1538 adf4c9e0 2022-07-03 thomas if (err)
1539 adf4c9e0 2022-07-03 thomas return err;
1544 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1548 e5a0f69f 2018-08-18 stsp return err;
1551 ef20f542 2022-06-26 thomas static int
1552 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1554 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1555 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1557 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1559 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1562 669b5ffa 2018-10-07 stsp return view->focussed;
1565 bcbd79e2 2018-08-19 stsp static const struct got_error *
1566 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1568 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1569 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1570 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1571 64486692 2022-07-07 thomas char *mode;
1572 fd823528 2018-10-22 stsp int fast_refresh = 10;
1573 1a76625f 2018-10-22 stsp int done = 0, errcode;
1575 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1576 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1577 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1579 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1581 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1582 1a76625f 2018-10-22 stsp if (errcode)
1583 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1585 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1586 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1588 1004088d 2018-09-29 stsp view->focussed = 1;
1589 878940b7 2018-09-29 stsp err = view->show(view);
1591 0cf4efb1 2018-09-29 stsp return err;
1592 0cf4efb1 2018-09-29 stsp update_panels();
1593 0cf4efb1 2018-09-29 stsp doupdate();
1594 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1595 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1596 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1597 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1598 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1600 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1603 9970f7fc 2020-12-03 stsp if (view->dying) {
1604 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1606 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1607 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1609 e78dc838 2020-12-04 stsp else if (view->parent)
1610 669b5ffa 2018-10-07 stsp prev = view->parent;
1612 e78dc838 2020-12-04 stsp if (view->parent) {
1613 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1614 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1615 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1616 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1617 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1618 40236d76 2022-06-23 thomas if (err)
1620 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1621 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1623 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1625 9970f7fc 2020-12-03 stsp err = view_close(view);
1627 e5a0f69f 2018-08-18 stsp goto done;
1629 e78dc838 2020-12-04 stsp view = NULL;
1630 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1631 e78dc838 2020-12-04 stsp if (v->focussed)
1634 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1635 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1637 e78dc838 2020-12-04 stsp view = prev;
1638 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1639 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1640 e78dc838 2020-12-04 stsp tog_view_list_head);
1642 e78dc838 2020-12-04 stsp if (view) {
1643 e78dc838 2020-12-04 stsp if (view->focus_child) {
1644 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1645 e78dc838 2020-12-04 stsp view = view->child;
1647 e78dc838 2020-12-04 stsp view->focussed = 1;
1651 bcbd79e2 2018-08-19 stsp if (new_view) {
1652 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1653 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1654 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1655 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1657 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1658 86c66b02 2018-10-18 stsp err = view_close(v);
1660 86c66b02 2018-10-18 stsp goto done;
1663 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1664 fed7eaa8 2018-10-24 stsp view = new_view;
1666 669b5ffa 2018-10-07 stsp if (view) {
1667 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1668 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1669 e78dc838 2020-12-04 stsp view = view->child;
1671 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1672 e78dc838 2020-12-04 stsp view = view->parent;
1674 e78dc838 2020-12-04 stsp show_panel(view->panel);
1675 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1676 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1677 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1678 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1680 1a76625f 2018-10-22 stsp goto done;
1682 669b5ffa 2018-10-07 stsp err = view->show(view);
1684 1a76625f 2018-10-22 stsp goto done;
1685 669b5ffa 2018-10-07 stsp if (view->child) {
1686 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1688 1a76625f 2018-10-22 stsp goto done;
1690 1a76625f 2018-10-22 stsp update_panels();
1691 1a76625f 2018-10-22 stsp doupdate();
1695 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1696 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1697 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1698 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1699 f2d749db 2022-07-12 thomas close_err = view_close(view);
1700 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1701 f2d749db 2022-07-12 thomas err = close_err;
1704 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1705 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1706 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1708 e5a0f69f 2018-08-18 stsp return err;
1711 4ed7e80c 2018-05-20 stsp __dead static void
1712 9f7d7167 2018-04-29 stsp usage_log(void)
1715 c70c5802 2018-08-01 stsp fprintf(stderr,
1716 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1717 9f7d7167 2018-04-29 stsp getprogname());
1721 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1722 80ddbec8 2018-04-29 stsp static const struct got_error *
1723 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1725 00dfcb92 2018-06-11 stsp char *vis = NULL;
1726 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1728 963b370f 2018-05-20 stsp *ws = NULL;
1729 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1730 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1731 00dfcb92 2018-06-11 stsp int vislen;
1732 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1733 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1735 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1736 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1738 00dfcb92 2018-06-11 stsp return err;
1739 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1740 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1741 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1742 a7f50699 2018-06-11 stsp goto done;
1746 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1747 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1748 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1749 a7f50699 2018-06-11 stsp goto done;
1752 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1753 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1755 00dfcb92 2018-06-11 stsp free(vis);
1756 963b370f 2018-05-20 stsp if (err) {
1757 963b370f 2018-05-20 stsp free(*ws);
1758 963b370f 2018-05-20 stsp *ws = NULL;
1759 963b370f 2018-05-20 stsp *wlen = 0;
1761 963b370f 2018-05-20 stsp return err;
1764 05171be4 2022-06-23 thomas static const struct got_error *
1765 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1767 05171be4 2022-06-23 thomas char *dst;
1768 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1770 05171be4 2022-06-23 thomas *ptr = NULL;
1771 05171be4 2022-06-23 thomas n = len = strlen(src);
1772 83316834 2022-06-23 thomas dst = malloc(n + 1);
1773 05171be4 2022-06-23 thomas if (dst == NULL)
1774 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1776 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1777 05171be4 2022-06-23 thomas const char c = src[idx];
1779 05171be4 2022-06-23 thomas if (c == '\t') {
1780 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1781 b235ad5d 2022-06-23 thomas char *p;
1783 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1784 83316834 2022-06-23 thomas if (p == NULL) {
1785 83316834 2022-06-23 thomas free(dst);
1786 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1789 83316834 2022-06-23 thomas dst = p;
1790 05171be4 2022-06-23 thomas n += nb;
1791 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1792 05171be4 2022-06-23 thomas sz += nb;
1794 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1798 05171be4 2022-06-23 thomas dst[sz] = '\0';
1799 05171be4 2022-06-23 thomas *ptr = dst;
1800 05171be4 2022-06-23 thomas return NULL;
1804 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1805 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1806 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1807 8d208d34 2022-06-23 thomas * *rcol.
1809 8d208d34 2022-06-23 thomas static int
1810 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1812 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1814 8d208d34 2022-06-23 thomas if (n == 0) {
1815 8d208d34 2022-06-23 thomas *rcol = cols;
1816 8d208d34 2022-06-23 thomas return off;
1819 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1820 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1821 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1823 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1825 8d208d34 2022-06-23 thomas if (width == -1) {
1826 8d208d34 2022-06-23 thomas width = 1;
1827 8d208d34 2022-06-23 thomas wline[i] = L'.';
1830 8d208d34 2022-06-23 thomas if (cols + width > n)
1832 8d208d34 2022-06-23 thomas cols += width;
1835 8d208d34 2022-06-23 thomas *rcol = cols;
1836 8d208d34 2022-06-23 thomas return i;
1840 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1841 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1842 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1844 f91a2b48 2022-06-23 thomas static const struct got_error *
1845 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1846 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1848 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1849 8d208d34 2022-06-23 thomas int cols;
1850 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1851 05171be4 2022-06-23 thomas char *exstr = NULL;
1852 963b370f 2018-05-20 stsp size_t wlen;
1853 8d208d34 2022-06-23 thomas int i, scrollx;
1855 963b370f 2018-05-20 stsp *wlinep = NULL;
1856 b700b5d6 2018-07-10 stsp *widthp = 0;
1858 05171be4 2022-06-23 thomas if (expand) {
1859 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1860 05171be4 2022-06-23 thomas if (err)
1861 05171be4 2022-06-23 thomas return err;
1864 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1865 05171be4 2022-06-23 thomas free(exstr);
1867 963b370f 2018-05-20 stsp return err;
1869 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1871 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1872 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1875 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1876 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1880 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1881 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1883 b700b5d6 2018-07-10 stsp if (widthp)
1884 b700b5d6 2018-07-10 stsp *widthp = cols;
1885 f91a2b48 2022-06-23 thomas if (scrollxp)
1886 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1888 963b370f 2018-05-20 stsp free(wline);
1890 963b370f 2018-05-20 stsp *wlinep = wline;
1891 963b370f 2018-05-20 stsp return err;
1894 8b473291 2019-02-21 stsp static const struct got_error*
1895 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1896 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1898 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1899 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1901 8b473291 2019-02-21 stsp const char *name;
1903 8b473291 2019-02-21 stsp *refs_str = NULL;
1905 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1906 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1907 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1910 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1911 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1913 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1914 8b473291 2019-02-21 stsp name += 5;
1915 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1916 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1918 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1919 8b473291 2019-02-21 stsp name += 6;
1920 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1921 8b473291 2019-02-21 stsp name += 8;
1922 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1923 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1926 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1929 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1930 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1931 5d844a1e 2019-08-13 stsp if (err) {
1932 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1933 48cae60d 2020-09-22 stsp free(ref_id);
1936 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1937 5d844a1e 2019-08-13 stsp err = NULL;
1938 5d844a1e 2019-08-13 stsp tag = NULL;
1941 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1942 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1943 48cae60d 2020-09-22 stsp free(ref_id);
1945 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1946 52b5abe1 2019-08-13 stsp if (cmp != 0)
1948 8b473291 2019-02-21 stsp s = *refs_str;
1949 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1950 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1951 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1953 8b473291 2019-02-21 stsp *refs_str = NULL;
1959 8b473291 2019-02-21 stsp return err;
1962 963b370f 2018-05-20 stsp static const struct got_error *
1963 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1964 27a741e5 2019-09-11 stsp int col_tab_align)
1966 e6b8b890 2020-12-29 naddy char *smallerthan;
1968 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1969 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1970 5813d178 2019-03-09 stsp author = smallerthan + 1;
1971 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1972 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1973 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1976 5813d178 2019-03-09 stsp static const struct got_error *
1977 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1978 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1979 8fdc79fe 2020-12-01 naddy int author_display_cols)
1981 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1982 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1983 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1984 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1985 5813d178 2019-03-09 stsp char *author = NULL;
1986 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1987 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1988 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1989 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1990 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1991 ccb26ccd 2018-11-05 stsp struct tm tm;
1992 45d799e2 2018-12-23 stsp time_t committer_time;
1993 11b20872 2019-11-08 stsp struct tog_color *tc;
1995 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1996 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1997 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1998 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1999 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2001 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2002 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2004 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2005 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2007 11b20872 2019-11-08 stsp wattr_on(view->window,
2008 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2009 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2011 11b20872 2019-11-08 stsp wattr_off(view->window,
2012 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2013 27a741e5 2019-09-11 stsp col = limit;
2014 b39d25c7 2018-07-10 stsp if (col > avail)
2015 b39d25c7 2018-07-10 stsp goto done;
2017 6570a66d 2019-11-08 stsp if (avail >= 120) {
2018 6570a66d 2019-11-08 stsp char *id_str;
2019 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2021 6570a66d 2019-11-08 stsp goto done;
2022 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2024 11b20872 2019-11-08 stsp wattr_on(view->window,
2025 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2026 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2028 11b20872 2019-11-08 stsp wattr_off(view->window,
2029 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2030 6570a66d 2019-11-08 stsp free(id_str);
2032 6570a66d 2019-11-08 stsp if (col > avail)
2033 6570a66d 2019-11-08 stsp goto done;
2036 f69c5a46 2022-07-19 thomas if (s->use_committer)
2037 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2039 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2040 5813d178 2019-03-09 stsp if (author == NULL) {
2041 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2042 80ddbec8 2018-04-29 stsp goto done;
2044 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2046 bb737323 2018-05-20 stsp goto done;
2047 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2049 11b20872 2019-11-08 stsp wattr_on(view->window,
2050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2051 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2053 11b20872 2019-11-08 stsp wattr_off(view->window,
2054 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2055 bb737323 2018-05-20 stsp col += author_width;
2056 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2057 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2059 bb737323 2018-05-20 stsp author_width++;
2061 9c2eaf34 2018-05-20 stsp if (col > avail)
2062 9c2eaf34 2018-05-20 stsp goto done;
2064 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2066 6d9fbc00 2018-04-29 stsp goto done;
2067 bb737323 2018-05-20 stsp logmsg = logmsg0;
2068 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2070 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2071 bb737323 2018-05-20 stsp if (newline)
2072 bb737323 2018-05-20 stsp *newline = '\0';
2073 f91a2b48 2022-06-23 thomas limit = avail - col;
2074 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2075 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2076 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2077 f91a2b48 2022-06-23 thomas limit, col, 1);
2078 331b1a16 2022-06-23 thomas if (err)
2079 331b1a16 2022-06-23 thomas goto done;
2080 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2081 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2082 27a741e5 2019-09-11 stsp while (col < avail) {
2083 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2087 80ddbec8 2018-04-29 stsp free(logmsg0);
2088 bb737323 2018-05-20 stsp free(wlogmsg);
2089 5813d178 2019-03-09 stsp free(author);
2090 bb737323 2018-05-20 stsp free(wauthor);
2091 80ddbec8 2018-04-29 stsp free(line);
2092 80ddbec8 2018-04-29 stsp return err;
2095 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2096 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2097 899d86c2 2018-05-10 stsp struct got_object_id *id)
2099 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2101 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2102 80ddbec8 2018-04-29 stsp if (entry == NULL)
2103 899d86c2 2018-05-10 stsp return NULL;
2105 899d86c2 2018-05-10 stsp entry->id = id;
2106 99db9666 2018-05-07 stsp entry->commit = commit;
2107 899d86c2 2018-05-10 stsp return entry;
2110 99db9666 2018-05-07 stsp static void
2111 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2113 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2115 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2116 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2117 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2118 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2119 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2120 99db9666 2018-05-07 stsp free(entry);
2123 99db9666 2018-05-07 stsp static void
2124 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2126 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2127 99db9666 2018-05-07 stsp pop_commit(commits);
2130 c4972b91 2018-05-07 stsp static const struct got_error *
2131 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2132 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2134 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2135 13add988 2019-10-15 stsp regmatch_t regmatch;
2136 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2138 13add988 2019-10-15 stsp *have_match = 0;
2140 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2142 13add988 2019-10-15 stsp return err;
2144 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2146 13add988 2019-10-15 stsp goto done;
2148 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2149 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2150 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2151 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2152 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2153 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2154 13add988 2019-10-15 stsp *have_match = 1;
2156 13add988 2019-10-15 stsp free(id_str);
2157 13add988 2019-10-15 stsp free(logmsg);
2158 13add988 2019-10-15 stsp return err;
2161 13add988 2019-10-15 stsp static const struct got_error *
2162 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2164 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2167 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2168 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2169 1a76625f 2018-10-22 stsp * while updating the display.
2172 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2173 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2174 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2175 1a76625f 2018-10-22 stsp int errcode;
2177 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2178 4e0d2870 2020-12-07 naddy NULL, NULL);
2179 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2182 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2185 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2186 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2187 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2191 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2192 1a76625f 2018-10-22 stsp if (errcode) {
2193 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2194 13add988 2019-10-15 stsp "pthread_mutex_lock");
2198 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2199 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2200 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2202 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2203 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2204 7c1452c1 2020-03-26 stsp int have_match;
2205 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2208 7c1452c1 2020-03-26 stsp if (have_match)
2209 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2212 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2213 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2214 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2215 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2218 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2220 9ba79e04 2018-06-11 stsp return err;
2223 2b779855 2020-12-05 naddy static void
2224 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2226 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2227 2b779855 2020-12-05 naddy int ncommits = 0;
2229 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2230 2b779855 2020-12-05 naddy while (entry) {
2231 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2232 2b779855 2020-12-05 naddy s->selected_entry = entry;
2235 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2236 2b779855 2020-12-05 naddy ncommits++;
2240 0553a4e3 2018-04-30 stsp static const struct got_error *
2241 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2243 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2244 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2245 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2246 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2247 60493ae3 2019-06-20 stsp int width;
2248 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2249 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2250 8b473291 2019-02-21 stsp char *refs_str = NULL;
2251 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2252 11b20872 2019-11-08 stsp struct tog_color *tc;
2253 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2255 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2256 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2258 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2259 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2260 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2261 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2263 ecb28ae0 2018-07-16 stsp return err;
2264 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2265 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2266 d2075bf3 2020-12-25 stsp if (refs) {
2267 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2268 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2270 d2075bf3 2020-12-25 stsp goto done;
2274 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2275 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2277 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2278 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2279 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2280 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2281 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2282 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2283 8f4ed634 2020-03-26 stsp goto done;
2286 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2288 f9686aa5 2020-03-27 stsp if (view->searching) {
2289 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2290 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2291 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2292 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2293 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2294 f9686aa5 2020-03-27 stsp search_str = "searching...";
2297 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2298 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2299 f9686aa5 2020-03-27 stsp search_str ? search_str :
2300 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2301 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2302 8f4ed634 2020-03-26 stsp goto done;
2306 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2307 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2308 91198554 2022-06-23 thomas "........................................",
2309 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2310 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2311 1a76625f 2018-10-22 stsp header = NULL;
2312 1a76625f 2018-10-22 stsp goto done;
2314 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2315 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2316 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2317 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2318 1a76625f 2018-10-22 stsp header = NULL;
2319 1a76625f 2018-10-22 stsp goto done;
2321 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2323 1a76625f 2018-10-22 stsp goto done;
2325 2814baeb 2018-08-01 stsp werase(view->window);
2327 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2328 a3404814 2018-09-02 stsp wstandout(view->window);
2329 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2331 11b20872 2019-11-08 stsp wattr_on(view->window,
2332 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2333 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2335 11b20872 2019-11-08 stsp wattr_off(view->window,
2336 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2337 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2338 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2341 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2342 a3404814 2018-09-02 stsp wstandend(view->window);
2343 ecb28ae0 2018-07-16 stsp free(wline);
2344 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2345 1a76625f 2018-10-22 stsp goto done;
2347 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2348 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2349 5813d178 2019-03-09 stsp ncommits = 0;
2350 05171be4 2022-06-23 thomas view->maxx = 0;
2351 5813d178 2019-03-09 stsp while (entry) {
2352 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2353 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2354 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2355 5813d178 2019-03-09 stsp int width;
2356 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2358 f69c5a46 2022-07-19 thomas if (s->use_committer)
2359 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2361 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2362 5813d178 2019-03-09 stsp if (author == NULL) {
2363 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2364 5813d178 2019-03-09 stsp goto done;
2366 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2367 27a741e5 2019-09-11 stsp date_display_cols);
2368 5813d178 2019-03-09 stsp if (author_cols < width)
2369 5813d178 2019-03-09 stsp author_cols = width;
2370 5813d178 2019-03-09 stsp free(wauthor);
2371 5813d178 2019-03-09 stsp free(author);
2372 ef944b8b 2022-07-21 thomas if (err)
2373 ef944b8b 2022-07-21 thomas goto done;
2374 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2375 05171be4 2022-06-23 thomas if (err)
2376 05171be4 2022-06-23 thomas goto done;
2377 05171be4 2022-06-23 thomas msg = msg0;
2378 05171be4 2022-06-23 thomas while (*msg == '\n')
2380 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2381 331b1a16 2022-06-23 thomas *eol = '\0';
2382 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2383 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2384 331b1a16 2022-06-23 thomas if (err)
2385 331b1a16 2022-06-23 thomas goto done;
2386 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2387 05171be4 2022-06-23 thomas free(msg0);
2388 331b1a16 2022-06-23 thomas free(wmsg);
2389 7ca04879 2019-10-19 stsp ncommits++;
2390 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2393 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2394 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2395 867c6645 2018-07-10 stsp ncommits = 0;
2396 899d86c2 2018-05-10 stsp while (entry) {
2397 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2399 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2400 2814baeb 2018-08-01 stsp wstandout(view->window);
2401 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2402 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2403 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2404 2814baeb 2018-08-01 stsp wstandend(view->window);
2406 60493ae3 2019-06-20 stsp goto done;
2407 0553a4e3 2018-04-30 stsp ncommits++;
2408 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2409 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2412 a5d43cac 2022-07-01 thomas view_border(view);
2414 1a76625f 2018-10-22 stsp free(id_str);
2415 8b473291 2019-02-21 stsp free(refs_str);
2416 1a76625f 2018-10-22 stsp free(ncommits_str);
2417 1a76625f 2018-10-22 stsp free(header);
2418 80ddbec8 2018-04-29 stsp return err;
2421 07b55e75 2018-05-10 stsp static void
2422 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2424 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2425 07b55e75 2018-05-10 stsp int nscrolled = 0;
2427 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2428 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2431 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2432 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2433 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2434 07b55e75 2018-05-10 stsp if (entry) {
2435 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2436 07b55e75 2018-05-10 stsp nscrolled++;
2441 aa075928 2018-05-10 stsp static const struct got_error *
2442 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2444 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2445 5e224a3e 2019-02-22 stsp int errcode;
2447 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2449 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2450 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2451 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2452 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2453 7aafa0d1 2019-02-22 stsp if (errcode)
2454 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2455 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2458 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2459 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2461 7c1452c1 2020-03-26 stsp if (!wait)
2464 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2465 ffe38506 2020-12-01 naddy show_log_view(view);
2466 7c1452c1 2020-03-26 stsp update_panels();
2467 7c1452c1 2020-03-26 stsp doupdate();
2469 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2470 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2471 82954512 2020-02-03 stsp if (errcode)
2472 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2473 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2475 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2476 ffe38506 2020-12-01 naddy show_log_view(view);
2477 7c1452c1 2020-03-26 stsp update_panels();
2478 7c1452c1 2020-03-26 stsp doupdate();
2481 5e224a3e 2019-02-22 stsp return NULL;
2484 a5d43cac 2022-07-01 thomas static const struct got_error *
2485 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2487 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2488 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2490 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2491 fe731b51 2022-07-14 thomas return NULL;
2493 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2494 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2495 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2497 a5d43cac 2022-07-01 thomas return err;
2500 5e224a3e 2019-02-22 stsp static const struct got_error *
2501 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2503 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2504 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2505 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2506 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2508 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2509 5e224a3e 2019-02-22 stsp return NULL;
2511 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2512 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2513 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2515 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2517 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2518 07dd3ed3 2022-08-06 thomas ncommits_needed - s->commits.ncommits;
2519 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2521 5e224a3e 2019-02-22 stsp return err;
2525 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2526 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2529 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2530 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2532 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2533 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2535 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2536 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2538 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2539 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2541 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2543 dd0a52c1 2018-05-20 stsp return err;
2546 cd0acaa7 2018-05-20 stsp static const struct got_error *
2547 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2548 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2549 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2551 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2552 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2553 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2555 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2556 15a94983 2018-12-23 stsp if (diff_view == NULL)
2557 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2559 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2560 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2561 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2562 e5a0f69f 2018-08-18 stsp if (err == NULL)
2563 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2564 cd0acaa7 2018-05-20 stsp return err;
2567 80ddbec8 2018-04-29 stsp static const struct got_error *
2568 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2569 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2571 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2573 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2574 941e9f74 2019-05-21 stsp if (parent == NULL)
2575 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2577 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2578 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2579 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2580 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2581 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2582 941e9f74 2019-05-21 stsp s->tree = subtree;
2583 941e9f74 2019-05-21 stsp s->selected = 0;
2584 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2585 941e9f74 2019-05-21 stsp return NULL;
2588 941e9f74 2019-05-21 stsp static const struct got_error *
2589 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2590 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2592 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2593 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2594 941e9f74 2019-05-21 stsp const char *p;
2595 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2597 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2599 941e9f74 2019-05-21 stsp while (*p) {
2600 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2601 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2602 56e0773d 2019-11-28 stsp char *te_name;
2604 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2607 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2608 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2609 941e9f74 2019-05-21 stsp if (slash == NULL)
2610 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2612 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2613 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2614 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2617 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2618 56e0773d 2019-11-28 stsp if (te == NULL) {
2619 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2620 56e0773d 2019-11-28 stsp free(te_name);
2623 56e0773d 2019-11-28 stsp free(te_name);
2624 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2626 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2627 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2629 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2630 941e9f74 2019-05-21 stsp if (slash)
2631 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2633 941e9f74 2019-05-21 stsp subpath = strdup(path);
2634 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2635 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2639 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2644 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2645 941e9f74 2019-05-21 stsp free(tree_id);
2649 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2650 941e9f74 2019-05-21 stsp if (err) {
2651 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2654 941e9f74 2019-05-21 stsp if (slash == NULL)
2656 941e9f74 2019-05-21 stsp free(subpath);
2657 941e9f74 2019-05-21 stsp subpath = NULL;
2658 941e9f74 2019-05-21 stsp p = slash;
2661 941e9f74 2019-05-21 stsp free(subpath);
2662 1a76625f 2018-10-22 stsp return err;
2665 61266923 2020-01-14 stsp static const struct got_error *
2666 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2667 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2668 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2670 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2671 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2672 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2674 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2675 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2676 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2678 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2680 55cccc34 2020-02-20 stsp return err;
2681 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2683 55cccc34 2020-02-20 stsp *new_view = tree_view;
2685 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2686 55cccc34 2020-02-20 stsp return NULL;
2688 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2691 55cccc34 2020-02-20 stsp static const struct got_error *
2692 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2694 61266923 2020-01-14 stsp sigset_t sigset;
2695 61266923 2020-01-14 stsp int errcode;
2697 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2698 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2700 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2701 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2702 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2703 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2704 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2705 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2706 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2707 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2708 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2710 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2711 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2712 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2714 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2715 61266923 2020-01-14 stsp if (errcode)
2716 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2718 61266923 2020-01-14 stsp return NULL;
2721 1a76625f 2018-10-22 stsp static void *
2722 1a76625f 2018-10-22 stsp log_thread(void *arg)
2724 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2725 1a76625f 2018-10-22 stsp int errcode = 0;
2726 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2727 1a76625f 2018-10-22 stsp int done = 0;
2730 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2731 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2733 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2734 f2d749db 2022-07-12 thomas if (errcode) {
2735 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2736 61266923 2020-01-14 stsp return (void *)err;
2739 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2740 f2d749db 2022-07-12 thomas if (err) {
2741 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2742 f2d749db 2022-07-12 thomas goto done;
2745 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2746 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2747 f2d749db 2022-07-12 thomas if (errcode) {
2748 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2749 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2750 f2d749db 2022-07-12 thomas goto done;
2752 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2753 1a76625f 2018-10-22 stsp if (err) {
2754 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2755 f2d749db 2022-07-12 thomas goto done;
2756 1a76625f 2018-10-22 stsp err = NULL;
2758 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2759 1a76625f 2018-10-22 stsp a->commits_needed--;
2761 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2762 3abe8080 2019-04-10 stsp if (errcode) {
2763 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2764 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2765 f2d749db 2022-07-12 thomas goto done;
2766 3abe8080 2019-04-10 stsp } else if (*a->quit)
2768 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2769 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2770 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2771 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2774 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2775 7c1452c1 2020-03-26 stsp if (errcode) {
2776 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2777 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2778 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2779 f2d749db 2022-07-12 thomas goto done;
2783 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2785 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2786 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2787 7c1452c1 2020-03-26 stsp &tog_mutex);
2788 f2d749db 2022-07-12 thomas if (errcode) {
2789 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2790 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2791 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2792 f2d749db 2022-07-12 thomas goto done;
2794 21355643 2020-12-06 stsp if (*a->quit)
2799 3abe8080 2019-04-10 stsp a->log_complete = 1;
2800 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2801 f2d749db 2022-07-12 thomas if (errcode)
2802 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2804 f2d749db 2022-07-12 thomas if (err) {
2805 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2806 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2808 1a76625f 2018-10-22 stsp return (void *)err;
2811 1a76625f 2018-10-22 stsp static const struct got_error *
2812 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2814 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2815 1a76625f 2018-10-22 stsp int errcode;
2817 1a76625f 2018-10-22 stsp if (s->thread) {
2818 1a76625f 2018-10-22 stsp s->quit = 1;
2819 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2820 1a76625f 2018-10-22 stsp if (errcode)
2821 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2822 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2823 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2824 1a76625f 2018-10-22 stsp if (errcode)
2825 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2826 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2827 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2828 1a76625f 2018-10-22 stsp if (errcode)
2829 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2830 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2831 1a76625f 2018-10-22 stsp if (errcode)
2832 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2833 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2834 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2837 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2838 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2839 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2842 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2843 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2844 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2845 1af5eddf 2022-06-23 thomas if (err == NULL)
2846 1af5eddf 2022-06-23 thomas err = pack_err;
2847 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2850 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2851 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2852 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2855 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2858 9343a5fb 2018-06-23 stsp static const struct got_error *
2859 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2861 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2862 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2863 276b94a1 2020-11-13 naddy int errcode;
2865 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2867 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2868 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2869 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2871 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2872 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2873 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2875 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2876 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2877 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2878 1a76625f 2018-10-22 stsp free(s->start_id);
2879 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2880 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2881 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2882 1a76625f 2018-10-22 stsp return err;
2885 1a76625f 2018-10-22 stsp static const struct got_error *
2886 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2888 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2890 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2891 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2892 60493ae3 2019-06-20 stsp return NULL;
2895 60493ae3 2019-06-20 stsp static const struct got_error *
2896 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2898 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2899 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2900 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2902 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2903 f9686aa5 2020-03-27 stsp show_log_view(view);
2904 f9686aa5 2020-03-27 stsp update_panels();
2905 f9686aa5 2020-03-27 stsp doupdate();
2907 96e2b566 2019-07-08 stsp if (s->search_entry) {
2908 13add988 2019-10-15 stsp int errcode, ch;
2909 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2910 13add988 2019-10-15 stsp if (errcode)
2911 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2912 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2913 13add988 2019-10-15 stsp ch = wgetch(view->window);
2914 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2915 13add988 2019-10-15 stsp if (errcode)
2916 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2917 13add988 2019-10-15 stsp "pthread_mutex_lock");
2918 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2919 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2920 678cbce5 2019-07-28 stsp return NULL;
2922 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2923 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2925 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2926 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2927 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2928 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2929 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2932 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2933 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2934 1f4d5162 2022-06-23 thomas * might have changed.
2936 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2937 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2938 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2940 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2941 b74feda6 2022-06-23 thomas } else {
2942 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2943 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2944 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2946 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2947 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2950 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2953 60493ae3 2019-06-20 stsp while (1) {
2954 13add988 2019-10-15 stsp int have_match = 0;
2956 60493ae3 2019-06-20 stsp if (entry == NULL) {
2957 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2958 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2959 f9967bca 2020-03-27 stsp view->search_next_done =
2960 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2961 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2962 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2963 f801134a 2019-06-25 stsp return NULL;
2966 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2967 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2968 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2970 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2971 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2974 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2975 13add988 2019-10-15 stsp &view->regex);
2978 13add988 2019-10-15 stsp if (have_match) {
2979 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2980 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2984 96e2b566 2019-07-08 stsp s->search_entry = entry;
2985 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2986 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2988 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2991 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2992 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2993 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2994 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2996 60493ae3 2019-06-20 stsp return err;
2999 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3000 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3002 60493ae3 2019-06-20 stsp return err;
3007 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3009 60493ae3 2019-06-20 stsp return NULL;
3012 60493ae3 2019-06-20 stsp static const struct got_error *
3013 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3014 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3015 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3017 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3018 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3019 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3020 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3021 1a76625f 2018-10-22 stsp int errcode;
3023 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3024 f135c941 2020-02-20 stsp free(s->in_repo_path);
3025 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3026 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3027 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3030 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3031 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3032 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3034 fb2756b9 2018-08-04 stsp s->repo = repo;
3035 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3036 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3037 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3038 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3039 9cd7cbd1 2020-12-07 stsp goto done;
3042 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3043 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3044 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3045 5036bf37 2018-09-24 stsp goto done;
3047 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3049 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3050 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3051 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3052 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3054 11b20872 2019-11-08 stsp goto done;
3055 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3056 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3057 11b20872 2019-11-08 stsp if (err) {
3058 11b20872 2019-11-08 stsp free_colors(&s->colors);
3059 11b20872 2019-11-08 stsp goto done;
3061 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3062 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3063 11b20872 2019-11-08 stsp if (err) {
3064 11b20872 2019-11-08 stsp free_colors(&s->colors);
3065 11b20872 2019-11-08 stsp goto done;
3069 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3070 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3071 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3072 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3073 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3074 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3076 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3077 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3078 1af5eddf 2022-06-23 thomas if (err)
3079 1af5eddf 2022-06-23 thomas goto done;
3081 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3082 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3083 7cd52833 2022-06-23 thomas if (err)
3084 7cd52833 2022-06-23 thomas goto done;
3085 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3086 b672a97a 2020-01-27 stsp !s->log_branches);
3088 1a76625f 2018-10-22 stsp goto done;
3089 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3090 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3092 c5b78334 2020-01-12 stsp goto done;
3094 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3095 1a76625f 2018-10-22 stsp if (errcode) {
3096 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3097 1a76625f 2018-10-22 stsp goto done;
3099 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3100 7c1452c1 2020-03-26 stsp if (errcode) {
3101 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3102 7c1452c1 2020-03-26 stsp goto done;
3105 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3106 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3107 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3108 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3109 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3110 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3111 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3112 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3113 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3114 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3115 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3116 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3117 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3120 1a76625f 2018-10-22 stsp close_log_view(view);
3121 ba4f502b 2018-08-04 stsp return err;
3124 e5a0f69f 2018-08-18 stsp static const struct got_error *
3125 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3127 f2f6d207 2020-11-24 stsp const struct got_error *err;
3128 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3130 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3131 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3132 2b380cc8 2018-10-24 stsp &s->thread_args);
3133 2b380cc8 2018-10-24 stsp if (errcode)
3134 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3135 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3136 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3138 f2f6d207 2020-11-24 stsp return err;
3142 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3145 b31f89ff 2022-07-01 thomas static void
3146 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3148 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3150 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3151 b31f89ff 2022-07-01 thomas view->count = 0;
3152 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3155 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3156 b31f89ff 2022-07-01 thomas || home)
3157 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3159 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3160 b31f89ff 2022-07-01 thomas --s->selected;
3162 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3164 b31f89ff 2022-07-01 thomas select_commit(s);
3168 b31f89ff 2022-07-01 thomas static const struct got_error *
3169 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3171 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3172 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3173 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3175 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3176 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3177 b31f89ff 2022-07-01 thomas return NULL;
3179 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3180 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3182 7ed048bd 2022-08-12 thomas if (!page) {
3183 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3184 b31f89ff 2022-07-01 thomas ++s->selected;
3186 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3187 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3188 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3191 7ed048bd 2022-08-12 thomas s->selected = 0;
3192 7ed048bd 2022-08-12 thomas entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3193 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3194 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3195 7ed048bd 2022-08-12 thomas if (entry == NULL)
3197 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3198 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3200 7ed048bd 2022-08-12 thomas if (n > 0)
3201 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3202 b31f89ff 2022-07-01 thomas } else {
3203 bd3f8225 2022-08-12 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1 &&
3204 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3205 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3206 bd3f8225 2022-08-12 thomas s->commits.ncommits - s->selected_entry->idx - 1);
3208 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3210 b31f89ff 2022-07-01 thomas if (err)
3211 b31f89ff 2022-07-01 thomas return err;
3214 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3215 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3217 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3218 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3219 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3220 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3223 b31f89ff 2022-07-01 thomas select_commit(s);
3225 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3226 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3227 b31f89ff 2022-07-01 thomas view->count = 0;
3229 b31f89ff 2022-07-01 thomas return NULL;
3232 a5d43cac 2022-07-01 thomas static void
3233 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3238 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3239 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3240 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3241 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3242 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3244 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3245 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3246 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3247 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3248 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3249 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3251 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3255 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3256 e5a0f69f 2018-08-18 stsp static const struct got_error *
3257 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3259 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3261 a5d43cac 2022-07-01 thomas view->nlines = y;
3262 64486692 2022-07-07 thomas view->ncols = COLS;
3263 a5d43cac 2022-07-01 thomas err = view_resize(view);
3264 a5d43cac 2022-07-01 thomas if (err)
3265 a5d43cac 2022-07-01 thomas return err;
3267 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3269 a5d43cac 2022-07-01 thomas return err;
3272 07dd3ed3 2022-08-06 thomas static const struct got_error *
3273 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3275 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3276 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3277 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3279 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3280 25100026 2022-08-13 thomas return NULL;
3282 07dd3ed3 2022-08-06 thomas g = view->gline;
3283 07dd3ed3 2022-08-06 thomas view->gline = 0;
3285 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3286 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3287 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3288 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3289 07dd3ed3 2022-08-06 thomas select_commit(s);
3290 07dd3ed3 2022-08-06 thomas return NULL;
3293 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3294 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3295 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3296 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3297 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3298 07dd3ed3 2022-08-06 thomas if (err)
3299 07dd3ed3 2022-08-06 thomas return err;
3300 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3301 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3303 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3304 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3306 07dd3ed3 2022-08-06 thomas select_commit(s);
3307 07dd3ed3 2022-08-06 thomas return NULL;
3311 a5d43cac 2022-07-01 thomas static const struct got_error *
3312 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3314 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3315 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3316 7ed048bd 2022-08-12 thomas int eos, nscroll;
3318 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3319 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3320 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3321 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3322 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3323 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3325 c0be8933 2022-08-12 thomas if (err)
3326 c0be8933 2022-08-12 thomas return err;
3329 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3330 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3331 068ab281 2022-07-01 thomas --eos; /* border */
3333 07dd3ed3 2022-08-06 thomas if (view->gline)
3334 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3336 528dedf3 2021-08-30 stsp switch (ch) {
3338 1e37a5c2 2019-05-12 jcs s->quit = 1;
3340 05171be4 2022-06-23 thomas case '0':
3341 05171be4 2022-06-23 thomas view->x = 0;
3343 05171be4 2022-06-23 thomas case '$':
3344 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3345 07b0611c 2022-06-23 thomas view->count = 0;
3347 05171be4 2022-06-23 thomas case KEY_RIGHT:
3348 05171be4 2022-06-23 thomas case 'l':
3349 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3350 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3352 07b0611c 2022-06-23 thomas view->count = 0;
3354 05171be4 2022-06-23 thomas case KEY_LEFT:
3355 05171be4 2022-06-23 thomas case 'h':
3356 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3357 07b0611c 2022-06-23 thomas if (view->x <= 0)
3358 07b0611c 2022-06-23 thomas view->count = 0;
3361 1e37a5c2 2019-05-12 jcs case KEY_UP:
3364 f7140bf5 2021-10-17 thomas case CTRL('p'):
3365 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3368 912a3f79 2021-08-30 j case KEY_HOME:
3369 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3370 07b0611c 2022-06-23 thomas view->count = 0;
3372 70f17a53 2022-06-13 thomas case CTRL('u'):
3373 23427b14 2022-06-23 thomas case 'u':
3374 70f17a53 2022-06-13 thomas nscroll /= 2;
3375 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3376 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3377 a4292ac5 2019-05-12 jcs case CTRL('b'):
3378 1c5e5faa 2022-06-23 thomas case 'b':
3379 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3382 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3385 f7140bf5 2021-10-17 thomas case CTRL('n'):
3386 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3388 f69c5a46 2022-07-19 thomas case '@':
3389 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3392 912a3f79 2021-08-30 j case KEY_END: {
3393 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3394 912a3f79 2021-08-30 j * traverse them all. */
3395 07b0611c 2022-06-23 thomas view->count = 0;
3396 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3397 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3398 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3399 7ed048bd 2022-08-12 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3400 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3403 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3404 23427b14 2022-06-23 thomas case 'd':
3405 70f17a53 2022-06-13 thomas nscroll /= 2;
3406 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3407 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3408 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3409 4c2d69cb 2022-06-23 thomas case 'f':
3410 b31f89ff 2022-07-01 thomas case ' ':
3411 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3413 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3414 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3415 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3416 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3417 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3418 2b779855 2020-12-05 naddy select_commit(s);
3419 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3420 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3421 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3422 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3423 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3426 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3427 444d5325 2022-07-03 thomas case '\r':
3428 07b0611c 2022-06-23 thomas view->count = 0;
3429 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3431 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3433 1be4947a 2022-07-22 thomas case 'T':
3434 07b0611c 2022-06-23 thomas view->count = 0;
3435 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3437 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3439 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3440 21355643 2020-12-06 stsp case CTRL('l'):
3442 07b0611c 2022-06-23 thomas view->count = 0;
3443 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3444 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3446 21355643 2020-12-06 stsp err = stop_log_thread(s);
3448 74cfe85e 2020-10-20 stsp return err;
3449 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3450 21355643 2020-12-06 stsp char *parent_path;
3451 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3453 21355643 2020-12-06 stsp return err;
3454 21355643 2020-12-06 stsp free(s->in_repo_path);
3455 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3456 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3457 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3458 21355643 2020-12-06 stsp struct got_object_id *start_id;
3459 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3460 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3461 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3463 21355643 2020-12-06 stsp return err;
3464 21355643 2020-12-06 stsp free(s->start_id);
3465 21355643 2020-12-06 stsp s->start_id = start_id;
3466 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3467 21355643 2020-12-06 stsp } else /* 'B' */
3468 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3470 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3471 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3472 bf7e79b3 2022-06-23 thomas if (err)
3473 bf7e79b3 2022-06-23 thomas return err;
3475 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3476 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3477 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3479 21355643 2020-12-06 stsp return err;
3480 51a10b52 2020-12-26 stsp tog_free_refs();
3481 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3483 ca51c541 2020-12-07 stsp return err;
3484 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3485 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3487 d01904d4 2019-06-25 stsp return err;
3488 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3489 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3491 b672a97a 2020-01-27 stsp return err;
3492 21355643 2020-12-06 stsp free_commits(&s->commits);
3493 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3494 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3495 21355643 2020-12-06 stsp s->selected_entry = NULL;
3496 21355643 2020-12-06 stsp s->selected = 0;
3497 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3498 21355643 2020-12-06 stsp s->quit = 0;
3499 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3500 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3501 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3502 94ecf40d 2022-07-22 thomas view->offset = 0;
3504 1be4947a 2022-07-22 thomas case 'R':
3505 07b0611c 2022-06-23 thomas view->count = 0;
3506 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3509 07b0611c 2022-06-23 thomas view->count = 0;
3513 80ddbec8 2018-04-29 stsp return err;
3516 4ed7e80c 2018-05-20 stsp static const struct got_error *
3517 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3519 c2db6724 2019-01-04 stsp const struct got_error *error;
3521 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3522 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3523 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3525 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3526 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3528 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3529 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3531 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3532 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3534 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3535 c2db6724 2019-01-04 stsp if (error != NULL)
3536 c2db6724 2019-01-04 stsp return error;
3538 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3539 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3541 c2db6724 2019-01-04 stsp return NULL;
3544 a915003a 2019-02-05 stsp static void
3545 a915003a 2019-02-05 stsp init_curses(void)
3548 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3549 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3550 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3552 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3553 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3554 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3555 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3556 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3558 a915003a 2019-02-05 stsp initscr();
3560 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3563 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3564 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3565 a915003a 2019-02-05 stsp curs_set(0);
3566 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3567 6d17833f 2019-11-08 stsp start_color();
3568 6d17833f 2019-11-08 stsp use_default_colors();
3572 c2db6724 2019-01-04 stsp static const struct got_error *
3573 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3574 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3576 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3578 f135c941 2020-02-20 stsp if (argc == 0) {
3579 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3580 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3581 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3582 f135c941 2020-02-20 stsp return NULL;
3585 f135c941 2020-02-20 stsp if (worktree) {
3586 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3589 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3591 f135c941 2020-02-20 stsp return err;
3592 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3593 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3594 bfd61697 2020-11-14 stsp p) == -1) {
3595 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3596 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3600 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3602 f135c941 2020-02-20 stsp return err;
3605 f135c941 2020-02-20 stsp static const struct got_error *
3606 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3608 80ddbec8 2018-04-29 stsp const struct got_error *error;
3609 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3610 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3611 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3612 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3613 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3614 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3615 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3616 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3617 04cc582a 2018-08-01 stsp struct tog_view *view;
3618 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3620 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3621 80ddbec8 2018-04-29 stsp switch (ch) {
3623 b672a97a 2020-01-27 stsp log_branches = 1;
3626 80ddbec8 2018-04-29 stsp start_commit = optarg;
3629 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3630 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3631 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3635 17020d27 2019-03-07 stsp usage_log();
3636 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3640 80ddbec8 2018-04-29 stsp argc -= optind;
3641 80ddbec8 2018-04-29 stsp argv += optind;
3643 f135c941 2020-02-20 stsp if (argc > 1)
3644 f135c941 2020-02-20 stsp usage_log();
3646 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3647 7cd52833 2022-06-23 thomas if (error != NULL)
3648 7cd52833 2022-06-23 thomas goto done;
3650 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3651 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3652 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3653 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3654 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3655 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3656 c156c7a4 2020-12-18 stsp goto done;
3657 a1fbf39a 2019-08-11 stsp if (worktree)
3658 6962eb72 2020-02-20 stsp repo_path =
3659 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3661 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3662 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3663 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3664 c156c7a4 2020-12-18 stsp goto done;
3668 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3669 80ddbec8 2018-04-29 stsp if (error != NULL)
3670 ecb28ae0 2018-07-16 stsp goto done;
3672 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3673 f135c941 2020-02-20 stsp repo, worktree);
3674 f135c941 2020-02-20 stsp if (error)
3675 f135c941 2020-02-20 stsp goto done;
3677 f135c941 2020-02-20 stsp init_curses();
3679 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3680 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3681 c02c541e 2019-03-29 stsp if (error)
3682 c02c541e 2019-03-29 stsp goto done;
3684 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3685 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3686 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3687 87670572 2020-12-26 naddy if (error)
3688 87670572 2020-12-26 naddy goto done;
3691 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3692 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3693 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3694 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3695 d8f38dc4 2020-12-05 stsp if (error)
3696 d8f38dc4 2020-12-05 stsp goto done;
3697 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3699 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3700 d8f38dc4 2020-12-05 stsp if (error == NULL)
3701 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3702 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3703 d8f38dc4 2020-12-05 stsp goto done;
3704 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3705 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3706 d8f38dc4 2020-12-05 stsp if (error)
3707 d8f38dc4 2020-12-05 stsp goto done;
3710 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3711 04cc582a 2018-08-01 stsp if (view == NULL) {
3712 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3713 04cc582a 2018-08-01 stsp goto done;
3715 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3716 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3717 ba4f502b 2018-08-04 stsp if (error)
3718 ba4f502b 2018-08-04 stsp goto done;
3719 2fc00ff4 2019-08-31 stsp if (worktree) {
3720 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3721 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3722 2fc00ff4 2019-08-31 stsp worktree = NULL;
3724 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3726 f135c941 2020-02-20 stsp free(in_repo_path);
3727 ecb28ae0 2018-07-16 stsp free(repo_path);
3728 ecb28ae0 2018-07-16 stsp free(cwd);
3729 899d86c2 2018-05-10 stsp free(start_id);
3730 d8f38dc4 2020-12-05 stsp free(label);
3732 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3733 1d0f4054 2021-06-17 stsp if (repo) {
3734 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3735 1d0f4054 2021-06-17 stsp if (error == NULL)
3736 1d0f4054 2021-06-17 stsp error = close_err;
3738 ec142235 2019-03-07 stsp if (worktree)
3739 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3740 7cd52833 2022-06-23 thomas if (pack_fds) {
3741 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3742 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3743 7cd52833 2022-06-23 thomas if (error == NULL)
3744 7cd52833 2022-06-23 thomas error = pack_err;
3746 51a10b52 2020-12-26 stsp tog_free_refs();
3747 80ddbec8 2018-04-29 stsp return error;
3750 4ed7e80c 2018-05-20 stsp __dead static void
3751 9f7d7167 2018-04-29 stsp usage_diff(void)
3754 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3755 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
3759 6d17833f 2019-11-08 stsp static int
3760 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3761 41605754 2020-11-12 stsp regmatch_t *regmatch)
3763 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3766 ef20f542 2022-06-26 thomas static struct tog_color *
3767 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3769 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3771 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3772 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3773 6d17833f 2019-11-08 stsp return tc;
3776 6d17833f 2019-11-08 stsp return NULL;
3779 4ed7e80c 2018-05-20 stsp static const struct got_error *
3780 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3781 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3783 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3784 666f7b10 2022-06-23 thomas char *exstr = NULL;
3785 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3786 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3787 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3788 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3790 41605754 2020-11-12 stsp *wtotal = 0;
3792 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3793 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3795 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3796 666f7b10 2022-06-23 thomas if (err)
3797 666f7b10 2022-06-23 thomas return err;
3799 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3800 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3801 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3802 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3803 666f7b10 2022-06-23 thomas goto done;
3805 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3806 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3807 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3808 cbea3800 2022-06-23 thomas goto done;
3810 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3811 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3812 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3813 cbea3800 2022-06-23 thomas goto done;
3816 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3817 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3818 cbea3800 2022-06-23 thomas col_tab_align, 1);
3819 cbea3800 2022-06-23 thomas if (err)
3820 cbea3800 2022-06-23 thomas goto done;
3821 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3822 05171be4 2022-06-23 thomas if (n) {
3823 cbea3800 2022-06-23 thomas free(wline);
3824 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3825 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3826 cbea3800 2022-06-23 thomas if (err)
3827 cbea3800 2022-06-23 thomas goto done;
3828 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3829 cbea3800 2022-06-23 thomas wlimit -= width;
3830 cbea3800 2022-06-23 thomas *wtotal += width;
3833 41605754 2020-11-12 stsp if (wlimit > 0) {
3834 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3835 cbea3800 2022-06-23 thomas size_t wlen;
3837 cbea3800 2022-06-23 thomas free(wline);
3838 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3839 cbea3800 2022-06-23 thomas col_tab_align, 1);
3840 cbea3800 2022-06-23 thomas if (err)
3841 cbea3800 2022-06-23 thomas goto done;
3842 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3843 cbea3800 2022-06-23 thomas while (i < wlen) {
3844 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3845 cbea3800 2022-06-23 thomas if (width == -1) {
3846 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3847 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3848 cbea3800 2022-06-23 thomas goto done;
3850 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3852 1c5e5faa 2022-06-23 thomas w += width;
3855 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3856 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3857 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3858 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3859 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3860 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3861 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3865 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3866 cbea3800 2022-06-23 thomas free(wline);
3867 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3868 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3869 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3870 cbea3800 2022-06-23 thomas col_tab_align, 1);
3871 cbea3800 2022-06-23 thomas if (err)
3872 cbea3800 2022-06-23 thomas goto done;
3873 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3874 cbea3800 2022-06-23 thomas } else {
3875 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3876 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3877 cbea3800 2022-06-23 thomas if (err)
3878 cbea3800 2022-06-23 thomas goto done;
3879 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3881 cbea3800 2022-06-23 thomas *wtotal += width2;
3884 05171be4 2022-06-23 thomas free(wline);
3885 666f7b10 2022-06-23 thomas free(exstr);
3886 cbea3800 2022-06-23 thomas free(seg0);
3887 cbea3800 2022-06-23 thomas free(seg1);
3888 cbea3800 2022-06-23 thomas free(seg2);
3889 cbea3800 2022-06-23 thomas return err;
3892 07dd3ed3 2022-08-06 thomas static int
3893 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
3895 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
3896 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
3898 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
3899 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
3901 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3902 07dd3ed3 2022-08-06 thomas selected = first;
3903 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3904 07dd3ed3 2022-08-06 thomas f = s->f;
3905 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
3906 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
3908 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3909 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
3910 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3911 07dd3ed3 2022-08-06 thomas f = s->blame.f;
3913 07dd3ed3 2022-08-06 thomas return 0;
3915 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
3916 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
3917 07dd3ed3 2022-08-06 thomas return 0;
3918 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3919 07dd3ed3 2022-08-06 thomas rewind(f);
3920 07dd3ed3 2022-08-06 thomas *eof = 0;
3921 07dd3ed3 2022-08-06 thomas *first = 1;
3922 07dd3ed3 2022-08-06 thomas *lineno = 0;
3923 07dd3ed3 2022-08-06 thomas *nprinted = 0;
3924 07dd3ed3 2022-08-06 thomas return 0;
3927 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
3928 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
3929 07dd3ed3 2022-08-06 thomas view->gline = 0;
3931 07dd3ed3 2022-08-06 thomas return 1;
3934 41605754 2020-11-12 stsp static const struct got_error *
3935 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3937 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3938 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3939 61e69b96 2018-05-20 stsp const struct got_error *err;
3940 82c78e96 2022-08-06 thomas int nprinted = 0;
3941 b304db33 2018-05-20 stsp char *line;
3942 826082fe 2020-12-10 stsp size_t linesize = 0;
3943 826082fe 2020-12-10 stsp ssize_t linelen;
3944 61e69b96 2018-05-20 stsp wchar_t *wline;
3945 e0b650dd 2018-05-20 stsp int width;
3946 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3947 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3948 fe621944 2020-11-10 stsp off_t line_offset;
3950 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
3951 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
3952 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3953 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3955 f7d12f7e 2018-08-01 stsp werase(view->window);
3957 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
3958 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
3960 a3404814 2018-09-02 stsp if (header) {
3961 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3962 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
3963 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
3965 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3966 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3967 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3969 135a2da0 2020-11-11 stsp free(line);
3971 a3404814 2018-09-02 stsp return err;
3973 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3974 a3404814 2018-09-02 stsp wstandout(view->window);
3975 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3976 e54cc94a 2020-11-11 stsp free(wline);
3977 e54cc94a 2020-11-11 stsp wline = NULL;
3978 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3979 a3404814 2018-09-02 stsp wstandend(view->window);
3980 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3981 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3983 a3404814 2018-09-02 stsp if (max_lines <= 1)
3984 a3404814 2018-09-02 stsp return NULL;
3985 a3404814 2018-09-02 stsp max_lines--;
3988 89f1a395 2020-12-01 naddy s->eof = 0;
3989 05171be4 2022-06-23 thomas view->maxx = 0;
3990 826082fe 2020-12-10 stsp line = NULL;
3991 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3992 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
3993 6568f0aa 2022-08-06 thomas attr_t attr = 0;
3995 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3996 826082fe 2020-12-10 stsp if (linelen == -1) {
3997 826082fe 2020-12-10 stsp if (feof(s->f)) {
3998 826082fe 2020-12-10 stsp s->eof = 1;
4001 826082fe 2020-12-10 stsp free(line);
4002 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4005 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4006 07dd3ed3 2022-08-06 thomas continue;
4007 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4008 07dd3ed3 2022-08-06 thomas continue;
4009 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4010 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4012 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4013 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4014 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4015 cbea3800 2022-06-23 thomas if (err) {
4016 cbea3800 2022-06-23 thomas free(line);
4017 cbea3800 2022-06-23 thomas return err;
4019 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4020 cbea3800 2022-06-23 thomas free(wline);
4021 cbea3800 2022-06-23 thomas wline = NULL;
4023 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4024 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4025 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4026 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4027 07dd3ed3 2022-08-06 thomas if (attr)
4028 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4029 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4030 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4031 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4032 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4033 41605754 2020-11-12 stsp if (err) {
4034 41605754 2020-11-12 stsp free(line);
4035 41605754 2020-11-12 stsp return err;
4038 f1c0ec19 2022-06-23 thomas int skip;
4039 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4040 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4041 f1c0ec19 2022-06-23 thomas if (err) {
4042 f1c0ec19 2022-06-23 thomas free(line);
4043 f1c0ec19 2022-06-23 thomas return err;
4045 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4046 f1c0ec19 2022-06-23 thomas free(wline);
4047 41605754 2020-11-12 stsp wline = NULL;
4049 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4050 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4051 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4052 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4053 07dd3ed3 2022-08-06 thomas } else {
4054 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4055 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4057 07dd3ed3 2022-08-06 thomas if (attr)
4058 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4059 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4060 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4062 826082fe 2020-12-10 stsp free(line);
4063 fe621944 2020-11-10 stsp if (nprinted >= 1)
4064 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4065 89f1a395 2020-12-01 naddy (nprinted - 1);
4067 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4069 a5d43cac 2022-07-01 thomas view_border(view);
4071 89f1a395 2020-12-01 naddy if (s->eof) {
4072 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4073 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4074 c3e9aa98 2019-05-13 jcs nprinted++;
4077 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4078 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4080 c3e9aa98 2019-05-13 jcs return err;
4083 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4084 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4085 e54cc94a 2020-11-11 stsp free(wline);
4086 e54cc94a 2020-11-11 stsp wline = NULL;
4087 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4090 26ed57b2 2018-05-19 stsp return NULL;
4093 abd2672a 2018-12-23 stsp static char *
4094 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4096 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4097 09867e48 2019-08-13 stsp char *p, *s;
4099 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4100 09867e48 2019-08-13 stsp if (tm == NULL)
4101 09867e48 2019-08-13 stsp return NULL;
4102 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4103 09867e48 2019-08-13 stsp if (s == NULL)
4104 09867e48 2019-08-13 stsp return NULL;
4105 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4107 abd2672a 2018-12-23 stsp *p = '\0';
4111 4ed7e80c 2018-05-20 stsp static const struct got_error *
4112 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4113 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4115 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4116 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4117 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4118 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4120 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4121 0208f208 2020-05-05 stsp if (qid != NULL) {
4122 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4123 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4124 ec242592 2022-04-22 thomas &qid->id);
4126 0208f208 2020-05-05 stsp return err;
4128 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4129 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4130 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4131 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4132 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4134 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4138 0208f208 2020-05-05 stsp if (tree_id1) {
4139 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4141 0208f208 2020-05-05 stsp goto done;
4144 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4145 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4147 0208f208 2020-05-05 stsp goto done;
4149 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4150 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4152 0208f208 2020-05-05 stsp if (tree1)
4153 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4154 0208f208 2020-05-05 stsp if (tree2)
4155 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4156 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4157 0208f208 2020-05-05 stsp return err;
4160 0208f208 2020-05-05 stsp static const struct got_error *
4161 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4162 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4164 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4166 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4167 fe621944 2020-11-10 stsp if (p == NULL)
4168 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4169 82c78e96 2022-08-06 thomas *lines = p;
4170 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4171 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4172 fe621944 2020-11-10 stsp (*nlines)++;
4174 fe621944 2020-11-10 stsp return NULL;
4177 fe621944 2020-11-10 stsp static const struct got_error *
4178 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4179 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4180 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4182 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4183 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4184 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4185 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4186 45d799e2 2018-12-23 stsp time_t committer_time;
4187 45d799e2 2018-12-23 stsp const char *author, *committer;
4188 8b473291 2019-02-21 stsp char *refs_str = NULL;
4189 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4190 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4191 fe621944 2020-11-10 stsp off_t outoff = 0;
4194 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4196 8b473291 2019-02-21 stsp if (refs) {
4197 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4199 8b473291 2019-02-21 stsp return err;
4202 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4204 abd2672a 2018-12-23 stsp return err;
4206 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4207 15a94983 2018-12-23 stsp if (err) {
4208 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4209 15a94983 2018-12-23 stsp goto done;
4212 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4214 fe621944 2020-11-10 stsp goto done;
4216 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4217 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4218 fe621944 2020-11-10 stsp if (n < 0) {
4219 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4220 abd2672a 2018-12-23 stsp goto done;
4222 fe621944 2020-11-10 stsp outoff += n;
4223 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4225 fe621944 2020-11-10 stsp goto done;
4227 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4228 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4229 fe621944 2020-11-10 stsp if (n < 0) {
4230 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4231 abd2672a 2018-12-23 stsp goto done;
4233 fe621944 2020-11-10 stsp outoff += n;
4234 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4236 fe621944 2020-11-10 stsp goto done;
4238 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4239 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4240 fe621944 2020-11-10 stsp if (datestr) {
4241 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4242 fe621944 2020-11-10 stsp if (n < 0) {
4243 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4244 fe621944 2020-11-10 stsp goto done;
4246 fe621944 2020-11-10 stsp outoff += n;
4247 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4248 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4250 fe621944 2020-11-10 stsp goto done;
4252 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4253 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4254 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4255 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4256 fe621944 2020-11-10 stsp if (n < 0) {
4257 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4258 fe621944 2020-11-10 stsp goto done;
4260 fe621944 2020-11-10 stsp outoff += n;
4261 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4262 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4264 fe621944 2020-11-10 stsp goto done;
4266 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4267 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4268 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4269 ac4dc263 2021-09-24 thomas int pn = 1;
4270 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4271 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4272 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4273 ac4dc263 2021-09-24 thomas if (err)
4274 ac4dc263 2021-09-24 thomas goto done;
4275 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4276 ac4dc263 2021-09-24 thomas if (n < 0) {
4277 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4278 ac4dc263 2021-09-24 thomas goto done;
4280 ac4dc263 2021-09-24 thomas outoff += n;
4281 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4282 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4283 ac4dc263 2021-09-24 thomas if (err)
4284 ac4dc263 2021-09-24 thomas goto done;
4285 ac4dc263 2021-09-24 thomas free(id_str);
4286 ac4dc263 2021-09-24 thomas id_str = NULL;
4290 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4292 5943eee2 2019-08-13 stsp goto done;
4293 fe621944 2020-11-10 stsp s = logmsg;
4294 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4295 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4296 fe621944 2020-11-10 stsp if (n < 0) {
4297 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4298 fe621944 2020-11-10 stsp goto done;
4300 fe621944 2020-11-10 stsp outoff += n;
4301 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4302 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4304 fe621944 2020-11-10 stsp goto done;
4307 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4309 0208f208 2020-05-05 stsp goto done;
4310 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4311 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4312 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4313 fe621944 2020-11-10 stsp if (n < 0) {
4314 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4315 fe621944 2020-11-10 stsp goto done;
4317 fe621944 2020-11-10 stsp outoff += n;
4318 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4319 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4321 fe621944 2020-11-10 stsp goto done;
4322 0208f208 2020-05-05 stsp free((char *)pe->path);
4323 0208f208 2020-05-05 stsp free(pe->data);
4326 0208f208 2020-05-05 stsp fputc('\n', outfile);
4328 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4330 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4331 abd2672a 2018-12-23 stsp free(id_str);
4332 5943eee2 2019-08-13 stsp free(logmsg);
4333 8b473291 2019-02-21 stsp free(refs_str);
4334 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4335 7510f233 2020-08-09 stsp if (err) {
4336 82c78e96 2022-08-06 thomas free(*lines);
4337 82c78e96 2022-08-06 thomas *lines = NULL;
4338 ae6a6978 2020-08-09 stsp *nlines = 0;
4340 fe621944 2020-11-10 stsp return err;
4343 abd2672a 2018-12-23 stsp static const struct got_error *
4344 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4346 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4347 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4348 15a94983 2018-12-23 stsp int obj_type;
4350 82c78e96 2022-08-06 thomas free(s->lines);
4351 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4352 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4353 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4354 fe621944 2020-11-10 stsp s->nlines = 0;
4356 511a516b 2018-05-19 stsp f = got_opentemp();
4357 48ae06ee 2018-10-18 stsp if (f == NULL) {
4358 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4359 48ae06ee 2018-10-18 stsp goto done;
4361 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4362 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4363 fb43ecf1 2019-02-11 stsp goto done;
4367 15a94983 2018-12-23 stsp if (s->id1)
4368 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4370 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4372 15a94983 2018-12-23 stsp goto done;
4374 15a94983 2018-12-23 stsp switch (obj_type) {
4375 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4376 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4377 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4378 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4379 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4381 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4382 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4383 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4384 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4385 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4387 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4388 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4389 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4390 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4391 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4393 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4395 3ffacbe1 2020-02-02 tracey goto done;
4396 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4397 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4398 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4399 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4400 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4401 f44b1f58 2020-02-02 tracey if (err)
4402 f44b1f58 2020-02-02 tracey goto done;
4403 f44b1f58 2020-02-02 tracey } else {
4404 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4405 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4406 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4407 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4408 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4410 f44b1f58 2020-02-02 tracey if (err)
4411 f44b1f58 2020-02-02 tracey goto done;
4416 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4418 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4419 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4420 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4421 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4425 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4429 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4430 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4431 48ae06ee 2018-10-18 stsp return err;
4434 f5215bb9 2019-02-22 stsp static void
4435 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4437 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4438 f5215bb9 2019-02-22 stsp update_panels();
4439 f5215bb9 2019-02-22 stsp doupdate();
4442 f44b1f58 2020-02-02 tracey static const struct got_error *
4443 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4445 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4447 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4448 f44b1f58 2020-02-02 tracey return NULL;
4451 f44b1f58 2020-02-02 tracey static const struct got_error *
4452 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4454 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4455 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4456 f44b1f58 2020-02-02 tracey int lineno;
4457 78eecffc 2022-06-23 thomas char *line = NULL;
4458 826082fe 2020-12-10 stsp size_t linesize = 0;
4459 826082fe 2020-12-10 stsp ssize_t linelen;
4461 f44b1f58 2020-02-02 tracey if (!view->searching) {
4462 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4463 f44b1f58 2020-02-02 tracey return NULL;
4466 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4467 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4468 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4470 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4472 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4474 f44b1f58 2020-02-02 tracey while (1) {
4475 f44b1f58 2020-02-02 tracey off_t offset;
4477 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4478 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4479 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4483 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4484 f44b1f58 2020-02-02 tracey lineno = 1;
4486 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4489 82c78e96 2022-08-06 thomas offset = s->lines[lineno - 1].offset;
4490 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4491 f44b1f58 2020-02-02 tracey free(line);
4492 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4494 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4495 78eecffc 2022-06-23 thomas if (linelen != -1) {
4496 78eecffc 2022-06-23 thomas char *exstr;
4497 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4498 78eecffc 2022-06-23 thomas if (err)
4500 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4501 78eecffc 2022-06-23 thomas &view->regmatch)) {
4502 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4503 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4504 78eecffc 2022-06-23 thomas free(exstr);
4507 78eecffc 2022-06-23 thomas free(exstr);
4509 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4510 f44b1f58 2020-02-02 tracey lineno++;
4512 f44b1f58 2020-02-02 tracey lineno--;
4514 826082fe 2020-12-10 stsp free(line);
4516 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4517 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4518 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4521 05171be4 2022-06-23 thomas return err;
4524 48ae06ee 2018-10-18 stsp static const struct got_error *
4525 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4527 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4528 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4530 a0f32f33 2022-06-13 thomas free(s->id1);
4531 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4532 a0f32f33 2022-06-13 thomas free(s->id2);
4533 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4534 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4535 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4536 a0f32f33 2022-06-13 thomas s->f = NULL;
4537 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4538 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4539 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4540 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4541 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4542 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4543 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4544 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4545 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4546 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4547 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4548 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4549 82c78e96 2022-08-06 thomas free(s->lines);
4550 82c78e96 2022-08-06 thomas s->lines = NULL;
4551 a0f32f33 2022-06-13 thomas s->nlines = 0;
4552 a0f32f33 2022-06-13 thomas return err;
4555 a0f32f33 2022-06-13 thomas static const struct got_error *
4556 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4557 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4558 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4559 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4561 48ae06ee 2018-10-18 stsp const struct got_error *err;
4562 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4564 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4565 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4566 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4568 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4569 15a94983 2018-12-23 stsp int type1, type2;
4570 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4572 15a94983 2018-12-23 stsp return err;
4573 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4575 15a94983 2018-12-23 stsp return err;
4577 15a94983 2018-12-23 stsp if (type1 != type2)
4578 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4580 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4581 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4582 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4583 f44b1f58 2020-02-02 tracey s->repo = repo;
4584 f44b1f58 2020-02-02 tracey s->id1 = id1;
4585 f44b1f58 2020-02-02 tracey s->id2 = id2;
4586 3dbaef42 2020-11-24 stsp s->label1 = label1;
4587 3dbaef42 2020-11-24 stsp s->label2 = label2;
4589 15a94983 2018-12-23 stsp if (id1) {
4590 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4591 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4592 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4594 5465d566 2020-02-01 tracey s->id1 = NULL;
4596 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4597 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4598 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4599 a0f32f33 2022-06-13 thomas goto done;
4602 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4603 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4604 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4605 9a1d7435 2022-06-23 thomas goto done;
4608 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4609 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4610 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4611 a0f32f33 2022-06-13 thomas goto done;
4614 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4615 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4616 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4617 19a6a6b5 2022-07-01 thomas goto done;
4620 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4621 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4622 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4623 19a6a6b5 2022-07-01 thomas goto done;
4626 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4627 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4628 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4629 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4630 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4631 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4632 5465d566 2020-02-01 tracey s->repo = repo;
4634 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4637 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
4638 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4639 6568f0aa 2022-08-06 thomas if (rc != ERR)
4640 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
4641 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4642 6568f0aa 2022-08-06 thomas if (rc != ERR)
4643 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
4644 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4645 6568f0aa 2022-08-06 thomas if (rc != ERR)
4646 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
4647 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4648 6568f0aa 2022-08-06 thomas if (rc != ERR)
4649 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
4650 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4651 6568f0aa 2022-08-06 thomas if (rc != ERR)
4652 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4653 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4654 6568f0aa 2022-08-06 thomas if (rc != ERR)
4655 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4656 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4657 6568f0aa 2022-08-06 thomas if (rc != ERR)
4658 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4659 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
4660 6568f0aa 2022-08-06 thomas if (rc != ERR)
4661 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
4662 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
4663 6568f0aa 2022-08-06 thomas if (rc == ERR) {
4664 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
4665 a0f32f33 2022-06-13 thomas goto done;
4669 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4670 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4671 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4672 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4674 5465d566 2020-02-01 tracey err = create_diff(s);
4676 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4677 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4678 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4679 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4680 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4681 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4683 a0f32f33 2022-06-13 thomas if (err)
4684 a0f32f33 2022-06-13 thomas close_diff_view(view);
4685 e5a0f69f 2018-08-18 stsp return err;
4688 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4689 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4691 a3404814 2018-09-02 stsp const struct got_error *err;
4692 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4693 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4694 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4696 a3404814 2018-09-02 stsp if (s->id1) {
4697 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4699 a3404814 2018-09-02 stsp return err;
4700 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
4702 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4704 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4706 a3404814 2018-09-02 stsp return err;
4707 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
4709 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4710 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4711 a3404814 2018-09-02 stsp free(id_str1);
4712 a3404814 2018-09-02 stsp free(id_str2);
4713 a3404814 2018-09-02 stsp return err;
4715 a3404814 2018-09-02 stsp free(id_str1);
4716 a3404814 2018-09-02 stsp free(id_str2);
4718 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4719 267bb3b8 2021-08-01 stsp free(header);
4720 267bb3b8 2021-08-01 stsp return err;
4723 15a087fe 2019-02-21 stsp static const struct got_error *
4724 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4725 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4727 d7a04538 2019-02-21 stsp const struct got_error *err;
4728 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4729 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4730 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4732 15a087fe 2019-02-21 stsp free(s->id2);
4733 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4734 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4735 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4737 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4739 d7a04538 2019-02-21 stsp return err;
4740 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4741 15a087fe 2019-02-21 stsp free(s->id1);
4742 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4743 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4744 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4745 15a087fe 2019-02-21 stsp return NULL;
4748 0cf4efb1 2018-09-29 stsp static const struct got_error *
4749 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4751 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4753 adf4c9e0 2022-07-03 thomas view->count = 0;
4754 adf4c9e0 2022-07-03 thomas wclear(view->window);
4755 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4756 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4757 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4758 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4759 adf4c9e0 2022-07-03 thomas return create_diff(s);
4762 82c78e96 2022-08-06 thomas static void
4763 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4765 82c78e96 2022-08-06 thomas int start, i;
4767 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
4769 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4770 82c78e96 2022-08-06 thomas if (i == 0)
4771 82c78e96 2022-08-06 thomas i = s->nlines - 1;
4772 82c78e96 2022-08-06 thomas if (--i == start)
4773 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4776 82c78e96 2022-08-06 thomas s->selected_line = 1;
4777 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4780 82c78e96 2022-08-06 thomas static void
4781 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4783 82c78e96 2022-08-06 thomas int start, i;
4785 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
4787 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4788 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
4790 82c78e96 2022-08-06 thomas if (++i == start)
4791 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4794 82c78e96 2022-08-06 thomas s->selected_line = 1;
4795 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4798 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4799 4fc71f3b 2022-07-12 thomas int, int, int);
4800 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4801 4fc71f3b 2022-07-12 thomas int, int);
4803 adf4c9e0 2022-07-03 thomas static const struct got_error *
4804 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4806 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4807 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4808 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4809 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4810 826082fe 2020-12-10 stsp char *line = NULL;
4811 826082fe 2020-12-10 stsp size_t linesize = 0;
4812 826082fe 2020-12-10 stsp ssize_t linelen;
4813 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4815 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
4817 e5a0f69f 2018-08-18 stsp switch (ch) {
4818 05171be4 2022-06-23 thomas case '0':
4819 05171be4 2022-06-23 thomas view->x = 0;
4821 05171be4 2022-06-23 thomas case '$':
4822 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4823 07b0611c 2022-06-23 thomas view->count = 0;
4825 05171be4 2022-06-23 thomas case KEY_RIGHT:
4826 05171be4 2022-06-23 thomas case 'l':
4827 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4828 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4830 07b0611c 2022-06-23 thomas view->count = 0;
4832 05171be4 2022-06-23 thomas case KEY_LEFT:
4833 05171be4 2022-06-23 thomas case 'h':
4834 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4835 07b0611c 2022-06-23 thomas if (view->x <= 0)
4836 07b0611c 2022-06-23 thomas view->count = 0;
4840 3dbaef42 2020-11-24 stsp if (ch == 'a')
4841 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4842 3dbaef42 2020-11-24 stsp if (ch == 'w')
4843 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4844 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4847 912a3f79 2021-08-30 j case KEY_HOME:
4848 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4849 07b0611c 2022-06-23 thomas view->count = 0;
4852 912a3f79 2021-08-30 j case KEY_END:
4853 07b0611c 2022-06-23 thomas view->count = 0;
4857 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4861 1e37a5c2 2019-05-12 jcs case KEY_UP:
4862 f7140bf5 2021-10-17 thomas case CTRL('p'):
4863 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4864 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4866 07b0611c 2022-06-23 thomas view->count = 0;
4868 70f17a53 2022-06-13 thomas case CTRL('u'):
4869 23427b14 2022-06-23 thomas case 'u':
4870 70f17a53 2022-06-13 thomas nscroll /= 2;
4871 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4872 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4873 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4874 1c5e5faa 2022-06-23 thomas case 'b':
4875 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4876 07b0611c 2022-06-23 thomas view->count = 0;
4880 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4881 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4884 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4885 f7140bf5 2021-10-17 thomas case CTRL('n'):
4886 1e37a5c2 2019-05-12 jcs if (!s->eof)
4887 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4889 07b0611c 2022-06-23 thomas view->count = 0;
4891 70f17a53 2022-06-13 thomas case CTRL('d'):
4892 23427b14 2022-06-23 thomas case 'd':
4893 70f17a53 2022-06-13 thomas nscroll /= 2;
4894 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4895 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4896 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4897 1c5e5faa 2022-06-23 thomas case 'f':
4899 07b0611c 2022-06-23 thomas if (s->eof) {
4900 07b0611c 2022-06-23 thomas view->count = 0;
4904 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4905 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4906 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4907 826082fe 2020-12-10 stsp if (linelen == -1) {
4908 826082fe 2020-12-10 stsp if (feof(s->f)) {
4909 826082fe 2020-12-10 stsp s->eof = 1;
4911 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4915 826082fe 2020-12-10 stsp free(line);
4917 82c78e96 2022-08-06 thomas case '(':
4918 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4920 82c78e96 2022-08-06 thomas case ')':
4921 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4923 82c78e96 2022-08-06 thomas case '{':
4924 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4926 82c78e96 2022-08-06 thomas case '}':
4927 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
4930 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4931 1e37a5c2 2019-05-12 jcs s->diff_context--;
4932 aa61903a 2021-12-31 thomas s->matched_line = 0;
4933 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4934 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4935 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4936 27829c9e 2020-11-21 stsp s->nlines) {
4937 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4938 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4941 07b0611c 2022-06-23 thomas view->count = 0;
4944 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4945 1e37a5c2 2019-05-12 jcs s->diff_context++;
4946 aa61903a 2021-12-31 thomas s->matched_line = 0;
4947 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4948 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4950 07b0611c 2022-06-23 thomas view->count = 0;
4954 777aae21 2022-07-20 thomas case 'K':
4956 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4957 4fc71f3b 2022-07-12 thomas case '>':
4958 4fc71f3b 2022-07-12 thomas case '.':
4959 777aae21 2022-07-20 thomas case 'J':
4960 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4961 07b0611c 2022-06-23 thomas view->count = 0;
4964 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4966 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4967 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4968 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4970 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4971 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4972 4fc71f3b 2022-07-12 thomas if (err)
4974 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4976 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4979 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4980 4fc71f3b 2022-07-12 thomas if (err)
4982 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4983 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4984 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4986 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4987 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4988 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4990 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4991 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4992 4fc71f3b 2022-07-12 thomas if (err)
4994 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4996 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4998 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4999 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5000 4fc71f3b 2022-07-12 thomas bs->selected_line);
5001 4fc71f3b 2022-07-12 thomas if (id == NULL)
5004 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5007 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5008 4fc71f3b 2022-07-12 thomas if (err)
5011 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5012 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5013 aa61903a 2021-12-31 thomas s->matched_line = 0;
5014 05171be4 2022-06-23 thomas view->x = 0;
5016 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5017 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5020 07b0611c 2022-06-23 thomas view->count = 0;
5024 bcbd79e2 2018-08-19 stsp return err;
5027 4ed7e80c 2018-05-20 stsp static const struct got_error *
5028 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5030 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5031 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5032 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5033 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5034 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5035 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5036 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5037 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5038 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5039 3dbaef42 2020-11-24 stsp const char *errstr;
5040 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5041 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5043 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5044 26ed57b2 2018-05-19 stsp switch (ch) {
5046 64453f7e 2020-11-21 stsp force_text_diff = 1;
5049 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5051 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5052 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5053 8f666e67 2022-02-12 thomas errstr, errstr);
5055 09b5bff8 2020-02-23 naddy case 'r':
5056 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5057 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5058 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5060 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5063 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5066 17020d27 2019-03-07 stsp usage_diff();
5067 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5071 26ed57b2 2018-05-19 stsp argc -= optind;
5072 26ed57b2 2018-05-19 stsp argv += optind;
5074 26ed57b2 2018-05-19 stsp if (argc == 0) {
5075 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5076 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5077 15a94983 2018-12-23 stsp id_str1 = argv[0];
5078 15a94983 2018-12-23 stsp id_str2 = argv[1];
5080 26ed57b2 2018-05-19 stsp usage_diff();
5082 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5083 7cd52833 2022-06-23 thomas if (error)
5084 7cd52833 2022-06-23 thomas goto done;
5086 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5087 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5088 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5089 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5090 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5091 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5092 c156c7a4 2020-12-18 stsp goto done;
5093 a273ac94 2020-02-23 naddy if (worktree)
5094 a273ac94 2020-02-23 naddy repo_path =
5095 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5097 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5098 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5099 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5100 c156c7a4 2020-12-18 stsp goto done;
5104 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5105 eb6600df 2019-01-04 stsp if (error)
5106 eb6600df 2019-01-04 stsp goto done;
5108 a273ac94 2020-02-23 naddy init_curses();
5110 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5111 51a10b52 2020-12-26 stsp if (error)
5112 51a10b52 2020-12-26 stsp goto done;
5114 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5115 26ed57b2 2018-05-19 stsp if (error)
5116 26ed57b2 2018-05-19 stsp goto done;
5118 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5119 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5120 26ed57b2 2018-05-19 stsp if (error)
5121 26ed57b2 2018-05-19 stsp goto done;
5123 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5124 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5125 26ed57b2 2018-05-19 stsp if (error)
5126 26ed57b2 2018-05-19 stsp goto done;
5128 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5129 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5130 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5131 ea5e7bb5 2018-08-01 stsp goto done;
5133 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5134 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5135 5dc9f4bc 2018-08-04 stsp if (error)
5136 5dc9f4bc 2018-08-04 stsp goto done;
5137 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5139 3dbaef42 2020-11-24 stsp free(label1);
5140 3dbaef42 2020-11-24 stsp free(label2);
5141 c02c541e 2019-03-29 stsp free(repo_path);
5142 a273ac94 2020-02-23 naddy free(cwd);
5143 1d0f4054 2021-06-17 stsp if (repo) {
5144 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5145 1d0f4054 2021-06-17 stsp if (error == NULL)
5146 1d0f4054 2021-06-17 stsp error = close_err;
5148 a273ac94 2020-02-23 naddy if (worktree)
5149 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5150 7cd52833 2022-06-23 thomas if (pack_fds) {
5151 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5152 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5153 7cd52833 2022-06-23 thomas if (error == NULL)
5154 7cd52833 2022-06-23 thomas error = pack_err;
5156 51a10b52 2020-12-26 stsp tog_free_refs();
5157 26ed57b2 2018-05-19 stsp return error;
5160 4ed7e80c 2018-05-20 stsp __dead static void
5161 9f7d7167 2018-04-29 stsp usage_blame(void)
5164 91198554 2022-06-23 thomas fprintf(stderr,
5165 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5166 9f7d7167 2018-04-29 stsp getprogname());
5170 84451b3e 2018-07-10 stsp struct tog_blame_line {
5171 84451b3e 2018-07-10 stsp int annotated;
5172 84451b3e 2018-07-10 stsp struct got_object_id *id;
5175 4ed7e80c 2018-05-20 stsp static const struct got_error *
5176 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5178 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5179 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5180 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5181 84451b3e 2018-07-10 stsp const struct got_error *err;
5182 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5183 826082fe 2020-12-10 stsp char *line = NULL;
5184 826082fe 2020-12-10 stsp size_t linesize = 0;
5185 826082fe 2020-12-10 stsp ssize_t linelen;
5186 84451b3e 2018-07-10 stsp wchar_t *wline;
5187 27a741e5 2019-09-11 stsp int width;
5188 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5189 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5190 ab089a2a 2018-07-12 stsp char *id_str;
5191 11b20872 2019-11-08 stsp struct tog_color *tc;
5193 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5195 ab089a2a 2018-07-12 stsp return err;
5197 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5198 f7d12f7e 2018-08-01 stsp werase(view->window);
5200 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5201 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5202 ab089a2a 2018-07-12 stsp free(id_str);
5203 ab089a2a 2018-07-12 stsp return err;
5206 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5207 ab089a2a 2018-07-12 stsp free(line);
5208 2550e4c3 2018-07-13 stsp line = NULL;
5210 1cae65b4 2019-09-22 stsp return err;
5211 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5212 a3404814 2018-09-02 stsp wstandout(view->window);
5213 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5215 11b20872 2019-11-08 stsp wattr_on(view->window,
5216 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5217 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5219 11b20872 2019-11-08 stsp wattr_off(view->window,
5220 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5221 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5222 a3404814 2018-09-02 stsp wstandend(view->window);
5223 2550e4c3 2018-07-13 stsp free(wline);
5224 2550e4c3 2018-07-13 stsp wline = NULL;
5225 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5226 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5228 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5229 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5231 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5232 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5233 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5234 ab089a2a 2018-07-12 stsp free(id_str);
5235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5237 ab089a2a 2018-07-12 stsp free(id_str);
5238 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5239 3f60a8ef 2018-07-10 stsp free(line);
5240 2550e4c3 2018-07-13 stsp line = NULL;
5242 3f60a8ef 2018-07-10 stsp return err;
5243 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5244 2550e4c3 2018-07-13 stsp free(wline);
5245 2550e4c3 2018-07-13 stsp wline = NULL;
5246 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5247 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5249 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5250 05171be4 2022-06-23 thomas view->maxx = 0;
5251 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5252 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5253 826082fe 2020-12-10 stsp if (linelen == -1) {
5254 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5255 826082fe 2020-12-10 stsp s->eof = 1;
5258 84451b3e 2018-07-10 stsp free(line);
5259 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5261 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5262 07dd3ed3 2022-08-06 thomas continue;
5263 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5266 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5267 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5268 cbea3800 2022-06-23 thomas if (err) {
5269 cbea3800 2022-06-23 thomas free(line);
5270 cbea3800 2022-06-23 thomas return err;
5272 cbea3800 2022-06-23 thomas free(wline);
5273 cbea3800 2022-06-23 thomas wline = NULL;
5274 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5276 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5277 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5279 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5280 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5281 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5282 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5283 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5284 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5285 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5286 8d0fe45a 2019-08-12 stsp char *id_str;
5287 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5288 91198554 2022-06-23 thomas blame_line->id);
5289 8d0fe45a 2019-08-12 stsp if (err) {
5290 8d0fe45a 2019-08-12 stsp free(line);
5291 8d0fe45a 2019-08-12 stsp return err;
5293 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5295 11b20872 2019-11-08 stsp wattr_on(view->window,
5296 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5297 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5299 11b20872 2019-11-08 stsp wattr_off(view->window,
5300 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5301 8d0fe45a 2019-08-12 stsp free(id_str);
5302 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5304 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5305 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5308 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5309 ee41ec32 2018-07-10 stsp prev_id = NULL;
5312 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5313 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5314 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5316 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5317 41605754 2020-11-12 stsp width = 9;
5318 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5319 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5320 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5321 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5322 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5323 41605754 2020-11-12 stsp if (err) {
5324 41605754 2020-11-12 stsp free(line);
5325 41605754 2020-11-12 stsp return err;
5327 41605754 2020-11-12 stsp width += 9;
5329 923086fd 2022-06-23 thomas int skip;
5330 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5331 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5332 923086fd 2022-06-23 thomas if (err) {
5333 923086fd 2022-06-23 thomas free(line);
5334 923086fd 2022-06-23 thomas return err;
5336 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5337 02bce7e2 2022-06-23 thomas width += 9;
5338 41605754 2020-11-12 stsp free(wline);
5339 41605754 2020-11-12 stsp wline = NULL;
5342 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5343 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5344 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5345 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5347 826082fe 2020-12-10 stsp free(line);
5348 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5350 a5d43cac 2022-07-01 thomas view_border(view);
5352 84451b3e 2018-07-10 stsp return NULL;
5355 84451b3e 2018-07-10 stsp static const struct got_error *
5356 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5357 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5359 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5360 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5361 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5362 1a76625f 2018-10-22 stsp int errcode;
5364 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5365 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5366 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5368 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5369 1a76625f 2018-10-22 stsp if (errcode)
5370 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5372 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5373 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5374 d68a0a7d 2018-07-10 stsp goto done;
5377 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5378 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5380 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5381 d68a0a7d 2018-07-10 stsp if (line->annotated)
5382 d68a0a7d 2018-07-10 stsp goto done;
5384 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5385 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5386 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5387 84451b3e 2018-07-10 stsp goto done;
5389 84451b3e 2018-07-10 stsp line->annotated = 1;
5391 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5392 1a76625f 2018-10-22 stsp if (errcode)
5393 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5394 84451b3e 2018-07-10 stsp return err;
5397 84451b3e 2018-07-10 stsp static void *
5398 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5400 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5401 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5402 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5403 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5404 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5406 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5407 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5408 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5410 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5411 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5412 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5413 9117a7b7 2022-07-01 thomas goto done;
5416 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5417 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5418 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5419 9117a7b7 2022-07-01 thomas goto done;
5421 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5422 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5423 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5424 9117a7b7 2022-07-01 thomas goto done;
5427 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5429 9117a7b7 2022-07-01 thomas goto done;
5431 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5432 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5433 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5434 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5435 fc06ba56 2019-08-22 stsp err = NULL;
5437 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5438 9117a7b7 2022-07-01 thomas if (errcode) {
5439 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5440 9117a7b7 2022-07-01 thomas goto done;
5443 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5444 1d0f4054 2021-06-17 stsp if (err == NULL)
5445 1d0f4054 2021-06-17 stsp err = close_err;
5446 c9beca56 2018-07-22 stsp ta->repo = NULL;
5447 c9beca56 2018-07-22 stsp *ta->complete = 1;
5449 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5450 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5451 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5454 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5455 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5456 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5457 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5458 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5459 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5460 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5461 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5463 18430de3 2018-07-10 stsp return (void *)err;
5466 245d91c1 2018-07-12 stsp static struct got_object_id *
5467 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5468 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5470 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5472 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5473 8d0fe45a 2019-08-12 stsp return NULL;
5475 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5476 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5477 4fc71f3b 2022-07-12 thomas return NULL;
5479 4fc71f3b 2022-07-12 thomas return line->id;
5482 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5483 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5484 4fc71f3b 2022-07-12 thomas int lineno)
5486 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5488 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5489 4fc71f3b 2022-07-12 thomas return NULL;
5491 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5492 245d91c1 2018-07-12 stsp if (!line->annotated)
5493 245d91c1 2018-07-12 stsp return NULL;
5495 245d91c1 2018-07-12 stsp return line->id;
5498 b880a918 2018-07-10 stsp static const struct got_error *
5499 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5501 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5504 245d91c1 2018-07-12 stsp if (blame->thread) {
5505 1a76625f 2018-10-22 stsp int errcode;
5506 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5507 1a76625f 2018-10-22 stsp if (errcode)
5508 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5509 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5510 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5511 1a76625f 2018-10-22 stsp if (errcode)
5512 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5513 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5514 1a76625f 2018-10-22 stsp if (errcode)
5515 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5516 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5517 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5518 245d91c1 2018-07-12 stsp err = NULL;
5519 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5521 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5522 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5523 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5524 1d0f4054 2021-06-17 stsp if (err == NULL)
5525 1d0f4054 2021-06-17 stsp err = close_err;
5526 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5528 245d91c1 2018-07-12 stsp if (blame->f) {
5529 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5530 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5531 245d91c1 2018-07-12 stsp blame->f = NULL;
5533 57670559 2018-12-23 stsp if (blame->lines) {
5534 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5535 57670559 2018-12-23 stsp free(blame->lines[i].id);
5536 57670559 2018-12-23 stsp free(blame->lines);
5537 57670559 2018-12-23 stsp blame->lines = NULL;
5539 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5540 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5541 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5542 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5543 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5544 7cd52833 2022-06-23 thomas if (err == NULL)
5545 7cd52833 2022-06-23 thomas err = pack_err;
5546 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5548 245d91c1 2018-07-12 stsp return err;
5551 245d91c1 2018-07-12 stsp static const struct got_error *
5552 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5554 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5555 fc06ba56 2019-08-22 stsp int *done = arg;
5556 fc06ba56 2019-08-22 stsp int errcode;
5558 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5559 fc06ba56 2019-08-22 stsp if (errcode)
5560 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5561 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5563 fc06ba56 2019-08-22 stsp if (*done)
5564 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5566 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5567 fc06ba56 2019-08-22 stsp if (errcode)
5568 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5569 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5571 fc06ba56 2019-08-22 stsp return err;
5574 fc06ba56 2019-08-22 stsp static const struct got_error *
5575 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5577 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5578 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5579 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5580 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5581 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5582 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5583 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5584 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5585 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5587 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5588 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5590 15a94983 2018-12-23 stsp return err;
5592 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5593 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5594 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5595 f4ae6ddb 2022-07-01 thomas goto done;
5598 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5599 945f9229 2022-04-16 thomas if (err)
5600 945f9229 2022-04-16 thomas goto done;
5602 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5604 84451b3e 2018-07-10 stsp goto done;
5606 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5607 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5608 84451b3e 2018-07-10 stsp goto done;
5611 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5613 a70480e0 2018-06-23 stsp goto done;
5614 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5615 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5616 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5617 84451b3e 2018-07-10 stsp goto done;
5619 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5620 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5622 1fddf795 2021-01-20 stsp goto done;
5623 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5624 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5625 84451b3e 2018-07-10 stsp goto done;
5628 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5629 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5630 b02560ec 2019-08-19 stsp blame->nlines--;
5632 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5633 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5634 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5635 84451b3e 2018-07-10 stsp goto done;
5638 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5640 bd24772e 2018-07-11 stsp goto done;
5641 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5642 7cd52833 2022-06-23 thomas pack_fds);
5643 7cd52833 2022-06-23 thomas if (err)
5644 7cd52833 2022-06-23 thomas goto done;
5646 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5647 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5648 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5649 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5650 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5651 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5652 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5653 245d91c1 2018-07-12 stsp goto done;
5655 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5657 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5658 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5659 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5660 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5661 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5662 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5663 a5388363 2020-12-01 naddy s->blame_complete = 0;
5665 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5666 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5667 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5668 f5a09613 2020-12-13 naddy s->selected_line = 1;
5670 aa61903a 2021-12-31 thomas s->matched_line = 0;
5673 945f9229 2022-04-16 thomas if (commit)
5674 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5675 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5676 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5678 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5679 27d434c2 2018-09-15 stsp free(obj_id);
5681 245d91c1 2018-07-12 stsp stop_blame(blame);
5682 245d91c1 2018-07-12 stsp return err;
5685 245d91c1 2018-07-12 stsp static const struct got_error *
5686 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5687 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5689 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5690 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5692 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5694 c4843652 2019-08-12 stsp s->path = strdup(path);
5695 c4843652 2019-08-12 stsp if (s->path == NULL)
5696 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5698 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5699 c4843652 2019-08-12 stsp if (err) {
5700 c4843652 2019-08-12 stsp free(s->path);
5701 7cbe629d 2018-08-04 stsp return err;
5704 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5705 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5706 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5707 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5708 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5709 fb2756b9 2018-08-04 stsp s->repo = repo;
5710 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5711 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5713 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5714 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5715 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5716 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5718 11b20872 2019-11-08 stsp return err;
5721 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5722 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5723 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5724 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5725 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5726 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5728 a5388363 2020-12-01 naddy return run_blame(view);
5731 e5a0f69f 2018-08-18 stsp static const struct got_error *
5732 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5734 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5735 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5737 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5738 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5740 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5741 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5742 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5743 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5744 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5747 e5a0f69f 2018-08-18 stsp free(s->path);
5748 11b20872 2019-11-08 stsp free_colors(&s->colors);
5749 e5a0f69f 2018-08-18 stsp return err;
5752 7cbe629d 2018-08-04 stsp static const struct got_error *
5753 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5755 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5757 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5758 6c4c42e0 2019-06-24 stsp return NULL;
5761 6c4c42e0 2019-06-24 stsp static const struct got_error *
5762 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5764 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5765 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5766 6c4c42e0 2019-06-24 stsp int lineno;
5767 78eecffc 2022-06-23 thomas char *line = NULL;
5768 826082fe 2020-12-10 stsp size_t linesize = 0;
5769 826082fe 2020-12-10 stsp ssize_t linelen;
5771 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5772 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5773 6c4c42e0 2019-06-24 stsp return NULL;
5776 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5777 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5778 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5780 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5782 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5784 6c4c42e0 2019-06-24 stsp while (1) {
5785 6c4c42e0 2019-06-24 stsp off_t offset;
5787 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5788 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5789 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5793 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5794 6c4c42e0 2019-06-24 stsp lineno = 1;
5796 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5799 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5800 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5801 6c4c42e0 2019-06-24 stsp free(line);
5802 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5804 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5805 78eecffc 2022-06-23 thomas if (linelen != -1) {
5806 78eecffc 2022-06-23 thomas char *exstr;
5807 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5808 78eecffc 2022-06-23 thomas if (err)
5810 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5811 78eecffc 2022-06-23 thomas &view->regmatch)) {
5812 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5813 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5814 78eecffc 2022-06-23 thomas free(exstr);
5817 78eecffc 2022-06-23 thomas free(exstr);
5819 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5824 826082fe 2020-12-10 stsp free(line);
5826 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5827 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5828 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5831 05171be4 2022-06-23 thomas return err;
5834 6c4c42e0 2019-06-24 stsp static const struct got_error *
5835 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5837 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5838 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5839 2b380cc8 2018-10-24 stsp int errcode;
5841 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5842 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5843 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5844 2b380cc8 2018-10-24 stsp if (errcode)
5845 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5847 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5850 51fe7530 2019-08-19 stsp if (s->blame_complete)
5851 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5853 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5855 a5d43cac 2022-07-01 thomas view_border(view);
5856 e5a0f69f 2018-08-18 stsp return err;
5859 e5a0f69f 2018-08-18 stsp static const struct got_error *
5860 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5861 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5863 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5864 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5866 eaeaa612 2022-07-20 thomas *new_view = NULL;
5868 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5869 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5870 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5872 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5873 eaeaa612 2022-07-20 thomas if (err)
5874 eaeaa612 2022-07-20 thomas view_close(log_view);
5876 eaeaa612 2022-07-20 thomas *new_view = log_view;
5878 eaeaa612 2022-07-20 thomas return err;
5881 eaeaa612 2022-07-20 thomas static const struct got_error *
5882 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5884 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5885 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
5886 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5887 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5889 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5890 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5891 a5d43cac 2022-07-01 thomas --eos; /* border */
5893 e5a0f69f 2018-08-18 stsp switch (ch) {
5894 05171be4 2022-06-23 thomas case '0':
5895 05171be4 2022-06-23 thomas view->x = 0;
5897 05171be4 2022-06-23 thomas case '$':
5898 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5899 07b0611c 2022-06-23 thomas view->count = 0;
5901 05171be4 2022-06-23 thomas case KEY_RIGHT:
5902 05171be4 2022-06-23 thomas case 'l':
5903 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5904 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5906 07b0611c 2022-06-23 thomas view->count = 0;
5908 05171be4 2022-06-23 thomas case KEY_LEFT:
5909 05171be4 2022-06-23 thomas case 'h':
5910 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5911 07b0611c 2022-06-23 thomas if (view->x <= 0)
5912 07b0611c 2022-06-23 thomas view->count = 0;
5915 1e37a5c2 2019-05-12 jcs s->done = 1;
5917 4deef56f 2021-09-02 naddy case 'g':
5918 4deef56f 2021-09-02 naddy case KEY_HOME:
5919 4deef56f 2021-09-02 naddy s->selected_line = 1;
5920 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5921 07b0611c 2022-06-23 thomas view->count = 0;
5923 4deef56f 2021-09-02 naddy case 'G':
5924 4deef56f 2021-09-02 naddy case KEY_END:
5925 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5926 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5927 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5929 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5930 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5932 07b0611c 2022-06-23 thomas view->count = 0;
5935 1e37a5c2 2019-05-12 jcs case KEY_UP:
5936 f7140bf5 2021-10-17 thomas case CTRL('p'):
5937 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5938 1e37a5c2 2019-05-12 jcs s->selected_line--;
5939 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5940 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5941 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5943 07b0611c 2022-06-23 thomas view->count = 0;
5945 70f17a53 2022-06-13 thomas case CTRL('u'):
5946 23427b14 2022-06-23 thomas case 'u':
5947 70f17a53 2022-06-13 thomas nscroll /= 2;
5948 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5949 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5950 ea025d1d 2020-02-22 naddy case CTRL('b'):
5951 1c5e5faa 2022-06-23 thomas case 'b':
5952 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5953 07b0611c 2022-06-23 thomas if (view->count > 1)
5954 07b0611c 2022-06-23 thomas nscroll += nscroll;
5955 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5956 07b0611c 2022-06-23 thomas view->count = 0;
5959 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5960 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5962 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5965 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5966 f7140bf5 2021-10-17 thomas case CTRL('n'):
5967 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5968 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5969 1e37a5c2 2019-05-12 jcs s->selected_line++;
5970 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5971 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5973 07b0611c 2022-06-23 thomas view->count = 0;
5975 1c5e5faa 2022-06-23 thomas case 'c':
5976 1e37a5c2 2019-05-12 jcs case 'p': {
5977 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5979 07b0611c 2022-06-23 thomas view->count = 0;
5980 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5981 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5982 1e37a5c2 2019-05-12 jcs if (id == NULL)
5984 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5985 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5986 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5987 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5988 1e37a5c2 2019-05-12 jcs int obj_type;
5989 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5990 1e37a5c2 2019-05-12 jcs s->repo, id);
5993 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5994 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5995 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5996 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5999 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6000 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6001 ec242592 2022-04-22 thomas s->repo, &pid->id);
6002 945f9229 2022-04-16 thomas if (err)
6004 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6005 945f9229 2022-04-16 thomas pcommit, s->path);
6006 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6007 e5a0f69f 2018-08-18 stsp if (err) {
6008 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6009 1e37a5c2 2019-05-12 jcs err = NULL;
6010 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6013 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6015 1e37a5c2 2019-05-12 jcs free(blob_id);
6016 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6017 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6018 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6021 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6022 ec242592 2022-04-22 thomas &pid->id);
6023 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6025 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6026 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6028 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6033 1e37a5c2 2019-05-12 jcs s->done = 1;
6034 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6035 1e37a5c2 2019-05-12 jcs s->done = 0;
6036 1e37a5c2 2019-05-12 jcs if (thread_err)
6038 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6039 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6040 a5388363 2020-12-01 naddy err = run_blame(view);
6045 1c5e5faa 2022-06-23 thomas case 'C': {
6046 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6048 07b0611c 2022-06-23 thomas view->count = 0;
6049 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6050 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6052 1e37a5c2 2019-05-12 jcs s->done = 1;
6053 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6054 1e37a5c2 2019-05-12 jcs s->done = 0;
6055 1e37a5c2 2019-05-12 jcs if (thread_err)
6057 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6058 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6059 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6060 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6061 a5388363 2020-12-01 naddy err = run_blame(view);
6066 2a31b33b 2022-07-23 thomas case 'L':
6067 eaeaa612 2022-07-20 thomas view->count = 0;
6068 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6069 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6070 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6071 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6073 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6074 1e37a5c2 2019-05-12 jcs case '\r': {
6075 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6076 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6077 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6079 07b0611c 2022-06-23 thomas view->count = 0;
6080 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6081 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6082 1e37a5c2 2019-05-12 jcs if (id == NULL)
6084 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6087 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6088 4fc71f3b 2022-07-12 thomas if (*new_view) {
6089 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6090 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6091 4fc71f3b 2022-07-12 thomas if (err)
6093 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6094 4fc71f3b 2022-07-12 thomas } else {
6095 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6096 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6098 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6099 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6100 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6101 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6102 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6106 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6107 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6108 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6110 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6113 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6114 4fc71f3b 2022-07-12 thomas s->selected_line;
6115 4fc71f3b 2022-07-12 thomas if (*new_view)
6116 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6117 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6118 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6119 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6120 a5d43cac 2022-07-01 thomas if (err)
6124 e78dc838 2020-12-04 stsp view->focussed = 0;
6125 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6126 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6127 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6128 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6129 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6130 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6133 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6134 40236d76 2022-06-23 thomas if (err)
6136 e78dc838 2020-12-04 stsp view->focus_child = 1;
6138 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6143 70f17a53 2022-06-13 thomas case CTRL('d'):
6144 23427b14 2022-06-23 thomas case 'd':
6145 70f17a53 2022-06-13 thomas nscroll /= 2;
6146 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6147 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6148 ea025d1d 2020-02-22 naddy case CTRL('f'):
6149 1c5e5faa 2022-06-23 thomas case 'f':
6151 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6152 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6153 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6154 07b0611c 2022-06-23 thomas view->count = 0;
6157 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6158 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6159 70f17a53 2022-06-13 thomas s->selected_line +=
6160 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6161 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6163 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6164 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6166 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6167 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6169 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6170 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6171 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6172 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6176 07b0611c 2022-06-23 thomas view->count = 0;
6179 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6182 adf4c9e0 2022-07-03 thomas static const struct got_error *
6183 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6185 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6186 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6188 adf4c9e0 2022-07-03 thomas view->count = 0;
6189 adf4c9e0 2022-07-03 thomas s->done = 1;
6190 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6191 adf4c9e0 2022-07-03 thomas s->done = 0;
6192 adf4c9e0 2022-07-03 thomas if (err)
6193 adf4c9e0 2022-07-03 thomas return err;
6194 adf4c9e0 2022-07-03 thomas return run_blame(view);
6197 a70480e0 2018-06-23 stsp static const struct got_error *
6198 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6200 a70480e0 2018-06-23 stsp const struct got_error *error;
6201 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6202 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6203 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6204 0587e10c 2020-07-23 stsp char *link_target = NULL;
6205 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6206 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6207 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6209 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6210 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6212 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6213 a70480e0 2018-06-23 stsp switch (ch) {
6215 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6218 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6219 69069811 2018-08-02 stsp if (repo_path == NULL)
6220 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6224 17020d27 2019-03-07 stsp usage_blame();
6225 a70480e0 2018-06-23 stsp /* NOTREACHED */
6229 a70480e0 2018-06-23 stsp argc -= optind;
6230 a70480e0 2018-06-23 stsp argv += optind;
6232 f135c941 2020-02-20 stsp if (argc != 1)
6233 a70480e0 2018-06-23 stsp usage_blame();
6235 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6236 7cd52833 2022-06-23 thomas if (error != NULL)
6237 7cd52833 2022-06-23 thomas goto done;
6239 69069811 2018-08-02 stsp if (repo_path == NULL) {
6240 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6241 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6242 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6243 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6244 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6245 c156c7a4 2020-12-18 stsp goto done;
6246 f135c941 2020-02-20 stsp if (worktree)
6247 eb41ed75 2019-02-05 stsp repo_path =
6248 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6250 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6251 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6252 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6253 c156c7a4 2020-12-18 stsp goto done;
6257 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6258 c02c541e 2019-03-29 stsp if (error != NULL)
6259 8e94dd5b 2019-01-04 stsp goto done;
6261 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6262 f135c941 2020-02-20 stsp worktree);
6263 c02c541e 2019-03-29 stsp if (error)
6264 92205607 2019-01-04 stsp goto done;
6266 f135c941 2020-02-20 stsp init_curses();
6268 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6269 51a10b52 2020-12-26 stsp if (error)
6270 51a10b52 2020-12-26 stsp goto done;
6272 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6273 eb41ed75 2019-02-05 stsp if (error)
6274 69069811 2018-08-02 stsp goto done;
6276 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6277 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6278 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6279 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6280 a70480e0 2018-06-23 stsp if (error != NULL)
6281 66b4983c 2018-06-23 stsp goto done;
6282 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6283 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6285 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6286 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6288 a19e88aa 2018-06-23 stsp if (error != NULL)
6289 8b473291 2019-02-21 stsp goto done;
6291 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6292 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6293 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6294 e1cd8fed 2018-08-01 stsp goto done;
6297 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6298 945f9229 2022-04-16 thomas if (error)
6299 945f9229 2022-04-16 thomas goto done;
6301 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6302 945f9229 2022-04-16 thomas commit, repo);
6303 7cbe629d 2018-08-04 stsp if (error)
6304 7cbe629d 2018-08-04 stsp goto done;
6306 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6307 78756c87 2020-11-24 stsp commit_id, repo);
6308 0587e10c 2020-07-23 stsp if (error)
6309 0587e10c 2020-07-23 stsp goto done;
6310 12314ad4 2019-08-31 stsp if (worktree) {
6311 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6312 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6313 12314ad4 2019-08-31 stsp worktree = NULL;
6315 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6317 69069811 2018-08-02 stsp free(repo_path);
6318 f135c941 2020-02-20 stsp free(in_repo_path);
6319 0587e10c 2020-07-23 stsp free(link_target);
6320 69069811 2018-08-02 stsp free(cwd);
6321 a70480e0 2018-06-23 stsp free(commit_id);
6322 945f9229 2022-04-16 thomas if (commit)
6323 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6324 eb41ed75 2019-02-05 stsp if (worktree)
6325 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6326 1d0f4054 2021-06-17 stsp if (repo) {
6327 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6328 1d0f4054 2021-06-17 stsp if (error == NULL)
6329 1d0f4054 2021-06-17 stsp error = close_err;
6331 7cd52833 2022-06-23 thomas if (pack_fds) {
6332 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6333 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6334 7cd52833 2022-06-23 thomas if (error == NULL)
6335 7cd52833 2022-06-23 thomas error = pack_err;
6337 51a10b52 2020-12-26 stsp tog_free_refs();
6338 a70480e0 2018-06-23 stsp return error;
6341 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6342 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6344 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6345 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6346 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6347 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6348 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6349 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6350 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6352 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6353 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6354 a5d43cac 2022-07-01 thomas --limit; /* border */
6356 f7d12f7e 2018-08-01 stsp werase(view->window);
6358 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6359 ffd1d5e5 2018-06-23 stsp return NULL;
6361 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6364 ffd1d5e5 2018-06-23 stsp return err;
6365 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6366 a3404814 2018-09-02 stsp wstandout(view->window);
6367 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6369 11b20872 2019-11-08 stsp wattr_on(view->window,
6370 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6371 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6373 11b20872 2019-11-08 stsp wattr_off(view->window,
6374 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6375 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6376 a3404814 2018-09-02 stsp wstandend(view->window);
6377 2550e4c3 2018-07-13 stsp free(wline);
6378 2550e4c3 2018-07-13 stsp wline = NULL;
6380 6f5f393a 2022-08-12 thomas i += s->selected;
6381 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6382 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6383 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6384 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6386 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6387 07dd3ed3 2022-08-06 thomas wprintw(view->window, " [%d/%d]", i,
6388 07dd3ed3 2022-08-06 thomas nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6390 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6391 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6392 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6393 ffd1d5e5 2018-06-23 stsp return NULL;
6394 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6397 ce52c690 2018-06-23 stsp return err;
6398 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6399 2550e4c3 2018-07-13 stsp free(wline);
6400 2550e4c3 2018-07-13 stsp wline = NULL;
6401 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6402 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6403 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6404 ffd1d5e5 2018-06-23 stsp return NULL;
6405 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6406 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6407 a1eca9bb 2018-06-23 stsp return NULL;
6409 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6410 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6411 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6412 0cf4efb1 2018-09-29 stsp if (view->focussed)
6413 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6414 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6416 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6417 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6418 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6419 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6420 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6421 ffd1d5e5 2018-06-23 stsp return NULL;
6425 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6428 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6429 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6430 848d6979 2019-08-12 stsp const char *modestr = "";
6431 56e0773d 2019-11-28 stsp mode_t mode;
6433 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6434 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6436 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6437 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6438 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6440 638f9024 2019-05-13 stsp return got_error_from_errno(
6441 230a42bd 2019-05-11 jcs "got_object_id_str");
6443 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6444 63c5ca5d 2019-08-24 stsp modestr = "$";
6445 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6448 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6449 d86d3b18 2020-12-01 naddy te, s->repo);
6450 0d6c6ee3 2020-05-20 stsp if (err) {
6451 0d6c6ee3 2020-05-20 stsp free(id_str);
6452 0d6c6ee3 2020-05-20 stsp return err;
6454 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6455 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6456 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6458 848d6979 2019-08-12 stsp modestr = "@";
6460 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6461 848d6979 2019-08-12 stsp modestr = "/";
6462 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6463 848d6979 2019-08-12 stsp modestr = "*";
6464 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6465 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6466 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6467 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6468 1d13200f 2018-07-12 stsp free(id_str);
6469 0d6c6ee3 2020-05-20 stsp free(link_target);
6470 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6472 1d13200f 2018-07-12 stsp free(id_str);
6473 0d6c6ee3 2020-05-20 stsp free(link_target);
6474 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6476 ffd1d5e5 2018-06-23 stsp if (err) {
6477 ffd1d5e5 2018-06-23 stsp free(line);
6480 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6481 0cf4efb1 2018-09-29 stsp if (view->focussed)
6482 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6483 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6485 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6487 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6488 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6489 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6491 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6492 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6493 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6494 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6495 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6496 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6497 ffd1d5e5 2018-06-23 stsp free(line);
6498 2550e4c3 2018-07-13 stsp free(wline);
6499 2550e4c3 2018-07-13 stsp wline = NULL;
6501 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6502 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6503 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6507 ffd1d5e5 2018-06-23 stsp return err;
6510 ffd1d5e5 2018-06-23 stsp static void
6511 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6513 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6514 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6515 fa86c4bf 2020-11-29 stsp int i = 0;
6517 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6520 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6521 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6522 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6523 fa86c4bf 2020-11-29 stsp if (!isroot)
6524 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6527 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6528 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6532 a5d43cac 2022-07-01 thomas static const struct got_error *
6533 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6535 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6536 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6537 ffd1d5e5 2018-06-23 stsp int n = 0;
6539 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6540 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6541 694d3271 2020-12-01 naddy s->first_displayed_entry);
6543 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6545 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6546 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6547 07dd3ed3 2022-08-06 thomas if (last) {
6548 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6549 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6551 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6552 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6553 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6557 a5d43cac 2022-07-01 thomas return NULL;
6560 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6561 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6562 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6564 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6565 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6566 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6568 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6569 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6570 56e0773d 2019-11-28 stsp + 1 /* slash */;
6572 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6574 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6575 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6576 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6578 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6579 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6580 d9765a41 2018-06-23 stsp while (pt) {
6581 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6582 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6583 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6584 cb2ebc8a 2018-06-23 stsp goto done;
6586 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6587 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6588 cb2ebc8a 2018-06-23 stsp goto done;
6590 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6593 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6594 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6595 ce52c690 2018-06-23 stsp goto done;
6599 ce52c690 2018-06-23 stsp if (err) {
6600 ce52c690 2018-06-23 stsp free(*path);
6601 ce52c690 2018-06-23 stsp *path = NULL;
6603 ce52c690 2018-06-23 stsp return err;
6606 ce52c690 2018-06-23 stsp static const struct got_error *
6607 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6608 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6609 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6611 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6612 ce52c690 2018-06-23 stsp char *path;
6613 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6615 a0de39f3 2019-08-09 stsp *new_view = NULL;
6617 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6619 ce52c690 2018-06-23 stsp return err;
6621 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6622 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6623 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6624 83ce39e3 2019-08-12 stsp goto done;
6627 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6628 e5a0f69f 2018-08-18 stsp if (err) {
6629 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6630 fc06ba56 2019-08-22 stsp err = NULL;
6631 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6633 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6635 83ce39e3 2019-08-12 stsp free(path);
6636 69efd4c4 2018-07-18 stsp return err;
6639 69efd4c4 2018-07-18 stsp static const struct got_error *
6640 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6641 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6643 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6644 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6645 69efd4c4 2018-07-18 stsp char *path;
6647 a0de39f3 2019-08-09 stsp *new_view = NULL;
6649 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6650 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6651 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6653 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6655 69efd4c4 2018-07-18 stsp return err;
6657 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6660 e5a0f69f 2018-08-18 stsp view_close(log_view);
6662 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6663 cb2ebc8a 2018-06-23 stsp free(path);
6664 cb2ebc8a 2018-06-23 stsp return err;
6667 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6668 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6669 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6671 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6672 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6673 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6674 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6676 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6677 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6679 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6680 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6681 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6683 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6685 bc573f3b 2021-07-10 stsp goto done;
6688 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6689 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6690 bc573f3b 2021-07-10 stsp * closed on demand.
6692 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6693 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6695 bc573f3b 2021-07-10 stsp goto done;
6696 bc573f3b 2021-07-10 stsp s->tree = s->root;
6698 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6699 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6700 ffd1d5e5 2018-06-23 stsp goto done;
6702 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6703 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6704 ffd1d5e5 2018-06-23 stsp goto done;
6707 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6708 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6709 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6710 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6711 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6712 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6713 9cd7cbd1 2020-12-07 stsp goto done;
6716 fb2756b9 2018-08-04 stsp s->repo = repo;
6718 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6719 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6720 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6721 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6723 c0b01bdb 2019-11-08 stsp goto done;
6724 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6725 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6727 c0b01bdb 2019-11-08 stsp goto done;
6728 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6729 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6730 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6732 c0b01bdb 2019-11-08 stsp goto done;
6734 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6735 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6736 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6738 11b20872 2019-11-08 stsp goto done;
6740 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6741 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6743 c0b01bdb 2019-11-08 stsp goto done;
6746 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6747 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6748 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6749 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6750 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6752 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6753 bc573f3b 2021-07-10 stsp if (commit)
6754 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6756 bc573f3b 2021-07-10 stsp close_tree_view(view);
6757 ad80ab7b 2018-08-04 stsp return err;
6760 e5a0f69f 2018-08-18 stsp static const struct got_error *
6761 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6763 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6765 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6766 fb2756b9 2018-08-04 stsp free(s->tree_label);
6767 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6768 6484ec90 2018-09-29 stsp free(s->commit_id);
6769 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6770 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6771 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6772 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6773 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6774 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6775 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6776 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6777 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6778 ad80ab7b 2018-08-04 stsp free(parent);
6781 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6782 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6783 bc573f3b 2021-07-10 stsp if (s->root)
6784 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6785 7c32bd05 2019-06-22 stsp return NULL;
6788 7c32bd05 2019-06-22 stsp static const struct got_error *
6789 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6791 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6793 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6794 7c32bd05 2019-06-22 stsp return NULL;
6797 7c32bd05 2019-06-22 stsp static int
6798 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6800 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6802 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6806 7c32bd05 2019-06-22 stsp static const struct got_error *
6807 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6809 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6810 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6812 7c32bd05 2019-06-22 stsp if (!view->searching) {
6813 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6814 7c32bd05 2019-06-22 stsp return NULL;
6817 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6818 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6819 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6820 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6821 56e0773d 2019-11-28 stsp s->selected_entry);
6823 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6825 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6826 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6828 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6829 56e0773d 2019-11-28 stsp s->selected_entry);
6832 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6833 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6834 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6835 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6837 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6840 7c32bd05 2019-06-22 stsp while (1) {
6841 56e0773d 2019-11-28 stsp if (te == NULL) {
6842 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6843 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6844 ac66afb8 2019-06-24 stsp return NULL;
6846 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6847 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6849 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6852 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6853 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6854 56e0773d 2019-11-28 stsp s->matched_entry = te;
6858 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6859 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6861 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6864 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6865 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6866 7c32bd05 2019-06-22 stsp s->selected = 0;
6869 e5a0f69f 2018-08-18 stsp return NULL;
6872 ad80ab7b 2018-08-04 stsp static const struct got_error *
6873 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6875 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6876 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6877 e5a0f69f 2018-08-18 stsp char *parent_path;
6879 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6881 e5a0f69f 2018-08-18 stsp return err;
6883 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6884 e5a0f69f 2018-08-18 stsp free(parent_path);
6886 a5d43cac 2022-07-01 thomas view_border(view);
6887 e5a0f69f 2018-08-18 stsp return err;
6890 07dd3ed3 2022-08-06 thomas static const struct got_error *
6891 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
6893 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
6894 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
6895 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
6896 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
6897 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
6898 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
6900 07dd3ed3 2022-08-06 thomas g = view->gline;
6901 07dd3ed3 2022-08-06 thomas view->gline = 0;
6903 07dd3ed3 2022-08-06 thomas if (g == 0)
6905 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
6906 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6908 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
6909 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
6910 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
6912 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
6913 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6914 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
6916 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
6917 07dd3ed3 2022-08-06 thomas last += off;
6919 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
6920 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6921 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
6924 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
6925 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
6926 07dd3ed3 2022-08-06 thomas i += off;
6929 07dd3ed3 2022-08-06 thomas if (i < g) {
6930 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
6931 07dd3ed3 2022-08-06 thomas if (err)
6932 07dd3ed3 2022-08-06 thomas return err;
6933 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
6934 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
6935 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
6936 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
6937 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6938 07dd3ed3 2022-08-06 thomas first += off;
6939 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6941 07dd3ed3 2022-08-06 thomas } else if (i > g)
6942 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
6944 07dd3ed3 2022-08-06 thomas if (g < nlines &&
6945 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6946 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
6948 07dd3ed3 2022-08-06 thomas return NULL;
6951 e5a0f69f 2018-08-18 stsp static const struct got_error *
6952 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6954 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6955 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6956 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6957 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
6959 07dd3ed3 2022-08-06 thomas if (view->gline)
6960 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
6962 e5a0f69f 2018-08-18 stsp switch (ch) {
6964 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6965 07b0611c 2022-06-23 thomas view->count = 0;
6967 1be4947a 2022-07-22 thomas case 'L':
6968 07b0611c 2022-06-23 thomas view->count = 0;
6969 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6971 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6973 1be4947a 2022-07-22 thomas case 'R':
6974 07b0611c 2022-06-23 thomas view->count = 0;
6975 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
6977 e4526bf5 2021-09-03 naddy case 'g':
6978 e4526bf5 2021-09-03 naddy case KEY_HOME:
6979 e4526bf5 2021-09-03 naddy s->selected = 0;
6980 07b0611c 2022-06-23 thomas view->count = 0;
6981 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6982 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6983 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6985 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6987 e4526bf5 2021-09-03 naddy case 'G':
6988 a5d43cac 2022-07-01 thomas case KEY_END: {
6989 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6991 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6992 a5d43cac 2022-07-01 thomas --eos; /* border */
6993 e4526bf5 2021-09-03 naddy s->selected = 0;
6994 07b0611c 2022-06-23 thomas view->count = 0;
6995 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6996 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6997 e4526bf5 2021-09-03 naddy if (te == NULL) {
6998 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6999 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7004 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7005 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7007 e4526bf5 2021-09-03 naddy if (n > 0)
7008 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7012 1e37a5c2 2019-05-12 jcs case KEY_UP:
7013 f7140bf5 2021-10-17 thomas case CTRL('p'):
7014 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7015 1e37a5c2 2019-05-12 jcs s->selected--;
7018 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7019 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7020 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7021 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7022 07b0611c 2022-06-23 thomas view->count = 0;
7024 70f17a53 2022-06-13 thomas case CTRL('u'):
7025 23427b14 2022-06-23 thomas case 'u':
7026 70f17a53 2022-06-13 thomas nscroll /= 2;
7027 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7028 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7029 ea025d1d 2020-02-22 naddy case CTRL('b'):
7030 1c5e5faa 2022-06-23 thomas case 'b':
7031 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7032 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7033 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7034 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7036 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7037 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7039 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7040 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7041 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7042 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7043 07b0611c 2022-06-23 thomas view->count = 0;
7046 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7047 f7140bf5 2021-10-17 thomas case CTRL('n'):
7048 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7049 1e37a5c2 2019-05-12 jcs s->selected++;
7052 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7053 07b0611c 2022-06-23 thomas == NULL) {
7054 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7055 07b0611c 2022-06-23 thomas view->count = 0;
7058 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7060 70f17a53 2022-06-13 thomas case CTRL('d'):
7061 23427b14 2022-06-23 thomas case 'd':
7062 70f17a53 2022-06-13 thomas nscroll /= 2;
7063 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7064 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7065 ea025d1d 2020-02-22 naddy case CTRL('f'):
7066 1c5e5faa 2022-06-23 thomas case 'f':
7067 4c2d69cb 2022-06-23 thomas case ' ':
7068 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7070 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7071 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7072 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7073 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7075 07b0611c 2022-06-23 thomas view->count = 0;
7078 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7080 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7082 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7083 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7084 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7085 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7086 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7087 07b0611c 2022-06-23 thomas view->count = 0;
7090 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7091 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7093 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7094 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7095 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7096 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7097 1e37a5c2 2019-05-12 jcs s->selected_entry =
7098 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7099 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7100 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7101 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7102 a5d43cac 2022-07-01 thomas if (err)
7105 1e37a5c2 2019-05-12 jcs free(parent);
7106 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7107 56e0773d 2019-11-28 stsp s->selected_entry))) {
7108 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7109 07b0611c 2022-06-23 thomas view->count = 0;
7110 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7111 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7114 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7115 941e9f74 2019-05-21 stsp if (err) {
7116 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7119 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7120 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7122 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7123 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7124 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7125 07b0611c 2022-06-23 thomas view->count = 0;
7128 07b0611c 2022-06-23 thomas view->count = 0;
7132 ffd1d5e5 2018-06-23 stsp return err;
7135 ffd1d5e5 2018-06-23 stsp __dead static void
7136 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7139 91198554 2022-06-23 thomas fprintf(stderr,
7140 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7141 ffd1d5e5 2018-06-23 stsp getprogname());
7145 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7146 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7148 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7149 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7150 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7151 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7152 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7153 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7154 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7155 4e97c21c 2020-12-06 stsp char *label = NULL;
7156 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7157 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7159 5221c383 2018-08-01 stsp struct tog_view *view;
7160 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7162 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7163 ffd1d5e5 2018-06-23 stsp switch (ch) {
7165 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7168 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7169 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7170 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7174 e99e2d15 2020-11-24 naddy usage_tree();
7175 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7179 ffd1d5e5 2018-06-23 stsp argc -= optind;
7180 ffd1d5e5 2018-06-23 stsp argv += optind;
7182 55cccc34 2020-02-20 stsp if (argc > 1)
7183 e99e2d15 2020-11-24 naddy usage_tree();
7185 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7186 7cd52833 2022-06-23 thomas if (error != NULL)
7187 7cd52833 2022-06-23 thomas goto done;
7189 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7190 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7191 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7192 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7193 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7194 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7195 c156c7a4 2020-12-18 stsp goto done;
7196 55cccc34 2020-02-20 stsp if (worktree)
7197 52185f70 2019-02-05 stsp repo_path =
7198 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7200 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7201 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7202 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7203 c156c7a4 2020-12-18 stsp goto done;
7207 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7208 c02c541e 2019-03-29 stsp if (error != NULL)
7209 52185f70 2019-02-05 stsp goto done;
7211 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7212 55cccc34 2020-02-20 stsp repo, worktree);
7213 55cccc34 2020-02-20 stsp if (error)
7214 55cccc34 2020-02-20 stsp goto done;
7216 55cccc34 2020-02-20 stsp init_curses();
7218 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7219 c02c541e 2019-03-29 stsp if (error)
7220 52185f70 2019-02-05 stsp goto done;
7222 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7223 51a10b52 2020-12-26 stsp if (error)
7224 51a10b52 2020-12-26 stsp goto done;
7226 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7227 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7228 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7229 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7230 4e97c21c 2020-12-06 stsp if (error)
7231 4e97c21c 2020-12-06 stsp goto done;
7232 4e97c21c 2020-12-06 stsp head_ref_name = label;
7234 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7235 4e97c21c 2020-12-06 stsp if (error == NULL)
7236 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7237 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7238 4e97c21c 2020-12-06 stsp goto done;
7239 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7240 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7241 4e97c21c 2020-12-06 stsp if (error)
7242 4e97c21c 2020-12-06 stsp goto done;
7245 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7246 945f9229 2022-04-16 thomas if (error)
7247 945f9229 2022-04-16 thomas goto done;
7249 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7250 5221c383 2018-08-01 stsp if (view == NULL) {
7251 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7252 5221c383 2018-08-01 stsp goto done;
7254 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7255 ad80ab7b 2018-08-04 stsp if (error)
7256 ad80ab7b 2018-08-04 stsp goto done;
7257 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7258 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7259 d91faf3b 2020-12-01 naddy in_repo_path);
7260 55cccc34 2020-02-20 stsp if (error)
7261 55cccc34 2020-02-20 stsp goto done;
7264 55cccc34 2020-02-20 stsp if (worktree) {
7265 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7266 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7267 55cccc34 2020-02-20 stsp worktree = NULL;
7269 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7271 52185f70 2019-02-05 stsp free(repo_path);
7272 e4a0e26d 2020-02-20 stsp free(cwd);
7273 ffd1d5e5 2018-06-23 stsp free(commit_id);
7274 4e97c21c 2020-12-06 stsp free(label);
7276 486cd271 2020-12-06 stsp got_ref_close(ref);
7277 1d0f4054 2021-06-17 stsp if (repo) {
7278 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7279 1d0f4054 2021-06-17 stsp if (error == NULL)
7280 1d0f4054 2021-06-17 stsp error = close_err;
7282 7cd52833 2022-06-23 thomas if (pack_fds) {
7283 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7284 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7285 7cd52833 2022-06-23 thomas if (error == NULL)
7286 7cd52833 2022-06-23 thomas error = pack_err;
7288 51a10b52 2020-12-26 stsp tog_free_refs();
7289 ffd1d5e5 2018-06-23 stsp return error;
7292 6458efa5 2020-11-24 stsp static const struct got_error *
7293 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7295 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7296 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7298 6458efa5 2020-11-24 stsp s->nrefs = 0;
7299 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7300 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7301 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7302 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7303 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7306 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7307 6458efa5 2020-11-24 stsp if (re == NULL)
7308 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7310 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7311 8924d611 2020-12-26 stsp if (re->ref == NULL)
7312 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7313 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7314 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7317 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7318 6458efa5 2020-11-24 stsp return NULL;
7321 ef20f542 2022-06-26 thomas static void
7322 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7324 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7326 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7327 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7328 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7329 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7334 6458efa5 2020-11-24 stsp static const struct got_error *
7335 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7337 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7338 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7340 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7341 6458efa5 2020-11-24 stsp s->repo = repo;
7343 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7344 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7346 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7348 6458efa5 2020-11-24 stsp return err;
7350 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7351 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7352 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7353 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7355 6458efa5 2020-11-24 stsp goto done;
7357 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7358 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7359 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7361 6458efa5 2020-11-24 stsp goto done;
7363 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7364 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7365 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7366 2183bbf6 2022-01-23 thomas if (err)
7367 2183bbf6 2022-01-23 thomas goto done;
7369 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7370 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7371 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7373 6458efa5 2020-11-24 stsp goto done;
7376 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7377 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7378 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7379 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7380 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7383 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7384 6458efa5 2020-11-24 stsp return err;
7387 6458efa5 2020-11-24 stsp static const struct got_error *
7388 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7390 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7392 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7393 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7395 6458efa5 2020-11-24 stsp return NULL;
7398 6458efa5 2020-11-24 stsp static const struct got_error *
7399 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7400 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7402 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7403 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7404 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7405 6458efa5 2020-11-24 stsp int obj_type;
7407 c42c9805 2020-11-24 stsp *commit_id = NULL;
7409 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7411 6458efa5 2020-11-24 stsp return err;
7413 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7415 6458efa5 2020-11-24 stsp goto done;
7417 6458efa5 2020-11-24 stsp switch (obj_type) {
7418 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7419 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7421 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7422 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7424 6458efa5 2020-11-24 stsp goto done;
7425 c42c9805 2020-11-24 stsp free(obj_id);
7426 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7427 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7429 6458efa5 2020-11-24 stsp goto done;
7430 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7431 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7432 c42c9805 2020-11-24 stsp goto done;
7434 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7435 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7436 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7437 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7438 c42c9805 2020-11-24 stsp goto done;
7442 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7448 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7449 c42c9805 2020-11-24 stsp if (err) {
7450 c42c9805 2020-11-24 stsp free(*commit_id);
7451 c42c9805 2020-11-24 stsp *commit_id = NULL;
7453 c42c9805 2020-11-24 stsp return err;
7456 c42c9805 2020-11-24 stsp static const struct got_error *
7457 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7458 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7460 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7461 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7462 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7464 c42c9805 2020-11-24 stsp *new_view = NULL;
7466 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7467 c42c9805 2020-11-24 stsp if (err) {
7468 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7469 c42c9805 2020-11-24 stsp return err;
7471 c42c9805 2020-11-24 stsp return NULL;
7474 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7475 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7476 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7477 6458efa5 2020-11-24 stsp goto done;
7480 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7481 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7484 6458efa5 2020-11-24 stsp view_close(log_view);
7486 6458efa5 2020-11-24 stsp *new_view = log_view;
7487 c42c9805 2020-11-24 stsp free(commit_id);
7488 6458efa5 2020-11-24 stsp return err;
7491 ce5b7c56 2019-07-09 stsp static void
7492 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7494 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7495 34ba6917 2020-11-30 stsp int i = 0;
7497 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7500 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7501 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7502 34ba6917 2020-11-30 stsp if (re == NULL)
7504 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7505 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7509 a5d43cac 2022-07-01 thomas static const struct got_error *
7510 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7512 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7513 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7514 6458efa5 2020-11-24 stsp int n = 0;
7516 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7517 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7519 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7521 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7522 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7523 07dd3ed3 2022-08-06 thomas if (last) {
7524 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7525 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7527 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7528 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7529 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7533 a5d43cac 2022-07-01 thomas return NULL;
7536 6458efa5 2020-11-24 stsp static const struct got_error *
7537 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7539 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7541 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7542 6458efa5 2020-11-24 stsp return NULL;
7545 6458efa5 2020-11-24 stsp static int
7546 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7548 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7550 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7554 6458efa5 2020-11-24 stsp static const struct got_error *
7555 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7557 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7558 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7560 6458efa5 2020-11-24 stsp if (!view->searching) {
7561 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7562 6458efa5 2020-11-24 stsp return NULL;
7565 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7566 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7567 6458efa5 2020-11-24 stsp if (s->selected_entry)
7568 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7570 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7571 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7573 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7574 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7576 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7577 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7580 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7581 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7582 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7583 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7585 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7588 6458efa5 2020-11-24 stsp while (1) {
7589 6458efa5 2020-11-24 stsp if (re == NULL) {
7590 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7591 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7592 6458efa5 2020-11-24 stsp return NULL;
7594 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7595 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7597 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7600 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7601 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7602 6458efa5 2020-11-24 stsp s->matched_entry = re;
7606 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7607 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7609 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7612 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7613 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7614 6458efa5 2020-11-24 stsp s->selected = 0;
7617 6458efa5 2020-11-24 stsp return NULL;
7620 6458efa5 2020-11-24 stsp static const struct got_error *
7621 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7623 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7624 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7625 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7626 6458efa5 2020-11-24 stsp char *line = NULL;
7627 6458efa5 2020-11-24 stsp wchar_t *wline;
7628 6458efa5 2020-11-24 stsp struct tog_color *tc;
7629 6458efa5 2020-11-24 stsp int width, n;
7630 6458efa5 2020-11-24 stsp int limit = view->nlines;
7632 6458efa5 2020-11-24 stsp werase(view->window);
7634 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7635 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7636 a5d43cac 2022-07-01 thomas --limit; /* border */
7638 6458efa5 2020-11-24 stsp if (limit == 0)
7639 6458efa5 2020-11-24 stsp return NULL;
7641 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7643 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7644 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7645 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7647 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7648 6458efa5 2020-11-24 stsp if (err) {
7649 6458efa5 2020-11-24 stsp free(line);
7650 6458efa5 2020-11-24 stsp return err;
7652 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7653 6458efa5 2020-11-24 stsp wstandout(view->window);
7654 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7655 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7656 6458efa5 2020-11-24 stsp wstandend(view->window);
7657 6458efa5 2020-11-24 stsp free(wline);
7658 6458efa5 2020-11-24 stsp wline = NULL;
7659 6458efa5 2020-11-24 stsp free(line);
7660 6458efa5 2020-11-24 stsp line = NULL;
7661 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7662 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7663 6458efa5 2020-11-24 stsp if (--limit <= 0)
7664 6458efa5 2020-11-24 stsp return NULL;
7667 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7668 6458efa5 2020-11-24 stsp char *line = NULL;
7669 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7671 84227eb1 2022-06-23 thomas if (s->show_date) {
7672 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7673 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7674 84227eb1 2022-06-23 thomas struct got_object_id *id;
7675 84227eb1 2022-06-23 thomas struct tm tm;
7676 84227eb1 2022-06-23 thomas time_t t;
7678 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7679 84227eb1 2022-06-23 thomas if (err)
7680 84227eb1 2022-06-23 thomas return err;
7681 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7682 84227eb1 2022-06-23 thomas if (err) {
7683 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7684 84227eb1 2022-06-23 thomas free(id);
7685 84227eb1 2022-06-23 thomas return err;
7687 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7689 84227eb1 2022-06-23 thomas if (err) {
7690 84227eb1 2022-06-23 thomas free(id);
7691 84227eb1 2022-06-23 thomas return err;
7693 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7694 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7695 84227eb1 2022-06-23 thomas } else {
7696 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7697 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7699 84227eb1 2022-06-23 thomas free(id);
7700 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7701 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7702 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7703 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7705 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7706 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7707 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7708 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7709 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7710 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7711 6458efa5 2020-11-24 stsp struct got_object_id *id;
7712 6458efa5 2020-11-24 stsp char *id_str;
7713 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7715 6458efa5 2020-11-24 stsp return err;
7716 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7717 6458efa5 2020-11-24 stsp if (err) {
7719 6458efa5 2020-11-24 stsp return err;
7721 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7722 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7723 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7725 6458efa5 2020-11-24 stsp free(id_str);
7726 6458efa5 2020-11-24 stsp return err;
7729 6458efa5 2020-11-24 stsp free(id_str);
7730 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7731 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7732 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7734 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7736 6458efa5 2020-11-24 stsp if (err) {
7737 6458efa5 2020-11-24 stsp free(line);
7738 6458efa5 2020-11-24 stsp return err;
7740 6458efa5 2020-11-24 stsp if (n == s->selected) {
7741 6458efa5 2020-11-24 stsp if (view->focussed)
7742 6458efa5 2020-11-24 stsp wstandout(view->window);
7743 6458efa5 2020-11-24 stsp s->selected_entry = re;
7745 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7747 6458efa5 2020-11-24 stsp wattr_on(view->window,
7748 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7749 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7751 6458efa5 2020-11-24 stsp wattr_off(view->window,
7752 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7753 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7754 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7755 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7756 6458efa5 2020-11-24 stsp wstandend(view->window);
7757 6458efa5 2020-11-24 stsp free(line);
7758 6458efa5 2020-11-24 stsp free(wline);
7759 6458efa5 2020-11-24 stsp wline = NULL;
7761 6458efa5 2020-11-24 stsp s->ndisplayed++;
7762 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7765 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7768 a5d43cac 2022-07-01 thomas view_border(view);
7769 6458efa5 2020-11-24 stsp return err;
7772 6458efa5 2020-11-24 stsp static const struct got_error *
7773 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7774 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7776 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7777 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7778 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7780 c42c9805 2020-11-24 stsp *new_view = NULL;
7782 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7783 c42c9805 2020-11-24 stsp if (err) {
7784 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7785 c42c9805 2020-11-24 stsp return err;
7787 c42c9805 2020-11-24 stsp return NULL;
7791 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7792 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7793 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7794 c42c9805 2020-11-24 stsp goto done;
7797 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7798 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7800 c42c9805 2020-11-24 stsp goto done;
7802 c42c9805 2020-11-24 stsp *new_view = tree_view;
7804 c42c9805 2020-11-24 stsp free(commit_id);
7805 c42c9805 2020-11-24 stsp return err;
7808 07dd3ed3 2022-08-06 thomas static const struct got_error *
7809 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
7811 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7812 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
7813 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
7815 07dd3ed3 2022-08-06 thomas g = view->gline;
7816 07dd3ed3 2022-08-06 thomas view->gline = 0;
7818 07dd3ed3 2022-08-06 thomas if (g == 0)
7820 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
7821 07dd3ed3 2022-08-06 thomas g = s->nrefs;
7823 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
7824 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
7825 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
7826 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7827 07dd3ed3 2022-08-06 thomas return NULL;
7830 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
7831 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
7832 07dd3ed3 2022-08-06 thomas if (err)
7833 07dd3ed3 2022-08-06 thomas return err;
7834 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7835 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
7836 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
7837 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7838 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
7839 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
7841 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
7842 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7844 07dd3ed3 2022-08-06 thomas return NULL;
7848 c42c9805 2020-11-24 stsp static const struct got_error *
7849 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7851 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7852 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7853 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7854 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
7856 07dd3ed3 2022-08-06 thomas if (view->gline)
7857 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
7859 6458efa5 2020-11-24 stsp switch (ch) {
7861 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7862 07b0611c 2022-06-23 thomas view->count = 0;
7864 84227eb1 2022-06-23 thomas case 'm':
7865 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7866 07b0611c 2022-06-23 thomas view->count = 0;
7868 98182bd0 2021-11-20 thomas case 'o':
7869 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7870 07b0611c 2022-06-23 thomas view->count = 0;
7871 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7872 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7873 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7874 c591440f 2021-11-20 thomas if (err)
7876 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7877 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7878 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7879 3bfadbd4 2021-11-20 thomas if (err)
7881 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7882 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7884 6458efa5 2020-11-24 stsp case KEY_ENTER:
7885 6458efa5 2020-11-24 stsp case '\r':
7886 07b0611c 2022-06-23 thomas view->count = 0;
7887 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7889 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7891 1be4947a 2022-07-22 thomas case 'T':
7892 07b0611c 2022-06-23 thomas view->count = 0;
7893 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7895 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
7897 e4526bf5 2021-09-03 naddy case 'g':
7898 e4526bf5 2021-09-03 naddy case KEY_HOME:
7899 e4526bf5 2021-09-03 naddy s->selected = 0;
7900 07b0611c 2022-06-23 thomas view->count = 0;
7901 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7903 e4526bf5 2021-09-03 naddy case 'G':
7904 a5d43cac 2022-07-01 thomas case KEY_END: {
7905 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7907 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7908 a5d43cac 2022-07-01 thomas --eos; /* border */
7909 e4526bf5 2021-09-03 naddy s->selected = 0;
7910 07b0611c 2022-06-23 thomas view->count = 0;
7911 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7912 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7913 e4526bf5 2021-09-03 naddy if (re == NULL)
7915 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7916 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7918 e4526bf5 2021-09-03 naddy if (n > 0)
7919 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7923 6458efa5 2020-11-24 stsp case KEY_UP:
7924 f7140bf5 2021-10-17 thomas case CTRL('p'):
7925 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7926 6458efa5 2020-11-24 stsp s->selected--;
7929 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7930 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7931 07b0611c 2022-06-23 thomas view->count = 0;
7933 70f17a53 2022-06-13 thomas case CTRL('u'):
7934 23427b14 2022-06-23 thomas case 'u':
7935 70f17a53 2022-06-13 thomas nscroll /= 2;
7936 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7937 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7938 6458efa5 2020-11-24 stsp case CTRL('b'):
7939 1c5e5faa 2022-06-23 thomas case 'b':
7940 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7941 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7942 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7943 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7944 07b0611c 2022-06-23 thomas view->count = 0;
7947 6458efa5 2020-11-24 stsp case KEY_DOWN:
7948 f7140bf5 2021-10-17 thomas case CTRL('n'):
7949 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7950 6458efa5 2020-11-24 stsp s->selected++;
7953 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7954 6458efa5 2020-11-24 stsp /* can't scroll any further */
7955 07b0611c 2022-06-23 thomas view->count = 0;
7958 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7960 70f17a53 2022-06-13 thomas case CTRL('d'):
7961 23427b14 2022-06-23 thomas case 'd':
7962 70f17a53 2022-06-13 thomas nscroll /= 2;
7963 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7964 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7965 6458efa5 2020-11-24 stsp case CTRL('f'):
7966 1c5e5faa 2022-06-23 thomas case 'f':
7967 4c2d69cb 2022-06-23 thomas case ' ':
7968 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7969 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7970 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7971 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7972 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7973 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7974 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7975 07b0611c 2022-06-23 thomas view->count = 0;
7978 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7980 6458efa5 2020-11-24 stsp case CTRL('l'):
7981 07b0611c 2022-06-23 thomas view->count = 0;
7982 8924d611 2020-12-26 stsp tog_free_refs();
7983 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7986 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7987 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7989 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7990 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7991 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7994 07b0611c 2022-06-23 thomas view->count = 0;
7998 6458efa5 2020-11-24 stsp return err;
8001 6458efa5 2020-11-24 stsp __dead static void
8002 6458efa5 2020-11-24 stsp usage_ref(void)
8005 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8006 6458efa5 2020-11-24 stsp getprogname());
8010 6458efa5 2020-11-24 stsp static const struct got_error *
8011 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8013 6458efa5 2020-11-24 stsp const struct got_error *error;
8014 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8015 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8016 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8018 6458efa5 2020-11-24 stsp struct tog_view *view;
8019 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8021 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8022 6458efa5 2020-11-24 stsp switch (ch) {
8024 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8025 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8026 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8030 e99e2d15 2020-11-24 naddy usage_ref();
8031 6458efa5 2020-11-24 stsp /* NOTREACHED */
8035 6458efa5 2020-11-24 stsp argc -= optind;
8036 6458efa5 2020-11-24 stsp argv += optind;
8038 6458efa5 2020-11-24 stsp if (argc > 1)
8039 e99e2d15 2020-11-24 naddy usage_ref();
8041 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8042 7cd52833 2022-06-23 thomas if (error != NULL)
8043 7cd52833 2022-06-23 thomas goto done;
8045 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8046 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8047 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8048 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8049 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8050 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8051 c156c7a4 2020-12-18 stsp goto done;
8052 6458efa5 2020-11-24 stsp if (worktree)
8053 6458efa5 2020-11-24 stsp repo_path =
8054 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8056 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8057 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8058 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8059 c156c7a4 2020-12-18 stsp goto done;
8063 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8064 6458efa5 2020-11-24 stsp if (error != NULL)
8065 6458efa5 2020-11-24 stsp goto done;
8067 6458efa5 2020-11-24 stsp init_curses();
8069 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8070 51a10b52 2020-12-26 stsp if (error)
8071 51a10b52 2020-12-26 stsp goto done;
8073 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8074 6458efa5 2020-11-24 stsp if (error)
8075 6458efa5 2020-11-24 stsp goto done;
8077 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8078 6458efa5 2020-11-24 stsp if (view == NULL) {
8079 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8080 6458efa5 2020-11-24 stsp goto done;
8083 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8084 6458efa5 2020-11-24 stsp if (error)
8085 6458efa5 2020-11-24 stsp goto done;
8087 6458efa5 2020-11-24 stsp if (worktree) {
8088 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8089 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8090 6458efa5 2020-11-24 stsp worktree = NULL;
8092 6458efa5 2020-11-24 stsp error = view_loop(view);
8094 6458efa5 2020-11-24 stsp free(repo_path);
8095 6458efa5 2020-11-24 stsp free(cwd);
8096 1d0f4054 2021-06-17 stsp if (repo) {
8097 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8098 1d0f4054 2021-06-17 stsp if (close_err)
8099 1d0f4054 2021-06-17 stsp error = close_err;
8101 7cd52833 2022-06-23 thomas if (pack_fds) {
8102 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8103 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8104 7cd52833 2022-06-23 thomas if (error == NULL)
8105 7cd52833 2022-06-23 thomas error = pack_err;
8107 51a10b52 2020-12-26 stsp tog_free_refs();
8108 6458efa5 2020-11-24 stsp return error;
8111 2a31b33b 2022-07-23 thomas static const struct got_error *
8112 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8113 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8115 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8117 2a31b33b 2022-07-23 thomas *new_view = NULL;
8119 2a31b33b 2022-07-23 thomas switch (request) {
8120 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8121 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8122 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8124 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8125 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8126 2a31b33b 2022-07-23 thomas view, s->repo);
8128 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8129 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8131 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8132 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8133 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8135 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8136 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8137 2a31b33b 2022-07-23 thomas s->repo);
8139 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8140 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8142 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8143 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8144 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8145 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8146 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8147 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8148 2a31b33b 2022-07-23 thomas &view->state.tree);
8149 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8150 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8151 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8152 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8154 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8155 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8157 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8158 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8159 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8160 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8161 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8162 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8163 2a31b33b 2022-07-23 thomas view->state.log.repo);
8164 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8165 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8166 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8167 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8169 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8170 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8172 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8173 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8174 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8175 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8176 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8177 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8178 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8179 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8181 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8182 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8183 2a31b33b 2022-07-23 thomas if (err)
8184 2a31b33b 2022-07-23 thomas view_close(*new_view);
8186 2a31b33b 2022-07-23 thomas default:
8187 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8190 2a31b33b 2022-07-23 thomas return err;
8194 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8195 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8197 6458efa5 2020-11-24 stsp static void
8198 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8200 a5d43cac 2022-07-01 thomas switch (view->type) {
8201 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8202 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8203 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8204 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8208 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8209 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8211 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8212 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8215 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8216 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8217 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8219 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8220 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8221 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8223 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8224 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8225 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8227 a5d43cac 2022-07-01 thomas default:
8231 a5d43cac 2022-07-01 thomas view->offset = 0;
8235 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8236 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8238 a5d43cac 2022-07-01 thomas static const struct got_error *
8239 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8241 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8242 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8243 a5d43cac 2022-07-01 thomas int *selected = NULL;
8244 a5d43cac 2022-07-01 thomas int header, offset;
8246 a5d43cac 2022-07-01 thomas switch (view->type) {
8247 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8248 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8249 a5d43cac 2022-07-01 thomas header = 3;
8250 a5d43cac 2022-07-01 thomas scrolld = NULL;
8251 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8252 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8253 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8254 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8255 a5d43cac 2022-07-01 thomas view->offset = offset;
8259 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8260 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8261 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8262 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8263 a5d43cac 2022-07-01 thomas selected = &s->selected;
8266 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8267 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8268 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8269 a5d43cac 2022-07-01 thomas header = 3;
8270 a5d43cac 2022-07-01 thomas selected = &s->selected;
8273 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8274 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8275 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8276 a5d43cac 2022-07-01 thomas header = 5;
8277 a5d43cac 2022-07-01 thomas selected = &s->selected;
8280 a5d43cac 2022-07-01 thomas default:
8281 a5d43cac 2022-07-01 thomas selected = NULL;
8282 a5d43cac 2022-07-01 thomas scrolld = NULL;
8283 a5d43cac 2022-07-01 thomas header = 0;
8287 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8288 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8289 a5d43cac 2022-07-01 thomas view->offset = offset;
8290 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8291 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8292 a5d43cac 2022-07-01 thomas *selected -= offset;
8296 a5d43cac 2022-07-01 thomas return err;
8299 a5d43cac 2022-07-01 thomas static void
8300 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8304 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8305 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8306 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8307 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8309 6879ba42 2020-10-01 naddy fputc('\n', fp);
8312 4ed7e80c 2018-05-20 stsp __dead static void
8313 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8315 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8317 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8318 6879ba42 2020-10-01 naddy getprogname());
8319 6879ba42 2020-10-01 naddy if (hflag) {
8320 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8321 6879ba42 2020-10-01 naddy list_commands(fp);
8323 6879ba42 2020-10-01 naddy exit(status);
8326 c2301be8 2018-04-30 stsp static char **
8327 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8329 ee85c5e8 2020-02-29 stsp va_list ap;
8330 c2301be8 2018-04-30 stsp char **argv;
8333 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8335 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8336 c2301be8 2018-04-30 stsp if (argv == NULL)
8337 c2301be8 2018-04-30 stsp err(1, "calloc");
8338 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8339 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8340 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8341 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8344 ee85c5e8 2020-02-29 stsp va_end(ap);
8345 c2301be8 2018-04-30 stsp return argv;
8349 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8350 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8351 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8352 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8353 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8355 ee85c5e8 2020-02-29 stsp static const struct got_error *
8356 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8358 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8359 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8360 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8361 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8362 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8363 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8364 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8365 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8366 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8368 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8369 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8370 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8372 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8373 7cd52833 2022-06-23 thomas if (error != NULL)
8374 7cd52833 2022-06-23 thomas goto done;
8376 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8377 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8378 ee85c5e8 2020-02-29 stsp goto done;
8380 ee85c5e8 2020-02-29 stsp if (worktree)
8381 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8383 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8384 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8385 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8386 ee85c5e8 2020-02-29 stsp goto done;
8389 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8390 ee85c5e8 2020-02-29 stsp if (error != NULL)
8391 ee85c5e8 2020-02-29 stsp goto done;
8393 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8394 ee85c5e8 2020-02-29 stsp repo, worktree);
8395 ee85c5e8 2020-02-29 stsp if (error)
8396 ee85c5e8 2020-02-29 stsp goto done;
8398 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8399 84de9106 2020-12-26 stsp if (error)
8400 84de9106 2020-12-26 stsp goto done;
8401 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8402 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8403 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8404 ee85c5e8 2020-02-29 stsp if (error)
8405 ee85c5e8 2020-02-29 stsp goto done;
8407 ee85c5e8 2020-02-29 stsp if (worktree) {
8408 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8409 ee85c5e8 2020-02-29 stsp worktree = NULL;
8412 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8413 945f9229 2022-04-16 thomas if (error)
8414 945f9229 2022-04-16 thomas goto done;
8416 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8417 ee85c5e8 2020-02-29 stsp if (error) {
8418 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8419 ee85c5e8 2020-02-29 stsp goto done;
8420 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8421 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8422 6879ba42 2020-10-01 naddy usage(1, 1);
8423 ee85c5e8 2020-02-29 stsp /* not reached */
8426 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8427 ee85c5e8 2020-02-29 stsp if (error)
8428 ee85c5e8 2020-02-29 stsp goto done;
8430 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8432 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8433 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8435 1d0f4054 2021-06-17 stsp if (repo) {
8436 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8437 1d0f4054 2021-06-17 stsp if (error == NULL)
8438 1d0f4054 2021-06-17 stsp error = close_err;
8440 945f9229 2022-04-16 thomas if (commit)
8441 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8442 ee85c5e8 2020-02-29 stsp if (worktree)
8443 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8444 7cd52833 2022-06-23 thomas if (pack_fds) {
8445 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8446 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8447 7cd52833 2022-06-23 thomas if (error == NULL)
8448 7cd52833 2022-06-23 thomas error = pack_err;
8451 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8452 ee85c5e8 2020-02-29 stsp free(commit_id);
8453 ee85c5e8 2020-02-29 stsp free(cwd);
8454 ee85c5e8 2020-02-29 stsp free(repo_path);
8455 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8456 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8458 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8459 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8460 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8462 87670572 2020-12-26 naddy tog_free_refs();
8463 ee85c5e8 2020-02-29 stsp return error;
8467 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8469 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8470 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8471 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8472 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8473 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8474 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8475 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8477 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8479 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8481 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8482 9f7d7167 2018-04-29 stsp switch (ch) {
8484 9f7d7167 2018-04-29 stsp hflag = 1;
8487 53ccebc2 2019-07-30 stsp Vflag = 1;
8490 6879ba42 2020-10-01 naddy usage(hflag, 1);
8491 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8495 9f7d7167 2018-04-29 stsp argc -= optind;
8496 9f7d7167 2018-04-29 stsp argv += optind;
8497 9814e6a3 2020-09-27 naddy optind = 1;
8498 c2301be8 2018-04-30 stsp optreset = 1;
8500 53ccebc2 2019-07-30 stsp if (Vflag) {
8501 53ccebc2 2019-07-30 stsp got_version_print_str();
8502 6879ba42 2020-10-01 naddy return 0;
8505 4010e238 2020-12-04 stsp #ifndef PROFILE
8506 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8507 4010e238 2020-12-04 stsp NULL) == -1)
8508 4010e238 2020-12-04 stsp err(1, "pledge");
8511 c2301be8 2018-04-30 stsp if (argc == 0) {
8512 f29d3e89 2018-06-23 stsp if (hflag)
8513 6879ba42 2020-10-01 naddy usage(hflag, 0);
8514 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8515 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8517 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8521 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8522 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8523 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8524 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8525 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8531 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8532 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8533 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8534 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8535 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8536 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8539 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8540 ee85c5e8 2020-02-29 stsp if (argc != 1)
8541 6879ba42 2020-10-01 naddy usage(0, 1);
8542 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8543 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8545 ee85c5e8 2020-02-29 stsp if (hflag)
8546 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8548 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8552 b46c1e04 2020-09-20 naddy putchar('\n');
8553 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8555 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8556 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8557 a2f4a359 2020-02-28 stsp free(cmd_argv);
8560 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8561 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);